-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
460 lines (385 loc) · 14.6 KB
/
main.js
File metadata and controls
460 lines (385 loc) · 14.6 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Set up scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
// scene.background = new THREE.Color(0xeaeaea);
// Set up camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 4;
camera.position.y = 2;
camera.lookAt(0, 0, 0);
// Set up renderer with shadow support
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Softer shadows
document.body.appendChild(renderer.domElement);
// Add lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
// Directional light for dramatic shadows
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 10, 5);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 50;
directionalLight.shadow.camera.left = -10;
directionalLight.shadow.camera.right = 10;
directionalLight.shadow.camera.top = 10;
directionalLight.shadow.camera.bottom = -10;
directionalLight.shadow.bias = -0.0005;
scene.add(directionalLight);
// Create floor plane for shadow
const floorGeometry = new THREE.PlaneGeometry(30, 30);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xeeeeee,
roughness: 0.8
});
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = -Math.PI / 2; // Rotate to be horizontal
floor.position.y = -1.5; // Position below the cube
floor.receiveShadow = true; // Enable shadow receiving
scene.add(floor);
// Current colors
let currentFaceColor = "#ffffff";
let currentTextColor = "#0000FF";
let currentBorderColor = "#999999";
let showBorder = false;
let currentFont = "Helvetica";
// Store fixed random alignments for each face
const faceAlignments = [];
for (let i = 0; i < 6; i++) {
const alignments = ['left', 'right',];
const baselines = ['top', 'bottom',];
faceAlignments.push({
textAlign: alignments[Math.floor(Math.random() * alignments.length)],
textBaseline: baselines[Math.floor(Math.random() * baselines.length)]
});
}
// Face materials
const materials = [];
const faceTexts = [
"this is not a cube", "you can just do things", "hello world", "don't be a square", "you are what you vibe", "@threejs"
];
// Create canvas textures for each face
for (let i = 0; i < 6; i++) {
const texture = createTextTexture(faceTexts[i], currentFaceColor, currentTextColor, i);
// Use MeshLambertMaterial instead of Basic to respond to lighting
const material = new THREE.MeshLambertMaterial({ map: texture });
materials.push(material);
}
// Create cube
const geometry = new THREE.BoxGeometry(2, 2, 2);
const cube = new THREE.Mesh(geometry, materials);
cube.castShadow = true; // Enable shadow casting
cube.position.y = 0; // Position above the floor
scene.add(cube);
// Controls for rotation
let isDragging = false;
let previousMousePosition = {
x: 0,
y: 0
};
let rotationSpeed = 0.01; // Increased from 0.005 for more sensitivity
let rotationMomentum = {
x: 0,
y: 0
};
const momentumFactor = 0.95; // How quickly momentum decreases (0-1)
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Mouse events for rotation
renderer.domElement.addEventListener('mousedown', (e) => {
isDragging = true;
previousMousePosition = {
x: e.clientX,
y: e.clientY
};
});
renderer.domElement.addEventListener('mousemove', (e) => {
if (isDragging) {
const deltaMove = {
x: e.clientX - previousMousePosition.x,
y: e.clientY - previousMousePosition.y
};
// Apply rotation directly
cube.rotation.y += deltaMove.x * rotationSpeed;
cube.rotation.x += deltaMove.y * rotationSpeed;
// Store momentum for continued rotation after release
rotationMomentum.x = deltaMove.x * rotationSpeed * 0.8;
rotationMomentum.y = deltaMove.y * rotationSpeed * 0.8;
previousMousePosition = {
x: e.clientX,
y: e.clientY
};
}
});
renderer.domElement.addEventListener('mouseup', () => {
isDragging = false;
// Don't reset the momentum - let it continue
});
renderer.domElement.addEventListener('mouseleave', () => {
isDragging = false;
// Don't reset the momentum - let it continue
});
// Touch events for mobile support
renderer.domElement.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
isDragging = true;
previousMousePosition = {
x: e.touches[0].clientX,
y: e.touches[0].clientY
};
}
});
renderer.domElement.addEventListener('touchmove', (e) => {
if (isDragging && e.touches.length === 1) {
const deltaMove = {
x: e.touches[0].clientX - previousMousePosition.x,
y: e.touches[0].clientY - previousMousePosition.y
};
// Apply rotation directly
cube.rotation.y += deltaMove.x * rotationSpeed;
cube.rotation.x += deltaMove.y * rotationSpeed;
// Store momentum for continued rotation after release
rotationMomentum.x = deltaMove.x * rotationSpeed * 0.8;
rotationMomentum.y = deltaMove.y * rotationSpeed * 0.8;
previousMousePosition = {
x: e.touches[0].clientX,
y: e.touches[0].clientY
};
}
});
renderer.domElement.addEventListener('touchend', () => {
isDragging = false;
// Don't reset the momentum - let it continue
});
// Control for adjusting shadow intensity and angle
const addShadowControls = () => {
const controlsDiv = document.getElementById('controls');
// Shadow section
const shadowSection = document.createElement('div');
shadowSection.className = 'shadow-controls';
// Shadow section label
const sectionLabel = document.createElement('div');
sectionLabel.className = 'section-label';
sectionLabel.textContent = 'Shadow';
shadowSection.appendChild(sectionLabel);
// Light intensity slider
const intensityContainer = document.createElement('div');
intensityContainer.className = 'slider-container';
const intensityLabel = document.createElement('label');
intensityLabel.htmlFor = 'shadow-intensity';
intensityLabel.textContent = 'Intensity:';
const intensitySlider = document.createElement('input');
intensitySlider.type = 'range';
intensitySlider.id = 'shadow-intensity';
intensitySlider.min = '0';
intensitySlider.max = '2';
intensitySlider.step = '0.1';
intensitySlider.value = '1';
intensityContainer.appendChild(intensityLabel);
intensityContainer.appendChild(intensitySlider);
shadowSection.appendChild(intensityContainer);
// Light angle slider
const angleContainer = document.createElement('div');
angleContainer.className = 'slider-container';
const angleLabel = document.createElement('label');
angleLabel.htmlFor = 'shadow-angle';
angleLabel.textContent = 'Angle:';
const angleSlider = document.createElement('input');
angleSlider.type = 'range';
angleSlider.id = 'shadow-angle';
angleSlider.min = '0';
angleSlider.max = '360';
angleSlider.step = '5';
angleSlider.value = '45';
angleContainer.appendChild(angleLabel);
angleContainer.appendChild(angleSlider);
shadowSection.appendChild(angleContainer);
// Shadow blur slider
const blurContainer = document.createElement('div');
blurContainer.className = 'slider-container';
const blurLabel = document.createElement('label');
blurLabel.htmlFor = 'shadow-blur';
blurLabel.textContent = 'Softness:';
const blurSlider = document.createElement('input');
blurSlider.type = 'range';
blurSlider.id = 'shadow-blur';
blurSlider.min = '0';
blurSlider.max = '20';
blurSlider.step = '1';
blurSlider.value = '5';
blurContainer.appendChild(blurLabel);
blurContainer.appendChild(blurSlider);
shadowSection.appendChild(blurContainer);
// Add the shadow controls to the main controls
controlsDiv.appendChild(shadowSection);
// Event listeners
intensitySlider.addEventListener('input', function() {
directionalLight.intensity = parseFloat(this.value);
});
angleSlider.addEventListener('input', function() {
const angle = parseInt(this.value) * Math.PI / 180;
const radius = 10;
directionalLight.position.x = Math.cos(angle) * radius;
directionalLight.position.z = Math.sin(angle) * radius;
});
blurSlider.addEventListener('input', function() {
const value = parseInt(this.value);
directionalLight.shadow.radius = value * 0.5;
directionalLight.shadow.bias = -0.0005 - (value * 0.0001);
});
};
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Apply momentum when not being dragged
if (!isDragging) {
cube.rotation.x += rotationMomentum.y;
cube.rotation.y += rotationMomentum.x;
// Gradually reduce momentum
rotationMomentum.x *= momentumFactor;
rotationMomentum.y *= momentumFactor;
// Add very subtle auto-rotation only when momentum is almost gone
if (Math.abs(rotationMomentum.x) < 0.0005 && Math.abs(rotationMomentum.y) < 0.0005) {
cube.rotation.y += 0.001;
cube.rotation.x += 0.0005;
}
}
renderer.render(scene, camera);
}
// Function to create text texture - FIXED VERSION
function createTextTexture(text, backgroundColor, textColor, faceIndex) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const size = 256;
canvas.width = size;
canvas.height = size;
// Fill background
context.fillStyle = backgroundColor;
context.fillRect(0, 0, size, size);
// Draw border if enabled
if (showBorder) {
context.strokeStyle = currentBorderColor;
context.lineWidth = 2;
context.strokeRect(2, 2, size - 2, size - 2);
}
// Configure text
context.fillStyle = textColor;
context.font = `bold 48px ${currentFont}`;
// Use the stored alignment for this face
const alignment = faceAlignments[faceIndex];
const textAlign = alignment.textAlign;
const textBaseline = alignment.textBaseline;
context.textAlign = textAlign;
context.textBaseline = textBaseline;
// Break text into multiple lines if too long
const maxLineWidth = size - 40;
const words = text.split(' ');
const lines = [];
let currentLine = words[0];
for(let i = 1; i < words.length; i++) {
const width = context.measureText(currentLine + ' ' + words[i]).width;
if (width < maxLineWidth) {
currentLine += ' ' + words[i];
} else {
lines.push(currentLine);
currentLine = words[i];
}
}
lines.push(currentLine);
// Calculate total text height
const lineHeight = 52;
const totalTextHeight = lines.length * lineHeight;
// Set text position based on alignment with fixes for multi-line text
let x = size / 2; // Default center
let y = size / 2; // Default middle
// Adjust x position based on alignment
if (textAlign === 'left') x = 20;
if (textAlign === 'right') x = size - 20;
// FIX: Adjust y position based on baseline and total text height
if (textBaseline === 'top') {
y = 20 + lineHeight; // Adjust for first line
} else if (textBaseline === 'bottom') {
// Position y so all lines fit within the canvas
y = size - 20 - ((lines.length - 1) * lineHeight);
} else { // middle
y = (size / 2) - (totalTextHeight / 2) + lineHeight;
}
// Draw each line of text
for(let i = 0; i < lines.length; i++) {
context.fillText(lines[i], x, y + i * lineHeight);
}
// Create texture from canvas
const texture = new THREE.CanvasTexture(canvas);
texture.needsUpdate = true;
return texture;
}
// Handle color selection
// Color pickers
document.getElementById('face-color-picker').addEventListener('input', function() {
currentFaceColor = this.value;
updateCube();
});
document.getElementById('text-color-picker').addEventListener('input', function() {
currentTextColor = this.value;
updateCube();
});
document.getElementById('border-color-picker').addEventListener('input', function() {
currentBorderColor = this.value;
updateCube();
});
// Border toggle
document.getElementById('wireframe-toggle').addEventListener('change', function() {
showBorder = this.checked;
updateCube();
});
// Font selection
document.getElementById('font-select').addEventListener('change', function() {
currentFont = this.value;
updateCube();
});
// Randomize alignment button
document.getElementById('randomize-alignment').addEventListener('click', function() {
// Create new random alignments for each face
for (let i = 0; i < 6; i++) {
const alignments = ['left', 'right', 'center'];
const baselines = ['top', 'middle', 'bottom'];
faceAlignments[i] = {
textAlign: alignments[Math.floor(Math.random() * alignments.length)],
textBaseline: baselines[Math.floor(Math.random() * baselines.length)]
};
}
// Update the cube with new alignments
updateCube();
});
// Function to update cube
function updateCube() {
const texts = [
document.getElementById('face-front').value,
document.getElementById('face-back').value,
document.getElementById('face-top').value,
document.getElementById('face-bottom').value,
document.getElementById('face-left').value,
document.getElementById('face-right').value
];
for (let i = 0; i < 6; i++) {
const texture = createTextTexture(texts[i], currentFaceColor, currentTextColor, i);
cube.material[i].map.dispose();
cube.material[i].map = texture;
cube.material[i].needsUpdate = true;
}
}
// Add input event listeners to all text inputs
document.querySelectorAll('input[type="text"]').forEach(input => {
input.addEventListener('input', updateCube);
});
// Add shadow controls after DOM is loaded
window.addEventListener('DOMContentLoaded', addShadowControls);
animate();