-
Notifications
You must be signed in to change notification settings - Fork 0
/
clay.html
284 lines (248 loc) · 9.33 KB
/
clay.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Enhanced Pottery Wheel Simulation</title>
<style>
body { margin: 0; overflow: hidden; background: #000; }
canvas { display: block; touch-action: none; }
#ui {
position: absolute;
top: 10px;
left: 10px;
color: #fff;
font-family: Arial, sans-serif;
z-index: 1;
background: rgba(0, 0, 0, 0.5);
padding: 10px;
border-radius: 5px;
opacity: 0; /* Start hidden */
transition: opacity 0.5s;
}
#ui button {
margin-top: 5px;
padding: 5px 10px;
font-size: 14px;
cursor: pointer;
}
#toolPanel {
margin-top: 10px;
}
#toolPanel button {
margin-right: 5px;
}
</style>
</head>
<body>
<div id="ui">
<button id="resetButton">Reset Clay</button>
<div id="toolPanel">
<button data-tool="smooth">Smooth</button>
<button data-tool="carve">Carve</button>
<button data-tool="pinch">Pinch</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/cannon-es@0.18.0/dist/cannon-es.js"></script>
<script>
// Basic Three.js setup
let scene, camera, renderer;
let potteryMesh, wheelMesh;
let isInteracting = false;
let previousTouches = [];
const maxRadius = 5;
const minRadius = 0.5;
const height = 12;
const segments = 100; // Increased for better detail
let currentTool = 'smooth';
init();
animate();
function init() {
// Scene and Camera
scene = new THREE.Scene();
scene.background = new THREE.Color(0x222222);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 8, 10);
camera.lookAt(0, 5, 0);
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
// Lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const spotLight = new THREE.SpotLight(0xffffff, 1);
spotLight.position.set(15, 40, 35);
spotLight.angle = Math.PI / 4;
spotLight.penumbra = 0.1;
spotLight.decay = 2;
spotLight.distance = 200;
spotLight.castShadow = true;
scene.add(spotLight);
// Pottery Wheel
const wheelGeometry = new THREE.CylinderGeometry(6, 6, 1, 64);
const wheelMaterial = new THREE.MeshStandardMaterial({ color: 0x555555 });
wheelMesh = new THREE.Mesh(wheelGeometry, wheelMaterial);
wheelMesh.position.y = 0.5;
wheelMesh.receiveShadow = true;
scene.add(wheelMesh);
// Ground Plane
const groundGeometry = new THREE.PlaneGeometry(200, 200);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x333333 });
const groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
groundMesh.rotation.x = -Math.PI / 2;
groundMesh.position.y = 0;
groundMesh.receiveShadow = true;
scene.add(groundMesh);
// Create Clay
createClay();
// Event Listeners
window.addEventListener('resize', onWindowResize, false);
const domElement = renderer.domElement;
domElement.addEventListener('pointerdown', onPointerDown, false);
domElement.addEventListener('pointerup', onPointerUp, false);
domElement.addEventListener('pointermove', onPointerMove, false);
domElement.addEventListener('contextmenu', event => event.preventDefault());
// UI Buttons
const uiElement = document.getElementById('ui');
document.getElementById('resetButton').addEventListener('click', resetClay);
document.querySelectorAll('#toolPanel button').forEach(button => {
button.addEventListener('click', () => {
currentTool = button.getAttribute('data-tool');
});
});
// Hide UI after inactivity
let uiTimeout;
function resetUITimeout() {
clearTimeout(uiTimeout);
uiElement.style.opacity = 1;
uiTimeout = setTimeout(() => {
uiElement.style.opacity = 0;
}, 3000);
}
resetUITimeout();
document.addEventListener('pointermove', resetUITimeout);
document.addEventListener('pointerdown', resetUITimeout);
}
function createClay() {
// Create initial profile curve for the clay
const profilePoints = [];
const step = height / segments;
for (let i = 0; i <= segments; i++) {
const y = i * step;
const radius = maxRadius;
profilePoints.push(new THREE.Vector2(radius, y));
}
// Create LatheGeometry from profile curve
const clayGeometry = new THREE.LatheGeometry(profilePoints, 200);
clayGeometry.computeVertexNormals();
// Clay Material with basic shininess
const clayMaterial = new THREE.MeshStandardMaterial({
color: 0xd2a679,
roughness: 0.6,
metalness: 0.3,
});
// Remove existing mesh if any
if (potteryMesh) {
scene.remove(potteryMesh);
potteryMesh.geometry.dispose();
potteryMesh.material.dispose();
}
potteryMesh = new THREE.Mesh(clayGeometry, clayMaterial);
potteryMesh.position.y = 0;
potteryMesh.castShadow = true;
potteryMesh.receiveShadow = true;
scene.add(potteryMesh);
}
function onPointerDown(event) {
isInteracting = true;
previousTouches = [getTouchPosition(event)];
event.target.setPointerCapture(event.pointerId);
// Haptic Feedback
if (navigator.vibrate) {
navigator.vibrate(50);
}
}
function onPointerUp(event) {
isInteracting = false;
previousTouches = [];
event.target.releasePointerCapture(event.pointerId);
}
function onPointerMove(event) {
if (isInteracting) {
const currentTouches = [getTouchPosition(event)];
if (previousTouches.length === currentTouches.length) {
if (currentTouches.length === 1) {
// Single touch deformation
const deltaY = currentTouches[0].y - previousTouches[0].y;
deformClay(deltaY, currentTouches[0].y);
}
// Haptic Feedback
if (navigator.vibrate) {
const intensity = Math.min(Math.abs(currentTouches[0].y - previousTouches[0].y), 100);
navigator.vibrate(intensity);
}
}
previousTouches = currentTouches;
}
}
function getTouchPosition(event) {
return { x: event.clientX, y: event.clientY };
}
function deformClay(deltaY, pointerY) {
const rect = renderer.domElement.getBoundingClientRect();
const normalizedY = ((pointerY - rect.top) / rect.height) * height;
const influenceRadius = 1.5;
const deformationStrength = deltaY * 0.05;
const positions = potteryMesh.geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const y = positions.getY(i);
const distance = Math.abs(y - normalizedY);
if (distance < influenceRadius) {
const factor = deformationStrength * (1 - distance / influenceRadius);
let x = positions.getX(i);
let z = positions.getZ(i);
const radius = Math.sqrt(x * x + z * z);
const angle = Math.atan2(z, x);
let newRadius = radius + factor;
switch (currentTool) {
case 'smooth':
newRadius = THREE.MathUtils.lerp(radius, newRadius, 0.5);
break;
case 'carve':
newRadius = radius - Math.abs(factor);
break;
case 'pinch':
newRadius = radius + factor;
break;
}
newRadius = THREE.MathUtils.clamp(newRadius, minRadius, maxRadius);
x = newRadius * Math.cos(angle);
z = newRadius * Math.sin(angle);
positions.setX(i, x);
positions.setZ(i, z);
}
}
positions.needsUpdate = true;
potteryMesh.geometry.computeVertexNormals();
}
function resetClay() {
createClay();
}
function animate() {
requestAnimationFrame(animate);
// Rotate the wheel and clay
const rotationSpeed = 0.02;
wheelMesh.rotation.y += rotationSpeed;
potteryMesh.rotation.y += rotationSpeed;
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>