I’m working on a project in the Three.JS Online Editor. I’m trying to make a Day / Night Cycle. It should loop through colors, setting the scene background colors, like this:
- Day
- Sunrise/Sunset
- Night
- Sunrise/Sunset
- Day … Etc., etc.,
And it should loop through these, forever.
I’ve gotten it to loop through two colors, but I can’t seem to get it to loop through all three.
Is there a way to do this? Here’s my code so far:
JavaScript
x
50
50
1
//var day = new THREE.Color(0xB8F4FF);
2
//var duskdawn = new THREE.Color(0xFF571F);
3
//var night = new THREE.Color(0x17012D);
4
5
//scene.background = new THREE.Color(0xB8F4FF);
6
7
let t = 0;
8
let tn = 0;
9
let cyc = 0;
10
11
//const day = new THREE.Color(0xB8F4FF);
12
var day = new THREE.Color(0xB8F4FF);
13
const duskdawn = new THREE.Color(0xFF571F);
14
const night = new THREE.Color(0x17012D);
15
16
animate();
17
18
function animate() {
19
20
requestAnimationFrame(animate);
21
t += 0.01;
22
tn += 0.01;
23
cyc = 0.9;
24
cyc += 0.1;
25
if(cyc % 2 == 1){
26
//day = new THREE.Color(0x17012D);
27
day = new THREE.Color(0xB8F4FF);
28
//scene.background.copy(day).lerp(duskdawn, 0.5 * (Math.sin(t) + 1));
29
scene.background.copy(day).lerp(duskdawn, 0.5 * (Math.sin(t) + 1));
30
day = new THREE.Color(0x17012D);
31
cyc += 0.1;
32
if(cyc != 1){
33
day = new THREE.Color(0x17012D);
34
}
35
/**/
36
}
37
if(cyc % 2 != 0){
38
//scene.background.copy(night).lerp(duskdawn, 0.5 * (Math.sin(tn) + 1));
39
//day = new THREE.Color(0xB8F4FF);
40
day = new THREE.Color(0x17012D);
41
scene.background.copy(day).lerp(duskdawn, 0.5 * (Math.sin(tn) + 1));
42
//day = new THREE.Color(0xB8F4FF);
43
cyc += 0.1;
44
//cyc = 0;
45
}
46
/**/
47
cyc = 0.9;
48
cyc += 0.1;
49
//cyc += 1;
50
}
Any help would be appreciated.
If anyone needs any more information, please, let me know!
Thanks!
Advertisement
Answer
Try it like so:
JavaScript
1
59
59
1
let camera, scene, renderer, clock;
2
3
const colors = [
4
new THREE.Color(0xff0000),
5
new THREE.Color(0xffff00),
6
new THREE.Color(0x00ff00),
7
new THREE.Color(0x0000ff)
8
];
9
10
const duration = 4; // 4s
11
12
init();
13
animate();
14
15
function init() {
16
17
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
18
camera.position.z = 1;
19
20
scene = new THREE.Scene();
21
scene.background = new THREE.Color();
22
23
clock = new THREE.Clock();
24
25
renderer = new THREE.WebGLRenderer();
26
renderer.setSize(window.innerWidth, window.innerHeight);
27
document.body.appendChild(renderer.domElement);
28
29
}
30
31
function animate() {
32
33
requestAnimationFrame(animate);
34
35
const time = clock.getElapsedTime();
36
37
animateBackground(time)
38
39
renderer.render(scene, camera);
40
41
}
42
43
function animateBackground(t) {
44
45
const f = Math.floor(duration / colors.length);
46
const i1 = Math.floor((t / f) % colors.length);
47
let i2 = i1 + 1;
48
49
if (i2 === colors.length) i2 = 0;
50
51
const color1 = colors[i1];
52
const color2 = colors[i2];
53
const a = (t / f) % colors.length % 1;
54
55
scene.background.copy(color1);
56
scene.background.lerp(color2, a);
57
58
59
}
JavaScript
1
3
1
body {
2
margin: 0;
3
}
JavaScript
1
1
1
<script src="https://cdn.jsdelivr.net/npm/three@0.124/build/three.js"></script>