Exemple #1
0
        private void UpdateColliders(ObiSolver solver)
        {
            for (int i = 0; i < colliders.Count; i++)
            {
                Collider source = colliders[i];
                if (source == null || !source.enabled || !source.gameObject.activeInHierarchy)
                {
                    continue;
                }

                Rigidbody   rb = source.GetComponentInParent <Rigidbody>();
                ObiCollider oc = source.GetComponent <ObiCollider>();

                // Get the adequate rigidBodyIndex. If several colliders share a rigidbody, they'll get the same rigidBodyIndex.
                int rigidBodyIndex = -1;
                if (rb != null)
                {
                    if (!rigidbodyIDs.TryGetValue(rb.GetInstanceID(), out rigidBodyIndex))
                    {
                        ObiRigidbody or = rb.GetComponent <ObiRigidbody>();

                        rigidBodyIndex = Oni.GetRigidbodyCount(oniColliderGroup);
                        Oni.SetRigidbodies(oniColliderGroup, new Oni.Rigidbody[] {
                            new Oni.Rigidbody(rb, (or != null) ? or.kinematicForParticles : false)
                        }, 1, rigidBodyIndex);
                        rigidbodyIDs[rb.GetInstanceID()] = rigidBodyIndex;
                    }
                }

                float        thickness = (oc != null)?oc.thickness:0;
                Oni.Collider collider  = new Oni.Collider();
                bool         supported = true;

                if (source is SphereCollider)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Sphere, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetSphereShapes(oniColliderGroup, new Oni.SphereShape[] {
                        new Oni.SphereShape(source as SphereCollider)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Sphere));
                }
                else if (source is BoxCollider)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Box, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetBoxShapes(oniColliderGroup, new Oni.BoxShape[] {
                        new Oni.BoxShape(source as BoxCollider)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Box));
                }
                else if (source is CapsuleCollider)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Capsule, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetCapsuleShapes(oniColliderGroup, new Oni.CapsuleShape[] {
                        new Oni.CapsuleShape(source as CapsuleCollider)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule));
                }
                else if (source is CharacterController)
                {
                    collider = new Oni.Collider(source, Oni.ShapeType.Capsule, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetCapsuleShapes(oniColliderGroup, new Oni.CapsuleShape[] {
                        new Oni.CapsuleShape(source as CharacterController)
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Capsule));
                }
                else if (source is TerrainCollider)
                {
                    TerrainCollider tc = source as TerrainCollider;

                    Oni.HeightData data;
                    if (!heightData.TryGetValue(tc, out data))
                    {
                        data = heightData[tc] = new Oni.HeightData(tc);
                    }

                    collider = new Oni.Collider(source, Oni.ShapeType.Heightmap, thickness, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Heightmap), rigidBodyIndex, (oc != null)?oc.materialIndex:0);

                    Oni.SetHeightmapShapes(oniColliderGroup, new Oni.HeightmapShape[] {
                        new Oni.HeightmapShape(tc, data.AddrOfHeightData())
                    }, 1, Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.Heightmap));
                }
                else if (source is MeshCollider)
                {
                    MeshCollider             mc = source as MeshCollider;
                    MeshColliderShapeAndData shapeAndData;

                    if (mc.sharedMesh != null)
                    {
                        // We can share the same triangle data across several instances of the same MeshCollider:
                        if (!meshColliderData.TryGetValue(mc.sharedMesh, out shapeAndData))
                        {
                            // Get current amount of triangle mesh shapes:
                            int shapeIndex = Oni.GetShapeCount(oniColliderGroup, Oni.ShapeType.TriangleMesh);

                            // Generate mesh collider triangle data and shape:
                            Oni.TriangleMeshData  data  = new Oni.TriangleMeshData(mc);
                            Oni.TriangleMeshShape shape = new Oni.TriangleMeshShape(mc,
                                                                                    (oc != null)?oc.meshColliderType:Oni.TriangleMeshShape.MeshColliderType.ThinTwoSided,
                                                                                    thickness,
                                                                                    data.AddrOfVertexData(),
                                                                                    data.AddrOfTriangleData());

                            // Pack both in a small wrapper:
                            shapeAndData = new MeshColliderShapeAndData(data, shape, shapeIndex);

                            // Tell Oni we want to define a new triangle mesh:
                            Oni.SetTriangleMeshShapes(oniColliderGroup,
                                                      new Oni.TriangleMeshShape[] { shapeAndData.meshShape },
                                                      1, shapeIndex);

                            Oni.UpdateTriangleMeshShapes(oniColliderGroup, 1, shapeIndex);

                            // Store mesh collider data:
                            meshColliderData[mc.sharedMesh] = shapeAndData;
                        }

                        collider = new Oni.Collider(source, Oni.ShapeType.TriangleMesh, thickness, shapeAndData.shapeIndex, rigidBodyIndex, (oc != null)?oc.materialIndex:0);
                    }
                }
                else
                {
                    supported = false;
                    Debug.LogWarning("Collider type " + source.GetType() + " not supported by Obi. Ignoring it.");
                }

                if (supported)
                {
                    // convert to colliders to solver's local space:
                    if (solver.simulateInLocalSpace)
                    {
                        collider.SetSpaceTransform(solver.transform);
                    }

                    Oni.SetColliders(oniColliderGroup, new Oni.Collider[] { collider }, 1, Oni.GetColliderCount(oniColliderGroup));
                }
            }
        }
Exemple #2
0
 public MeshColliderShapeAndData(Oni.TriangleMeshData meshData, Oni.TriangleMeshShape meshShape, int shapeIndex)
 {
     this.meshData   = meshData;
     this.meshShape  = meshShape;
     this.shapeIndex = shapeIndex;
 }