1
1
import * as THREE from "three" ;
2
2
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls" ;
3
3
import RAPIER from "@dimforge/rapier3d" ;
4
+ import { Matrix4 , Quaternion , Vector3 } from "three" ;
4
5
5
6
const BOX_INSTANCE_INDEX = 0 ;
6
7
const BALL_INSTANCE_INDEX = 1 ;
@@ -14,7 +15,6 @@ interface InstanceDesc {
14
15
groupId : number ;
15
16
instanceId : number ;
16
17
elementId : number ;
17
- highlighted : boolean ;
18
18
scale ?: THREE . Vector3 ;
19
19
}
20
20
@@ -61,9 +61,12 @@ function genHeightfieldGeometry(collider: RAPIER.Collider) {
61
61
} ;
62
62
}
63
63
64
+ const _position = new Vector3 ( ) ;
65
+ const _rotation = new Quaternion ( ) ;
66
+ const _matrix = new Matrix4 ( ) ;
67
+
64
68
export class Graphics {
65
69
raycaster : THREE . Raycaster ;
66
- highlightedCollider : null | number ;
67
70
coll2instance : Map < number , InstanceDesc > ;
68
71
coll2mesh : Map < number , THREE . Mesh > ;
69
72
rb2colls : Map < number , Array < RAPIER . Collider > > ;
@@ -79,7 +82,6 @@ export class Graphics {
79
82
80
83
constructor ( ) {
81
84
this . raycaster = new THREE . Raycaster ( ) ;
82
- this . highlightedCollider = null ;
83
85
this . coll2instance = new Map ( ) ;
84
86
this . coll2mesh = new Map ( ) ;
85
87
this . rb2colls = new Map ( ) ;
@@ -195,13 +197,9 @@ export class Graphics {
195
197
} ) ;
196
198
}
197
199
198
- render ( world : RAPIER . World , debugRender : boolean ) {
200
+ render ( world : RAPIER . World , debugRender : boolean , alpha : number ) {
199
201
kk += 1 ;
200
202
this . controls . update ( ) ;
201
- // if (kk % 100 == 0) {
202
- // console.log(this.camera.position);
203
- // console.log(this.controls.target);
204
- // }
205
203
206
204
this . light . position . set (
207
205
this . camera . position . x ,
@@ -224,7 +222,7 @@ export class Graphics {
224
222
this . lines . visible = false ;
225
223
}
226
224
227
- this . updatePositions ( world ) ;
225
+ this . updatePositions ( world , alpha ) ;
228
226
this . renderer . render ( this . scene , this . camera ) ;
229
227
}
230
228
@@ -242,80 +240,37 @@ export class Graphics {
242
240
this . controls . update ( ) ;
243
241
}
244
242
245
- highlightInstanceId ( ) {
246
- return this . colorPalette . length - 1 ;
247
- }
248
-
249
- highlightCollider ( handle : number ) {
250
- if ( handle == this . highlightedCollider )
251
- // Avoid flickering when moving the mouse on a single collider.
252
- return ;
253
-
254
- if ( this . highlightedCollider != null ) {
255
- let desc = this . coll2instance . get ( this . highlightedCollider ) ;
256
-
257
- if ( ! ! desc ) {
258
- desc . highlighted = false ;
259
- this . instanceGroups [ desc . groupId ] [
260
- this . highlightInstanceId ( )
261
- ] . count = 0 ;
262
- }
263
- }
264
- if ( handle != null ) {
265
- let desc = this . coll2instance . get ( handle ) ;
266
-
267
- if ( ! ! desc ) {
268
- if ( desc . instanceId != 0 )
269
- // Don't highlight static/kinematic bodies.
270
- desc . highlighted = true ;
271
- }
272
- }
273
- this . highlightedCollider = handle ;
274
- }
243
+ updatePositions ( world : RAPIER . World , alpha : number ) {
244
+ world . forEachCollider ( ( collider ) => {
245
+ let gfx = this . coll2instance . get ( collider . handle ) ;
246
+ let translation = collider . translation ( ) ;
247
+ let rotation = collider . rotation ( ) ;
275
248
276
- updatePositions ( world : RAPIER . World ) {
277
- world . forEachCollider ( ( elt ) => {
278
- let gfx = this . coll2instance . get ( elt . handle ) ;
279
- let translation = elt . translation ( ) ;
280
- let rotation = elt . rotation ( ) ;
249
+ _position . set ( translation . x , translation . y , translation . z ) ;
250
+ _rotation . set ( rotation . x , rotation . y , rotation . z , rotation . w ) ;
281
251
282
252
if ( ! ! gfx ) {
283
253
let instance = this . instanceGroups [ gfx . groupId ] [ gfx . instanceId ] ;
284
- dummy . scale . set ( gfx . scale . x , gfx . scale . y , gfx . scale . z ) ;
285
- dummy . position . set ( translation . x , translation . y , translation . z ) ;
286
- dummy . quaternion . set (
287
- rotation . x ,
288
- rotation . y ,
289
- rotation . z ,
290
- rotation . w ,
254
+
255
+ instance . getMatrixAt ( gfx . elementId , _matrix ) ;
256
+ _matrix . decompose (
257
+ dummy . position ,
258
+ dummy . quaternion ,
259
+ dummy . scale ,
291
260
) ;
261
+ dummy . position . lerp ( _position , alpha ) ;
262
+ dummy . quaternion . slerp ( _rotation , alpha ) ;
292
263
dummy . updateMatrix ( ) ;
293
- instance . setMatrixAt ( gfx . elementId , dummy . matrix ) ;
294
-
295
- let highlightInstance =
296
- this . instanceGroups [ gfx . groupId ] [
297
- this . highlightInstanceId ( )
298
- ] ;
299
- if ( gfx . highlighted ) {
300
- highlightInstance . count = 1 ;
301
- highlightInstance . setMatrixAt ( 0 , dummy . matrix ) ;
302
- }
303
264
265
+ instance . setMatrixAt ( gfx . elementId , dummy . matrix ) ;
304
266
instance . instanceMatrix . needsUpdate = true ;
305
- highlightInstance . instanceMatrix . needsUpdate = true ;
306
267
}
307
268
308
- let mesh = this . coll2mesh . get ( elt . handle ) ;
269
+ let mesh = this . coll2mesh . get ( collider . handle ) ;
309
270
310
271
if ( ! ! mesh ) {
311
- mesh . position . set ( translation . x , translation . y , translation . z ) ;
312
- mesh . quaternion . set (
313
- rotation . x ,
314
- rotation . y ,
315
- rotation . z ,
316
- rotation . w ,
317
- ) ;
318
- mesh . updateMatrix ( ) ;
272
+ mesh . position . lerp ( _position , alpha ) ;
273
+ mesh . quaternion . slerp ( _rotation , alpha ) ;
319
274
}
320
275
} ) ;
321
276
}
@@ -337,21 +292,6 @@ export class Graphics {
337
292
this . colorIndex = 0 ;
338
293
}
339
294
340
- // applyModifications(RAPIER: RAPIER_API, world: RAPIER.World, modifications) {
341
- // if (!!modifications) {
342
- // modifications.addCollider.forEach(coll => {
343
- // let collider = world.getCollider(coll.handle);
344
- // this.addCollider(RAPIER, world, collider);
345
- // });
346
- // modifications.removeRigidBody.forEach(body => {
347
- // if (!!this.rb2colls.get(body.handle)) {
348
- // this.rb2colls.get(body.handle).forEach(coll => this.removeCollider(coll));
349
- // this.rb2colls.delete(body.handle);
350
- // }
351
- // });
352
- // }
353
- // }
354
-
355
295
removeRigidBody ( body : RAPIER . RigidBody ) {
356
296
if ( ! ! this . rb2colls . get ( body . handle ) ) {
357
297
this . rb2colls
@@ -399,7 +339,6 @@ export class Graphics {
399
339
groupId : 0 ,
400
340
instanceId : parent . isFixed ( ) ? 0 : this . colorIndex + 1 ,
401
341
elementId : 0 ,
402
- highlighted : false ,
403
342
} ;
404
343
405
344
switch ( collider . shapeType ( ) ) {
@@ -495,12 +434,6 @@ export class Graphics {
495
434
instance . count += 1 ;
496
435
}
497
436
498
- let highlightInstance =
499
- this . instanceGroups [ instanceDesc . groupId ] [
500
- this . highlightInstanceId ( )
501
- ] ;
502
- highlightInstance . count = 0 ;
503
-
504
437
let t = collider . translation ( ) ;
505
438
let r = collider . rotation ( ) ;
506
439
dummy . position . set ( t . x , t . y , t . z ) ;
0 commit comments