forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics_rapier_basic.html
More file actions
200 lines (138 loc) · 5.23 KB
/
Copy pathphysics_rapier_basic.html
File metadata and controls
200 lines (138 loc) · 5.23 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js physics - rapier3d basic</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta property="og:title" content="three.js physics - rapier3d basic">
<meta property="og:type" content="website">
<meta property="og:url" content="https://threejs.org/examples/physics_rapier_basic.html">
<meta property="og:image" content="https://threejs.org/examples/screenshots/physics_rapier_basic.jpg">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
color: #333;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> physics - <a href="https://github.com/dimforge/rapier.js" target="_blank">rapier</a> basic
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { RapierPhysics } from 'three/addons/physics/RapierPhysics.js';
import { RapierHelper } from 'three/addons/helpers/RapierHelper.js';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import Stats from 'three/addons/libs/stats.module.js';
let camera, scene, renderer, stats, controls;
let physics, physicsHelper;
init();
async function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xbfd1e5 );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
camera.position.set( 0, 3, 10 );
const ambient = new THREE.HemisphereLight( 0x555555, 0xFFFFFF );
scene.add( ambient );
const light = new THREE.DirectionalLight( 0xffffff, 4 );
light.position.set( 0, 12.5, 12.5 );
light.castShadow = true;
light.shadow.radius = 3;
light.shadow.blurSamples = 8;
light.shadow.mapSize.width = 1024;
light.shadow.mapSize.height = 1024;
const size = 10;
light.shadow.camera.left = - size;
light.shadow.camera.bottom = - size;
light.shadow.camera.right = size;
light.shadow.camera.top = size;
light.shadow.camera.near = 1;
light.shadow.camera.far = 50;
scene.add( light );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
document.body.appendChild( renderer.domElement );
renderer.setAnimationLoop( animate );
controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true;
controls.target = new THREE.Vector3( 0, 2, 0 );
controls.update();
const geometry = new THREE.BoxGeometry( 10, 0.5, 10 );
const material = new THREE.MeshStandardMaterial( { color: 0xFFFFFF } );
const floor = new THREE.Mesh( geometry, material );
floor.receiveShadow = true;
floor.position.y = - 0.25;
floor.userData.physics = { mass: 0 };
scene.add( floor );
new THREE.TextureLoader().load( 'textures/grid.png', function ( texture ) {
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 20, 20 );
floor.material.map = texture;
floor.material.needsUpdate = true;
} );
stats = new Stats();
document.body.appendChild( stats.dom );
initPhysics();
onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );
}
async function initPhysics() {
//Initialize physics engine using the script in the jsm/physics folder
physics = await RapierPhysics();
physics.addScene( scene );
addBody( );
//Optionally display collider outlines
physicsHelper = new RapierHelper( physics.world );
scene.add( physicsHelper );
setInterval( addBody, 1000 );
}
const geometries = [
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.SphereGeometry( 0.5 ),
new RoundedBoxGeometry( 1, 1, 1, 2, 0.25 )
];
function addBody( ) {
const geometry = geometries[ Math.floor( Math.random() * geometries.length ) ];
const material = new THREE.MeshStandardMaterial( { color: Math.floor( Math.random() * 0xFFFFFF ) } );
const mesh = new THREE.Mesh( geometry, material );
mesh.castShadow = true;
mesh.position.set( Math.random() * 2 - 1, Math.random() * 3 + 6, Math.random() * 2 - 1 );
scene.add( mesh );
//parameter 2 - mass, parameter 3 - restitution ( how bouncy )
physics.addMesh( mesh, 1, 0.5 );
}
function onWindowResize( ) {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
for ( const object of scene.children ) {
if ( object.isMesh ) {
if ( object.position.y < - 10 ) {
scene.remove( object );
physics.removeMesh( object );
}
}
}
if ( physicsHelper ) physicsHelper.update();
controls.update();
renderer.render( scene, camera );
stats.update();
}
</script>
</body>
</html>