瀏覽代碼

Merge pull request #5952 from MackeyK24/master

Ammo.btConvexHullShape Support
David Catuhe 6 年之前
父節點
當前提交
cd62aac5a7
共有 3 個文件被更改,包括 61 次插入1 次删除
  1. 1 1
      dist/preview release/what's new.md
  2. 56 0
      src/Physics/Plugins/ammoJSPlugin.ts
  3. 4 0
      src/Physics/physicsImpostor.ts

+ 1 - 1
dist/preview release/what's new.md

@@ -1,9 +1,9 @@
 # 4.0.0
 
 ## Major updates
-
 - New [fancy forum](https://forum.babylonjs.com)!! ([Deltakosh](https://github.com/deltakosh))
 - [Inspector v2.0](https://doc.babylonjs.com/features/playground_debuglayer). [Dev log](https://medium.com/@babylonjs/dev-log-creating-the-new-inspector-b15c50900205) ([Deltakosh](https://github.com/deltakosh))
+- Added support for [Convex Hull Impostor][https://github.com/kripken/ammo.js/blob/master/bullet/src/BulletCollision/CollisionShapes/btConvexHullShape.h] using Ammo.js plugin ([MackeyK24](https://github.com/mackeyk24))
 - Added support for [parallel shader compilation](https://www.khronos.org/registry/webgl/extensions/KHR_parallel_shader_compile/) ([Deltakosh](https://github.com/deltakosh))
 - Added [Object Based Motion Blur](http://doc.babylonjs.com/how_to/using_motionblurpostprocess) post-process ([julien-moreau](https://github.com/julien-moreau))
 - Added [support for AmmoJS](https://doc.babylonjs.com/how_to/using_the_physics_engine) as a physics plugin (Composite objects, motors, joints) ([TrevorDev](https://github.com/TrevorDev))

+ 56 - 0
src/Physics/Plugins/ammoJSPlugin.ts

@@ -426,6 +426,51 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
         return triangleCount;
     }
 
+    // adds all verticies (including child verticies) to the convex hull shape
+    private _addHullVerts(btConvexHullShape: any, topLevelObject: IPhysicsEnabledObject, object: IPhysicsEnabledObject) {
+        var triangleCount = 0;
+        if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
+            var indices = object.getIndices();
+            if (!indices) {
+                indices = [];
+            }
+            var vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
+            if (!vertexPositions) {
+                vertexPositions = [];
+            }
+            object.computeWorldMatrix(false);
+            var faceCount = indices.length / 3;
+            for (var i = 0; i < faceCount; i++) {
+                var triPoints = [];
+                for (var point = 0; point < 3; point++) {
+                    var v = new Vector3(vertexPositions[(indices[(i * 3) + point] * 3) + 0], vertexPositions[(indices[(i * 3) + point] * 3) + 1], vertexPositions[(indices[(i * 3) + point] * 3) + 2]);
+                    v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
+                    v.subtractInPlace(topLevelObject.position);
+                    var vec: any;
+                    if (point == 0) {
+                        vec = this._tmpAmmoVectorA;
+                    } else if (point == 1) {
+                        vec = this._tmpAmmoVectorB;
+                    } else {
+                        vec = this._tmpAmmoVectorC;
+                    }
+                    vec.setValue(v.x, v.y, v.z);
+
+                    triPoints.push(vec);
+                }
+                btConvexHullShape.addPoint(triPoints[0], true);
+                btConvexHullShape.addPoint(triPoints[1], true);
+                btConvexHullShape.addPoint(triPoints[2], true);
+                triangleCount++;
+            }
+
+            object.getChildMeshes().forEach((m) => {
+                triangleCount += this._addHullVerts(btConvexHullShape, topLevelObject, m);
+            });
+        }
+        return triangleCount;
+    }
+
     private _createShape(impostor: PhysicsImpostor, ignoreChildren = false) {
         var object = impostor.object;
 
@@ -500,6 +545,17 @@ export class AmmoJSPlugin implements IPhysicsEnginePlugin {
                     returnValue = new Ammo.btBvhTriangleMeshShape(tetraMesh);
                 }
                 break;
+            case PhysicsImpostor.ConvexHullImpostor:
+                var convexMesh = new Ammo.btConvexHullShape();
+                var triangeCount = this._addHullVerts(convexMesh, object, object);
+                if (triangeCount == 0) {
+                    // Cleanup Unused Convex Hull Shape
+                    impostor._pluginData.toDispose.concat([convexMesh]);
+                    returnValue = new Ammo.btCompoundShape();
+                } else {
+                    returnValue = convexMesh;
+                }
+                break;
             case PhysicsImpostor.NoImpostor:
                 // Fill with sphere but collision is disabled on the rigid body in generatePhysicsBody, using an empty shape caused unexpected movement with joints
                 returnValue = new Ammo.btSphereShape(extendSize.x / 2);

+ 4 - 0
src/Physics/physicsImpostor.ts

@@ -1025,4 +1025,8 @@ export class PhysicsImpostor {
      * Heightmap-Imposter type
      */
     public static HeightmapImpostor = 9;
+    /**
+     * ConvexHull-Impostor type (Ammo.js plugin only)
+     */
+    public static ConvexHullImpostor = 10;
 }