public override void UpdateRenderer(object sender, EventArgs e)
        {
            if (section == null || extrudedMesh == null)
            {
                return;
            }

            rope.SmoothCurvesFromParticles();

            CreateMeshIfNeeded();
            ClearMeshData();

            float actualToRestLengthRatio = rope.SmoothLength / rope.RestLength;

            int sectionSegments    = section.Segments;
            int verticesPerSection = sectionSegments + 1;                       // the last vertex in each section must be duplicated, due to uv wraparound.

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

            // we will define and transport a reference frame along the curve using parallel transport method:
            if (frame == null)
            {
                frame = new ObiRope.CurveFrame();
            }
            frame.Reset();
            frame.SetTwist(-sectionTwist * rope.SmoothSections * uvAnchor);

            // 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 <ObiRope.CurveSection> 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:
                    rope.TransportFrame(frame, curve[i], sectionTwist);                  //curve[i].positionAndRadius,tangent,rope.sectionTwist);

                    // update tear prefabs (first segment of not first curve, last segment of not last curve)
                    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;

                    // Loop around each segment:
                    for (int j = 0; j <= sectionSegments; ++j)
                    {
                        vertices.Add(frame.position + (section.vertices[j].x * frame.normal + section.vertices[j].y * frame.binormal) * sectionThickness);
                        normals.Add(vertices[vertices.Count - 1] - frame.position);
                        texTangent   = -Vector3.Cross(normals[normals.Count - 1], frame.tangent);
                        texTangent.w = 1;
                        tangents.Add(texTangent);

                        vertColors.Add(curve[i].color);
                        uv.Set((j / (float)sectionSegments) * uvScale.x, vCoord);
                        uvs.Add(uv);

                        if (j < sectionSegments && i < curve.Count - 1)
                        {
                            tris.Add(sectionIndex * verticesPerSection + j);
                            tris.Add(sectionIndex * verticesPerSection + (j + 1));
                            tris.Add((sectionIndex + 1) * verticesPerSection + j);

                            tris.Add(sectionIndex * verticesPerSection + (j + 1));
                            tris.Add((sectionIndex + 1) * verticesPerSection + (j + 1));
                            tris.Add((sectionIndex + 1) * verticesPerSection + j);
                        }
                    }

                    sectionIndex++;
                }
            }

            CommitMeshData();
        }
        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 ObiRope.CurveFrame();
            }
            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 <ObiRope.CurveSection> 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(frame.tangent, normal);
                    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 * 2 + 1);
                        tris.Add((sectionIndex + 1) * 2);

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

                    sectionIndex++;
                }
            }

            CommitMeshData();
        }
Esempio n. 3
0
        public override void Update(Camera camera)
        {
            if (mesh == null || rope.ropeMesh == null)
            {
                return;
            }

            rope.SmoothCurvesFromParticles();

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

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

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

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

            // 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 ObiRope.CurveFrame();
            }
            frame.Reset();
            frame.SetTwistAndTangent(-rope.sectionTwist * rope.SmoothSections * rope.uvAnchor, 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.normal[0];
            basis[yo + 1] = frame.normal[1];
            basis[yo + 2] = frame.normal[2];

            basis[zo]     = frame.binormal[0];
            basis[zo + 1] = frame.binormal[1];
            basis[zo + 2] = frame.binormal[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[nextIndex].positionAndRadius - curve[index].positionAndRadius;
                    prevV            = curve[index].positionAndRadius - curve[prevIndex].positionAndRadius;
                    tangent          = (nextV + prevV).normalized;
                    sectionMagnitude = nextV.magnitude;

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

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

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

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

                // calculate deformed vertex position:
                Vector3 offsetFromCurve = Vector3.Scale(inputVertices[vIndex], actualScale);
                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();
        }
        public override void UpdateRenderer(object sender, EventArgs e)
        {
            // In case there are no link prefabs to instantiate:
            if (linkPrefabs.Count == 0)
            {
                return;
            }

            // Regenerate instances if needed:
            if (linkInstances == null || linkInstances.Count < rope.TotalParticles)
            {
                CreateChainLinkInstances();
            }

            int constraintCount = rope.GetStructuralConstraintCount();

            // we will define and transport a reference frame along the curve using parallel transport method:
            if (frame == null)
            {
                frame = new ObiRope.CurveFrame();
            }
            frame.Reset();
            frame.SetTwist(-sectionTwist * constraintCount * twistAnchor);

            int lastParticle = -1;
            int tearCount    = 0;

            for (int i = 0; i < constraintCount; ++i)
            {
                int particle1 = -1, particle2 = -1;
                rope.GetStructuralConstraintParticles(i, ref particle1, ref particle2);

                Vector3 pos        = rope.GetParticlePosition(particle1);
                Vector3 nextPos    = rope.GetParticlePosition(particle2);
                Vector3 linkVector = nextPos - pos;
                Vector3 tangent    = linkVector.normalized;

                // update tear prefab at the first side of tear:
                if (i > 0 && particle1 != lastParticle)
                {
                    rope.UpdateTearPrefab(frame, ref tearCount, false);
                    // reset frame at discontinuities:
                    frame.Reset();
                }

                // update frame: TODO: allow mesh rendering offset from torsion (twist)
                rope.TransportFrame(frame, new ObiRopeBase.CurveSection(nextPos, tangent, rope.GetParticleOrientation(particle1) * Vector3.up, Color.white), sectionTwist);

                // update tear prefab at the other side of the tear:
                if (i > 0 && particle1 != lastParticle)
                {
                    frame.position = pos;
                    rope.UpdateTearPrefab(frame, ref tearCount, false);
                }

                // update start/end prefabs:
                if (!rope.Closed)
                {
                    if (i == 0 && rope.startPrefabInstance != null)
                    {
                        rope.PlaceObjectAtCurveFrame(frame, rope.startPrefabInstance, Space.World, false);
                    }
                    else if (i == constraintCount - 1 && rope.endPrefabInstance != null)
                    {
                        frame.position = nextPos;
                        rope.PlaceObjectAtCurveFrame(frame, rope.endPrefabInstance, Space.World, true);
                    }
                }

                if (linkInstances[i] != null)
                {
                    linkInstances[i].SetActive(true);
                    Transform linkTransform = linkInstances[i].transform;
                    linkTransform.position   = pos + linkVector * 0.5f;
                    linkTransform.localScale = rope.thicknessFromParticles ? (rope.principalRadii[particle1][0] / rope.thickness) * linkScale : linkScale;
                    linkTransform.rotation   = Quaternion.LookRotation(tangent, frame.normal);
                }

                lastParticle = particle2;
            }

            for (int i = constraintCount; i < linkInstances.Count; ++i)
            {
                if (linkInstances[i] != null)
                {
                    linkInstances[i].SetActive(false);
                }
            }
        }
Esempio n. 5
0
        public override void Update(Camera camera)
        {
            if (linkInstances.Count == 0)
            {
                return;
            }

            ObiDistanceConstraintBatch distanceBatch = rope.DistanceConstraints.GetFirstBatch();

            // we will define and transport a reference frame along the curve using parallel transport method:
            if (frame == null)
            {
                frame = new ObiRope.CurveFrame();
            }
            frame.Reset();
            frame.SetTwist(-rope.sectionTwist * distanceBatch.ConstraintCount * rope.uvAnchor);

            int lastParticle = -1;
            int tearCount    = 0;

            for (int i = 0; i < distanceBatch.ConstraintCount; ++i)
            {
                int particle1 = distanceBatch.springIndices[i * 2];
                int particle2 = distanceBatch.springIndices[i * 2 + 1];

                Vector3 pos        = rope.GetParticlePosition(particle1);
                Vector3 nextPos    = rope.GetParticlePosition(particle2);
                Vector3 linkVector = nextPos - pos;
                Vector3 tangent    = linkVector.normalized;

                if (i > 0 && particle1 != lastParticle)
                {
                    // update tear prefab at the first side of tear:
                    if (rope.tearPrefabPool != null && tearCount < rope.tearPrefabPool.Length)
                    {
                        if (!rope.tearPrefabPool[tearCount].activeSelf)
                        {
                            rope.tearPrefabPool[tearCount].SetActive(true);
                        }

                        rope.PlaceObjectAtCurveFrame(frame, rope.tearPrefabPool[tearCount], Space.World, true);
                        tearCount++;
                    }

                    // reset frame at discontinuities:
                    frame.Reset();
                }

                // update frame:
                frame.Transport(nextPos, tangent, rope.sectionTwist);

                // update tear prefab at the other side of the tear:
                if (i > 0 && particle1 != lastParticle && rope.tearPrefabPool != null && tearCount < rope.tearPrefabPool.Length)
                {
                    if (!rope.tearPrefabPool[tearCount].activeSelf)
                    {
                        rope.tearPrefabPool[tearCount].SetActive(true);
                    }

                    frame.position = pos;
                    rope.PlaceObjectAtCurveFrame(frame, rope.tearPrefabPool[tearCount], Space.World, false);
                    tearCount++;
                }

                // update start/end prefabs:
                if (!rope.Closed)
                {
                    if (i == 0 && rope.startPrefabInstance != null)
                    {
                        rope.PlaceObjectAtCurveFrame(frame, rope.startPrefabInstance, Space.World, false);
                    }
                    else if (i == distanceBatch.ConstraintCount - 1 && rope.endPrefabInstance != null)
                    {
                        frame.position = nextPos;
                        rope.PlaceObjectAtCurveFrame(frame, rope.endPrefabInstance, Space.World, true);
                    }
                }

                if (linkInstances[i] != null)
                {
                    linkInstances[i].SetActive(true);
                    Transform linkTransform = linkInstances[i].transform;
                    linkTransform.position   = pos + linkVector * 0.5f;
                    linkTransform.localScale = rope.thicknessFromParticles ? (rope.solidRadii[particle1] / rope.thickness) * linkScale : linkScale;
                    linkTransform.rotation   = Quaternion.LookRotation(tangent, frame.normal);
                }

                lastParticle = particle2;
            }

            for (int i = distanceBatch.ConstraintCount; i < linkInstances.Count; ++i)
            {
                if (linkInstances[i] != null)
                {
                    linkInstances[i].SetActive(false);
                }
            }
        }