public void UpdateSoftBody(SoftBody softBody, ShapeData shapeData) { // Could just allocate a Vector3 array here at each frame, but reusing shapeData.SoftBodyData is faster. // Probably uses more memory though. shapeData.VertexCount = softBody.GetVertexNormalData(out shapeData.SoftBodyData); shapeData.SetDynamicVertexBuffer(device, shapeData.SoftBodyData); if (softBody.Faces.Count == 0 && softBody.Tetras.Count == 0) { shapeData.PrimitiveType = PrimitiveType.LineList; } }
ShapeData CreateShape(CollisionShape shape) { ShapeData shapeData = new ShapeData(); uint[] indices; Vector3[] vertices = CreateShape(shape, out indices); shapeData.SetVertexNormalBuffer(device, vertices); if (indices != null) { shapeData.IndexCount = indices.Length; ushort[] indices_s = CompactIndexBuffer(indices); if (indices_s != null) { shapeData.SetIndexBuffer(device, indices_s); } else { shapeData.SetIndexBuffer(device, indices); } } return shapeData; }
public void InitInstancedRender(AlignedCollisionObjectArray objects) { // Clear instance data foreach (ShapeData s in shapes.Values) { s.InstanceDataList.Clear(); } removeList.Clear(); int i = objects.Count - 1; for (; i >= 0; i--) { CollisionObject colObj = objects[i]; BulletSharp.Matrix transform; if (colObj is RigidBody) { DefaultMotionState motionState = (colObj as RigidBody).MotionState as DefaultMotionState; if (motionState != null) { transform = motionState.GraphicsWorldTrans; } else { colObj.GetWorldTransform(out transform); } } else if (colObj is SoftBody) { if (demo.IsDebugDrawEnabled) { continue; } transform = BulletSharp.Matrix.Identity; } else { colObj.GetWorldTransform(out transform); } InitInstanceData(colObj, colObj.CollisionShape, ref transform); } foreach (KeyValuePair <CollisionShape, ShapeData> sh in shapes) { ShapeData s = sh.Value; if (s.InstanceDataList.Count == 0) { removeList.Add(sh.Key); } /* * // Is the instance buffer the right size? * if (s.InstanceDataBuffer.Description.SizeInBytes != s.InstanceDataList.Count * InstanceData.SizeInBytes) * { * // No, recreate it * s.InstanceDataBuffer.Dispose(); * * if (s.InstanceDataList.Count == 0) * { * if (s.IndexBuffer != null) * s.IndexBuffer.Dispose(); * s.VertexBuffer.Dispose(); * removeList.Add(sh.Key); * continue; * } * * instanceDataDesc.SizeInBytes = s.InstanceDataList.Count * InstanceData.SizeInBytes; * s.InstanceDataBuffer = new Buffer(device, instanceDataDesc); * s.BufferBindings[1] = new VertexBufferBinding(s.InstanceDataBuffer, InstanceData.SizeInBytes, 0); * } * * // Copy the instance data over to the instance buffer * using (var data = s.InstanceDataBuffer.Map(MapMode.WriteDiscard)) * { * data.WriteRange(s.InstanceDataList.ToArray()); * s.InstanceDataBuffer.Unmap(); * } */ } if (removeList.Count != 0) { for (i = removeList.Count - 1; i >= 0; i--) { shapes.Remove(removeList[i]); } } }