-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
249 lines (201 loc) · 8.42 KB
/
index.html
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
245
246
247
248
249
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="/ammo.wasm.js"></script>
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { AmmoPhysics } from '/physics.js';
let camera, scene, renderer;
let physics, position, controls;
let isMouseDown = false;
window.addEventListener('pointermove', onPointerMove);
window.addEventListener('mousedown', (e) => isMouseDown |= e.button == 0);
window.addEventListener('mouseup', (e) => {
if (e.button == 0) {
isMouseDown = false;
}
});
init();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function onPointerMove(event) {
// calculate pointer position in normalized device coordinates
// (-1 to +1) for both components
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = - (event.clientY / window.innerHeight) * 2 + 1;
}
let detectionPlane;
let boxes = [];
async function init() {
physics = await AmmoPhysics();
position = new THREE.Vector3();
//
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(- 1, 1.5, 2);
camera.lookAt(0, 0.5, 0);
scene = new THREE.Scene();
scene.background = new THREE.Color(0x666666);
const hemiLight = new THREE.HemisphereLight();
hemiLight.intensity = 0.35;
scene.add(hemiLight);
const dirLight = new THREE.DirectionalLight();
dirLight.position.set(5, 5, 5);
dirLight.castShadow = true;
dirLight.shadow.camera.zoom = 2;
scene.add(dirLight);
const floor = new THREE.Mesh(
new THREE.BoxGeometry(.21 * 3, .1, .21 * 3),
new THREE.MeshPhysicalMaterial({ color: 0x111111 })
);
floor.position.y = -.1;
floor.receiveShadow = true;
floor.castShadow = true;
scene.add(floor);
physics.addMesh(floor);
detectionPlane = new THREE.Mesh(
new THREE.BoxGeometry(10, 1, 10),
new THREE.MeshPhysicalMaterial({ color: 0xFF0000 })
);
detectionPlane.position.y = -.6;
detectionPlane.receiveShadow = true;
scene.add(detectionPlane);
physics.addMesh(detectionPlane, 1);
let balls = new THREE.Mesh(
new THREE.BoxGeometry(10, 1, 10),
new THREE.MeshPhysicalMaterial({ color: 0xFF0000 })
);
balls.position.y = -1.65;
balls.receiveShadow = true;
scene.add(balls);
physics.addMesh(balls);
// Boxes
const texture = new THREE.TextureLoader().load("textures/wood.jpg");
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1, 1);
const normals = new THREE.TextureLoader().load("textures/NormalMap.png");
normals.wrapS = THREE.RepeatWrapping;
normals.wrapT = THREE.RepeatWrapping;
normals.repeat.set(1, 1);
let isDeus = false;
const geometryBox = new THREE.BoxGeometry(0.6, 0.1, 0.2);
for (let j = 0; j < 20; j++) {
for (let k = -1; k < 2; k++) {
let material = new THREE.MeshPhysicalMaterial({
color: 0xffffff * Math.random(),
clearcoat: 1.0,
clearcoatNormalMap: normals,
clearcoatRoughness: 1.0,
clearcoatNormalScale: { x: .1, y: .1 }
});
let box = new THREE.Mesh(geometryBox, material);
box.material.map = texture;
box.castShadow = true;
box.receiveShadow = true;
let i = j * 3 + k;
if (j % 2 == 0) {
box.rotation.y = 0;
box.position.x = 0;
box.position.y = j * .11;
box.position.z = k * .21;
} else {
box.rotation.y = Math.PI / 2;
box.position.x = k * .21;
box.position.y = j * .11;
box.position.z = 0;
}
box.userData = {
canTouch: true,
touched: false
};
boxes.push(box);
scene.add(box);
physics.addMesh(box, .5);
let callback = new Ammo.ConcreteContactResultCallback();
let once = true;
callback.addSingleResult = (cp, colObj0Wrap, partId0, index0, colObj1Wrap, partId1, index1) => {
if (once) {
once = false;
if (!box.userData.touched) {
if (!isDeus) {
alert("FAIL FAIL FAIL FAIL");
isDeus = true;
}
} else {
mustCommit = false;
}
physics.world.removeCollisionObject(physics.meshMap.get(box));
scene.remove(box);
}
};
callbacks.push(callback);
}
}
//
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild(renderer.domElement);
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.target.y = .5;
controls.mouseButtons = {
LEFT: THREE.MOUSE.NONE,
MIDDLE: THREE.MOUSE.PAN,
RIGHT: THREE.MOUSE.ROTATE
}
animate();
}
let callbacks = [];
let mustCommit = false;
function animate() {
requestAnimationFrame(animate);
raycaster.setFromCamera(pointer, camera);
physics.step();
renderer.render(scene, camera);
for (let i = 0; i < boxes.length; i++) {
const box = boxes[i];
const callback = callbacks[i];
physics.world.contactPairTest(physics.meshMap.get(box), physics.meshMap.get(detectionPlane), callback);
}
const intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0 && isMouseDown) {
let intersect = intersects[0];
let obj = intersect.object;
if (obj.userData.canTouch && (!mustCommit || obj.userData.touched)) {
obj.userData.touched = true;
mustCommit = true;
let intersectPos = intersect.point.clone().sub(obj.position);
let force = intersectPos.clone().normalize().multiplyScalar(20);
physics.applyForce(obj, force, intersectPos);
}
}
controls.update();
}
</script>
</body>
</html>