예제 #1
0
        private void RefreshInternal(DirtyFlags dirtyFlags)
        {
            if (!m_spline || m_spline.Count < 2)
            {
                return;
            }

            if (!m_updatePosition && m_updateRotation == RotationMode.No)
            {
                return;
            }

            BezierSpline.Segment segment = m_spline.GetSegmentAt(m_normalizedT);

            switch (m_updateRotation)
            {
            case RotationMode.UseSplineNormals:
                if (m_rotationOffset == Vector3.zero)
                {
                    transform.rotation = Quaternion.LookRotation(segment.GetTangent(), segment.GetNormal());
                }
                else
                {
                    transform.rotation = Quaternion.LookRotation(segment.GetTangent(), segment.GetNormal()) * Quaternion.Euler(m_rotationOffset);
                }

                break;

            case RotationMode.UseEndPointRotations:
                if (m_rotationOffset == Vector3.zero)
                {
                    transform.rotation = Quaternion.LerpUnclamped(segment.point1.rotation, segment.point2.rotation, segment.localT);
                }
                else
                {
                    transform.rotation = Quaternion.LerpUnclamped(segment.point1.rotation, segment.point2.rotation, segment.localT) * Quaternion.Euler(m_rotationOffset);
                }

                break;
            }

            if (m_updatePosition && (dirtyFlags & DirtyFlags.SplineShapeChanged) == DirtyFlags.SplineShapeChanged)
            {
                if (m_positionOffset == Vector3.zero)
                {
                    transform.position = segment.GetPoint();
                }
                else
                {
                    transform.position = segment.GetPoint() + transform.rotation * m_positionOffset;
                }
            }

#if UNITY_EDITOR
            transform.hasChanged = false;
#endif
        }
예제 #2
0
        public override void Execute(float deltaTime)
        {
            BezierSpline spline  = Spline;
            float        t       = highQuality ? spline.evenlySpacedPoints.GetPercentageAtNormalizedT(NormalizedT) : NormalizedT;
            bool         forward = MovingForward;

            for (int i = 0; i < tailObjects.Count; i++)
            {
                Transform tailObject = tailObjects[i];
                Vector3   tailPosition;
                float     tailNormalizedT;
                if (highQuality)
                {
                    if (forward)
                    {
                        t -= tailObjectDistances[i] / spline.evenlySpacedPoints.splineLength;
                    }
                    else
                    {
                        t += tailObjectDistances[i] / spline.evenlySpacedPoints.splineLength;
                    }

                    tailNormalizedT = spline.evenlySpacedPoints.GetNormalizedTAtPercentage(t);
                    tailPosition    = spline.GetPoint(tailNormalizedT);
                }
                else
                {
                    tailPosition    = spline.MoveAlongSpline(ref t, forward ? -tailObjectDistances[i] : tailObjectDistances[i]);
                    tailNormalizedT = t;
                }

                tailObject.position = Vector3.Lerp(tailObject.position, tailPosition, movementLerpModifier * deltaTime);

                if (lookAt == LookAtMode.Forward)
                {
                    BezierSpline.Segment segment = spline.GetSegmentAt(tailNormalizedT);
                    tailObject.rotation = Quaternion.Lerp(tailObject.rotation, Quaternion.LookRotation(forward ? segment.GetTangent() : -segment.GetTangent(), segment.GetNormal()), rotationLerpModifier * deltaTime);
                }
                else if (lookAt == LookAtMode.SplineExtraData)
                {
                    tailObject.rotation = Quaternion.Lerp(tailObject.rotation, spline.GetExtraData(tailNormalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
                }
            }
        }
        public override void Execute(float deltaTime)
        {
            float _normalizedT = highQuality ? spline.evenlySpacedPoints.GetNormalizedTAtPercentage(m_normalizedT) : m_normalizedT;

            transform.position = Vector3.Lerp(transform.position, spline.GetPoint(_normalizedT), movementLerpModifier * deltaTime);

            if (lookAt == LookAtMode.Forward)
            {
                BezierSpline.Segment segment = spline.GetSegmentAt(_normalizedT);
                Quaternion           targetRotation;
                if (isGoingForward)
                {
                    targetRotation = Quaternion.LookRotation(segment.GetTangent(), segment.GetNormal());
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-segment.GetTangent(), segment.GetNormal());
                }

                transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationLerpModifier * deltaTime);
            }
            else if (lookAt == LookAtMode.SplineExtraData)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, spline.GetExtraData(_normalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
            }

            if (isGoingForward)
            {
                m_normalizedT += deltaTime / travelTime;

                if (m_normalizedT > 1f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT -= 1f;
                    }
                    else
                    {
                        m_normalizedT  = 2f - m_normalizedT;
                        isGoingForward = false;
                    }

                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompletedCalledAt1 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                m_normalizedT -= deltaTime / travelTime;

                if (m_normalizedT < 0f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT += 1f;
                    }
                    else
                    {
                        m_normalizedT  = -m_normalizedT;
                        isGoingForward = true;
                    }

                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompletedCalledAt0 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }
        public void Refresh()
        {
            if (!m_spline)
            {
                return;
            }

            if (!meshFilter || (meshFilter.sharedMesh && meshFilter.sharedMesh != mesh && meshFilter.sharedMesh != originalMesh))
            {
                Initialize();
            }

            if (!originalMesh)
            {
                return;
            }

            if (vertices == null || vertices.Length != originalVertices.Length)
            {
                vertices = new Vector3[originalVertices.Length];
            }

            if (m_normalsMode == VectorMode.ModifyOriginals)
            {
                if (originalNormals == null)
                {
                    originalNormals = originalMesh.normals;
                }

                if (originalNormals == null || originalNormals.Length < originalVertices.Length)                  // If somehow above statement returned null
                {
                    normals = null;
                }
                else if (normals == null || normals.Length != originalNormals.Length)
                {
                    normals = new Vector3[originalNormals.Length];
                }
            }
            else
            {
                normals = null;
            }

            if (m_tangentsMode == VectorMode.ModifyOriginals)
            {
                if (originalTangents == null)
                {
                    originalTangents = originalMesh.tangents;
                }

                if (originalTangents == null || originalTangents.Length < originalVertices.Length)                  // If somehow above statement returned null
                {
                    tangents = null;
                }
                else if (tangents == null || tangents.Length != originalTangents.Length)
                {
                    tangents = new Vector4[originalTangents.Length];
                }
            }
            else
            {
                tangents = null;
            }

            Vector2 _splineSampleRange = m_splineSampleRange;

            if (m_invertDirection)
            {
                float temp = _splineSampleRange.x;
                _splineSampleRange.x = _splineSampleRange.y;
                _splineSampleRange.y = temp;
            }

            bool  isSampleRangeForwards = _splineSampleRange.x <= _splineSampleRange.y;
            float splineSampleLength    = _splineSampleRange.y - _splineSampleRange.x;
            bool  dontInvertModifiedVertexAttributes = (m_thicknessMultiplier.x > 0f && m_thicknessMultiplier.y > 0f);

            BezierSpline.EvenlySpacedPointsHolder evenlySpacedPoints = m_highQuality ? m_spline.evenlySpacedPoints : null;

            Vector3 initialPoint = m_spline.GetPoint(0f);

            for (int i = 0; i < originalVertices.Length; i++)
            {
                Vector3 vertex = originalVertices[i];

                float   vertexPosition;
                Vector3 vertexOffset;
                switch (m_bendAxis)
                {
                case Axis.X:
                    vertexPosition = vertex.x;
                    vertexOffset   = new Vector3(vertex.z * m_thicknessMultiplier.x, 0f, vertex.y * m_thicknessMultiplier.y);
                    break;

                case Axis.Y:
                default:
                    vertexPosition = vertex.y;
                    vertexOffset   = new Vector3(vertex.x * m_thicknessMultiplier.x, 0f, vertex.z * m_thicknessMultiplier.y);
                    break;

                case Axis.Z:
                    vertexPosition = vertex.z;
                    vertexOffset   = new Vector3(vertex.y * m_thicknessMultiplier.x, 0f, vertex.x * m_thicknessMultiplier.y);
                    break;
                }

                float normalizedT = _splineSampleRange.x + (vertexPosition - minVertex) * _1OverVertexRange * splineSampleLength;                   // Remap from [minVertex,maxVertex] to _splineSampleRange
                if (m_highQuality)
                {
                    normalizedT = evenlySpacedPoints.GetNormalizedTAtPercentage(normalizedT);
                }

                BezierSpline.Segment segment = m_spline.GetSegmentAt(normalizedT);

                Vector3    point    = segment.GetPoint() - initialPoint;
                Vector3    tangent  = isSampleRangeForwards ? segment.GetTangent() : -segment.GetTangent();
                Quaternion rotation = Quaternion.AngleAxis(m_extraRotation, tangent) * Quaternion.LookRotation(segment.GetNormal(), tangent);

                Vector3 direction = rotation * vertexOffset;
                vertices[i] = point + direction;

                if (normals != null)                  // The only case this happens is when Normals Mode is ModifyOriginals and the original mesh has normals
                {
                    normals[i] = rotation * (dontInvertModifiedVertexAttributes ? originalNormals[i] : -originalNormals[i]);
                }
                if (tangents != null)                  // The only case this happens is when Tangents Mode is ModifyOriginals and the original mesh has tangents
                {
                    float tangentW = originalTangents[i].w;
                    tangents[i]   = rotation * (dontInvertModifiedVertexAttributes ? originalTangents[i] : -originalTangents[i]);
                    tangents[i].w = tangentW;
                }
            }

            mesh.vertices = vertices;
            if (m_normalsMode == VectorMode.ModifyOriginals)
            {
                mesh.normals = normals;
            }
            if (m_tangentsMode == VectorMode.ModifyOriginals)
            {
                mesh.tangents = tangents;
            }

            if (m_normalsMode == VectorMode.RecalculateFromScratch)
            {
                mesh.RecalculateNormals();

#if UNITY_EDITOR
                // Cache original normals so that we can reset normals in OnValidate when normals are reset back to DontModify
                if (originalNormals == null)
                {
                    originalNormals = originalMesh.normals;
                }
#endif
            }

            if (m_tangentsMode == VectorMode.RecalculateFromScratch)
            {
                mesh.RecalculateTangents();

#if UNITY_EDITOR
                // Cache original tangents so that we can reset tangents in OnValidate when tangents are reset back to DontModify
                if (originalTangents == null)
                {
                    originalTangents = originalMesh.tangents;
                }
#endif
            }

            mesh.RecalculateBounds();
        }
예제 #5
0
        public override void Execute(float deltaTime)
        {
            float targetSpeed = (isGoingForward) ? speed : -speed;

            Vector3 targetPos = spline.MoveAlongSpline(ref m_normalizedT, targetSpeed * deltaTime);

            transform.position = targetPos;
            //transform.position = Vector3.Lerp( transform.position, targetPos, movementLerpModifier * deltaTime );

            bool movingForward = MovingForward;

            if (lookAt == LookAtMode.Forward)
            {
                BezierSpline.Segment segment = spline.GetSegmentAt(m_normalizedT);
                Quaternion           targetRotation;
                if (movingForward)
                {
                    targetRotation = Quaternion.LookRotation(segment.GetTangent(), segment.GetNormal());
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-segment.GetTangent(), segment.GetNormal());
                }

                transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationLerpModifier * deltaTime);
            }
            else if (lookAt == LookAtMode.SplineExtraData)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, spline.GetExtraData(m_normalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
            }

            if (movingForward)
            {
                if (m_normalizedT >= 1f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT -= 1f;
                    }
                    else
                    {
                        m_normalizedT  = 2f - m_normalizedT;
                        isGoingForward = !isGoingForward;
                    }

                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompletedCalledAt1 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                if (m_normalizedT <= 0f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT += 1f;
                    }
                    else
                    {
                        m_normalizedT  = -m_normalizedT;
                        isGoingForward = !isGoingForward;
                    }

                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompletedCalledAt0 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }