forked from BabylonJS/Babylon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysicsViewer.ts
More file actions
351 lines (301 loc) · 13.8 KB
/
Copy pathphysicsViewer.ts
File metadata and controls
351 lines (301 loc) · 13.8 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { Nullable } from "../types";
import { Scene } from "../scene";
import { AbstractMesh } from "../Meshes/abstractMesh";
import { Mesh } from "../Meshes/mesh";
import { CreateBox } from "../Meshes/Builders/boxBuilder";
import { CreateSphere } from "../Meshes/Builders/sphereBuilder";
import { Quaternion, Vector3 } from "../Maths/math.vector";
import { Color3 } from '../Maths/math.color';
import { Material } from "../Materials/material";
import { EngineStore } from "../Engines/engineStore";
import { StandardMaterial } from "../Materials/standardMaterial";
import { IPhysicsEnginePlugin } from "../Physics/IPhysicsEngine";
import { PhysicsImpostor } from "../Physics/physicsImpostor";
import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
import { CreateCylinder } from '../Meshes/Builders/cylinderBuilder';
import { CreateCapsule, ICreateCapsuleOptions } from '../Meshes/Builders/capsuleBuilder';
/**
* Used to show the physics impostor around the specific mesh
*/
export class PhysicsViewer {
/** @hidden */
protected _impostors: Array<Nullable<PhysicsImpostor>> = [];
/** @hidden */
protected _meshes: Array<Nullable<AbstractMesh>> = [];
/** @hidden */
protected _scene: Nullable<Scene>;
/** @hidden */
protected _numMeshes = 0;
/** @hidden */
protected _physicsEnginePlugin: Nullable<IPhysicsEnginePlugin>;
private _renderFunction: () => void;
private _utilityLayer: Nullable<UtilityLayerRenderer>;
private _debugBoxMesh: Mesh;
private _debugSphereMesh: Mesh;
private _debugCapsuleMesh: Mesh;
private _debugCylinderMesh: Mesh;
private _debugMaterial: StandardMaterial;
private _debugMeshMeshes = new Array<Mesh>();
/**
* Creates a new PhysicsViewer
* @param scene defines the hosting scene
*/
constructor(scene: Scene) {
this._scene = scene || EngineStore.LastCreatedScene;
let physicEngine = this._scene.getPhysicsEngine();
if (physicEngine) {
this._physicsEnginePlugin = physicEngine.getPhysicsPlugin();
}
this._utilityLayer = new UtilityLayerRenderer(this._scene, false);
this._utilityLayer.pickUtilitySceneFirst = false;
this._utilityLayer.utilityLayerScene.autoClearDepthAndStencil = true;
}
/** @hidden */
protected _updateDebugMeshes(): void {
var plugin = this._physicsEnginePlugin;
for (var i = 0; i < this._numMeshes; i++) {
let impostor = this._impostors[i];
if (!impostor) {
continue;
}
if (impostor.isDisposed) {
this.hideImpostor(this._impostors[i--]);
} else {
if (impostor.type === PhysicsImpostor.MeshImpostor) {
continue;
}
let mesh = this._meshes[i];
if (mesh && plugin) {
plugin.syncMeshWithImpostor(mesh, impostor);
}
}
}
}
/**
* Renders a specified physic impostor
* @param impostor defines the impostor to render
* @param targetMesh defines the mesh represented by the impostor
* @returns the new debug mesh used to render the impostor
*/
public showImpostor(impostor: PhysicsImpostor, targetMesh?: Mesh): Nullable<AbstractMesh> {
if (!this._scene) {
return null;
}
for (var i = 0; i < this._numMeshes; i++) {
if (this._impostors[i] == impostor) {
return null;
}
}
var debugMesh = this._getDebugMesh(impostor, targetMesh);
if (debugMesh) {
this._impostors[this._numMeshes] = impostor;
this._meshes[this._numMeshes] = debugMesh;
if (this._numMeshes === 0) {
this._renderFunction = this._updateDebugMeshes.bind(this);
this._scene.registerBeforeRender(this._renderFunction);
}
this._numMeshes++;
}
return debugMesh;
}
/**
* Hides a specified physic impostor
* @param impostor defines the impostor to hide
*/
public hideImpostor(impostor: Nullable<PhysicsImpostor>) {
if (!impostor || !this._scene || !this._utilityLayer) {
return;
}
var removed = false;
const utilityLayerScene = this._utilityLayer.utilityLayerScene;
for (var i = 0; i < this._numMeshes; i++) {
if (this._impostors[i] == impostor) {
let mesh = this._meshes[i];
if (!mesh) {
continue;
}
utilityLayerScene.removeMesh(mesh);
mesh.dispose();
let index = this._debugMeshMeshes.indexOf(mesh as Mesh);
if (index > -1) {
this._debugMeshMeshes.splice(index, 1);
}
this._numMeshes--;
if (this._numMeshes > 0) {
this._meshes[i] = this._meshes[this._numMeshes];
this._impostors[i] = this._impostors[this._numMeshes];
this._meshes[this._numMeshes] = null;
this._impostors[this._numMeshes] = null;
} else {
this._meshes[0] = null;
this._impostors[0] = null;
}
removed = true;
break;
}
}
if (removed && this._numMeshes === 0) {
this._scene.unregisterBeforeRender(this._renderFunction);
}
}
private _getDebugMaterial(scene: Scene): Material {
if (!this._debugMaterial) {
this._debugMaterial = new StandardMaterial('', scene);
this._debugMaterial.wireframe = true;
this._debugMaterial.emissiveColor = Color3.White();
this._debugMaterial.disableLighting = true;
}
return this._debugMaterial;
}
private _getDebugBoxMesh(scene: Scene): AbstractMesh {
if (!this._debugBoxMesh) {
this._debugBoxMesh = CreateBox('physicsBodyBoxViewMesh', { size: 1 }, scene);
this._debugBoxMesh.rotationQuaternion = Quaternion.Identity();
this._debugBoxMesh.material = this._getDebugMaterial(scene);
this._debugBoxMesh.setEnabled(false);
}
return this._debugBoxMesh.createInstance('physicsBodyBoxViewInstance');
}
private _getDebugSphereMesh(scene: Scene): AbstractMesh {
if (!this._debugSphereMesh) {
this._debugSphereMesh = CreateSphere('physicsBodySphereViewMesh', { diameter: 1 }, scene);
this._debugSphereMesh.rotationQuaternion = Quaternion.Identity();
this._debugSphereMesh.material = this._getDebugMaterial(scene);
this._debugSphereMesh.setEnabled(false);
}
return this._debugSphereMesh.createInstance('physicsBodySphereViewInstance');
}
private _getDebugCapsuleMesh(scene: Scene): AbstractMesh {
if (!this._debugCapsuleMesh) {
this._debugCapsuleMesh = CreateCapsule('physicsBodyCapsuleViewMesh', { height: 1 } as ICreateCapsuleOptions, scene);
this._debugCapsuleMesh.rotationQuaternion = Quaternion.Identity();
this._debugCapsuleMesh.material = this._getDebugMaterial(scene);
this._debugCapsuleMesh.setEnabled(false);
}
return this._debugCapsuleMesh.createInstance('physicsBodyCapsuleViewInstance');
}
private _getDebugCylinderMesh(scene: Scene): AbstractMesh {
if (!this._debugCylinderMesh) {
this._debugCylinderMesh = CreateCylinder('physicsBodyCylinderViewMesh', { diameterTop: 1, diameterBottom: 1, height: 1 }, scene);
this._debugCylinderMesh.rotationQuaternion = Quaternion.Identity();
this._debugCylinderMesh.material = this._getDebugMaterial(scene);
this._debugCylinderMesh.setEnabled(false);
}
return this._debugCylinderMesh.createInstance('physicsBodyCylinderViewInstance');
}
private _getDebugMeshMesh(mesh: Mesh, scene: Scene): AbstractMesh {
var wireframeOver = new Mesh(mesh.name, scene, null, mesh);
wireframeOver.setParent(mesh);
wireframeOver.position = Vector3.Zero();
wireframeOver.material = this._getDebugMaterial(scene);
this._debugMeshMeshes.push(wireframeOver);
return wireframeOver;
}
private _getDebugMesh(impostor: PhysicsImpostor, targetMesh?: Mesh): Nullable<AbstractMesh> {
if (!this._utilityLayer) {
return null;
}
// Only create child impostor debug meshes when evaluating the parent
if (targetMesh && targetMesh.parent && (targetMesh.parent as Mesh).physicsImpostor) {
return null;
}
var mesh: Nullable<AbstractMesh> = null;
const utilityLayerScene = this._utilityLayer.utilityLayerScene;
switch (impostor.type) {
case PhysicsImpostor.BoxImpostor:
mesh = this._getDebugBoxMesh(utilityLayerScene);
impostor.getBoxSizeToRef(mesh.scaling);
break;
case PhysicsImpostor.SphereImpostor:
mesh = this._getDebugSphereMesh(utilityLayerScene);
var radius = impostor.getRadius();
mesh.scaling.x = radius * 2;
mesh.scaling.y = radius * 2;
mesh.scaling.z = radius * 2;
break;
case PhysicsImpostor.CapsuleImpostor:
mesh = this._getDebugCapsuleMesh(utilityLayerScene);
var bi = impostor.object.getBoundingInfo();
mesh.scaling.x = (bi.boundingBox.maximum.x - bi.boundingBox.minimum.x) * 2 * impostor.object.scaling.x;
mesh.scaling.y = (bi.boundingBox.maximum.y - bi.boundingBox.minimum.y) * impostor.object.scaling.y;
mesh.scaling.z = (bi.boundingBox.maximum.z - bi.boundingBox.minimum.z) * 2 * impostor.object.scaling.z;
break;
case PhysicsImpostor.MeshImpostor:
if (targetMesh) {
mesh = this._getDebugMeshMesh(targetMesh, utilityLayerScene);
}
break;
case PhysicsImpostor.NoImpostor:
if (targetMesh) {
// Handle compound impostors
var childMeshes = targetMesh.getChildMeshes().filter((c) => { return c.physicsImpostor ? 1 : 0; });
childMeshes.forEach((m) => {
if (m.physicsImpostor && m.getClassName() === "Mesh") {
const boundingInfo = m.getBoundingInfo();
const min = boundingInfo.boundingBox.minimum;
const max = boundingInfo.boundingBox.maximum;
switch (m.physicsImpostor.type) {
case PhysicsImpostor.BoxImpostor:
mesh = this._getDebugBoxMesh(utilityLayerScene);
mesh.position.copyFrom(min);
mesh.position.addInPlace(max);
mesh.position.scaleInPlace(0.5);
break;
case PhysicsImpostor.SphereImpostor:
mesh = this._getDebugSphereMesh(utilityLayerScene);
break;
case PhysicsImpostor.CylinderImpostor:
mesh = this._getDebugCylinderMesh(utilityLayerScene);
break;
default:
mesh = null;
break;
}
if (mesh) {
mesh.scaling.x = max.x - min.x;
mesh.scaling.y = max.y - min.y;
mesh.scaling.z = max.z - min.z;
mesh.parent = m;
}
}
});
}
mesh = null;
break;
case PhysicsImpostor.CylinderImpostor:
mesh = this._getDebugCylinderMesh(utilityLayerScene);
var bi = impostor.object.getBoundingInfo();
mesh.scaling.x = (bi.boundingBox.maximum.x - bi.boundingBox.minimum.x) * impostor.object.scaling.x;
mesh.scaling.y = (bi.boundingBox.maximum.y - bi.boundingBox.minimum.y) * impostor.object.scaling.y;
mesh.scaling.z = (bi.boundingBox.maximum.z - bi.boundingBox.minimum.z) * impostor.object.scaling.z;
break;
}
return mesh;
}
/** Releases all resources */
public dispose() {
let count = this._numMeshes;
for (var index = 0; index < count; index++) {
this.hideImpostor(this._impostors[0]);
}
if (this._debugBoxMesh) {
this._debugBoxMesh.dispose();
}
if (this._debugSphereMesh) {
this._debugSphereMesh.dispose();
}
if (this._debugCylinderMesh) {
this._debugCylinderMesh.dispose();
}
if (this._debugMaterial) {
this._debugMaterial.dispose();
}
this._impostors.length = 0;
this._scene = null;
this._physicsEnginePlugin = null;
if (this._utilityLayer) {
this._utilityLayer.dispose();
this._utilityLayer = null;
}
}
}