public void InitInstancedRender() { // Clear instance data foreach (PrimitiveData shapeData in shapes.Values) { shapeData.Instances.Clear(); } // Gather instance data foreach (var colObj in World.CollisionObjectArray) { var shape = colObj.CollisionShape; if (shape.ShapeType == BroadphaseNativeType.SoftBodyShape) { //disable softbody } else { BulletSharp.Math.Matrix transform; colObj.GetWorldTransform(out transform); InitRigidBodyInstance(colObj, shape, ref transform); } } foreach (KeyValuePair <CollisionShape, PrimitiveData> shape in shapes) { PrimitiveData shapeData = shape.Value; if (shapeData.Instances.Count == 0) { removeList.Add(shape.Key); } } // Remove shapes that had no instances if (removeList.Count != 0) { foreach (var shape in removeList) { shapes.Remove(shape); } removeList.Clear(); } }
PrimitiveData InitShapeData(CollisionShape shape) { PrimitiveData shapeData; if (shapes.TryGetValue(shape, out shapeData) == false) { if (shape.ShapeType == BroadphaseNativeType.SoftBodyShape) { shapeData = new PrimitiveData(); } else { shapeData = CreateShape(shape); } shapes.Add(shape, shapeData); } return(shapeData); }
PrimitiveData CreateShape(CollisionShape shape) { PrimitiveData shapeData = new PrimitiveData(); uint[] indices; BulletSharp.Math.Vector3[] vertexBuffer = PrimitiveMeshFactory.CreateShape(shape, out indices); if (vertexBuffer != null) { shapeData.VertexCount = vertexBuffer.Length / 2; BulletSharp.Math.Vector3[] vertices = new BulletSharp.Math.Vector3[shapeData.VertexCount]; BulletSharp.Math.Vector3[] normals = new BulletSharp.Math.Vector3[shapeData.VertexCount]; int i; for (i = 0; i < shapeData.VertexCount; i++) { vertices[i] = vertexBuffer[i * 2]; normals[i] = vertexBuffer[i * 2 + 1]; } shapeData.SetVertexBuffer(vertices); } if (indices != null) { ushort[] indices_s = PrimitiveMeshFactory.CompactIndexBuffer(indices); if (indices_s != null) { shapeData.SetIndexBuffer(indices_s); } else { shapeData.SetIndexBuffer(indices); } shapeData.ElementCount = indices.Length; } return(shapeData); }