void SetTBN(int vertex1, int vertex2, int vertex3) { Mesh.ObjVertex v1 = objVertices[vertex1]; Mesh.ObjVertex v2 = objVertices[vertex2]; Mesh.ObjVertex v3 = objVertices[vertex3]; Vector2 dUV1 = v2.TexCoord - v1.TexCoord; Vector2 dUV2 = v3.TexCoord - v1.TexCoord; Vector3 dPos1 = v2.Vertex - v1.Vertex; Vector3 dPos2 = v3.Vertex - v1.Vertex; float r = 1 / (dUV1.X * dUV2.Y - dUV1.Y * dUV2.X); Vector3 tangent = (dPos1 * dUV2.Y - dPos2 * dUV1.Y) * r; Vector3 bitangent = (dPos2 * dUV1.X - dPos1 * dUV2.X) * r; v1.Tangent = tangent; v1.Bitangent = bitangent; v2.Tangent = tangent; v2.Bitangent = bitangent; v3.Tangent = tangent; v3.Bitangent = bitangent; objVertices[vertex1] = v1; objVertices[vertex2] = v2; objVertices[vertex3] = v3; }
int AddObjVertex(ref Vector3 vertex, ref Vector2 texCoord, ref Vector3 normal) { Mesh.ObjVertex newObjVertex = new Mesh.ObjVertex(); newObjVertex.Vertex = vertex; newObjVertex.TexCoord = texCoord; newObjVertex.Normal = normal; objVertices.Add(newObjVertex); return(objVertices.Count - 1); }