forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.js
More file actions
584 lines (493 loc) · 20.4 KB
/
system.js
File metadata and controls
584 lines (493 loc) · 20.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
pc.extend(pc, function () {
var _schema = [
'enabled',
'type',
'halfExtents',
'radius',
'axis',
'height',
'asset',
'shape',
'model'
];
/**
* @name pc.CollisionComponentSystem
* @description Creates a new CollisionComponentSystem.
* @class Manages creation of {@link pc.CollisionComponent}s.
* @param {pc.Application} app The running {pc.Application}
* @extends pc.ComponentSystem
*/
var CollisionComponentSystem = function CollisionComponentSystem (app) {
this.id = "collision";
this.description = "Specifies a collision volume.";
app.systems.add(this.id, this);
this.ComponentType = pc.CollisionComponent;
this.DataType = pc.CollisionComponentData;
this.schema = _schema;
this.implementations = { };
this.on('remove', this.onRemove, this);
pc.ComponentSystem.on('update', this.onUpdate, this);
};
CollisionComponentSystem = pc.inherits(CollisionComponentSystem, pc.ComponentSystem);
pc.Component._buildAccessors(pc.CollisionComponent.prototype, _schema);
CollisionComponentSystem.prototype = pc.extend(CollisionComponentSystem.prototype, {
onLibraryLoaded: function () {
if (typeof Ammo !== 'undefined') {
//
} else {
// Unbind the update function if we haven't loaded Ammo by now
pc.ComponentSystem.off('update', this.onUpdate, this);
}
},
initializeComponentData: function (component, _data, properties) {
// duplicate the input data because we are modifying it
var idx;
var data = {};
properties = ['type', 'halfExtents', 'radius', 'axis', 'height', 'shape', 'model', 'asset', 'enabled'];
properties.forEach(function (prop) {
data[prop] = _data[prop];
});
// asset takes priority over model
// but they are both trying to change the mesh
// so remove one of them to avoid conflicts
if (_data.hasOwnProperty('asset')) {
idx = properties.indexOf('model');
if (idx !== -1) {
properties.splice(idx, 1);
}
} else if (_data.hasOwnProperty('model')) {
idx = properties.indexOf('asset');
if (idx !== -1) {
properties.splice(idx, 1);
}
}
if (!data.type) {
data.type = component.data.type;
}
component.data.type = data.type;
if (data.halfExtents && pc.type(data.halfExtents) === 'array') {
data.halfExtents = new pc.Vec3(data.halfExtents[0], data.halfExtents[1], data.halfExtents[2]);
}
var impl = this._createImplementation(data.type);
impl.beforeInitialize(component, data);
CollisionComponentSystem._super.initializeComponentData.call(this.system, component, data, properties);
impl.afterInitialize(component, data);
},
/**
* @private
* @description
* Creates an implementation based on the collision type and caches it
* in an internal implementations structure, before returning it.
*/
_createImplementation: function (type) {
if (this.implementations[type] === undefined) {
var impl;
switch (type) {
case 'box':
impl = new CollisionBoxSystemImpl(this);
break;
case 'sphere':
impl = new CollisionSphereSystemImpl(this);
break;
case 'capsule':
impl = new CollisionCapsuleSystemImpl(this);
break;
case 'cylinder':
impl = new CollisionCylinderSystemImpl(this);
break;
case 'mesh':
impl = new CollisionMeshSystemImpl(this);
break;
default:
throw "Invalid collision system type: " + type;
}
this.implementations[type] = impl;
}
return this.implementations[type];
},
/**
* @private
* @description Gets an existing implementation for the specified entity
*/
_getImplementation: function (entity) {
return this.implementations[entity.collision.data.type];
},
cloneComponent: function (entity, clone) {
return this._getImplementation(entity).clone(entity, clone);
},
onRemove: function (entity, data) {
this.implementations[data.type].remove(entity, data);
},
onUpdate: function (dt) {
var id, entity, data;
var components = this.store;
for (id in components) {
entity = components[id].entity;
data = components[id].data;
if (data.enabled && entity.enabled) {
if (!entity.rigidbody && entity.trigger) {
entity.trigger.syncEntityToBody();
}
}
}
},
onTransformChanged: function(component, position, rotation, scale) {
this.implementations[component.data.type].updateTransform(component, position, rotation, scale);
},
/**
* @private
* @description Destroys the previous collision type and creates a new one
* based on the new type provided
*/
changeType: function (component, previousType, newType) {
this.implementations[previousType].remove( component.entity, component.data);
this._createImplementation(newType).reset(component, component.data);
},
/**
* @private
* @description Recreates rigid bodies or triggers for the specified component
*/
recreatePhysicalShapes: function (component) {
this.implementations[component.data.type].recreatePhysicalShapes(component);
}
});
/**
* @private
* @description Collision system implementations
*/
var CollisionSystemImpl = function (system) {
this.system = system;
};
CollisionSystemImpl.prototype = {
/**
* @private
* @description Called before the call to system.super.initializeComponentData is made
*/
beforeInitialize: function (component, data) {
data.shape = this.createPhysicalShape(component.entity, data);
data.model = new pc.Model();
data.model.graph = new pc.GraphNode();
},
/**
* @private
* @description Called after the call to system.super.initializeComponentData is made
*/
afterInitialize: function (component, data) {
this.recreatePhysicalShapes(component);
component.data.initialized = true;
},
/**
* @private
* @description Called when a collision component changes type in order to
* recreate debug and physical shapes
*/
reset: function (component, data) {
this.beforeInitialize(component, data);
this.afterInitialize(component, data);
},
/**
* @private
* @description Re-creates rigid bodies / triggers
*/
recreatePhysicalShapes: function (component) {
var entity = component.entity;
var data = component.data;
if (typeof Ammo !== 'undefined') {
data.shape = this.createPhysicalShape(component.entity, data);
if (entity.rigidbody) {
entity.rigidbody.disableSimulation();
entity.rigidbody.createBody();
} else {
if (!entity.trigger) {
entity.trigger = new pc.Trigger(this.system.app, component, data);
} else {
entity.trigger.initialize(data);
}
}
}
},
/**
* @private
* @description Creates a physical shape for the collision. This consists
* of the actual shape that will be used for the rigid bodies / triggers of
* the collision.
*/
createPhysicalShape: function (entity, data) {
return undefined;
},
updateTransform: function(component, position, rotation, scale) {
if (component.entity.trigger) {
component.entity.trigger.syncEntityToBody();
}
},
/**
* @private
* @description Called when the collision is removed
*/
remove: function (entity, data) {
var app = this.system.app;
if (entity.rigidbody && entity.rigidbody.body) {
app.systems.rigidbody.removeBody(entity.rigidbody.body);
entity.rigidbody.disableSimulation();
}
if (entity.trigger) {
entity.trigger.destroy();
delete entity.trigger;
}
if (app.scene.containsModel(data.model)) {
app.root.removeChild(data.model.graph);
app.scene.removeModel(data.model);
}
},
/**
* @private
* @description Called when the collision is cloned to another entity
*/
clone: function (entity, clone) {
var src = this.system.dataStore[entity._guid];
var data = {
enabled: src.data.enabled,
type: src.data.type,
halfExtents: [src.data.halfExtents.x, src.data.halfExtents.y, src.data.halfExtents.z],
radius: src.data.radius,
axis: src.data.axis,
height: src.data.height,
asset: src.data.asset,
model: src.data.model
};
return this.system.addComponent(clone, data);
}
};
/**
* @private
* @description Box Collision System
*/
var CollisionBoxSystemImpl = function (system) {};
CollisionBoxSystemImpl = pc.inherits(CollisionBoxSystemImpl, CollisionSystemImpl);
CollisionBoxSystemImpl.prototype = pc.extend(CollisionBoxSystemImpl.prototype, {
createPhysicalShape: function (entity, data) {
if (typeof Ammo !== 'undefined') {
var he = data.halfExtents;
var ammoHe = new Ammo.btVector3(he ? he.x : 0.5, he ? he.y : 0.5, he ? he.z : 0.5);
return new Ammo.btBoxShape(ammoHe);
} else {
return undefined;
}
}
});
/**
* @private
* @description Sphere Collision System
*/
var CollisionSphereSystemImpl = function (system) {};
CollisionSphereSystemImpl = pc.inherits(CollisionSphereSystemImpl, CollisionSystemImpl);
CollisionSphereSystemImpl.prototype = pc.extend(CollisionSphereSystemImpl.prototype, {
createPhysicalShape: function (entity, data) {
if (typeof Ammo !== 'undefined') {
return new Ammo.btSphereShape(data.radius);
} else {
return undefined;
}
}
});
/**
* @private
* @description Capsule Collision System
*/
var CollisionCapsuleSystemImpl = function (system) {};
CollisionCapsuleSystemImpl = pc.inherits(CollisionCapsuleSystemImpl, CollisionSystemImpl);
CollisionCapsuleSystemImpl.prototype = pc.extend(CollisionCapsuleSystemImpl.prototype, {
createPhysicalShape: function (entity, data) {
var shape = null;
var axis = (data.axis !== undefined) ? data.axis : 1;
var radius = data.radius || 0.5;
var height = Math.max((data.height || 2) - 2 * radius, 0);
if (typeof Ammo !== 'undefined') {
switch (axis) {
case 0:
shape = new Ammo.btCapsuleShapeX(radius, height);
break;
case 1:
shape = new Ammo.btCapsuleShape(radius, height);
break;
case 2:
shape = new Ammo.btCapsuleShapeZ(radius, height);
break;
}
}
return shape;
}
});
/**
* @private
* @description Cylinder Collision System
*/
var CollisionCylinderSystemImpl = function (system) {};
CollisionCylinderSystemImpl = pc.inherits(CollisionCylinderSystemImpl, CollisionSystemImpl);
CollisionCylinderSystemImpl.prototype = pc.extend(CollisionCylinderSystemImpl.prototype, {
createPhysicalShape: function (entity, data) {
var halfExtents = null;
var shape = null;
var axis = (data.axis !== undefined) ? data.axis : 1;
var radius = (data.radius !== undefined) ? data.radius : 0.5;
var height = (data.height !== undefined) ? data.height : 1;
if (typeof Ammo !== 'undefined') {
switch (axis) {
case 0:
halfExtents = new Ammo.btVector3(height * 0.5, radius, radius);
shape = new Ammo.btCylinderShapeX(halfExtents);
break;
case 1:
halfExtents = new Ammo.btVector3(radius, height * 0.5, radius);
shape = new Ammo.btCylinderShape(halfExtents);
break;
case 2:
halfExtents = new Ammo.btVector3(radius, radius, height * 0.5);
shape = new Ammo.btCylinderShapeZ(halfExtents);
break;
}
}
return shape;
}
});
/**
* @private
* @description Mesh Collision System
*/
var CollisionMeshSystemImpl = function (system) { };
CollisionMeshSystemImpl = pc.inherits(CollisionMeshSystemImpl, CollisionSystemImpl);
CollisionMeshSystemImpl.prototype = pc.extend(CollisionMeshSystemImpl.prototype, {
// override for the mesh implementation because the asset model needs
// special handling
beforeInitialize: function (component, data) {},
createPhysicalShape: function (entity, data) {
if (typeof Ammo !== 'undefined' && data.model) {
var model = data.model;
var shape = new Ammo.btCompoundShape();
var i, j;
for (i = 0; i < model.meshInstances.length; i++) {
var meshInstance = model.meshInstances[i];
var mesh = meshInstance.mesh;
var ib = mesh.indexBuffer[pc.RENDERSTYLE_SOLID];
var vb = mesh.vertexBuffer;
var format = vb.getFormat();
var stride = format.size / 4;
var positions;
for (j = 0; j < format.elements.length; j++) {
var element = format.elements[j];
if (element.name === pc.SEMANTIC_POSITION) {
positions = new Float32Array(vb.lock(), element.offset);
}
}
var indices = new Uint16Array(ib.lock());
var numTriangles = mesh.primitive[0].count / 3;
var v1 = new Ammo.btVector3();
var v2 = new Ammo.btVector3();
var v3 = new Ammo.btVector3();
var i1, i2, i3;
var base = mesh.primitive[0].base;
var triMesh = new Ammo.btTriangleMesh();
for (j = 0; j < numTriangles; j++) {
i1 = indices[base+j*3] * stride;
i2 = indices[base+j*3+1] * stride;
i3 = indices[base+j*3+2] * stride;
v1.setValue(positions[i1], positions[i1 + 1], positions[i1 + 2]);
v2.setValue(positions[i2], positions[i2 + 1], positions[i2 + 2]);
v3.setValue(positions[i3], positions[i3 + 1], positions[i3 + 2]);
triMesh.addTriangle(v1, v2, v3, true);
}
var useQuantizedAabbCompression = true;
var triMeshShape = new Ammo.btBvhTriangleMeshShape(triMesh, useQuantizedAabbCompression);
var wtm = meshInstance.node.getWorldTransform();
var scl = wtm.getScale();
triMeshShape.setLocalScaling(new Ammo.btVector3(scl.x, scl.y, scl.z));
var pos = meshInstance.node.getPosition();
var rot = meshInstance.node.getRotation();
var transform = new Ammo.btTransform();
transform.setIdentity();
transform.getOrigin().setValue(pos.x, pos.y, pos.z);
var ammoQuat = new Ammo.btQuaternion();
ammoQuat.setValue(rot.x, rot.y, rot.z, rot.w);
transform.setRotation(ammoQuat);
shape.addChildShape(transform, triMeshShape);
}
var entityTransform = entity.getWorldTransform();
var scale = entityTransform.getScale();
var vec = new Ammo.btVector3();
vec.setValue(scale.x, scale.y, scale.z);
shape.setLocalScaling(vec);
return shape;
} else {
return undefined;
}
},
recreatePhysicalShapes: function (component) {
var data = component.data;
if (data.asset !== null && component.enabled && component.entity.enabled) {
this.loadModelAsset(component);
} else {
this.doRecreatePhysicalShape(component);
}
},
loadModelAsset: function (component) {
var self = this;
var id = component.data.asset;
var data = component.data;
var assets = this.system.app.assets;
var asset = assets.get(id);
if (asset) {
asset.ready(function (asset) {
data.model = asset.resource;
self.doRecreatePhysicalShape(component);
});
assets.load(asset);
} else {
assets.once("add:" + id, function (asset) {
asset.ready(function (asset) {
data.model = asset.resource;
self.doRecreatePhysicalShape(component);
});
assets.load(asset);
});
}
},
doRecreatePhysicalShape: function (component) {
var entity = component.entity;
var data = component.data;
if (data.model) {
if (data.shape) {
Ammo.destroy(data.shape);
}
data.shape = this.createPhysicalShape(entity, data);
if (entity.rigidbody) {
entity.rigidbody.createBody();
} else {
if (!entity.trigger) {
entity.trigger = new pc.Trigger(this.system.app, component, data);
} else {
entity.trigger.initialize(data);
}
}
} else {
this.remove(entity, data);
}
},
updateTransform: function (component, position, rotation, scale) {
if (component.shape) {
var entityTransform = component.entity.getWorldTransform();
var worldScale = entityTransform.getScale();
// if the scale changed then recreate the shape
var previousScale = component.shape.getLocalScaling();
if (worldScale.x !== previousScale.x() ||
worldScale.y !== previousScale.y() ||
worldScale.z !== previousScale.z() ) {
this.doRecreatePhysicalShape(component);
}
}
CollisionMeshSystemImpl._super.updateTransform.call(this, component, position, rotation, scale);
}
});
return {
CollisionComponentSystem: CollisionComponentSystem
};
}());