private UnityMesh CreateMesh(List <Vector3d> lPath) { DMesh3 dMesh = meshGen.CreateMesh(lPath, qPoly.ToList(), tmpSeam); // Defer normal calculation if we reduce smoothing later. if (triangleReductionFactor > 1f) { dMesh.ReduceTriangles(triangleReductionFactor, !reduceSmoothing); } else if (!reduceSmoothing) { MeshNormals.QuickCompute(dMesh); } UnityMesh uMesh = dMesh.ToUnityMesh(); uMesh.seam = meshGen.Seam; // TODO if (reduceSmoothing) { uMesh.IncreaseVertexCount(smoothingThresholdAngle); } return(uMesh); }
private void Update() { // Waiting for new mesh. if (tmpMesh != null) { chunkCount++; InitializeChunk(); tmpMesh = null; meshGenPending = false; } // Demo - player is always moving forward. if (player.isReady && !meshGenPending) { if (player.chunk.id > chunkCount - 15) { BatchUpdate(ringsPerChunk); } if (Chunk.nActive > 20) { Chunk.Release(transform.GetChild(0).GetComponent <Chunk>()); } } // AnimateSharedMaterial(); }
private async Task CreateChunk() { List <Vector3d> lPath = qPath.ToList(); tmpAbsPath.Clear(); tmpRelPath.Clear(); Vector3d endPos = lPath[nPolys]; float hrzAngle = Vector2.SignedAngle(Vector2.up, (Vector2)endPos.xz); for (int i = 0; i < lPath.Count; i++) { tmpAbsPath.Add((Vector3)lPath[i]); // Offset and rotate path positions. lPath[i] = chunkRot * (Vector3)(lPath[i] - chunkPos); tmpRelPath.Add((Vector3)lPath[i]); } tmpChunk = Chunk.Retrieve(chunkPos, Quaternion.Inverse(chunkRot)); // Position & rotation offset for next chunk prefab. chunkPos = (Vector3)endPos; chunkRot = Quaternion.AngleAxis(hrzAngle, Vector3.up); for (int i = 0; i < tmpSeam.Count; i++) { // Seam vertices global to local. tmpSeam[i] = tmpChunk.transform.InverseTransformPoint((Vector3)tmpSeam[i]); } meshGenPending = true; tmpMesh = await Task.Run(() => CreateMesh(lPath)); }
public static UnityMesh ToUnityMesh(this DMesh3 dMesh, bool bNorm = true, bool bUV = false, bool bCol = false) { bNorm &= dMesh.HasVertexNormals; bUV &= dMesh.HasVertexUVs; bCol &= dMesh.HasVertexColors; int[] vertexMap = new int[dMesh.VerticesBuffer.Length]; int[] triangleMap = new int[dMesh.TrianglesBuffer.Length]; int[] triangles = new int[dMesh.TriangleCount * 3]; List <Vector3d> vertices = new List <Vector3d>(); List <Vector3f> normals = new List <Vector3f>(); List <Vector2f> uv = new List <Vector2f>(); List <Colorf> colors = new List <Colorf>(); List <int> vertexUseCount = new List <int>(); NewVertexInfo vInfo = new NewVertexInfo(new Vector3d(), new Vector3f(), new Vector3f(), new Vector2f()); IEnumerator e = dMesh.TrianglesRefCounts.GetEnumerator(); int ti = 0; while (e.MoveNext()) { int iRef = (int)e.Current; Index3i triangle = dMesh.GetTriangle(iRef); triangleMap[iRef] = ti; for (int i = 0; i < 3; i++) { int vertIndex = triangle[i]; if (vertexMap[vertIndex] == 0) { vertexUseCount.Add(1); dMesh.GetVertex(vertIndex, ref vInfo, bNorm, bCol, bUV); vertices.Add(new Vector3f((float)vInfo.v.x, (float)vInfo.v.y, (float)vInfo.v.z)); vertexMap[vertIndex] = vertices.Count - 1; if (bNorm) { normals.Add(vInfo.n); } if (bUV) { uv.Add(vInfo.uv); } if (bCol) { colors.Add(new Colorf(vInfo.c.x, vInfo.c.y, vInfo.c.z)); } } else { vertexUseCount[vertexMap[vertIndex]]++; } triangles[ti * 3 + i] = vertexMap[vertIndex]; } ti++; } UnityMesh uMesh = new UnityMesh(vertexUseCount.ToArray(), triangles, vertices, normals, uv, colors); // Triangle normals and neighbors. e = dMesh.TrianglesRefCounts.GetEnumerator(); while (e.MoveNext()) { int iRef = (int)e.Current; int[] nb = dMesh.GetTriNeighbourTris(iRef).array; int[] neighbors = new int[3]; for (int i = 0; i < 3; i++) { neighbors[i] = (nb[i] != -1) ? triangleMap[nb[i]] : -1; } uMesh.AddTriangleInfo(triangleMap[iRef], (Vector3f)dMesh.GetTriNormal(iRef), neighbors); } return(uMesh); }