Skip to content

Commit

Permalink
Calclate Tangents.
Browse files Browse the repository at this point in the history
  • Loading branch information
matsudayuma committed Jul 11, 2016
1 parent 5012ed9 commit 70a20fb
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 6 deletions.
73 changes: 73 additions & 0 deletions Assets/IcoSphereCreator/IcoSphereCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static Mesh Create(int n, float radius)
mesh.triangles = triangles;
mesh.uv = uv;
mesh.RecalculateNormals();
CreateTangents(mesh);

return mesh;
}
Expand Down Expand Up @@ -157,4 +158,76 @@ static void CreateUV(int n, Vector3[] vertices, Vector2[] uv)
uv[7 * tt + 0].x = 0.625f;

}

static void CreateTangents(Mesh mesh)
{
int[] triangles = mesh.triangles;
Vector3[] vertices = mesh.vertices;
Vector2[] uv = mesh.uv;
Vector3[] normals = mesh.normals;

int triangleCount = triangles.Length;
int vertexCount = vertices.Length;

Vector3[] tan1 = new Vector3[vertexCount];
Vector3[] tan2 = new Vector3[vertexCount];

Vector4[] tangents = new Vector4[vertexCount];

for (int i = 0; i < triangleCount; i += 3)
{
int i1 = triangles[i + 0];
int i2 = triangles[i + 1];
int i3 = triangles[i + 2];

Vector3 v1 = vertices[i1];
Vector3 v2 = vertices[i2];
Vector3 v3 = vertices[i3];

Vector2 w1 = uv[i1];
Vector2 w2 = uv[i2];
Vector2 w3 = uv[i3];

float x1 = v2.x - v1.x;
float x2 = v3.x - v1.x;
float y1 = v2.y - v1.y;
float y2 = v3.y - v1.y;
float z1 = v2.z - v1.z;
float z2 = v3.z - v1.z;

float s1 = w2.x - w1.x;
float s2 = w3.x - w1.x;
float t1 = w2.y - w1.y;
float t2 = w3.y - w1.y;

float r = 1.0f / (s1 * t2 - s2 * t1);

Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);

tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;

tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
}


for (int i = 0; i < vertexCount; ++i)
{
Vector3 n = normals[i];
Vector3 t = tan1[i];

Vector3.OrthoNormalize(ref n, ref t);
tangents[i].x = t.x;
tangents[i].y = t.y;
tangents[i].z = t.z;

tangents[i].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0f) ? -1.0f : 1.0f;
}

mesh.tangents = tangents;
}
}
Binary file modified Assets/IcoSphereCreator/Samples/Mesh/IcoSphere1.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/IcoSphereCreator/Samples/Mesh/IcoSphere1.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/IcoSphereCreator/Samples/Mesh/IcoSphere2.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/IcoSphereCreator/Samples/Mesh/IcoSphere2.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/IcoSphereCreator/Samples/Mesh/IcoSphere3.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/IcoSphereCreator/Samples/Mesh/IcoSphere3.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/IcoSphereCreator/Samples/Mesh/IcoSphere4.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/IcoSphereCreator/Samples/Mesh/IcoSphere4.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/IcoSphereCreator/Samples/Mesh/IcoSphere5.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/IcoSphereCreator/Samples/Mesh/IcoSphere5.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/IcoSphereCreator/Samples/Mesh/IcoSphere6.asset
Binary file not shown.
2 changes: 1 addition & 1 deletion Assets/IcoSphereCreator/Samples/Mesh/IcoSphere6.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified IcoSphereCreator.unitypackage
Binary file not shown.

0 comments on commit 70a20fb

Please sign in to comment.