protected override Vector3f layout_item(SceneUIElement e)
        {
            AxisAlignedBox2f box = Container.Bounds2D;

            Vector3f vNewPos3 = Vector3f.Zero;   // 2.5d center coordinate

            IBoxModelElement boxElem = e as IBoxModelElement;

            if (PinConstraints.ContainsKey(e))
            {
                Pin pin = PinConstraints[e];

                Vector2f SourcePos = pin.FromF();
                Vector2f PinToPos  = pin.ToF();
                vNewPos3 = BoxModel.SetObjectPosition(boxElem, SourcePos, PinToPos, pin.fZ);
            }
            else if (boxElem != null)
            {
                vNewPos3 = BoxModel.SetObjectPosition(boxElem, BoxPosition.Center, box.Center, 0);
            }
            else
            {
                // do nothing?
            }

            return(vNewPos3);
        }
Example #2
0
        public override void OnSolverStepEnd(float deltaTime)
        {
            base.OnSolverStepEnd(deltaTime);

            if (isActiveAndEnabled)
            {
                // breakable pin constraints:
                PinConstraints.BreakConstraints();
            }
        }
Example #3
0
        public override void OnSolverStepEnd()
        {
            base.OnSolverStepEnd();

            // Break pin constraints when needed:
            if (isActiveAndEnabled)
            {
                PinConstraints.BreakConstraints();
            }
        }
Example #4
0
 public override void OnSolverStepEnd()
 {
     // Break pin constraints when needed:
     if (isActiveAndEnabled)
     {
         if (PinConstraints.GetBatches().Count > 0)
         {
             ((ObiPinConstraintBatch)PinConstraints.GetBatches()[0]).BreakConstraints();
         }
     }
 }
Example #5
0
        public override void OnSolverStepEnd()
        {
            base.OnSolverStepEnd();

            if (isActiveAndEnabled)
            {
                ApplyTearing();

                // breakable pin constraints:
                PinConstraints.BreakConstraints();
            }
        }
Example #6
0
        protected override void layout_item(SceneUIElement e)
        {
            AxisAlignedBox2f box = Container.Bounds2D;

            IBoxModelElement boxElem = e as IBoxModelElement;
            IElementFrame    eFramed = e as IElementFrame;

            if (PinConstraints.ContainsKey(e))
            {
                Pin pin = PinConstraints[e];

                // evaluate pin constraints in 2D box space
                Vector2f SourcePos = pin.FromF();
                Vector2f PinToPos  = pin.ToF();

                // map center of object into box space
                //  note: ignores orientation!
                Frame3f  objF    = eFramed.GetObjectFrame();
                Vector2f center2 = Region.To2DCoords(objF.Origin);

                // construct new 2D position
                Vector2f vOffset = SourcePos - center2;
                Vector2f vNewPos = PinToPos - vOffset;

                // map 2D position back to 3D surface and orient object
                Frame3f frame = Region.From2DCoords(vNewPos, pin.fZ);
                eFramed.SetObjectFrame(frame);
            }
            else if (boxElem != null)
            {
                // position object at center of box region
                Frame3f frame = Region.From2DCoords(Vector2f.Zero, 0);
                eFramed.SetObjectFrame(frame);
            }
            else
            {
                // do nothing?
            }
        }
Example #7
0
        protected override void layout_item(SceneUIElement e)
        {
            AxisAlignedBox2f box = Container.Bounds2D;

            IBoxModelElement boxElem = e as IBoxModelElement;

            if (PinConstraints.ContainsKey(e))
            {
                Pin pin = PinConstraints[e];

                Vector2f SourcePos = pin.FromF();
                Vector2f PinToPos  = pin.ToF();
                BoxModel.SetObjectPosition(boxElem, SourcePos, PinToPos, pin.fZ);
            }
            else if (boxElem != null)
            {
                BoxModel.SetObjectPosition(boxElem, BoxPosition.Center, box.Center, 0);
            }
            else
            {
                // do nothing?
            }
        }
Example #8
0
        /**
         * Generates the particle based physical representation of the cloth mesh. This is the initialization method for the cloth object
         * and should not be called directly once the object has been created.
         */
        public override IEnumerator GeneratePhysicRepresentationForMesh()
        {
            initialized  = false;
            initializing = false;

            if (sharedTopology == null)
            {
                Debug.LogError("No ObiMeshTopology provided. Cannot initialize physical representation.");
                yield break;
            }
            else if (!sharedTopology.Initialized)
            {
                Debug.LogError("The provided ObiMeshTopology contains no data. Cannot initialize physical representation.");
                yield break;
            }

            initializing = true;

            RemoveFromSolver(null);

            ResetTopology();

            maxVertexValency = 0;

            pooledParticles = (int)((topology.heFaces.Length * 3 - topology.heVertices.Length) * tearCapacity);
            usedParticles   = topology.heVertices.Length;

            int totalParticles = usedParticles + pooledParticles;

            active              = new bool[totalParticles];
            positions           = new Vector3[totalParticles];
            restPositions       = new Vector4[totalParticles];
            velocities          = new Vector3[totalParticles];
            vorticities         = new Vector3[totalParticles];
            invMasses           = new float[totalParticles];
            solidRadii          = new float[totalParticles];
            phases              = new int[totalParticles];
            areaContribution    = new float[totalParticles];
            tearResistance      = new float[totalParticles];
            deformableTriangles = new int[topology.heFaces.Length * 3];

            // Create a particle for each vertex, and gather per-vertex data (area, valency)
            for (int i = 0; i < topology.heVertices.Length; i++)
            {
                Oni.Vertex vertex = topology.heVertices[i];

                // Get the particle's area contribution.
                areaContribution[i] = 0;
                foreach (Oni.Face face in topology.GetNeighbourFacesEnumerator(vertex))
                {
                    areaContribution[i] += topology.GetFaceArea(face) / 3;
                }

                // Calculate particle's valency:
                int valency = 0;
                foreach (Oni.HalfEdge edge in topology.GetNeighbourEdgesEnumerator(vertex))
                {
                    valency++;
                }
                maxVertexValency = Mathf.Max(maxVertexValency, valency);

                // Get the shortest neighbour edge, particle radius will be half of its length.
                float minEdgeLength = Single.MaxValue;
                foreach (Oni.HalfEdge edge in topology.GetNeighbourEdgesEnumerator(vertex))
                {
                    minEdgeLength = Mathf.Min(minEdgeLength, Vector3.Distance(topology.heVertices[topology.GetHalfEdgeStartVertex(edge)].position,
                                                                              topology.heVertices[edge.endVertex].position));
                }
                active[i]           = true;
                tearResistance[i]   = 1;
                invMasses[i]        = (areaContribution[i] > 0) ? (1.0f / (0.05f * areaContribution[i])) : 0;
                positions[i]        = vertex.position;
                restPositions[i]    = positions[i];
                restPositions[i][3] = 0;         // activate rest position.
                solidRadii[i]       = minEdgeLength * 0.5f;
                phases[i]           = Oni.MakePhase(gameObject.layer, selfCollisions?Oni.ParticlePhase.SelfCollide:0);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating particles...", i / (float)topology.heVertices.Length));
                }
            }

            // Initialize basic data for pooled particles:
            for (int i = topology.heVertices.Length; i < pooledParticles; i++)
            {
                active[i]         = false;
                tearResistance[i] = 1;
                invMasses[i]      = 1.0f / 0.05f;
                solidRadii[i]     = 0.1f;
                phases[i]         = Oni.MakePhase(gameObject.layer, selfCollisions?Oni.ParticlePhase.SelfCollide:0);

                if (i % 100 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRope: generating pooled particles...", i / (float)pooledParticles));
                }
            }

            // Generate deformable triangles:
            for (int i = 0; i < topology.heFaces.Length; i++)
            {
                Oni.Face face = topology.heFaces[i];

                Oni.HalfEdge e1 = topology.heHalfEdges[face.halfEdge];
                Oni.HalfEdge e2 = topology.heHalfEdges[e1.nextHalfEdge];
                Oni.HalfEdge e3 = topology.heHalfEdges[e2.nextHalfEdge];

                deformableTriangles[i * 3]     = e1.endVertex;
                deformableTriangles[i * 3 + 1] = e2.endVertex;
                deformableTriangles[i * 3 + 2] = e3.endVertex;

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating deformable geometry...", i / (float)topology.heFaces.Length));
                }
            }

            List <ObiMeshTopology.HEEdge> edges = topology.GetEdgeList();

            DistanceConstraints.Clear();
            ObiDistanceConstraintBatch distanceBatch = new ObiDistanceConstraintBatch(false, false);

            DistanceConstraints.AddBatch(distanceBatch);

            // Initialize constraint-halfedge map for cloth tearing purposes: TODO: reset on awake!!!
            distanceConstraintMap = new int[topology.heHalfEdges.Length];
            for (int i = 0; i < distanceConstraintMap.Length; i++)
            {
                distanceConstraintMap[i] = -1;
            }

            // Create distance springs:
            for (int i = 0; i < edges.Count; i++)
            {
                distanceConstraintMap[edges[i].halfEdgeIndex] = i;
                Oni.HalfEdge hedge       = topology.heHalfEdges[edges[i].halfEdgeIndex];
                Oni.Vertex   startVertex = topology.heVertices[topology.GetHalfEdgeStartVertex(hedge)];
                Oni.Vertex   endVertex   = topology.heVertices[hedge.endVertex];

                distanceBatch.AddConstraint(topology.GetHalfEdgeStartVertex(hedge), hedge.endVertex, Vector3.Distance(startVertex.position, endVertex.position), 1, 1);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating structural constraints...", i / (float)topology.heHalfEdges.Length));
                }
            }

            // Create aerodynamic constraints:
            AerodynamicConstraints.Clear();
            ObiAerodynamicConstraintBatch aeroBatch = new ObiAerodynamicConstraintBatch(false, false);

            AerodynamicConstraints.AddBatch(aeroBatch);

            for (int i = 0; i < topology.heVertices.Length; i++)
            {
                aeroBatch.AddConstraint(i,
                                        areaContribution[i],
                                        AerodynamicConstraints.dragCoefficient,
                                        AerodynamicConstraints.liftCoefficient);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating aerodynamic constraints...", i / (float)topology.heFaces.Length));
                }
            }

            BendingConstraints.Clear();
            ObiBendConstraintBatch bendBatch = new ObiBendConstraintBatch(false, false);

            BendingConstraints.AddBatch(bendBatch);

            bendConstraintOffsets = new int[topology.heVertices.Length + 1];

            Dictionary <int, int> cons = new Dictionary <int, int>();

            for (int i = 0; i < topology.heVertices.Length; i++)
            {
                Oni.Vertex vertex = topology.heVertices[i];

                bendConstraintOffsets[i] = bendBatch.ConstraintCount;

                foreach (Oni.Vertex n1 in topology.GetNeighbourVerticesEnumerator(vertex))
                {
                    float      cosBest = 0;
                    Oni.Vertex vBest   = n1;

                    foreach (Oni.Vertex n2 in topology.GetNeighbourVerticesEnumerator(vertex))
                    {
                        float cos = Vector3.Dot((n1.position - vertex.position).normalized,
                                                (n2.position - vertex.position).normalized);
                        if (cos < cosBest)
                        {
                            cosBest = cos;
                            vBest   = n2;
                        }
                    }

                    if (!cons.ContainsKey(vBest.index) || cons[vBest.index] != n1.index)
                    {
                        cons[n1.index] = vBest.index;

                        float[] restPos = new float[] { n1.position[0], n1.position[1], n1.position[2],
                                                        vBest.position[0], vBest.position[1], vBest.position[2],
                                                        vertex.position[0], vertex.position[1], vertex.position[2] };
                        float restBend = Oni.BendingConstraintRest(restPos);
                        bendBatch.AddConstraint(n1.index, vBest.index, vertex.index, restBend, 0, 1);
                    }
                }

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: adding bend constraints...", i / (float)sharedTopology.heVertices.Length));
                }
            }
            bendConstraintOffsets[topology.heVertices.Length] = bendBatch.ConstraintCount;

            //Initialize pin constraints:
            PinConstraints.Clear();
            ObiPinConstraintBatch pinBatch = new ObiPinConstraintBatch(false, false);

            PinConstraints.AddBatch(pinBatch);

            AddToSolver(null);

            initializing = false;
            initialized  = true;

            InitializeWithRegularMesh();

            pooledVertices = (int)((topology.heFaces.Length * 3 - sharedMesh.vertexCount) * tearCapacity);
        }
        protected override Vector3f layout_item(SceneUIElement e)
        {
            AxisAlignedBox2f box = Container.Bounds2D;

            IBoxModelElement boxElem = e as IBoxModelElement;
            IElementFrame    eFramed = e as IElementFrame;

            Vector3f vNewPos3 = Vector3f.Zero;   // 2.5d center coordinate

            if (PinConstraints.ContainsKey(e))
            {
                Pin pin = PinConstraints[e];

                // [TODO] We have to xform the center of the object. But if we pin
                //   a corner, we want to enforce the corner position in 3D (eg on curved surf).
                //   Currently we pin the corner in 2D, but then conver that to a 2D-center
                //   and then use that position. So on curved surf, things overlap, etc

                // evaluate pin constraints in 2D box space
                Vector2f SourcePos = pin.FromF();
                Vector2f PinToPos  = pin.ToF();

                // map center of object into box space
                //  note: ignores orientation!
                //Frame3f objF = eFramed.GetObjectFrame();
                //Vector2f center2 = Region.To2DCoords(objF.Origin);
                Vector2f center2 = Vector2f.Zero;

                // construct new 2D position
                Vector2f vOffset = SourcePos - center2;
                Vector2f vNewPos = PinToPos - vOffset;
                vNewPos3 = new Vector3f(vNewPos.x, vNewPos.y, pin.fZ);

                // map 2D position back to 3D surface and orient object
                Frame3f frame = Region.From2DCoords(vNewPos, pin.fZ);
                eFramed.SetObjectFrame(frame);
            }
            else if (boxElem != null)
            {
                Vector2f vNewPos = Vector2f.Zero;
                vNewPos3 = new Vector3f(vNewPos.x, vNewPos.y, 0);

                // position object at center of box region
                Frame3f frame = Region.From2DCoords(vNewPos, 0);
                eFramed.SetObjectFrame(frame);
            }
            else
            {
                // do nothing?
            }

            if (PostTransforms.ContainsKey(e))
            {
                foreach (var xform in PostTransforms[e])
                {
                    xform(e);
                }
            }

            return(vNewPos3);
        }
Example #10
0
        /**
         * Generates the particle based physical representation of the rope. This is the initialization method for the rope object
         * and should not be called directly once the object has been created.
         */
        protected override IEnumerator Initialize()
        {
            initialized           = false;
            initializing          = true;
            interParticleDistance = -1;

            RemoveFromSolver(null);

            if (ropePath == null)
            {
                Debug.LogError("Cannot initialize rope. There's no ropePath present. Please provide a spline to define the shape of the rope");
                yield break;
            }

            ropePath.RecalculateSplineLenght(0.00001f, 7);
            closed     = ropePath.closed;
            restLength = ropePath.Length;

            usedParticles  = Mathf.CeilToInt(restLength / thickness * resolution) + (closed ? 0:1);
            totalParticles = usedParticles + pooledParticles;             //allocate extra particles to allow for lenght change and tearing.

            active         = new bool[totalParticles];
            positions      = new Vector3[totalParticles];
            velocities     = new Vector3[totalParticles];
            invMasses      = new float[totalParticles];
            principalRadii = new Vector3[totalParticles];
            phases         = new int[totalParticles];
            restPositions  = new Vector4[totalParticles];
            tearResistance = new float[totalParticles];
            colors         = new Color[totalParticles];

            int numSegments = usedParticles - (closed ? 0:1);

            if (numSegments > 0)
            {
                interParticleDistance = restLength / (float)numSegments;
            }
            else
            {
                interParticleDistance = 0;
            }

            float radius = interParticleDistance * resolution;

            for (int i = 0; i < usedParticles; i++)
            {
                active[i]    = true;
                invMasses[i] = 1.0f / DEFAULT_PARTICLE_MASS;
                float mu = ropePath.GetMuAtLenght(interParticleDistance * i);
                positions[i]      = transform.InverseTransformPoint(ropePath.transform.TransformPoint(ropePath.GetPositionAt(mu)));
                principalRadii[i] = Vector3.one * radius;
                phases[i]         = Oni.MakePhase(1, selfCollisions?Oni.ParticlePhase.SelfCollide:0);
                tearResistance[i] = 1;
                colors[i]         = Color.white;

                if (i % 100 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRope: generating particles...", i / (float)usedParticles));
                }
            }

            // Initialize basic data for pooled particles:
            for (int i = usedParticles; i < totalParticles; i++)
            {
                active[i]         = false;
                invMasses[i]      = 1.0f / DEFAULT_PARTICLE_MASS;
                principalRadii[i] = Vector3.one * radius;
                phases[i]         = Oni.MakePhase(1, selfCollisions?Oni.ParticlePhase.SelfCollide:0);
                tearResistance[i] = 1;
                colors[i]         = Color.white;

                if (i % 100 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRope: generating particles...", i / (float)usedParticles));
                }
            }

            DistanceConstraints.Clear();
            ObiDistanceConstraintBatch distanceBatch = new ObiDistanceConstraintBatch(false, false, MIN_YOUNG_MODULUS, MAX_YOUNG_MODULUS);

            DistanceConstraints.AddBatch(distanceBatch);

            for (int i = 0; i < numSegments; i++)
            {
                distanceBatch.AddConstraint(i, (i + 1) % (ropePath.closed ? usedParticles:usedParticles + 1), interParticleDistance, 1, 1);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRope: generating structural constraints...", i / (float)numSegments));
                }
            }

            BendingConstraints.Clear();
            ObiBendConstraintBatch bendingBatch = new ObiBendConstraintBatch(false, false, MIN_YOUNG_MODULUS, MAX_YOUNG_MODULUS);

            BendingConstraints.AddBatch(bendingBatch);
            for (int i = 0; i < usedParticles - (closed?0:2); i++)
            {
                // rope bending constraints always try to keep it completely straight:
                bendingBatch.AddConstraint(i, (i + 2) % usedParticles, (i + 1) % usedParticles, 0, 0, 1);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRope: adding bend constraints...", i / (float)usedParticles));
                }
            }

            // Initialize tether constraints:
            TetherConstraints.Clear();

            // Initialize pin constraints:
            PinConstraints.Clear();
            ObiPinConstraintBatch pinBatch = new ObiPinConstraintBatch(false, false, 0, MAX_YOUNG_MODULUS);

            PinConstraints.AddBatch(pinBatch);

            initializing = false;
            initialized  = true;

            RegenerateRestPositions();
        }
Example #11
0
        /**
         * Generates the particle based physical representation of the rope. This is the initialization method for the rope object
         * and should not be called directly once the object has been created.
         */
        protected override IEnumerator Initialize()
        {
            initialized           = false;
            initializing          = true;
            interParticleDistance = -1;

            RemoveFromSolver(null);

            if (ropePath == null)
            {
                Debug.LogError("Cannot initialize rope. There's no ropePath present. Please provide a spline to define the shape of the rope");
                yield break;
            }

            ropePath.RecalculateSplineLenght(0.00001f, 7);
            closed     = ropePath.closed;
            restLength = ropePath.Length;

            usedParticles  = Mathf.CeilToInt(restLength / thickness * resolution) + (closed ? 0:1);
            totalParticles = usedParticles;

            active              = new bool[totalParticles];
            positions           = new Vector3[totalParticles];
            orientations        = new Quaternion[totalParticles];
            velocities          = new Vector3[totalParticles];
            angularVelocities   = new Vector3[totalParticles];
            invMasses           = new float[totalParticles];
            invRotationalMasses = new float[totalParticles];
            principalRadii      = new Vector3[totalParticles];
            phases              = new int[totalParticles];
            restPositions       = new Vector4[totalParticles];
            restOrientations    = new Quaternion[totalParticles];
            colors              = new Color[totalParticles];

            int numSegments = usedParticles - (closed ? 0:1);

            if (numSegments > 0)
            {
                interParticleDistance = restLength / (float)numSegments;
            }
            else
            {
                interParticleDistance = 0;
            }

            float radius = interParticleDistance * resolution;

            for (int i = 0; i < usedParticles; i++)
            {
                active[i]              = true;
                invMasses[i]           = 1.0f / DEFAULT_PARTICLE_MASS;
                invRotationalMasses[i] = 1.0f / DEFAULT_PARTICLE_ROTATIONAL_MASS;
                float mu = ropePath.GetMuAtLenght(interParticleDistance * i);
                positions[i]      = transform.InverseTransformPoint(ropePath.transform.TransformPoint(ropePath.GetPositionAt(mu)));
                principalRadii[i] = Vector3.one * radius;
                phases[i]         = Oni.MakePhase(1, selfCollisions?Oni.ParticlePhase.SelfCollide:0);
                colors[i]         = Color.white;

                if (i % 100 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRod: generating particles...", i / (float)usedParticles));
                }
            }

            StretchShearConstraints.Clear();
            ObiStretchShearConstraintBatch stretchBatch = new ObiStretchShearConstraintBatch(false, false, MIN_YOUNG_MODULUS, MAX_YOUNG_MODULUS);

            StretchShearConstraints.AddBatch(stretchBatch);

            // rotation minimizing frame:
            ObiCurveFrame frame = new ObiCurveFrame();

            frame.Reset();

            for (int i = 0; i < numSegments; i++)
            {
                int next = (i + 1) % (ropePath.closed ? usedParticles:usedParticles + 1);

                float   mu     = ropePath.GetMuAtLenght(interParticleDistance * i);
                Vector3 normal = transform.InverseTransformVector(ropePath.transform.TransformVector(ropePath.GetNormalAt(mu)));

                frame.Transport(positions[i], (positions[next] - positions[i]).normalized, 0);

                orientations[i]     = Quaternion.LookRotation(frame.tangent, normal);
                restOrientations[i] = orientations[i];

                // Also set the orientation of the next particle. If it is not the last one, we will overwrite it.
                // This makes sure that open rods provide an orientation for their last particle (or rather, a phantom segment past the last particle).

                orientations[next]     = orientations[i];
                restOrientations[next] = orientations[i];

                stretchBatch.AddConstraint(i, next, interParticleDistance, Quaternion.identity, Vector3.one);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRod: generating structural constraints...", i / (float)numSegments));
                }
            }

            BendTwistConstraints.Clear();
            ObiBendTwistConstraintBatch twistBatch = new ObiBendTwistConstraintBatch(false, false, MIN_YOUNG_MODULUS, MAX_YOUNG_MODULUS);

            BendTwistConstraints.AddBatch(twistBatch);

            // the last bend constraint couples the last segment and a phantom segment past the last particle.
            for (int i = 0; i < numSegments; i++)
            {
                int next = (i + 1) % (ropePath.closed ? usedParticles:usedParticles + 1);

                Quaternion darboux = keepInitialShape ? ObiUtils.RestDarboux(orientations[i], orientations[next]) : Quaternion.identity;
                twistBatch.AddConstraint(i, next, darboux, Vector3.one);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiRod: generating structural constraints...", i / (float)numSegments));
                }
            }

            ChainConstraints.Clear();
            ObiChainConstraintBatch chainBatch = new ObiChainConstraintBatch(false, false, MIN_YOUNG_MODULUS, MAX_YOUNG_MODULUS);

            ChainConstraints.AddBatch(chainBatch);

            int[] indices = new int[usedParticles + (closed ? 1:0)];

            for (int i = 0; i < usedParticles; ++i)
            {
                indices[i] = i;
            }

            // Add the first particle as the last index of the chain, if closed.
            if (closed)
            {
                indices[usedParticles] = 0;
            }

            chainBatch.AddConstraint(indices, interParticleDistance, 1, 1);


            // Initialize tether constraints:
            TetherConstraints.Clear();

            // Initialize pin constraints:
            PinConstraints.Clear();
            ObiPinConstraintBatch pinBatch = new ObiPinConstraintBatch(false, false, MIN_YOUNG_MODULUS, MAX_YOUNG_MODULUS);

            PinConstraints.AddBatch(pinBatch);

            initializing = false;
            initialized  = true;

            RegenerateRestPositions();
        }
Example #12
0
        /**
         * Generates the particle based physical representation of the cloth mesh. This is the initialization method for the cloth object
         * and should not be called directly once the object has been created.
         */
        protected override IEnumerator Initialize()
        {
            initialized  = false;
            initializing = false;

            if (sharedTopology == null)
            {
                Debug.LogError("No ObiMeshTopology provided. Cannot initialize physical representation.");
                yield break;
            }
            else if (!sharedTopology.Initialized)
            {
                Debug.LogError("The provided ObiMeshTopology contains no data. Cannot initialize physical representation.");
                yield break;
            }

            initializing = true;

            RemoveFromSolver(null);

            GameObject.DestroyImmediate(topology);
            topology = GameObject.Instantiate(sharedTopology);

            active              = new bool[topology.heVertices.Length];
            positions           = new Vector3[topology.heVertices.Length];
            restPositions       = new Vector4[topology.heVertices.Length];
            velocities          = new Vector3[topology.heVertices.Length];
            invMasses           = new float[topology.heVertices.Length];
            principalRadii      = new Vector3[topology.heVertices.Length];
            phases              = new int[topology.heVertices.Length];
            areaContribution    = new float[topology.heVertices.Length];
            deformableTriangles = new int[topology.heFaces.Length * 3];

            initialScaleMatrix.SetTRS(Vector3.zero, Quaternion.identity, transform.lossyScale);

            // Create a particle for each vertex:
            for (int i = 0; i < topology.heVertices.Length; i++)
            {
                Oni.Vertex vertex = topology.heVertices[i];

                // Get the particle's area contribution.
                areaContribution[i] = 0;
                foreach (Oni.Face face in topology.GetNeighbourFacesEnumerator(vertex))
                {
                    areaContribution[i] += topology.GetFaceArea(face) / 3;
                }

                // Get the shortest neighbour edge, particle radius will be half of its length.
                float minEdgeLength = Single.MaxValue;
                foreach (Oni.HalfEdge edge in topology.GetNeighbourEdgesEnumerator(vertex))
                {
                    // vertices at each end of the edge:
                    Vector3 v1 = initialScaleMatrix * topology.heVertices[topology.GetHalfEdgeStartVertex(edge)].position;
                    Vector3 v2 = initialScaleMatrix * topology.heVertices[edge.endVertex].position;

                    minEdgeLength = Mathf.Min(minEdgeLength, Vector3.Distance(v1, v2));
                }

                active[i]           = true;
                invMasses[i]        = (skinnedMeshRenderer == null && areaContribution[i] > 0) ? (1.0f / (DEFAULT_PARTICLE_MASS * areaContribution[i])) : 0;
                positions[i]        = initialScaleMatrix * vertex.position;
                restPositions[i]    = positions[i];
                restPositions[i][3] = 1;         // activate rest position.
                principalRadii[i]   = Vector3.one * minEdgeLength * 0.5f;
                phases[i]           = Oni.MakePhase(1, selfCollisions?Oni.ParticlePhase.SelfCollide:0);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating particles...", i / (float)topology.heVertices.Length));
                }
            }

            // Generate deformable triangles:
            for (int i = 0; i < topology.heFaces.Length; i++)
            {
                Oni.Face face = topology.heFaces[i];

                Oni.HalfEdge e1 = topology.heHalfEdges[face.halfEdge];
                Oni.HalfEdge e2 = topology.heHalfEdges[e1.nextHalfEdge];
                Oni.HalfEdge e3 = topology.heHalfEdges[e2.nextHalfEdge];

                deformableTriangles[i * 3]     = e1.endVertex;
                deformableTriangles[i * 3 + 1] = e2.endVertex;
                deformableTriangles[i * 3 + 2] = e3.endVertex;

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating deformable geometry...", i / (float)topology.heFaces.Length));
                }
            }

            List <ObiMeshTopology.HEEdge> edges = topology.GetEdgeList();

            DistanceConstraints.Clear();
            ObiDistanceConstraintBatch distanceBatch = new ObiDistanceConstraintBatch(true, false);

            DistanceConstraints.AddBatch(distanceBatch);

            // Create distance springs:
            for (int i = 0; i < edges.Count; i++)
            {
                Oni.HalfEdge hedge       = topology.heHalfEdges[edges[i].halfEdgeIndex];
                Oni.Vertex   startVertex = topology.heVertices[topology.GetHalfEdgeStartVertex(hedge)];
                Oni.Vertex   endVertex   = topology.heVertices[hedge.endVertex];

                distanceBatch.AddConstraint(topology.GetHalfEdgeStartVertex(hedge), hedge.endVertex, Vector3.Distance(initialScaleMatrix * startVertex.position, initialScaleMatrix * endVertex.position), 1, 1);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating structural constraints...", i / (float)topology.heHalfEdges.Length));
                }
            }

            // Cook distance constraints, for better cache and SIMD use:
            distanceBatch.Cook();

            // Create aerodynamic constraints:
            AerodynamicConstraints.Clear();
            ObiAerodynamicConstraintBatch aeroBatch = new ObiAerodynamicConstraintBatch(false, false);

            AerodynamicConstraints.AddBatch(aeroBatch);

            for (int i = 0; i < topology.heVertices.Length; i++)
            {
                aeroBatch.AddConstraint(i,
                                        areaContribution[i],
                                        AerodynamicConstraints.dragCoefficient,
                                        AerodynamicConstraints.liftCoefficient);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating aerodynamic constraints...", i / (float)topology.heFaces.Length));
                }
            }

            //Create skin constraints (if needed)
            if (skinnedMeshRenderer != null)
            {
                SkinConstraints.Clear();
                ObiSkinConstraintBatch skinBatch = new ObiSkinConstraintBatch(true, false);
                SkinConstraints.AddBatch(skinBatch);

                for (int i = 0; i < topology.heVertices.Length; ++i)
                {
                    skinBatch.AddConstraint(i, initialScaleMatrix * topology.heVertices[i].position, Vector3.up, 0.05f, 0.1f, 0, 1);

                    if (i % 500 == 0)
                    {
                        yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating skin constraints...", i / (float)topology.heVertices.Length));
                    }
                }

                for (int i = 0; i < topology.normals.Length; ++i)
                {
                    skinBatch.skinNormals[topology.visualMap[i]] = topology.normals[i];
                }

                skinBatch.Cook();
            }

            //Create pressure constraints if the mesh is closed:
            VolumeConstraints.Clear();

            if (topology.IsClosed)
            {
                ObiVolumeConstraintBatch volumeBatch = new ObiVolumeConstraintBatch(false, false);
                VolumeConstraints.AddBatch(volumeBatch);

                float avgInitialScale = (initialScaleMatrix.m00 + initialScaleMatrix.m11 + initialScaleMatrix.m22) * 0.33f;

                int[] triangleIndices = new int[topology.heFaces.Length * 3];
                for (int i = 0; i < topology.heFaces.Length; i++)
                {
                    Oni.Face face = topology.heFaces[i];

                    Oni.HalfEdge e1 = topology.heHalfEdges[face.halfEdge];
                    Oni.HalfEdge e2 = topology.heHalfEdges[e1.nextHalfEdge];
                    Oni.HalfEdge e3 = topology.heHalfEdges[e2.nextHalfEdge];

                    triangleIndices[i * 3]     = e1.endVertex;
                    triangleIndices[i * 3 + 1] = e2.endVertex;
                    triangleIndices[i * 3 + 2] = e3.endVertex;

                    if (i % 500 == 0)
                    {
                        yield return(new CoroutineJob.ProgressInfo("ObiCloth: generating volume constraints...", i / (float)topology.heFaces.Length));
                    }
                }

                volumeBatch.AddConstraint(triangleIndices, topology.MeshVolume * avgInitialScale, 1, 1);
            }

            //Create bending constraints:
            BendingConstraints.Clear();
            ObiBendConstraintBatch bendBatch = new ObiBendConstraintBatch(true, false);

            BendingConstraints.AddBatch(bendBatch);

            Dictionary <int, int> cons = new Dictionary <int, int>();

            for (int i = 0; i < topology.heVertices.Length; i++)
            {
                Oni.Vertex vertex = topology.heVertices[i];

                foreach (Oni.Vertex n1 in topology.GetNeighbourVerticesEnumerator(vertex))
                {
                    float      cosBest = 0;
                    Oni.Vertex vBest   = n1;

                    foreach (Oni.Vertex n2 in topology.GetNeighbourVerticesEnumerator(vertex))
                    {
                        float cos = Vector3.Dot((n1.position - vertex.position).normalized,
                                                (n2.position - vertex.position).normalized);
                        if (cos < cosBest)
                        {
                            cosBest = cos;
                            vBest   = n2;
                        }
                    }

                    if (!cons.ContainsKey(vBest.index) || cons[vBest.index] != n1.index)
                    {
                        cons[n1.index] = vBest.index;

                        Vector3 n1Pos     = initialScaleMatrix * n1.position;
                        Vector3 bestPos   = initialScaleMatrix * vBest.position;
                        Vector3 vertexPos = initialScaleMatrix * vertex.position;

                        float[] bendRestPositions = new float[] { n1Pos[0], n1Pos[1], n1Pos[2],
                                                                  bestPos[0], bestPos[1], bestPos[2],
                                                                  vertexPos[0], vertexPos[1], vertexPos[2] };

                        float restBend = Oni.BendingConstraintRest(bendRestPositions);
                        bendBatch.AddConstraint(n1.index, vBest.index, vertex.index, restBend, 0, 1);
                    }
                }

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiCloth: adding bend constraints...", i / (float)sharedTopology.heVertices.Length));
                }
            }

            bendBatch.Cook();

            // Initialize tether constraints:
            TetherConstraints.Clear();

            // Initialize pin constraints:
            PinConstraints.Clear();
            ObiPinConstraintBatch pinBatch = new ObiPinConstraintBatch(false, false);

            PinConstraints.AddBatch(pinBatch);

            initializing = false;
            initialized  = true;

            if (skinnedMeshRenderer == null)
            {
                InitializeWithRegularMesh();
            }
            else
            {
                InitializeWithSkinnedMesh();
            }
        }
Example #13
0
        protected override IEnumerator Initialize()
        {
            initialized  = false;
            initializing = false;

            RemoveFromSolver(null);

            if (inputMesh == null)
            {
                Debug.LogError("No input mesh provided. Cannot initialize physical representation.");
                yield break;
            }

            initializing = true;

            initialScaleMatrix.SetTRS(Vector3.zero, Quaternion.identity, transform.lossyScale);

            Vector3[]      vertices  = inputMesh.vertices;
            Vector3[]      normals   = inputMesh.normals;
            List <Vector3> particles = new List <Vector3>();

            // Add particles to every vertex, as long as they are not too close to the already added ones:
            for (int i = 0; i < vertices.Length; ++i)
            {
                bool    intersects   = false;
                Vector3 vertexScaled = initialScaleMatrix * vertices[i];

                for (int j = 0; j < particles.Count; ++j)
                {
                    if (Vector3.Distance(vertexScaled, particles[j]) < particleRadius * 2 * (1 - particleOverlap))
                    {
                        intersects = true;
                        break;
                    }
                }
                if (intersects)
                {
                    continue;
                }

                particles.Add(vertexScaled);
            }

            active              = new bool[particles.Count];
            positions           = new Vector3[particles.Count];
            orientations        = new Quaternion[particles.Count];
            restPositions       = new Vector4[particles.Count];
            restOrientations    = new Quaternion[particles.Count];
            velocities          = new Vector3[particles.Count];
            angularVelocities   = new Vector3[particles.Count];
            invMasses           = new float[particles.Count];
            invRotationalMasses = new float[particles.Count];
            principalRadii      = new Vector3[particles.Count];
            phases              = new int[particles.Count];

            for (int i = 0; i < particles.Count; ++i)
            {
                // Perform ellipsoid fitting:
                Vector3        avgNormal         = Vector3.zero;
                List <Vector3> neighbourVertices = new List <Vector3>();

                for (int j = 0; j < vertices.Length; ++j)
                {
                    Vector3 vertexScaled = initialScaleMatrix * vertices[j];

                    if (Vector3.Distance(vertexScaled, particles[i]) < anisotropyNeighborhood)
                    {
                        neighbourVertices.Add(vertexScaled);
                        avgNormal += normals[j];
                    }
                }
                if (neighbourVertices.Count > 0)
                {
                    avgNormal /= neighbourVertices.Count;
                }

                Vector3    centroid        = particles[i];
                Quaternion orientation     = Quaternion.identity;
                Vector3    principalValues = Vector3.one;
                Oni.GetPointCloudAnisotropy(neighbourVertices.ToArray(), neighbourVertices.Count, maxAnisotropy, particleRadius, ref avgNormal, ref centroid, ref orientation, ref principalValues);

                active[i] = true;
                invRotationalMasses[i] = invMasses[i] = 1.0f;
                positions[i]           = Vector3.Lerp(particles[i], centroid, shapeSmoothing);
                restPositions[i]       = positions[i];
                orientations[i]        = orientation;
                restOrientations[i]    = orientation;
                restPositions[i][3]    = 1;              // activate rest position.
                principalRadii[i]      = principalValues;
                phases[i] = Oni.MakePhase(1, (selfCollisions?Oni.ParticlePhase.SelfCollide:0) | (oneSided?Oni.ParticlePhase.OneSided:0));
            }

            //Create shape matching clusters:
            ShapeMatchingConstraints.Clear();

            ObiShapeMatchingConstraintBatch shapeBatch = new ObiShapeMatchingConstraintBatch(false, false);

            ShapeMatchingConstraints.AddBatch(shapeBatch);

            List <int> indices = new List <int>();

            // Generate soft clusters:
            //if (makeSoftClusters){
            for (int i = 0; i < particles.Count; ++i)
            {
                indices.Clear();
                indices.Add(i);
                for (int j = 0; j < particles.Count; ++j)
                {
                    if (i != j && Vector3.Distance(particles[j], particles[i]) < softClusterRadius)
                    {
                        indices.Add(j);
                    }
                }

                shapeBatch.AddConstraint(indices.ToArray(), 1, 0, 0, false);

                if (i % 500 == 0)
                {
                    yield return(new CoroutineJob.ProgressInfo("ObiSoftbody: generating shape matching constraints...", i / (float)particles.Count));
                }
            }
            //}

            //if (makeSolidCluster){

            /*indices.Clear();
             * for (int i = 0; i < particles.Count; ++i)
             *      indices.Add(i);
             * shapeBatch.AddConstraint(indices.ToArray(),1,0,0,true);*/
            //}

            // Initialize pin constraints:
            PinConstraints.Clear();
            ObiPinConstraintBatch pinBatch = new ObiPinConstraintBatch(false, false);

            PinConstraints.AddBatch(pinBatch);

            initializing = false;
            initialized  = true;
        }