コード例 #1
0
        public static MeshCollider GeneratePlanarCollision(this CinemachinePathBase path)
        {
            float halfWidth     = path.m_Appearance.width / 2f;
            float maxStepLength = 1f / path.m_Resolution;
            int   maxPoints     = Mathf.CeilToInt(path.MaxPos / maxStepLength) * k_vertsPerQuad;
            int   maxTris       = maxPoints / k_vertsPerQuad * (k_vertsPerTri * k_trisPerQuad);


            List <Vector3> vertices  = new List <Vector3>(maxPoints);
            List <Vector3> normals   = new List <Vector3>(maxPoints);
            List <int>     triangles = new List <int>(maxTris);


            PathMeshNode prevNode = new PathMeshNode(path, path.MinPos);

            for (float tdx = path.MinPos + maxStepLength; tdx < path.MaxPos; tdx += maxStepLength)
            {
                PathMeshNode nextNode = new PathMeshNode(path, tdx);
                {
                    vertices.Add(prevNode.LeftPosition);
                    vertices.Add(nextNode.RightPosition);
                    vertices.Add(prevNode.RightPosition);
                    vertices.Add(nextNode.LeftPosition);


                    triangles.Add(vertices.Count - 4);
                    triangles.Add(vertices.Count - 3);
                    triangles.Add(vertices.Count - 2);

                    triangles.Add(vertices.Count - 4);
                    triangles.Add(vertices.Count - 1);
                    triangles.Add(vertices.Count - 3);


                    normals.Add(prevNode.Normal);
                    normals.Add(nextNode.Normal);
                    normals.Add(prevNode.Normal);
                    normals.Add(nextNode.Normal);
                }
                prevNode = nextNode;
            }

            GameObject collisionObj = new GameObject($"Collision_{path.name}");

            collisionObj.transform.SetParent(path.transform, false);
            collisionObj.gameObject.layer = path.gameObject.layer;

            MeshCollider collider = collisionObj.AddComponent <MeshCollider>();

            collider.sharedMesh = new Mesh()
            {
                name = path.name,

                vertices  = vertices.ToArray(),
                normals   = normals.ToArray(),
                triangles = triangles.ToArray(),
            };

            return(collider);
        }
コード例 #2
0
        internal static void DrawPathGizmo(CinemachinePathBase path, Color pathColor)
        {
            // Draw the path
            var colorOld = Gizmos.color;

            Gizmos.color = pathColor;
            var step    = 1f / path.m_Resolution;
            var lastPos = path.EvaluatePosition(path.MinPos);
            var lastW   = path.EvaluateOrientation(path.MinPos)
                          * Vector3.right * path.m_Appearance.width / 2;

            for (var t = path.MinPos + step; t <= path.MaxPos + step / 2; t += step)
            {
                var p  = path.EvaluatePosition(t);
                var q  = path.EvaluateOrientation(t);
                var w  = q * Vector3.right * path.m_Appearance.width / 2;
                var w2 = w * 1.2f;
                var p0 = p - w2;
                var p1 = p + w2;
                Gizmos.DrawLine(p0, p1);
                Gizmos.DrawLine(lastPos - lastW, p - w);
                Gizmos.DrawLine(lastPos + lastW, p + w);
#if false
// Show the normals, for debugging
                Gizmos.color = Color.red;
                Vector3 y = (q * Vector3.up) * width / 2;
                Gizmos.DrawLine(p, p + y);
                Gizmos.color = pathColor;
#endif
                lastPos = p;
                lastW   = w;
            }

            Gizmos.color = colorOld;
        }
コード例 #3
0
        public static void DrawPathGizmo(CinemachinePathBase path, Color pathColor)
        {
            // Draw the path
            Color colorOld = Gizmos.color;

            Gizmos.color = pathColor;
            float   step    = 1f / path.m_Resolution;
            Vector3 lastPos = path.EvaluatePosition(path.MinPos);
            Vector3 lastW   = (path.EvaluateOrientation(path.MinPos)
                               * Vector3.right) * path.m_Appearance.width / 2;

            for (float t = path.MinPos + step; t <= path.MaxPos + step / 2; t += step)
            {
                Vector3    p  = path.EvaluatePosition(t);
                Quaternion q  = path.EvaluateOrientation(t);
                Vector3    w  = (q * Vector3.right) * path.m_Appearance.width / 2;
                Vector3    w2 = w * 1.2f;
                Vector3    p0 = p - w2;
                Vector3    p1 = p + w2;
                Gizmos.DrawLine(p0, p1);
                Gizmos.DrawLine(lastPos - lastW, p - w);
                Gizmos.DrawLine(lastPos + lastW, p + w);
#if false
                // Show the normals, for debugging
                Gizmos.color = Color.red;
                Vector3 y = (q * Vector3.up) * path.m_Appearance.width / 2;
                Gizmos.DrawLine(p, p + y);
                Gizmos.color = pathColor;
#endif
                lastPos = p;
                lastW   = w;
            }
            Gizmos.color = colorOld;
        }
コード例 #4
0
    private void Start()
    {
        Collider collider = GetComponent <Collider>();


        if (collider != null)
        {
            collider.isTrigger = true;

            if (collider.GetType() == typeof(BoxCollider))
            {
                colliderType = ColliderType.Box;
                blendRange   = GetComponent <BoxCollider>().size.z / 2;
            }
            else if (collider.GetType() == typeof(SphereCollider))
            {
                colliderType = ColliderType.Sphere;
                blendRange   = GetComponent <SphereCollider>().radius;

                if (type == Type.Transition)
                {
                    Debug.LogError("Transition volume types only works with BoxColliders", this);
                    Debug.Break();
                }
            }
            else if (collider.GetType() == typeof(MeshCollider))
            {
                colliderType = ColliderType.Mesh;

                if (type != Type.Curve)
                {
                    Debug.LogError("MeshColliders only work with curve volume types.", this);
                    Debug.Break();
                }
            }

            if (type == Type.Curve)
            {
                path = GetComponent <CinemachinePathBase>();

                if (path != null)
                {
                    blendRange = path.MaxPos;
                }
                else
                {
                    Debug.LogError("Curve volume types needs a CinemachinePath or CinemachineSmoothPath Component.", this);
                    Debug.Break();
                }
            }
        }
        else
        {
            Debug.LogError("WeightVolume needs a collider. Add an appropriate collider.", this);
            Debug.Break();
        }
    }
コード例 #5
0
 private static void DrawTrackeDollyGizmos(CinemachineTrackedDolly target, GizmoType selectionType)
 {
     if (target.IsValid)
     {
         CinemachinePathBase path = target.m_Path;
         if (path != null)
         {
             CinemachinePathEditor.DrawPathGizmo(path, path.m_Appearance.pathColor);
         }
     }
 }
コード例 #6
0
 bool HAS_PATH(Type type)
 {
     path = GetComponent <CinemachinePathBase>();
     if (type == Type.Curve && path == null)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
コード例 #7
0
            public PathMeshNode(CinemachinePathBase path, float time)
            {
                Vector3    center      = path.EvaluatePosition(time);
                Quaternion orientation = path.EvaluateOrientation(time);

                Normal = orientation * Vector3.up;

                float   halfWidth    = path.m_Appearance.width / 2f;
                Vector3 originOffset = path.transform.position;

                RightPosition  = center + orientation * Vector3.right * halfWidth;
                RightPosition -= originOffset;

                LeftPosition  = center + orientation * Vector3.left * halfWidth;
                LeftPosition -= originOffset;
            }
コード例 #8
0
 private static void DrawTrackeDollyGizmos(CinemachineTrackedDolly target, GizmoType selectionType)
 {
     if (target.IsValid)
     {
         CinemachinePathBase path = target.m_Path;
         if (path != null)
         {
             CinemachinePathEditor.DrawPathGizmo(path, path.m_Appearance.pathColor);
             Vector3 pos = path.EvaluatePositionAtUnit(target.m_PathPosition, target.m_PositionUnits);
             Color oldColor = Gizmos.color;
             Gizmos.color = CinemachineCore.Instance.IsLive(target.VirtualCamera)
                 ? CinemachineSettings.CinemachineCoreSettings.ActiveGizmoColour
                 : CinemachineSettings.CinemachineCoreSettings.InactiveGizmoColour;
             Gizmos.DrawLine(pos, target.VirtualCamera.State.RawPosition);
             Gizmos.color = oldColor;
         }
     }
 }
コード例 #9
0
        public static int GetClosestPoint(this CinemachinePathBase path, Vector3 samplePosition)
        {
            float closestDistSqr = Mathf.Infinity;
            int   closestPos     = -1;

            for (int pos = (int)path.MinPos; pos < path.MaxPos; ++pos)
            {
                Vector3 worldPos = path.EvaluatePosition(pos);
                float   distSqr  = (worldPos - samplePosition).sqrMagnitude;

                if (distSqr < closestDistSqr)
                {
                    closestDistSqr = distSqr;
                    closestPos     = pos;
                }
            }

            return(closestPos);
        }
コード例 #10
0
        public static void PopulateLineRenderPoints(this CinemachinePathBase path, LineRenderer renderer)
        {
            float maxStepLength = 1f / path.m_Resolution;
            int   maxPoints     = Mathf.CeilToInt(path.MaxPos / maxStepLength) + 1;

            Vector3[] worldPoints = new Vector3[maxPoints];

            int idx = 0;

            for (float tdx = 0; tdx < path.MaxPos; tdx += maxStepLength)
            {
                Vector3 worldPos = path.EvaluatePosition(tdx);
                worldPoints[idx++] = worldPos;
            }

            renderer.positionCount = maxPoints;
            renderer.SetPositions(worldPoints);

            renderer.useWorldSpace = true;
            renderer.loop          = path.Looped;
        }
コード例 #11
0
 private void Awake()
 {
     m_path         = GetComponentInParent <CinemachinePathBase>();
     m_lineRenderer = GetComponentInChildren <LineRenderer>();
 }
コード例 #12
0
    private void Start()
    {
        brain         = Camera.main.GetComponent <CinemachineBrain>();
        mixer         = GetComponent <CinemachineMixingCamera>();
        BCamera       = mixer.ChildCameras[0];
        mixer.enabled = false;

        Collider collider = GetComponent <Collider>();

        if (collider != null)
        {
            collider.isTrigger = true;

            if (collider.GetType() == typeof(SphereCollider))
            {
                if (type != Type.Focus)
                {
                    Debug.LogError("Type is set to " + type.ToString() + " and has a Sphere Collider attached. Sphere Colliders only work with focus volumes.", this);
                    Debug.Break();
                }

                colliderType = ColliderType.sphere;
                blendRange   = GetComponent <SphereCollider>().radius;
            }
            else if (collider.GetType() == typeof(BoxCollider))
            {
                colliderType = ColliderType.box;
                blendRange   = GetComponent <BoxCollider>().size.z / 2;
            }
            else if (collider.GetType() == typeof(MeshCollider))
            {
                if (type != Type.Composite)
                {
                    Debug.LogError("Type is set to " + type.ToString() + " and has a Mesh Colliders attached. Mesh Colliders only work with composite volumes.", this);
                    Debug.Break();
                }

                colliderType = ColliderType.mesh;
            }
        }
        else
        {
            Debug.LogError(name + " needs a collider. The Mixing Volume type is set to " + type + ". Add an appropriate Collider.", this);
            Debug.Break();
        }

        if (type == Type.Curve)
        {
            path = GetComponent <CinemachinePathBase>();

            if (path == null)
            {
                Debug.LogError("The MixingVolume type is set to Curve. Add an CinemachinePath component.", this);
                Debug.Break();
            }
            else
            {
                blendRange = path.MaxPos;
            }
        }
    }