diff --git a/.gitignore b/.gitignore index 8af868e..789f6cb 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,7 @@ ExportedObj/ *.pidb.meta *.pdb.meta *.mdb.meta +*.meta # Unity3D generated file on crash reports sysinfo.txt diff --git a/Runtime/Scripts/Fragment/FragmentData.cs b/Runtime/Scripts/Fragment/FragmentData.cs index fccd201..d9b786c 100644 --- a/Runtime/Scripts/Fragment/FragmentData.cs +++ b/Runtime/Scripts/Fragment/FragmentData.cs @@ -101,6 +101,7 @@ public FragmentData(Mesh mesh) { var positions = mesh.vertices; var normals = mesh.normals; + var tangents = mesh.tangents; var uv = mesh.uv; this.Vertices = new List(mesh.vertexCount); @@ -111,7 +112,7 @@ public FragmentData(Mesh mesh) // Add mesh vertices for (int i = 0; i < positions.Length; i++) { - this.Vertices.Add(new MeshVertex(positions[i], normals[i], uv[i])); + this.Vertices.Add(new MeshVertex(positions[i], normals[i], tangents[i], uv[i])); } // Only meshes with one submesh are currently supported @@ -137,9 +138,9 @@ public FragmentData(Mesh mesh) /// The vertex normal /// The vertex UV coordinates /// Returns the index of the vertex in the cutVertices array - public void AddCutFaceVertex(Vector3 position, Vector3 normal, Vector2 uv) + public void AddCutFaceVertex(Vector3 position, Vector3 normal, Vector4 tangent, Vector2 uv) { - var vertex = new MeshVertex(position, normal, uv); + var vertex = new MeshVertex(position, normal, tangent, uv); // Add the vertex to both the normal mesh vertex data and the cut face vertex data // The vertex on the cut face will have different normal/uv coordinates which are @@ -290,6 +291,7 @@ public Mesh ToMesh() { new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3), new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3), + new VertexAttributeDescriptor(VertexAttribute.Tangent, VertexAttributeFormat.Float32, 4), new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2), }; diff --git a/Runtime/Scripts/Fragment/MeshSlicer.cs b/Runtime/Scripts/Fragment/MeshSlicer.cs index e728427..6637e95 100644 --- a/Runtime/Scripts/Fragment/MeshSlicer.cs +++ b/Runtime/Scripts/Fragment/MeshSlicer.cs @@ -275,11 +275,16 @@ private static void SplitTriangle(int v1_idx, var uv13 = v1.uv + s13 * (v3.uv - v1.uv); var uv23 = v2.uv + s23 * (v3.uv - v2.uv); + var tang13 = Vector4.Lerp(v2.tangent, v3.tangent, s13).normalized; + tang13.w = Mathf.Lerp(v2.tangent.w, v3.tangent.w, s13); + var tang23 = Vector4.Lerp(v2.tangent, v3.tangent, s23).normalized; + tang23.w = Mathf.Lerp(v2.tangent.w, v3.tangent.w, s23); + // Add vertices/normals/uv for the intersection points to each mesh - topSlice.AddCutFaceVertex(v13, norm13, uv13); - topSlice.AddCutFaceVertex(v23, norm23, uv23); - bottomSlice.AddCutFaceVertex(v13, norm13, uv13); - bottomSlice.AddCutFaceVertex(v23, norm23, uv23); + topSlice.AddCutFaceVertex(v13, norm13, tang13, uv13); + topSlice.AddCutFaceVertex(v23, norm23, tang13, uv23); + bottomSlice.AddCutFaceVertex(v13, norm13, tang23, uv13); + bottomSlice.AddCutFaceVertex(v23, norm23, tang23, uv23); // Indices for the intersection vertices (for the original mesh data) int index13_A = topSlice.Vertices.Count - 2; diff --git a/Runtime/Scripts/Fragment/MeshVertex.cs b/Runtime/Scripts/Fragment/MeshVertex.cs index 4264ea1..f4edda0 100644 --- a/Runtime/Scripts/Fragment/MeshVertex.cs +++ b/Runtime/Scripts/Fragment/MeshVertex.cs @@ -8,12 +8,14 @@ public struct MeshVertex { public Vector3 position; public Vector3 normal; + public Vector4 tangent; public Vector2 uv; public MeshVertex(Vector3 position) { this.position = position; this.normal = Vector3.zero; + this.tangent = Vector4.zero; this.uv = Vector2.zero; } @@ -21,6 +23,15 @@ public MeshVertex(Vector3 position, Vector3 normal, Vector2 uv) { this.position = position; this.normal = normal; + this.tangent = Vector4.zero; + this.uv = uv; + } + + public MeshVertex(Vector3 position, Vector3 normal, Vector4 tangents, Vector2 uv) + { + this.position = position; + this.normal = normal; + this.tangent = tangents; this.uv = uv; }