-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
171 lines (131 loc) · 5.08 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
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
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
function onMouseMove(event) {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
}
window.addEventListener('mousemove', onMouseMove, false);
import starsTexture from './img/star2.jpg';
import earthTexture from './img/earthm.jpg';
import lunarTexture from './img/lunar.jpg';
const ShuttleUrl = new URL('./discovery_space_shuttle/scene.gltf', import.meta.url);
const SatelliteUrl = new URL('./Satellite/scene.gltf', import.meta.url);
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(
50,
window.innerWidth / window.innerHeight,
0.1,
1000
);
const orbit = new OrbitControls(camera, renderer.domElement);
camera.position.set(-80, 10, 90);
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 earthGeo = new THREE.SphereGeometry(16, 30, 30);
const earthMat = new THREE.MeshBasicMaterial({
map: textureLoader.load(earthTexture)
});
const earth = new THREE.Mesh(earthGeo, earthMat);
scene.add(earth);
function createPlanete(size, texture, position) {
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);
scene.add(obj);
mesh.position.x = position;
return { mesh, obj };
}
const lunar = createPlanete(2.8, lunarTexture, 50);
const pointLight = new THREE.PointLight(0xFFFFFF, 2, 300);
scene.add(pointLight);
const shuttleOrbitRadius = 40;
const shuttleOrbitSpeed = 0.001;
const shuttleOrbitCenter = new THREE.Vector3(0, 0, 0);
let angle = 0;
const assetLoader = new GLTFLoader();
let model;
assetLoader.load(ShuttleUrl.href, function (gltf) {
model = gltf.scene;
scene.add(model);
model.scale.set(0.1, 0.1, 0.1);
// Position the shuttle near the moon
model.position.set(52, 0, 0);
// Add a point light to illuminate the
const modelPointLight = new THREE.PointLight(0xffffff, 2);
modelPointLight.position.set(-5, 30, 10);
model.add(modelPointLight);
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
}, undefined, function (error) {
console.error(error);
});
// Define variables for satellite orbit
const satelliteOrbitRadius = 20; // Radius of the orbit around the Earth
const satelliteOrbitSpeed = 0.02; // Speed of the orbit
let satelliteAngle = 0; // Initialize angle for satellite's orbit
const satellitetCenter = new THREE.Vector3(0, 0, 0);
// Load the satellite model
const satelliteAssetLoader = new GLTFLoader();
let satelliteModel; // Declare satellite model variable globally
satelliteAssetLoader.load(SatelliteUrl.href, function (gltf) {
satelliteModel = gltf.scene;
scene.add(satelliteModel);
// Scale down the satellite
satelliteModel.scale.set(0.1, 0.1, 0.1);
}, undefined, function (error) {
console.error(error);
});
function animate() {
// Self-rotation of earth and lunar
earth.rotateY(0.004);
lunar.mesh.rotateY(0.009);
raycaster.setFromCamera(mouse, camera);
// Around-earth-rotation of lunar
lunar.obj.rotateY(0.001);
// Update angle for shuttle's orbit
angle -= shuttleOrbitSpeed; // Change the direction of orbit
// Calculate the new position of the shuttle
const shuttleX = shuttleOrbitCenter.x + shuttleOrbitRadius * Math.cos(angle);
const shuttleZ = shuttleOrbitCenter.z + shuttleOrbitRadius * Math.sin(angle);
// Update the position of the space shuttle
if (model) { // Ensure the model is loaded
model.position.set(shuttleX, 0, shuttleZ);
}
// Update angle for satellite's orbit
satelliteAngle -= satelliteOrbitSpeed;
// Calculate the new position of the satellite
const satelliteX = satellitetCenter.x + satelliteOrbitRadius * Math.cos(satelliteAngle);
const satelliteZ = satellitetCenter.z + satelliteOrbitRadius * Math.sin(satelliteAngle);
// Update the position of the satellite
if (satelliteModel) { // Ensure the model is loaded
satelliteModel.position.set(satelliteX, 0, satelliteZ);
}
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);
});