From f4bb8da1290189f530f7fdcb4a94a4fb57a92e39 Mon Sep 17 00:00:00 2001 From: Andy Baker Date: Sun, 15 May 2022 13:28:32 +0100 Subject: [PATCH] Split mesh data generation from Unity mesh stuff so that it's possible to do that part in a background thread --- com.ixxy.polyhydra/Core/Runtime/OpParams.cs | 1 - com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs | 94 +++++++++++++++------ package.json | 2 +- 3 files changed, 67 insertions(+), 30 deletions(-) diff --git a/com.ixxy.polyhydra/Core/Runtime/OpParams.cs b/com.ixxy.polyhydra/Core/Runtime/OpParams.cs index 836e39c..7e6a602 100644 --- a/com.ixxy.polyhydra/Core/Runtime/OpParams.cs +++ b/com.ixxy.polyhydra/Core/Runtime/OpParams.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using UnityEngine; diff --git a/com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs b/com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs index 6dabc33..52e56af 100644 --- a/com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs +++ b/com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs @@ -753,7 +753,7 @@ public Bounds GetBounds() return bounds; } - public Mesh BuildUnityMesh( + public MeshData BuildMeshData( bool generateSubmeshes = false, Color[] colors = null, ColorMethods colorMethod = ColorMethods.ByRole, @@ -771,11 +771,6 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis) } if (colors == null) colors = DefaultFaceColors; - var target = new Mesh(); - if (largeMeshFormat) - { - target.indexFormat = IndexFormat.UInt32; - } var meshTriangles = new List(); var meshVertices = new List(); @@ -791,7 +786,7 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis) List uniqueTags = null; var submeshTriangles = new List>(); - + // Strip down to Face-Vertex structure var points = ListVerticesByPoints(); var faceIndices = ListFacesByVertexIndices(); @@ -896,19 +891,19 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis) faceTris.Add(index++); edgeUVs.Add(new Vector2(0, 0)); barycentricUVs.Add(new Vector3(0, 0, 1)); - + meshVertices.Add(points[faceIndex[edgeIndex]]); meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis)); faceTris.Add(index++); edgeUVs.Add(new Vector2(1, 1)); barycentricUVs.Add(new Vector3(0, 1, 0)); - + meshVertices.Add(points[faceIndex[(edgeIndex + 1) % face.Sides]]); meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis)); faceTris.Add(index++); edgeUVs.Add(new Vector2(1, 1)); barycentricUVs.Add(new Vector3(1, 0, 0)); - + meshNormals.AddRange(Enumerable.Repeat(faceNormal, 3)); meshColors.AddRange(Enumerable.Repeat(color, 3)); miscUVs1.AddRange(Enumerable.Repeat(miscUV1, 3)); @@ -930,19 +925,19 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis) faceTris.Add(index++); edgeUVs.Add(new Vector2(0, 0)); barycentricUVs.Add(new Vector3(0, 0, 1)); - + meshVertices.Add(newTris[t].v2.Position); meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis)); faceTris.Add(index++); edgeUVs.Add(new Vector2(1, 1)); barycentricUVs.Add(new Vector3(0, 1, 0)); - + meshVertices.Add(newTris[t].v3.Position); meshUVs.Add(calcUV(meshVertices[index], xAxis, yAxis)); faceTris.Add(index++); edgeUVs.Add(new Vector2(1, 1)); barycentricUVs.Add(new Vector3(1, 0, 0)); - + meshNormals.AddRange(Enumerable.Repeat(faceNormal, 3)); meshColors.AddRange(Enumerable.Repeat(color, 3)); miscUVs1.AddRange(Enumerable.Repeat(miscUV1, 3)); @@ -950,7 +945,7 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis) } faceTris.Reverse(); - } + } } else { @@ -1010,33 +1005,76 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis) meshTriangles.AddRange(faceTris); } } - + + var meshData = new MeshData + { + meshVertices = meshVertices, + meshNormals = meshNormals, + submeshTriangles = submeshTriangles, + meshTriangles = meshTriangles, + meshColors = meshColors, + generateSubmeshes = generateSubmeshes, + largeMeshFormat = largeMeshFormat, + meshUVs = meshUVs, + edgeUVs = edgeUVs, + barycentricUVs = barycentricUVs, + miscUVs1 = miscUVs1, + miscUVs2 = miscUVs2 + }; + + return meshData; + } + + public struct MeshData + { + public List meshVertices; + public List meshNormals; + public List> submeshTriangles; + public List meshTriangles; + public List meshColors; + public bool generateSubmeshes; + public bool largeMeshFormat; + public List meshUVs; + public List edgeUVs; + public List barycentricUVs; + public List miscUVs1; + public List miscUVs2; + } + + public Mesh BuildUnityMesh(MeshData meshData) + { + var target = new Mesh(); + if (meshData.largeMeshFormat) + { + target.indexFormat = IndexFormat.UInt32; + } + // TODO Do we really want to jitter verts here? // It was a quick fix for z-fighting but I haven't really tested how effective it is // or looked into alternatives - target.vertices = meshVertices.Select(x => Jitter(x)).ToArray(); + target.vertices = meshData.meshVertices.Select(x => Jitter(x)).ToArray(); - target.normals = meshNormals.ToArray(); + target.normals = meshData.meshNormals.ToArray(); - if (generateSubmeshes) + if (meshData.generateSubmeshes) { - target.subMeshCount = submeshTriangles.Count; - for (var i = 0; i < submeshTriangles.Count; i++) + target.subMeshCount = meshData.submeshTriangles.Count; + for (var i = 0; i < meshData.submeshTriangles.Count; i++) { - target.SetTriangles(submeshTriangles[i], i); + target.SetTriangles(meshData.submeshTriangles[i], i); } } else { - target.triangles = meshTriangles.ToArray(); + target.triangles = meshData.meshTriangles.ToArray(); } - target.colors32 = meshColors.ToArray(); - target.SetUVs(0, meshUVs); - target.SetUVs(1, edgeUVs); - target.SetUVs(2, barycentricUVs); - target.SetUVs(3, miscUVs1); - target.SetUVs(4, miscUVs2); + target.colors32 = meshData.meshColors.ToArray(); + target.SetUVs(0, meshData.meshUVs); + target.SetUVs(1, meshData.edgeUVs); + target.SetUVs(2, meshData.barycentricUVs); + target.SetUVs(3, meshData.miscUVs1); + target.SetUVs(4, meshData.miscUVs2); target.RecalculateTangents(); return target; diff --git a/package.json b/package.json index c71af5a..3cd6862 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.ixxy.polyhydra.core", - "version": "1.0.14", + "version": "1.0.15", "displayName": "Polyhydra Core", "description": "Core mesh library for Polyhydra. Procedural generation of geometric forms in Unity.", "unity": "2019.4",