Ejemplo n.º 1
0
        public void UpdateNow()
        {
            if (needsVerticesUpdate)
            {
                RenderMesh.vertices = renderVertices;
                RenderMesh.RecalculateNormals();
                RenderMesh.RecalculateBounds();

                ColliderMesh.vertices = colliderVertices;
                ColliderMesh.RecalculateBounds();
                // turning collider off and on to force it to update
                meshCollider.enabled = false;
                // ReSharper disable once Unity.InefficientPropertyAccess
                meshCollider.enabled = true;

                overlayMesh.UpdateCollider();

                needsVerticesUpdate = false;
            }

            if (needsUvUpdate)
            {
                RenderMesh.uv2 = uv2;
                needsUvUpdate  = false;
            }
        }
Ejemplo n.º 2
0
 public MeshCollider(
     ColliderMesh colMesh,
     BulletSharp.CollisionFilterGroups group = BulletSharp.CollisionFilterGroups.DefaultFilter,
     BulletSharp.CollisionFilterGroups mask  = BulletSharp.CollisionFilterGroups.AllFilter)
 {
     this.colMesh = colMesh;
     collideGroup = group;
     collideMask  = mask;
 }
Ejemplo n.º 3
0
        public static ColliderMesh FromNwColliderMesh(NwColliderMesh mesh)
        {
            var m = new ColliderMesh();

            m.Vertices = Conv(mesh.Vertices, (v) => new Vector3(v.X, v.Y, v.Z));
            m.Indices  = mesh.Indices;

            return(m);
        }
Ejemplo n.º 4
0
    public override bool Prepare(MegaModContext mc)
    {
        if (colmesh == null)
        {
            colmesh = new ColliderMesh();
        }

        if (colmesh.obj != hitObject && hitObject != null)
        {
            //Debug.Log("building");

            colmesh = new ColliderMesh();

            // This is colldier mesh not this mesh
            colmesh.mesh = MegaUtils.GetMesh(hitObject);
            //Debug.Log("verts " + colmesh.mesh.vertexCount);

            colmesh.verts   = colmesh.mesh.vertices;
            colmesh.tris    = colmesh.mesh.triangles;
            colmesh.normals = colmesh.mesh.normals;
            //colmesh.tree = KDTree.MakeFromPoints(colmesh.verts);
            //colmesh.trilist = new VertTriList(colmesh.mesh);
            colmesh.obj = hitObject;
            Debug.Log("Col info built");
        }
        //Debug.Log("Done");

        if (hitObject)
        {
            localPos = transform.worldToLocalMatrix.MultiplyPoint(hitObject.transform.position);
            col      = hitObject.collider;
        }

        if (hitObject == null)
        {
            return(false);
        }
        //if ( col == null )
        //return false;

        affected.Clear();
        distances.Clear();

        if (offsets == null || offsets.Length != mc.mod.verts.Length)
        {
            offsets = new Vector3[mc.mod.verts.Length];
        }

        if (normals == null || normals.Length != verts.Length)
        {
            normals = mc.mod.mesh.normals;              // get current normals
        }
        if (penetration == null || penetration.Length != mc.mod.verts.Length)
        {
            penetration = new float[mc.mod.verts.Length];
        }
        mat = Matrix4x4.identity;

        SetAxis(mat);

        return(true);
    }
Ejemplo n.º 5
0
	public override bool Prepare(MegaModContext mc)
	{
		if ( colmesh == null )
		{
			colmesh = new ColliderMesh();
		}

		if ( colmesh.obj != hitObject && hitObject != null )
		{
			//Debug.Log("building");

			colmesh = new ColliderMesh();

			// This is colldier mesh not this mesh
			colmesh.mesh = MegaUtils.GetMesh(hitObject);
			//Debug.Log("verts " + colmesh.mesh.vertexCount);

			colmesh.verts = colmesh.mesh.vertices;
			colmesh.tris = colmesh.mesh.triangles;
			colmesh.normals = colmesh.mesh.normals;
			colmesh.tree = KDTree.MakeFromPoints(colmesh.verts);
			colmesh.trilist = new VertTriList(colmesh.mesh);
			colmesh.obj = hitObject;
			Debug.Log("Col info built");
		}
		//Debug.Log("Done");

		if ( hitObject )
		{
			localPos = transform.worldToLocalMatrix.MultiplyPoint(hitObject.transform.position);
			col = hitObject.collider;
		}

		if ( hitObject == null )
			return false;
		//if ( col == null )
			//return false;


		affected.Clear();
		distances.Clear();

		if ( offsets == null || offsets.Length != mc.mod.verts.Length )
			offsets = new Vector3[mc.mod.verts.Length];

		if ( normals == null || normals.Length != verts.Length )
			normals = mc.mod.mesh.normals;	// get current normals

		if ( penetration == null || penetration.Length != mc.mod.verts.Length )
		{
			penetration = new float[mc.mod.verts.Length];
		}
		mat = Matrix4x4.identity;

		SetAxis(mat);

		return true;
	}
Ejemplo n.º 6
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;
            }
        }
Ejemplo n.º 7
0
 public MeshCollider(ColliderMesh colMesh)
 {
     this.colMesh = colMesh;
 }
        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);
            }
        }