-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
110 lines (91 loc) · 3.34 KB
/
index.js
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
import { GUI } from 'dat.gui';
import { Group, Vector2 } from 'three';
import { settings } from './settings';
import { createCamera } from './src/camera';
import { createStars } from './src/Layers/stars';
import { createClock, createScene, createWebGlRenderer } from './src/Three';
import { generatePlanetByType } from './src/utils';
function initScene() {
const container = document.querySelector('#root');
const aspect = container.clientWidth / container.clientHeight;
const scene = createScene();
const clock = createClock();
const camera = createCamera(75, aspect, 0.1, 100000);
let totalX = 0;
let moveX = 0;
let holding = false
container.addEventListener("mousedown", (e) =>{
holding = true;
},false);
container.addEventListener("mouseup", (e) =>{
holding = false;
},false);
container.addEventListener('mousemove', (e) => {
e.preventDefault();
totalX += Math.abs(e.movementX);
moveX += e.movementX;
}, false);
const planetGroup = new Group()
// add starting planet
planetGroup.add(generatePlanetByType(settings.planetOptions[0]));
scene.add(planetGroup)
// use dat.gui to play around
const gui = new GUI({ name: "Pixel planets" })
gui.add(settings, 'planetTypes', settings.planetOptions).onChange((v) => {
planetGroup.children.pop().remove()
planetGroup.add(generatePlanetByType(v));
});
gui.add(settings, "seed").onChange(() => {
planetGroup.children.forEach(planet => {
planet.children.forEach(layer => {
if(layer.material.uniforms["seed"])
{
layer.material.uniforms["seed"].value = settings.seedValue
}
// for asteroids
if (layer.material.uniforms["size"]) {
layer.material.uniforms["size"].value = Math.random() * 10
}
});
});
})
const skySpeed = 0.00001;
const renderer = createWebGlRenderer();
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
// // prepare background layer
// const backgroundLayerDust = createDustLayer();
// scene.add(backgroundLayerDust);
// const backgroundLayerNebula = createNebulaLayer();
// scene.add(backgroundLayerNebula);
// prepare star particles
const starGroup = createStars(1000);
scene.add(starGroup);
document.getElementById("root").appendChild(renderer.domElement);
camera.position.z = 1;
function animate() {
requestAnimationFrame(animate);
// animate planets
planetGroup.children.forEach(planet => {
planet.children.forEach(layer => {
if(layer.material.uniforms["time"])
{
layer.material.uniforms["time"].value = clock.getElapsedTime();
layer.material.uniforms["time_speed"].value += holding ? moveX*0.01 : 0;
}
});
});
//update camera
camera.updateProjectionMatrix();
// animate space
starGroup.rotateY(skySpeed);
starGroup.rotateX(skySpeed);
starGroup.rotateZ(skySpeed);
renderer.render(scene, camera);
moveX = totalX = 0;
};
animate();
}
window.onload = () => {
initScene()
}