-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (108 loc) · 4.06 KB
/
script.js
File metadata and controls
137 lines (108 loc) · 4.06 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
import * as THREE from 'three';
import { MTLLoader } from 'MTLLoader';
import { OBJLoader } from 'OBJLoader';
import { Physics, RigidBody } from 'three-ammo';
const mtlLoader = new MTLLoader();
// Create the scene
const scene = new THREE.Scene();
// Set up the camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// Set up the renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Add a light source
const light = new THREE.AmbientLight(0xffffff, 0.5); // Soft white light
scene.add(light);
// Additional directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(0, 1, 0);
scene.add(directionalLight);
let objMesh, physics;
// Function to initialize Ammo.js physics
async function initPhysics() {
console.log("Initializing physics.");
await Physics();
// Create physics world
physics = new Physics({ maxSubSteps: 10 });
// Create a ground plane using a rigid body
const groundGeometry = new THREE.PlaneGeometry(100, 100);
const groundMaterial = new THREE.MeshStandardMaterial({ color: 0x226622 });
const groundMesh = new THREE.Mesh(groundGeometry, groundMaterial);
groundMesh.rotation.x = -Math.PI / 2; // Rotate the mesh instead of the rigidBody
groundMesh.position.y = -1;
scene.add(groundMesh); // Add the ground mesh to the scene
const groundBody = new RigidBody({
mass: 0, // Static body
shape: 'box',
mesh: groundMesh,
scene: scene,
});
physics.addMesh(groundMesh, 0); // Add ground mesh to physics world with 0 mass (static)
return physics;
}
// Drop an object onto the scene floor every 2 seconds
function dropObjects(physics) {
console.log("Dropping objects.");
setInterval(() => {
if (objMesh) {
// Clone the mesh for a new dropping object
const objMeshClone = objMesh.clone();
scene.add(objMeshClone); // Add the clone to the scene
// Create a rigid body for the object
const rigidBody = new RigidBody({
mass: 1,
shape: 'convexHull',
mesh: objMeshClone,
scene: scene,
});
console.log("Object mesh added to scene.");
physics.addMesh(objMeshClone, 1); // Add the clone to the physics world with 1 mass (dynamic)
// Set the initial position of the object
rigidBody.mesh.position.set(0, 10, 0); // Drop from 10 units above the ground
}
}, 2000); // Repeat every 2000 milliseconds (2 seconds)
}
// Initialize both the physics and set up dropping objects when the model is loaded
mtlLoader.load('figurine.mtl', materials => {
materials.preload();
const objLoader = new OBJLoader();
objLoader.setMaterials(materials);
objLoader.load('figurine.obj', object => {
console.log("Model loaded.");
objMesh = object;
objMesh.scale.set(0.5, 0.5, 0.5); // Preserved existing scale
objMesh.position.set(0, 0, 0); // Preserved existing position
scene.add(objMesh);
// Initialize physics and start dropping objects
initPhysics().then(p => {
physics = p;
dropObjects(physics);
});
animate();
});
});
// Modified animate function to include physics update
function animate() {
console.log("Animating frame.");
requestAnimationFrame(animate);
if (physics) {
physics.update(1 / 60); // Update the physics world
}
console.log("Rendering frame.");
renderer.render(scene, camera);
}
// Ensure the renderer and any other components handle resizes:
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Temporary code to make sure Three.js is rendering
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
animate(); // make sure this is still being called