-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
244 lines (215 loc) · 6.93 KB
/
script.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
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
let renderer,
camera,
planet,
moon,
sphereBg,
terrainGeometry,
container = document.getElementById('canvas_container'),
timeout_Debounce,
frame = 0,
cameraDx = 0.05,
count = 0,
t = 0
/* Lines values */
let lineTotal = 1000
let linesGeometry = new THREE.BufferGeometry()
linesGeometry.setAttribute(
'position',
new THREE.BufferAttribute(new Float32Array(6 * lineTotal), 3)
)
linesGeometry.setAttribute(
'velocity',
new THREE.BufferAttribute(new Float32Array(2 * lineTotal), 1)
)
let l_positionAttr = linesGeometry.getAttribute('position')
let l_vertex_Array = linesGeometry.getAttribute('position').array
let l_velocity_Array = linesGeometry.getAttribute('velocity').array
init()
animate()
function init() {
scene = new THREE.Scene()
scene.background = new THREE.Color('#000000')
scene.fog = new THREE.Fog('#3c1e02', 0.5, 50)
camera = new THREE.PerspectiveCamera(
55,
window.innerWidth / window.innerHeight,
0.01,
1000
)
camera.position.set(0, 1, 32)
pointLight1 = new THREE.PointLight('#ffffff', 1, 0)
pointLight1.position.set(0, 30, 30)
scene.add(pointLight1)
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
})
renderer.setSize(container.clientWidth, container.clientHeight)
renderer.setPixelRatio(window.devicePixelRatio)
container.appendChild(renderer.domElement)
const loader = new THREE.TextureLoader()
// Planet
const texturePlanet = loader.load(
'https://i.ibb.co/h94JBXy/saturn3-ljge5g.jpg'
)
texturePlanet.anisotropy = 16
const planetGeometry = new THREE.SphereBufferGeometry(10, 50, 50)
const planetMaterial = new THREE.MeshLambertMaterial({
map: texturePlanet,
fog: false,
})
planet = new THREE.Mesh(planetGeometry, planetMaterial)
planet.position.set(0, 8, -30)
scene.add(planet)
//Moon
const textureMoon = loader.load('https://i.ibb.co/64zn361/moon-ndengb.jpg')
textureMoon.anisotropy = 16
let moonGeometry = new THREE.SphereBufferGeometry(2, 32, 32)
let moonMaterial = new THREE.MeshPhongMaterial({
map: textureMoon,
fog: false,
})
moon = new THREE.Mesh(moonGeometry, moonMaterial)
moon.position.set(0, 8, 0)
scene.add(moon)
// Sphere Background
const textureSphereBg = loader.load(
'https://i.ibb.co/JCsHJpp/stars2-qx9prz.jpg'
)
textureSphereBg.anisotropy = 16
const geometrySphereBg = new THREE.SphereBufferGeometry(150, 32, 32)
const materialSphereBg = new THREE.MeshBasicMaterial({
side: THREE.BackSide,
map: textureSphereBg,
fog: false,
})
sphereBg = new THREE.Mesh(geometrySphereBg, materialSphereBg)
sphereBg.position.set(0, 50, 0)
scene.add(sphereBg)
// Terrain
const textureTerrain = loader.load()
textureTerrain.rotation = THREE.MathUtils.degToRad(5)
terrainGeometry = new THREE.PlaneBufferGeometry(70, 70, 20, 20)
const terrainMaterial = new THREE.MeshBasicMaterial({
map: textureTerrain,
fog: true,
})
terrain = new THREE.Mesh(terrainGeometry, terrainMaterial)
terrain.rotation.x = -0.47 * Math.PI
terrain.rotation.z = THREE.Math.degToRad(90)
scene.add(terrain)
let t_vertex_Array = terrainGeometry.getAttribute('position').array
terrainGeometry.getAttribute('position').setUsage(THREE.DynamicDrawUsage)
terrainGeometry.setAttribute(
'myZ',
new THREE.BufferAttribute(new Float32Array(t_vertex_Array.length / 3), 1)
)
let t_myZ_Array = terrainGeometry.getAttribute('myZ').array
for (let i = 0; i < t_vertex_Array.length; i++) {
t_myZ_Array[i] = THREE.MathUtils.randInt(0, 5)
}
//Terain Lines
const terrain_line = new THREE.LineSegments(
terrainGeometry,
new THREE.LineBasicMaterial({
color: '#fff',
fog: false,
})
)
terrain_line.rotation.x = -0.47 * Math.PI
terrain_line.rotation.z = THREE.Math.degToRad(90)
scene.add(terrain_line)
// Stars
for (let i = 0; i < lineTotal; i++) {
let x = THREE.MathUtils.randInt(-100, 100)
let y = THREE.MathUtils.randInt(10, 40)
if (x < 7 && x > -7 && y < 20) x += 14
let z = THREE.MathUtils.randInt(0, -300)
l_vertex_Array[6 * i + 0] = l_vertex_Array[6 * i + 3] = x
l_vertex_Array[6 * i + 1] = l_vertex_Array[6 * i + 4] = y
l_vertex_Array[6 * i + 2] = l_vertex_Array[6 * i + 5] = z
l_velocity_Array[2 * i] = l_velocity_Array[2 * i + 1] = 0
}
let starsMaterial = new THREE.LineBasicMaterial({
color: '#ffffff',
transparent: true,
opacity: 0.5,
fog: false,
})
let lines = new THREE.LineSegments(linesGeometry, starsMaterial)
linesGeometry.getAttribute('position').setUsage(THREE.DynamicDrawUsage)
scene.add(lines)
}
function animate() {
planet.rotation.y += 0.002
sphereBg.rotation.x += 0.002
sphereBg.rotation.y += 0.002
sphereBg.rotation.z += 0.002
// Moon Animation
moon.rotation.y -= 0.007
moon.rotation.x -= 0.007
moon.position.x = 15 * Math.cos(t) + 0
moon.position.z = 20 * Math.sin(t) - 35
t += 0.015
// Terrain Animation
let t_vertex_Array = terrainGeometry.getAttribute('position').array
let t_myZ_Array = terrainGeometry.getAttribute('myZ').array
for (let i = 0; i < t_vertex_Array.length; i++) {
if (i >= 210 && i <= 250) t_vertex_Array[i * 3 + 2] = 0
else {
t_vertex_Array[i * 3 + 2] =
Math.sin(i + count * 0.0003) * (t_myZ_Array[i] - t_myZ_Array[i] * 0.5)
count += 0.1
}
}
// Stars Animation
for (let i = 0; i < lineTotal; i++) {
l_velocity_Array[2 * i] += 0.0049
l_velocity_Array[2 * i + 1] += 0.005
l_vertex_Array[6 * i + 2] += l_velocity_Array[2 * i]
l_vertex_Array[6 * i + 5] += l_velocity_Array[2 * i + 1]
if (l_vertex_Array[6 * i + 2] > 50) {
l_vertex_Array[6 * i + 2] = l_vertex_Array[6 * i + 5] =
THREE.MathUtils.randInt(-200, 10)
l_velocity_Array[2 * i] = 0
l_velocity_Array[2 * i + 1] = 0
}
}
//Camera Movement
camera.position.x += cameraDx
camera.position.y = -1.2 * (1 - Math.abs(frame / 2000 - 0.5) / 0.5)
camera.lookAt(0, 0, 0)
frame += 8
if (frame > 2000) frame = 0
if (camera.position.x > 18) cameraDx = -cameraDx
if (camera.position.x < -18) cameraDx = Math.abs(cameraDx)
l_positionAttr.needsUpdate = true
terrainGeometry.attributes.position.needsUpdate = true
renderer.render(scene, camera)
requestAnimationFrame(animate)
}
/* Resize */
window.addEventListener('resize', () => {
clearTimeout(timeout_Debounce)
timeout_Debounce = setTimeout(onWindowResize, 80)
})
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight
camera.updateProjectionMatrix()
renderer.setSize(container.clientWidth, container.clientHeight)
}
/* Fullscreen btn */
let fullscreen
let fsEnter = document.getElementById('fullscr')
fsEnter.addEventListener('click', function (e) {
e.preventDefault()
if (!fullscreen) {
fullscreen = true
document.documentElement.requestFullscreen()
fsEnter.innerHTML = 'Exit Fullscreen'
} else {
fullscreen = false
document.exitFullscreen()
fsEnter.innerHTML = 'Go Fullscreen'
}
})