sketch.js: make 3D text follow camera
This commit is contained in:
parent
4c7933b802
commit
3232913c81
28
sketch.js
28
sketch.js
|
|
@ -142,9 +142,13 @@ class Text3D {
|
||||||
this.textString = textString;
|
this.textString = textString;
|
||||||
this.depth = 150; // Number of layers for 3D effect
|
this.depth = 150; // Number of layers for 3D effect
|
||||||
this.depthSpacing = 0.15; // Depth spacing between layers
|
this.depthSpacing = 0.15; // Depth spacing between layers
|
||||||
|
this.rotationY = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
display() {
|
display() {
|
||||||
|
// Apply rotation toward the camera
|
||||||
|
rotateY(this.rotationY);
|
||||||
|
|
||||||
push();
|
push();
|
||||||
for (let i = 0; i < this.depth; i++) {
|
for (let i = 0; i < this.depth; i++) {
|
||||||
let r = map(i, 0, this.depth - 1, this.fromColor[0], this.toColor[0]);
|
let r = map(i, 0, this.depth - 1, this.fromColor[0], this.toColor[0]);
|
||||||
|
|
@ -157,6 +161,20 @@ class Text3D {
|
||||||
}
|
}
|
||||||
pop();
|
pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update() {
|
||||||
|
// Get the camera position
|
||||||
|
let camX = camera.eyeX;
|
||||||
|
let camZ = camera.eyeZ;
|
||||||
|
|
||||||
|
// Get the camera position
|
||||||
|
let targetRotY = atan2(camX, camZ); // Calculate target Y rotation
|
||||||
|
|
||||||
|
// Ensure shortest rotation path by normalizing angles
|
||||||
|
let delta = (targetRotY - this.rotationY + 540) % 360 - 180; // Keeps within -180 to 180 range
|
||||||
|
this.rotationY += delta * 0.05; // Interpolate rotation smoothly
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function preload() {
|
function preload() {
|
||||||
|
|
@ -173,6 +191,9 @@ function setup() {
|
||||||
textAlign(CENTER, CENTER);
|
textAlign(CENTER, CENTER);
|
||||||
randomSeed()
|
randomSeed()
|
||||||
|
|
||||||
|
// Initialize camera to get its position
|
||||||
|
camera = createCamera();
|
||||||
|
|
||||||
// Initialize 3D text
|
// Initialize 3D text
|
||||||
startGradient = [210, 0, 255]; // Start color
|
startGradient = [210, 0, 255]; // Start color
|
||||||
endGradient = [0, 255, 180]; // End color
|
endGradient = [0, 255, 180]; // End color
|
||||||
|
|
@ -211,13 +232,12 @@ function draw() {
|
||||||
180, -180, 0 // direction
|
180, -180, 0 // direction
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update the onion object
|
// Update objects
|
||||||
onionObj.update();
|
onionObj.update();
|
||||||
|
text3D.update();
|
||||||
|
|
||||||
// Display the onion and its orbiting objects
|
// Display the onion, orbiting objects and the 3D text
|
||||||
onionObj.display();
|
onionObj.display();
|
||||||
|
|
||||||
// Display 3D text
|
|
||||||
text3D.display();
|
text3D.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue