Skip to content

Commit

Permalink
Split mesh data generation from Unity mesh stuff so that it's possibl…
Browse files Browse the repository at this point in the history
…e to do that part in a background thread
  • Loading branch information
andybak committed May 15, 2022
1 parent 87b02ba commit f4bb8da
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
1 change: 0 additions & 1 deletion com.ixxy.polyhydra/Core/Runtime/OpParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

Expand Down
94 changes: 66 additions & 28 deletions com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<int>();
var meshVertices = new List<Vector3>();
Expand All @@ -791,7 +786,7 @@ Vector2 calcUV(Vector3 point, Vector3 xAxis, Vector3 yAxis)
List<string> uniqueTags = null;

var submeshTriangles = new List<List<int>>();

// Strip down to Face-Vertex structure
var points = ListVerticesByPoints();
var faceIndices = ListFacesByVertexIndices();
Expand Down Expand Up @@ -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));
Expand All @@ -930,27 +925,27 @@ 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));
miscUVs2.AddRange(Enumerable.Repeat(miscUV2, 3));
}

faceTris.Reverse();
}
}
}
else
{
Expand Down Expand Up @@ -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<Vector3> meshVertices;
public List<Vector3> meshNormals;
public List<List<int>> submeshTriangles;
public List<int> meshTriangles;
public List<Color32> meshColors;
public bool generateSubmeshes;
public bool largeMeshFormat;
public List<Vector2> meshUVs;
public List<Vector2> edgeUVs;
public List<Vector3> barycentricUVs;
public List<Vector4> miscUVs1;
public List<Vector4> 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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit f4bb8da

Please sign in to comment.