-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
115 lines (93 loc) · 3.47 KB
/
main.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
import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
import { GUI } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/lil-gui.esm.min.js';
import { GLTFLoader } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/GLTFLoader.js';
// Scene setup
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(2, 2, 5);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
// Create Axes Helper
const axesHelper = new THREE.AxesHelper(1.5);
scene.add(axesHelper);
// Scene lighting
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
scene.add(directionalLight);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
// Create Gimbal System
const createGimbal = (size, color) => {
const group = new THREE.Group();
const geometry = new THREE.TorusGeometry(size, 0.05, 16, 100);
const material = new THREE.MeshLambertMaterial({ color });
const torus = new THREE.Mesh(geometry, material);
group.add(torus);
const pivot1 = new THREE.Mesh(
new THREE.SphereGeometry(0.05, 16, 16),
material
);
pivot1.position.x = size+0.05;
group.add(pivot1);
const pivot2 = new THREE.Mesh(
new THREE.SphereGeometry(0.05, 16, 16),
material
);
pivot2.position.x = -size - 0.05;
group.add(pivot2);
return group;
};
const gimbalX = createGimbal(1, 0xff0000);
gimbalX.rotation.x = 0;
scene.add(gimbalX);
const gimbalY = createGimbal(0.85, 0x00ff00);
const relX = new THREE.Object3D()
relX.rotation.z = Math.PI / 2;
relX.rotation.y = Math.PI / 2;
gimbalX.add(relX);
relX.add(gimbalY);
const gimbalZ = createGimbal(0.7, 0x0000ff);
const relY = new THREE.Object3D()
relY.rotation.z = Math.PI / 2;
relY.rotation.y = Math.PI / 2;
gimbalY.add(relY);
relY.add(gimbalZ);
const plane = await new GLTFLoader().loadAsync('plane.glb');
plane.scene.rotation.x = Math.PI / 2;
plane.scene.scale.set(0.08, 0.08, -0.08);
plane.scene.position.z = -.1;
plane.scene.position.y = +.1;
gimbalZ.add(plane.scene)
// Just a box...
// const cube = new THREE.Mesh(new THREE.BoxGeometry(0.3, 0.3, 0.3), new THREE.MeshNormalMaterial());
// gimbalZ.add(cube);
// GUI Controls
const gui = new GUI();
const rotationControls = {Rx: 0, Ry: 0, Rz: 0};
gui.add(rotationControls, 'Rx', -Math.PI/2, Math.PI/2, 0.01).onChange(value => gimbalX.rotation.x = value);
gui.add(rotationControls, 'Ry', -Math.PI/2, Math.PI/2, 0.01).onChange(value => gimbalY.rotation.x = value);
gui.add(rotationControls, 'Rz', -Math.PI/2, Math.PI/2, 0.01).onChange(value => gimbalZ.rotation.x = value);
// // Reset button
// gui.add({
// reset: () => {
// gimbalX.rotation.x = 0;
// gimbalY.rotation.x = 0;
// gimbalZ.rotation.x = 0;
// }
// }, 'reset').name('Reset Rotations');
// Animation loop
const animate = () => {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
animate();
// Handle window resize
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});