Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 2
0
        public void UpdateBodiesInfo()
        {
            oniColliders.Clear();
            oniRigidbodies.Clear();
            oniSpheres.Clear();
            oniBoxes.Clear();
            oniCapsules.Clear();
            oniHeightmaps.Clear();

            rigidbodyIDs.Clear();

            for (int i = 0; i < colliders.Count; i++)
            {
                Collider source = colliders[i];
                if (source == null)
                {
                    continue;
                }

                Rigidbody   rb = colliders[i].GetComponentInParent <Rigidbody>();
                ObiCollider oc = colliders[i].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))
                    {
                        rigidBodyIndex = oniRigidbodies.Count;
                        oniRigidbodies.Add(new Oni.Rigidbody(rb));
                        rigidbodyIDs[rb.GetInstanceID()] = rigidBodyIndex;
                    }
                }

                if (source is SphereCollider)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Sphere, oniSpheres.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniSpheres.Add(new Oni.SphereShape(source as SphereCollider));
                }
                else if (source is BoxCollider)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Box, oniBoxes.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniBoxes.Add(new Oni.BoxShape(source as BoxCollider));
                }
                else if (source is CapsuleCollider)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Capsule, oniCapsules.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniCapsules.Add(new Oni.CapsuleShape(source as CapsuleCollider));
                }
                else if (source is CharacterController)
                {
                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Capsule, oniCapsules.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniCapsules.Add(new Oni.CapsuleShape(source as CharacterController));
                }
                else if (source is TerrainCollider)
                {
                    TerrainCollider tc = source as TerrainCollider;
                    if (!heightData.ContainsKey(tc))
                    {
                        heightData[tc] = new Oni.HeightData(source as TerrainCollider);
                    }

                    oniColliders.Add(new Oni.Collider(source, Oni.ShapeType.Heightmap, oniHeightmaps.Count, rigidBodyIndex, (oc != null)?oc.materialIndex:0));
                    oniHeightmaps.Add(new Oni.HeightmapShape(source as TerrainCollider, heightData[tc].AddrOfHeightData()));
                }
                else
                {
                    Debug.LogWarning(source.GetType() + " not supported by Obi. Ignoring it.");
                }
            }

            UpdateColliders();
            UpdateRigidbodies();
            UpdateSpheres();
            UpdateBoxes();
            UpdateCapsules();
            UpdateHeightmaps();
        }