private void RecreateCollider3D() { Ferr2DT_DynamicMesh dMesh = new Ferr2DT_DynamicMesh(); List <Vector2> verts = GetColliderVerts(); // create the solid mesh for it for (int i = 0; i < verts.Count; i++) { if (path.closed && i == verts.Count - 1) { dMesh.AddVertex(verts[0]); } else { dMesh.AddVertex(verts[i]); } } dMesh.ExtrudeZ(depth, fill == Ferr2DT_FillMode.InvertedClosed); // remove any faces the user may not want if (!collidersTop) { dMesh.RemoveFaces(new Vector3(0, 1, 0), 45); } if (!collidersLeft) { dMesh.RemoveFaces(new Vector3(-1, 0, 0), 45); } if (!collidersRight) { dMesh.RemoveFaces(new Vector3(1, 0, 0), 45); } if (!collidersBottom) { dMesh.RemoveFaces(new Vector3(0, -1, 0), 45); } // make sure there's a MeshCollider component on this object if (GetComponent <MeshCollider>() == null) { gameObject.AddComponent <MeshCollider>(); } if (physicsMaterial != null) { GetComponent <MeshCollider>().sharedMaterial = physicsMaterial; } // compile the mesh! Mesh m = GetComponent <MeshCollider>().sharedMesh; string name = "Ferr2DT_PathCollider_" + gameObject.name + gameObject.GetInstanceID(); if (m == null || m.name != name) { GetComponent <MeshCollider>().sharedMesh = m = new Mesh(); m.name = name; } GetComponent <MeshCollider>().sharedMesh = null; dMesh.Build(ref m); GetComponent <MeshCollider>().sharedMesh = m; }
/// <summary> /// The Ferr2DT_IPath method, gets called automatically whenever the Ferr2DT path gets updated in the /// editor. This will completely recreate the the visual mesh (only) for the terrain. If you want /// To recreate the collider as well, that's a separate call to RecreateCollider. /// </summary> public void RecreatePath() { //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //sw.Start(); if (path == null) { path = GetComponent <Ferr2D_Path> (); } if (dMesh == null) { dMesh = new Ferr2DT_DynamicMesh(); } if (terrainMaterial == null) { Debug.LogWarning("Cannot create terrain without a Terrain Material!"); return; } MatchOverrides(); // double check the materials! ForceMaterial(terrainMaterial, true, false); dMesh.Clear(); dMesh.color = vertexColor; if (path.Count < 2) { GetComponent <MeshFilter>().sharedMesh = null; return; } // make sure we can keep a consistent scale for the texture if (terrainMaterial.edgeMaterial.mainTexture != null && terrainMaterial.edgeMaterial.mainTexture != null) { unitsPerUV.x = terrainMaterial.edgeMaterial.mainTexture.width / pixelsPerUnit; unitsPerUV.y = terrainMaterial.edgeMaterial.mainTexture.height / pixelsPerUnit; } if (fill != Ferr2DT_FillMode.FillOnlyClosed && fill != Ferr2DT_FillMode.FillOnlySkirt) { // split the path into segments based on the split angle List <List <Vector2> > segments = new List <List <Vector2> > (); List <Ferr2DT_TerrainDirection> dirs = new List <Ferr2DT_TerrainDirection>(); segments = GetSegments(path.GetVerts(smoothPath, splitDist, splitCorners), out dirs); if (dirs.Count < segments.Count) { dirs.Add(directionOverrides[directionOverrides.Count - 1]); } List <int> order = new List <int>(); for (int i = 0; i < segments.Count; i++) { order.Add(i); } order.Sort( new Ferr2DT_Comparer <int>( (x, y) => GetDescription(segments[y]).zOffset.CompareTo(GetDescription(segments[x]).zOffset) )); // process the segments into meshes for (int i = 0; i < order.Count; i++) { AddSegment(segments[order[i]], order.Count <= 1 && path.closed, dirs[order[i]]); } } int[] submesh1 = dMesh.GetCurrentTriangleList(); // add a fill if the user desires if ((fill == Ferr2DT_FillMode.Skirt || fill == Ferr2DT_FillMode.FillOnlySkirt) && terrainMaterial.fillMaterial != null) { AddFill(true); } else if ((fill == Ferr2DT_FillMode.Closed || fill == Ferr2DT_FillMode.InvertedClosed || fill == Ferr2DT_FillMode.FillOnlyClosed) && terrainMaterial.fillMaterial != null) { AddFill(false); } else if (fill == Ferr2DT_FillMode.None) { } int[] submesh2 = dMesh.GetCurrentTriangleList(submesh1.Length); // compile the mesh! Mesh m = GetComponent <MeshFilter>().sharedMesh; string name = "Ferr2DT_PathMesh_" + gameObject.name + gameObject.GetInstanceID(); if (m == null || m.name != name) { GetComponent <MeshFilter>().sharedMesh = m = new Mesh(); m.name = name; } dMesh.Build(ref m); // set up submeshes and submaterials m.subMeshCount = 2; if (renderer.sharedMaterials.Length < 2) { Material[] old = renderer.sharedMaterials; renderer.sharedMaterials = new Material[2]; if (old.Length > 0) { renderer.sharedMaterials[0] = old[0]; } } m.SetTriangles(submesh1, 1); m.SetTriangles(submesh2, 0); //sw.Stop(); //Debug.Log("Creating mesh took: " + sw.Elapsed.TotalMilliseconds + "ms"); }