-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
166 lines (139 loc) · 4.93 KB
/
main.js
File metadata and controls
166 lines (139 loc) · 4.93 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import * as THREE from 'three';
import { gsap } from 'gsap';
import vertexShaderEarth from './shaders/earthShaders/vertex.glsl'
import fragmentShaderEarth from './shaders/earthShaders/fragment.glsl'
import atmoVertexEarth from './shaders/earthShaders/atmoVertex.glsl'
import atmoFragmentEarth from './shaders/earthShaders/atmoFragment.glsl'
import stars from './helpers/useStars';
const size = {
width: innerWidth,
height: innerHeight
}
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, size.width / size.height, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(size.width, size.height);
renderer.setPixelRatio(window.devicePixelRatio)
document.body.appendChild(renderer.domElement);
const geometryEarth = new THREE.SphereGeometry(5, 50, 50);
const materialEarth = new THREE.ShaderMaterial({
vertexShader: vertexShaderEarth,
fragmentShader: fragmentShaderEarth,
uniforms: {
globeTexture: {
value: new THREE.TextureLoader().load('./image/earth.jpg')
}
}
});
const earth = new THREE.Mesh(geometryEarth, materialEarth);
const atmoGeometry = new THREE.SphereGeometry(5, 50, 50)
const atmoMaterial = new THREE.ShaderMaterial({
vertexShader: atmoVertexEarth,
fragmentShader: atmoFragmentEarth,
blending: THREE.AdditiveBlending,
side: THREE.BackSide
})
const atmosphereEarth = new THREE.Mesh(atmoGeometry, atmoMaterial)
atmosphereEarth.scale.set(1.08, 1.08, 1.08)
scene.add(atmosphereEarth)
//moon
const geometryMoon = new THREE.SphereGeometry(0.3, 30, 30);
const materialMoon = new THREE.ShaderMaterial({
vertexShader: vertexShaderEarth,
fragmentShader: fragmentShaderEarth,
uniforms: {
globeTexture: {
value: new THREE.TextureLoader().load('./image/moon.jpg')
}
}
});
const moon = new THREE.Mesh(geometryMoon, materialMoon);
//atmo moon
const atmoGeometryMoon = new THREE.SphereGeometry(0.3, 30, 30)
const atmoMaterialMoon = new THREE.ShaderMaterial({
vertexShader: atmoVertexEarth,
fragmentShader: atmoFragmentEarth,
blending: THREE.AdditiveBlending,
side: THREE.BackSide
})
const atmosphereMoon = new THREE.Mesh(atmoGeometryMoon, atmoMaterialMoon)
atmosphereMoon.scale.set(1.2, 1.2, 1.2)
scene.add(atmosphereMoon)
//atmo moon
const group = new THREE.Group()
const moonGroup = new THREE.Group()
moonGroup.position.x = 10
moonGroup.add(moon)
moonGroup.add(atmosphereMoon)
group.add(earth)
group.add(moonGroup)
scene.add(group)
scene.add(stars)
camera.position.z = 100;
const clock = new THREE.Clock()
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
const elapsedTime = clock.getElapsedTime()
earth.rotation.y += 0.01
const moonOrbitRadius = 8;
const moonOrbitSpeed = 0.3;
const moonPosition = new THREE.Vector3();
//for satellite
// moonPosition.x = Math.cos(moonOrbitSpeed * Date.now() * 0.1) * moonOrbitRadius/2;
// moonPosition.z = Math.sin(moonOrbitSpeed * Date.now() * 0.1) * moonOrbitRadius;
// moonPosition.y = Math.cos(moonOrbitSpeed * Date.now() * 0.1) * moonOrbitRadius/2;
moonPosition.x = Math.cos(moonOrbitSpeed * elapsedTime) * moonOrbitRadius;
moonPosition.z = Math.sin(moonOrbitSpeed * elapsedTime) * moonOrbitRadius;
moonGroup.position.copy(moonPosition);
moonGroup.rotation.y += 0.1;
// moon.lookAt(earth.position);
}
animate();
const mouse = {
x: 0,
y: 0
}
addEventListener('mousemove', (event) => {
mouse.x = (event.clientX / innerWidth) * 2 - 1
mouse.y = -(event.clientY / innerHeight) * 2 + 1
gsap.to(group.rotation, { y: mouse.x * 0.6, x: -mouse.y * 0.5, duration: 1 })
})
window.addEventListener('resize', function () {
var width = window.innerWidth;
var height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
let isResized = false
function onCanvasClick(event) {
// Calculate the mouse position in normalized device coordinates
const mouse = new THREE.Vector2();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
// Create a raycaster and an array to store the intersected objects
const raycaster = new THREE.Raycaster();
const intersects = [];
// Update the raycaster's origin based on the camera's position
raycaster.setFromCamera(mouse, camera);
// Perform the intersection check and populate the 'intersects' array
raycaster.intersectObject(earth, true, intersects);
// If there are intersections
if (intersects.length > 0) {
const clickedMesh = intersects[0].object;
// Example: Change the color of the clicked mesh
console.log(clickedMesh, 'clickedMesh')
console.log(mouse, 'mouse')
if (isResized === false) {
gsap.to(camera.position, { z: 15, duration: 1 })
} else {
gsap.to(camera.position, { z: 100, duration: 1 })
}
isResized = !isResized
}
}
// Assuming 'renderer' is your Three.js renderer
renderer.domElement.addEventListener('click', onCanvasClick);