Skip to content

Calculate tangents. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ExportedObj/
*.pidb.meta
*.pdb.meta
*.mdb.meta
*.meta

# Unity3D generated file on crash reports
sysinfo.txt
Expand Down
8 changes: 5 additions & 3 deletions Runtime/Scripts/Fragment/FragmentData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshVertex>(mesh.vertexCount);
Expand All @@ -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
Expand All @@ -137,9 +138,9 @@ public FragmentData(Mesh mesh)
/// <param name="normal">The vertex normal</param>
/// <param name="uv">The vertex UV coordinates</param>
/// <returns>Returns the index of the vertex in the cutVertices array</returns>
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
Expand Down Expand Up @@ -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),
};

Expand Down
13 changes: 9 additions & 4 deletions Runtime/Scripts/Fragment/MeshSlicer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this is correct, I was inspired by http://answers.unity.com/comments/485110/view.html


// 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;
Expand Down
11 changes: 11 additions & 0 deletions Runtime/Scripts/Fragment/MeshVertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,30 @@ 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;
}

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;
}

Expand Down