void PackLightmapUVs(Mesh m, int packID, BakeAttribute bakeAttribute) { List <Vector3> normals = new List <Vector3>(); m.GetNormals(normals); List <Vector4> tangents = new List <Vector4>(); m.GetTangents(tangents); Vector3[] pdeltaPositions = new Vector3[m.vertexCount]; Vector3[] pdeltaNormals = new Vector3[m.vertexCount]; Vector3[] pdeltaTangents = new Vector3[m.vertexCount]; m.GetBlendShapeFrameVertices(packID, 0, pdeltaPositions, pdeltaNormals, pdeltaTangents); List <Vector4> uv = new List <Vector4>(); m.GetUVs(1, uv); for (int i = 0; i < uv.Count; i++) // We bake positions using the normals and tangents as an orthogonal basis. { Vector3 packrat = ToTangentSpace(pdeltaPositions[i], normals[i], tangents[i]); switch (bakeAttribute) { case BakeAttribute.X: uv[i] = new Vector4(uv[i].x, uv[i].y, uv[i].z, packrat.x); break; case BakeAttribute.Y: uv[i] = new Vector4(uv[i].x, uv[i].y, uv[i].z, packrat.y); break; case BakeAttribute.Z: uv[i] = new Vector4(uv[i].x, uv[i].y, uv[i].z, packrat.z); break; } } m.SetUVs(1, uv); }
void Bake(Mesh m, int blendShapeID, BakeType type, int dest, int packID, BakeAttribute bakeAttribute) { List <Vector3> normals = new List <Vector3>(); m.GetNormals(normals); List <Vector4> tangents = new List <Vector4>(); m.GetTangents(tangents); Vector3[] deltaPositions = new Vector3[m.vertexCount]; Vector3[] deltaNormals = new Vector3[m.vertexCount]; Vector3[] deltaTangents = new Vector3[m.vertexCount]; m.GetBlendShapeFrameVertices(blendShapeID, 0, deltaPositions, deltaNormals, deltaTangents); Vector3[] pdeltaPositions = new Vector3[m.vertexCount]; Vector3[] pdeltaNormals = new Vector3[m.vertexCount]; Vector3[] pdeltaTangents = new Vector3[m.vertexCount]; m.GetBlendShapeFrameVertices(packID, 0, pdeltaPositions, pdeltaNormals, pdeltaTangents); List <Vector4> uv = new List <Vector4>(); switch (type) { case BakeType.DeltaPosition: for (int i = 0; i < m.vertexCount; i++) // We bake positions using the normals and tangents as an orthogonal basis. { Vector3 XYZ = ToTangentSpace(deltaPositions[i], normals[i], tangents[i]); Vector3 packrat = ToTangentSpace(pdeltaPositions[i], normals[i], tangents[i]); switch (bakeAttribute) { case BakeAttribute.X: uv.Add(new Vector4(XYZ.x, XYZ.y, XYZ.z, packrat.x)); break; case BakeAttribute.Y: uv.Add(new Vector4(XYZ.x, XYZ.y, XYZ.z, packrat.y)); break; case BakeAttribute.Z: uv.Add(new Vector4(XYZ.x, XYZ.y, XYZ.z, packrat.z)); break; } } break; } m.SetUVs(dest, uv); }