Beispiel #1
0
        public void Refresh(bool wrapAndDeform = true, int controlPointIndex = -1)
        {
            if (wrapAndDeform)
            {
                for (int i = 0; i < m_segments.Count; ++i)
                {
                    DestroyImmediate(m_segments[i].gameObject);
                }
                m_segments.Clear();

                Vector3 from;
                Vector3 to;
                Mesh.GetBounds(m_axis, out from, out to);

                m_contacts         = Mesh.FindContacts(m_axis);
                m_colliderContacts = ColliderMesh.FindContacts(from, to, m_axis);

                for (int i = 0; i < m_spline.SegmentsCount; ++i)
                {
                    GameObject segmentGO = new GameObject();
                    segmentGO.SetActive(false);
                    segmentGO.name = "Segment";
                    segmentGO.transform.SetParent(transform, false);

                    Segment      segment         = segmentGO.AddComponent <Segment>();
                    MeshRenderer segmentRenderer = segmentGO.AddComponent <MeshRenderer>();
                    MeshFilter   segmentFilter   = segmentGO.AddComponent <MeshFilter>();
                    MeshCollider segmentCollider = segmentGO.AddComponent <MeshCollider>();

                    segmentRenderer.sharedMaterials = m_meshRenderer.sharedMaterials;
                    segmentFilter.sharedMesh        = Instantiate(Mesh);
                    segmentCollider.sharedMesh      = Instantiate(ColliderMesh);

                    segmentGO.SetActive(true);

                    segment.Wrap(segmentFilter.sharedMesh, segmentCollider.sharedMesh, Axis, new[] { i }, Approximation);
                    segment.Deform(this, Mesh, ColliderMesh, false);
                    m_segments.Add(segment);
                }
            }
            else
            {
                if (controlPointIndex == -1)
                {
                    for (int i = 0; i < m_spline.SegmentsCount; ++i)
                    {
                        m_segments[i].Deform(this, Mesh, ColliderMesh, false);
                    }
                }
                else
                {
                    int s0 = controlPointIndex - 2;
                    int s1 = controlPointIndex - 1;
                    for (int i = 0; i < 3; ++i)
                    {
                        if (0 <= s0 && s0 < m_segments.Count)
                        {
                            m_segments[s0].Deform(this, Mesh, ColliderMesh, false);
                        }
                        s0--;
                    }

                    for (int i = 0; i < 3; ++i)
                    {
                        if (0 <= s1 && s1 < m_segments.Count)
                        {
                            m_segments[s1].Deform(this, Mesh, ColliderMesh, false);
                        }
                        s1++;
                    }
                }
            }

            Segment prev = null;

            for (int i = 0; i < m_spline.SegmentsCount; ++i)
            {
                Segment segment = m_segments[i];
                segment.SlerpContacts(this, Mesh, ColliderMesh, prev, null, false);
                prev = segment;
            }
        }
        public void Refresh(bool wrapAndDeform = true, int controlPointIndex = -1)
        {
            if (IsOriginalMeshVisible)
            {
                return;
            }

            if (wrapAndDeform)
            {
                m_spline.Refresh(false);

                for (int i = 0; i < m_segments.Count; ++i)
                {
                    DestroyImmediate(m_segments[i].gameObject);
                }
                m_segments.Clear();

                Vector3 from;
                Vector3 to;
                Mesh.GetBounds(m_axis, out from, out to);

                m_contacts = Mesh.FindContacts(m_axis);
                if (ColliderMesh != null)
                {
                    m_colliderContacts = ColliderMesh.FindContacts(from, to, m_axis);
                }

                int   segmentsCount = m_spline.SegmentsCount / (m_pointsPerSegment + 1);
                int[] indices       = new int[m_pointsPerSegment + 1];
                for (int i = 0; i < segmentsCount; ++i)
                {
                    GameObject segmentGO = new GameObject();
                    segmentGO.hideFlags = HideFlags.DontSave;
                    segmentGO.SetActive(false);
                    segmentGO.name = "Segment";
                    segmentGO.transform.SetParent(transform, false);

                    Segment      segment         = segmentGO.AddComponent <Segment>();
                    MeshRenderer segmentRenderer = segmentGO.AddComponent <MeshRenderer>();
                    MeshFilter   segmentFilter   = segmentGO.AddComponent <MeshFilter>();
                    MeshCollider segmentCollider = segmentGO.AddComponent <MeshCollider>();

                    segmentRenderer.sharedMaterials = m_meshRenderer.sharedMaterials;
                    if (Mesh != null)
                    {
                        segmentFilter.sharedMesh = Instantiate(Mesh);
                    }

                    if (ColliderMesh != null)
                    {
                        segmentCollider.sharedMesh = Instantiate(ColliderMesh);
                    }

                    segmentGO.SetActive(true);

                    for (int j = 0; j < indices.Length; ++j)
                    {
                        indices[j] = i * indices.Length + j;
                    }

                    segment.Wrap(segmentFilter, segmentCollider, Axis, indices, Approximation);
                    segment.Deform(this, Mesh, ColliderMesh, false);
                    m_segments.Add(segment);
                }
            }
            else
            {
                if (controlPointIndex == -1)
                {
                    for (int i = 0; i < m_segments.Count; ++i)
                    {
                        m_segments[i].Deform(this, Mesh, ColliderMesh, false);
                    }
                }
                else
                {
                    int s0 = (controlPointIndex - 2) / (m_pointsPerSegment + 1);
                    int s1 = (controlPointIndex - 1) / (m_pointsPerSegment + 1);
                    for (int i = 0; i < 3; ++i)
                    {
                        if (0 <= s0 && s0 < m_segments.Count)
                        {
                            m_segments[s0].Deform(this, Mesh, ColliderMesh, false);
                        }
                        s0--;
                    }

                    for (int i = 0; i < 3; ++i)
                    {
                        if (0 <= s1 && s1 < m_segments.Count)
                        {
                            m_segments[s1].Deform(this, Mesh, ColliderMesh, false);
                        }
                        s1++;
                    }
                }
            }

            Segment prev = null;

            for (int i = 0; i < m_segments.Count; ++i)
            {
                Segment segment = m_segments[i];
                segment.SlerpContacts(this, Mesh, ColliderMesh, prev, null, false);
                prev = segment;
            }

            if (Refreshed != null)
            {
                Refreshed(this);
            }
        }