Esempio n. 1
0
 public override void TransportFrame(ObiCurveFrame frame, ObiCurveSection section, float sectionTwist)
 {
     if (frame != null)
     {
         frame.Transport(section, sectionTwist);
     }
 }
Esempio n. 2
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();
        }
Esempio n. 3
0
        public override void UpdateRenderer(object sender, EventArgs e)
        {
            if (mesh == null)
            {
                return;
            }

            rope.SmoothCurvesFromParticles();

            if (rope.curves.Count == 0)
            {
                return;
            }

            ObiList <ObiCurveSection> curve = rope.curves[0];

            if (curve.Count < 2)
            {
                return;
            }

            float actualToRestLengthRatio = stretchWithRope ? rope.SmoothLength / rope.RestLength : 1;

            // squashing factor, makes mesh thinner when stretched and thicker when compresssed.
            float squashing = Mathf.Clamp(1 + volumeScaling * (1 / Mathf.Max(actualToRestLengthRatio, 0.01f) - 1), 0.01f, 2);

            // Calculate scale along swept axis so that the mesh spans the entire lenght of the rope if required.
            Vector3 actualScale = scale;

            if (spanEntireLength)
            {
                actualScale[(int)axis] = rope.RestLength / meshSizeAlongAxis;
            }

            float   previousVertexValue = 0;
            float   meshLength          = 0;
            int     index            = 0;
            int     nextIndex        = 1;
            int     prevIndex        = 0;
            Vector3 nextV            = curve[nextIndex].positionAndRadius - curve[index].positionAndRadius;
            Vector3 prevV            = curve[index].positionAndRadius - curve[prevIndex].positionAndRadius;
            Vector3 tangent          = (nextV + prevV).normalized;
            float   sectionMagnitude = nextV.magnitude;

            // we will define and transport a reference frame along the curve using parallel transport method:
            if (frame == null)
            {
                frame = new ObiCurveFrame();
            }
            frame.Reset();
            frame.SetTwistAndTangent(-sectionTwist * rope.SmoothSections * twistAnchor, tangent);

            // set frame's initial position:
            frame.position = curve[index].positionAndRadius;

            // basis matrix for deforming the mesh, also calculate column offsets based on swept axis:
            Matrix4x4 basis = new Matrix4x4();
            int       xo    = ((int)axis) % 3 * 4;
            int       yo    = ((int)axis + 1) % 3 * 4;
            int       zo    = ((int)axis + 2) % 3 * 4;

            basis[xo]     = frame.tangent[0];
            basis[xo + 1] = frame.tangent[1];
            basis[xo + 2] = frame.tangent[2];

            basis[yo]     = frame.binormal[0];
            basis[yo + 1] = frame.binormal[1];
            basis[yo + 2] = frame.binormal[2];

            basis[zo]     = frame.normal[0];
            basis[zo + 1] = frame.normal[1];
            basis[zo + 2] = frame.normal[2];

            for (int i = 0; i < orderedVertices.Length; ++i)
            {
                int   vIndex      = orderedVertices[i];
                float vertexValue = inputVertices[vIndex][(int)axis] * actualScale[(int)axis] + offset;

                // Calculate how much we've advanced in the sort axis since the last vertex:
                meshLength         += (vertexValue - previousVertexValue) * actualToRestLengthRatio;
                previousVertexValue = vertexValue;

                // If we have advanced to the next section of the curve:
                while (meshLength > sectionMagnitude && sectionMagnitude > Mathf.Epsilon)
                {
                    meshLength -= sectionMagnitude;
                    index       = Mathf.Min(index + 1, curve.Count - 1);

                    // Calculate previous and next curve indices:
                    nextIndex = Mathf.Min(index + 1, curve.Count - 1);
                    prevIndex = Mathf.Max(index - 1, 0);

                    // Calculate current tangent as the vector between previous and next curve points:
                    nextV = curve[index].positionAndRadius - curve[nextIndex].positionAndRadius;

                    /*prevV = curve[index].positionAndRadius - curve[prevIndex].positionAndRadius;
                     * tangent = (nextV + prevV).normalized;*/
                    sectionMagnitude = nextV.magnitude;                     // TODO: revisar, ya que la w debería ser cero, no debe influir el radio.

                    // Transport frame:
                    frame.Transport(curve[index], sectionTwist);

                    // Update basis matrix:
                    basis[xo]     = frame.tangent[0];
                    basis[xo + 1] = frame.tangent[1];
                    basis[xo + 2] = frame.tangent[2];

                    basis[yo]     = frame.binormal[0];
                    basis[yo + 1] = frame.binormal[1];
                    basis[yo + 2] = frame.binormal[2];

                    basis[zo]     = frame.normal[0];
                    basis[zo + 1] = frame.normal[1];
                    basis[zo + 2] = frame.normal[2];
                }

                float sectionThickness = rope.thicknessFromParticles ? curve[index].positionAndRadius.w : rope.thickness;

                // calculate deformed vertex position:
                Vector3 offsetFromCurve = Vector3.Scale(inputVertices[vIndex], actualScale * sectionThickness * squashing);
                offsetFromCurve[(int)axis] = meshLength;

                vertices[vIndex]   = frame.position + basis.MultiplyVector(offsetFromCurve);
                normals[vIndex]    = basis.MultiplyVector(inputNormals[vIndex]);
                tangents[vIndex]   = basis * inputTangents[vIndex];               // avoids expensive implicit conversion from Vector4 to Vector3.
                tangents[vIndex].w = inputTangents[vIndex].w;
            }

            CommitMeshData();
        }
Esempio n. 4
0
        public void UpdateRenderer(Camera camera)
        {
            if (camera == null || !rope.gameObject.activeInHierarchy)
            {
                return;
            }

            rope.SmoothCurvesFromParticles();

            CreateMeshIfNeeded();
            ClearMeshData();

            float actualToRestLengthRatio = rope.SmoothLength / rope.RestLength;

            float vCoord       = -uvScale.y * rope.RestLength * uvAnchor;       // v texture coordinate.
            int   sectionIndex = 0;
            int   tearCount    = 0;

            Vector3 localSpaceCamera = rope.transform.InverseTransformPoint(camera.transform.position);

            // we will define and transport a reference frame along the curve using parallel transport method:
            if (frame == null)
            {
                frame = new ObiCurveFrame();
            }
            frame.Reset();

            // for closed curves, last frame of the last curve must be equal to first frame of first curve.
            Vector3 firstTangent = Vector3.forward;

            Vector4 texTangent = Vector4.zero;
            Vector2 uv         = Vector2.zero;

            for (int c = 0; c < rope.curves.Count; ++c)
            {
                ObiList <ObiCurveSection> curve = rope.curves[c];

                // Reinitialize frame for each curve.
                frame.Reset();

                for (int i = 0; i < curve.Count; ++i)
                {
                    // Calculate previous and next curve indices:
                    //int nextIndex = Mathf.Min(i+1,curve.Count-1);
                    int prevIndex = Mathf.Max(i - 1, 0);

                    // Calculate current tangent as the vector between previous and next curve points:

                    /*Vector3 nextV;
                     *
                     * // The next tangent of the last segment of the last curve in a closed rope, is the first tangent again:
                     * if (rope.Closed && c == rope.curves.Count-1 && i == curve.Count-1 )
                     *      nextV = firstTangent;
                     * else
                     *      nextV = curve[nextIndex].positionAndRadius - curve[i].positionAndRadius;
                     *
                     * Vector3 prevV = curve[i].positionAndRadius - curve[prevIndex].positionAndRadius;
                     * Vector3 tangent = nextV + prevV;*/

                    // update frame:
                    frame.Transport(curve[i], 0);

                    // update tear prefabs:
                    if (c > 0 && i == 0)
                    {
                        rope.UpdateTearPrefab(frame, ref tearCount, false);
                    }
                    if (c < rope.curves.Count - 1 && i == curve.Count - 1)
                    {
                        rope.UpdateTearPrefab(frame, ref tearCount, true);
                    }

                    // update start/end prefabs:
                    if (c == 0 && i == 0)
                    {
                        // store first tangent of the first curve (for closed ropes):
                        firstTangent = frame.tangent;

                        if (rope.startPrefabInstance != null && !rope.Closed)
                        {
                            rope.PlaceObjectAtCurveFrame(frame, rope.startPrefabInstance, Space.Self, false);
                        }
                    }
                    else if (c == rope.curves.Count - 1 && i == curve.Count - 1 && rope.endPrefabInstance != null && !rope.Closed)
                    {
                        rope.PlaceObjectAtCurveFrame(frame, rope.endPrefabInstance, Space.Self, true);
                    }

                    // advance v texcoord:
                    vCoord += uvScale.y * (Vector3.Distance(curve[i].positionAndRadius, curve[prevIndex].positionAndRadius) /
                                           (normalizeV?rope.SmoothLength:actualToRestLengthRatio));

                    // calculate section thickness (either constant, or particle radius based):
                    float sectionThickness = (rope.thicknessFromParticles ? curve[i].positionAndRadius.w : rope.thickness) * sectionThicknessScale;

                    Vector3 normal = frame.position - localSpaceCamera;
                    normal.Normalize();

                    Vector3 bitangent = Vector3.Cross(normal, frame.tangent);
                    bitangent.Normalize();

                    vertices.Add(frame.position + bitangent * sectionThickness);
                    vertices.Add(frame.position - bitangent * sectionThickness);

                    normals.Add(-normal);
                    normals.Add(-normal);

                    texTangent   = -bitangent;
                    texTangent.w = 1;
                    tangents.Add(texTangent);
                    tangents.Add(texTangent);

                    vertColors.Add(curve[i].color);
                    vertColors.Add(curve[i].color);

                    uv.Set(0, vCoord);
                    uvs.Add(uv);
                    uv.Set(1, vCoord);
                    uvs.Add(uv);

                    if (i < curve.Count - 1)
                    {
                        tris.Add(sectionIndex * 2);
                        tris.Add((sectionIndex + 1) * 2);
                        tris.Add(sectionIndex * 2 + 1);

                        tris.Add(sectionIndex * 2 + 1);
                        tris.Add((sectionIndex + 1) * 2);
                        tris.Add((sectionIndex + 1) * 2 + 1);
                    }

                    sectionIndex++;
                }
            }

            CommitMeshData();
        }