-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (120 loc) · 4.09 KB
/
script.js
File metadata and controls
143 lines (120 loc) · 4.09 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
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';
import starsTexture from './img/stars.jpg';
import sunTexture from './img/sun.jpg';
import mercuryTexture from './img/mercury.jpg';
import venusTexture from './img/venus.jpg';
import earthTexture from './img/earth.jpg';
import marsTexture from './img/mars.jpg';
import jupiterTexture from './img/jupiter.jpg';
import saturnTexture from './img/saturn.jpg';
import saturnRingTexture from './img/saturn ring.png';
import uranusTexture from './img/uranus.jpg';
import uranusRingTexture from './img/uranus ring.png';
import neptuneTexture from './img/neptune.jpg';
import plutoTexture from './img/pluto.jpg';
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const orbit = new OrbitControls(camera, renderer.domElement);
camera.position.set(-90, 140, 300);
orbit.update();
const ambientLight = new THREE.AmbientLight(0x333333);
scene.add(ambientLight);
const cubeTextureLoader = new THREE.CubeTextureLoader();
scene.background = cubeTextureLoader.load([
starsTexture,
starsTexture,
starsTexture,
starsTexture,
starsTexture,
starsTexture
]);
const textureLoader = new THREE.TextureLoader();
const sunGeo = new THREE.SphereGeometry(16, 30, 30);
const sunMat = new THREE.MeshBasicMaterial({
map: textureLoader.load(sunTexture)
});
const sun = new THREE.Mesh(sunGeo, sunMat);
scene.add(sun);
function createPlanete(size, texture, positionx, ring) {
const geo = new THREE.SphereGeometry(size, 30, 30);
const mat = new THREE.MeshStandardMaterial({
map: textureLoader.load(texture)
});
const mesh = new THREE.Mesh(geo, mat);
const obj = new THREE.Object3D();
obj.add(mesh);
if(ring) {
const ringGeo = new THREE.RingGeometry(
ring.innerRadius,
ring.outerRadius,
32);
const ringMat = new THREE.MeshBasicMaterial({
map: textureLoader.load(ring.texture),
side: THREE.DoubleSide
});
const ringMesh = new THREE.Mesh(ringGeo, ringMat);
obj.add(ringMesh);
ringMesh.position.x = positionx;
ringMesh.rotation.x = -0.5 * Math.PI;
}
scene.add(obj);
mesh.position.x = positionx;
return {mesh, obj}
}
const mercury = createPlanete(3.2, mercuryTexture, 28);
const venus = createPlanete(5.8, venusTexture, -44);
const earth = createPlanete(6, earthTexture, 62);
const mars = createPlanete(4, marsTexture, -78);
const jupiter = createPlanete(12, jupiterTexture, 100);
const saturn = createPlanete(10, saturnTexture, -138, {
innerRadius: 10,
outerRadius: 20,
texture: saturnRingTexture
});
const uranus = createPlanete(7, uranusTexture, 176 ,{
innerRadius: 7,
outerRadius: 12,
texture: uranusRingTexture
});
const neptune = createPlanete(7, neptuneTexture, -200);
const pluto = createPlanete(2.8, plutoTexture, 212);
const pointLight = new THREE.PointLight(0xFFFFFF, 2, 1000);
scene.add(pointLight);
function animate() {
sun.rotateY(0.004);
mercury.mesh.rotateY(0.005)
venus.mesh.rotateY(0.002);
earth.mesh.rotateY(0.02);
mars.mesh.rotateY(0.018);
jupiter.mesh.rotateY(0.04);
saturn.mesh.rotateY(0.038);
uranus.mesh.rotateY(0.03);
neptune.mesh.rotateY(0.032);
pluto.mesh.rotateY(0.008);
//Around-sun-rotation
mercury.obj.rotateY(0.04);
venus.obj.rotateY(0.015);
earth.obj.rotateY(0.01);
mars.obj.rotateY(0.008);
jupiter.obj.rotateY(0.002);
saturn.obj.rotateY(0.0009);
uranus.obj.rotateY(0.0004);
neptune.obj.rotateY(0.0001);
pluto.obj.rotateY(0.00007);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});