Skip to content

Commit

Permalink
Move coordinate space conversion into the UnityGLTF layer (#105)
Browse files Browse the repository at this point in the history
* Moving coordinate system flipping into the Unity layer

* Updating path creation for StreamingAssets

* Adding documentation to GLTFUnityHelpers

* Slight reworking of the helper functions to leave the passed in arrays untouched.
  • Loading branch information
keveleigh authored and Blake Gross committed Jan 27, 2018
1 parent 36cd23b commit 0682767
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 91 deletions.
52 changes: 10 additions & 42 deletions GLTFSerialization/GLTFSerialization/Schema/Accessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,77 +501,45 @@ public Vector2[] AsTexcoordArray(ref NumericArray contents, byte[] bufferViewDat
{
if (contents.AsTexcoords != null) return contents.AsTexcoords;

var arr = AsVector2Array(ref contents, bufferViewData, offset);
for (var i = 0; i < arr.Length; i++)
{
arr[i].Y = 1 - arr[i].Y;
}

contents.AsTexcoords = arr;
contents.AsTexcoords = AsVector2Array(ref contents, bufferViewData, offset);

return arr;
return contents.AsTexcoords;
}

public Vector3[] AsVertexArray(ref NumericArray contents, byte[] bufferViewData, int offset)
{
if (contents.AsVertices != null) return contents.AsVertices;

var arr = AsVector3Array(ref contents, bufferViewData, offset);
for (var i = 0; i < arr.Length; i++)
{
arr[i].Z *= -1;
}

contents.AsVertices = arr;
contents.AsVertices = AsVector3Array(ref contents, bufferViewData, offset);

return arr;
return contents.AsVertices;
}

public Vector3[] AsNormalArray(ref NumericArray contents, byte[] bufferViewData, int offset)
{
if (contents.AsNormals != null) return contents.AsNormals;

var arr = AsVector3Array(ref contents, bufferViewData, offset);
for (var i = 0; i < arr.Length; i++)
{
arr[i].Z *= -1;
}

contents.AsNormals = arr;
contents.AsNormals = AsVector3Array(ref contents, bufferViewData, offset);

return arr;
return contents.AsNormals;
}

public Vector4[] AsTangentArray(ref NumericArray contents, byte[] bufferViewData, int offset)
{
if (contents.AsTangents != null) return contents.AsTangents;

var arr = AsVector4Array(ref contents, bufferViewData, offset);
for (var i = 0; i < arr.Length; i++)
{
arr[i].W *= -1;
}

contents.AsTangents = arr;
contents.AsTangents = AsVector4Array(ref contents, bufferViewData, offset);

return arr;
return contents.AsTangents;
}

public uint[] AsTriangles(ref NumericArray contents, byte[] bufferViewData, int offset)
{
if (contents.AsTriangles != null) return contents.AsTriangles;

var arr = AsUIntArray(ref contents, bufferViewData, offset);
for (var i = 0; i < arr.Length; i += 3)
{
var temp = arr[i];
arr[i] = arr[i + 2];
arr[i + 2] = temp;
}
contents.AsTriangles = AsUIntArray(ref contents, bufferViewData, offset);

contents.AsTriangles = arr;

return arr;
return contents.AsTriangles;
}

private static int GetDiscreteElement(byte[] bufferViewData, int offset, GLTFComponentType type)
Expand Down
2 changes: 1 addition & 1 deletion UnityGLTF/Assets/UnityGLTF/Scripts/GLTFComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ IEnumerator Start()
FileStream gltfStream = null;
if (UseStream)
{
var fullPath = Application.streamingAssetsPath + Url;
var fullPath = Path.Combine(Application.streamingAssetsPath, Url);
gltfStream = File.OpenRead(fullPath);
loader = new GLTFSceneImporter(
fullPath,
Expand Down
54 changes: 6 additions & 48 deletions UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,19 @@ private MeshPrimitive[] ExportPrimitive(GameObject gameObject)
AccessorId aPosition = null, aNormal = null, aTangent = null,
aTexcoord0 = null, aTexcoord1 = null, aColor0 = null;

aPosition = ExportAccessor(InvertZ(meshObj.vertices));
aPosition = ExportAccessor(GLTFUnityHelpers.FlipVectorArrayHandedness(meshObj.vertices));

if (meshObj.normals.Length != 0)
aNormal = ExportAccessor(InvertZ(meshObj.normals));
aNormal = ExportAccessor(GLTFUnityHelpers.FlipVectorArrayHandedness(meshObj.normals));

if (meshObj.tangents.Length != 0)
aTangent = ExportAccessor(InvertW(meshObj.tangents));
aTangent = ExportAccessor(GLTFUnityHelpers.FlipVectorArrayHandedness(meshObj.tangents));

if (meshObj.uv.Length != 0)
aTexcoord0 = ExportAccessor(FlipY(meshObj.uv));
aTexcoord0 = ExportAccessor(GLTFUnityHelpers.FlipTexCoordArrayV(meshObj.uv));

if (meshObj.uv2.Length != 0)
aTexcoord1 = ExportAccessor(FlipY(meshObj.uv2));
aTexcoord1 = ExportAccessor(GLTFUnityHelpers.FlipTexCoordArrayV(meshObj.uv2));

if (meshObj.colors.Length != 0)
aColor0 = ExportAccessor(meshObj.colors);
Expand All @@ -327,7 +327,7 @@ private MeshPrimitive[] ExportPrimitive(GameObject gameObject)
var primitive = new MeshPrimitive();

var triangles = meshObj.GetTriangles(submesh);
primitive.Indices = ExportAccessor(FlipFaces(triangles), true);
primitive.Indices = ExportAccessor(GLTFUnityHelpers.FlipFaces(triangles), true);

primitive.Attributes = new Dictionary<string, AccessorId>();
primitive.Attributes.Add(SemanticProperties.POSITION, aPosition);
Expand Down Expand Up @@ -728,48 +728,6 @@ private SamplerId ExportSampler(UnityEngine.Texture texture)
return samplerId;
}

private Vector2[] FlipY(Vector2[] arr)
{
var len = arr.Length;
for(var i = 0; i < len; i++)
{
arr[i].y = 1 - arr[i].y;
}
return arr;
}

private Vector3[] InvertZ(Vector3[] arr)
{
var len = arr.Length;
for(var i = 0; i < len; i++)
{
arr[i].z = -arr[i].z;
}
return arr;
}

private Vector4[] InvertW(Vector4[] arr)
{
var len = arr.Length;
for(var i = 0; i < len; i++)
{
arr[i].w = -arr[i].w;
}
return arr;
}

private int[] FlipFaces(int[] arr)
{
var triangles = new int[arr.Length];
for (int i = 0; i < arr.Length; i += 3)
{
triangles[i + 2] = arr[i];
triangles[i + 1] = arr[i + 1];
triangles[i] = arr[i + 2];
}
return triangles;
}

private AccessorId ExportAccessor(int[] arr, bool isIndices = false)
{
var count = arr.Length;
Expand Down
37 changes: 37 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,43 @@ protected virtual void BuildMeshAttributes(MeshPrimitive primitive, int meshID,
}

GLTFHelpers.BuildMeshAttributes(ref attributeAccessors);

// Flip vectors and triangles to the Unity coordinate system.
if (attributeAccessors.ContainsKey(SemanticProperties.POSITION))
{
NumericArray resultArray = attributeAccessors[SemanticProperties.POSITION].AccessorContent;
resultArray.AsVertices = GLTFUnityHelpers.FlipVectorArrayHandedness(resultArray.AsVertices);
attributeAccessors[SemanticProperties.POSITION].AccessorContent = resultArray;
}
if (attributeAccessors.ContainsKey(SemanticProperties.INDICES))
{
NumericArray resultArray = attributeAccessors[SemanticProperties.INDICES].AccessorContent;
resultArray.AsTriangles = GLTFUnityHelpers.FlipFaces(resultArray.AsTriangles);
attributeAccessors[SemanticProperties.INDICES].AccessorContent = resultArray;
}
if (attributeAccessors.ContainsKey(SemanticProperties.NORMAL))
{
NumericArray resultArray = attributeAccessors[SemanticProperties.NORMAL].AccessorContent;
resultArray.AsNormals = GLTFUnityHelpers.FlipVectorArrayHandedness(resultArray.AsNormals);
attributeAccessors[SemanticProperties.NORMAL].AccessorContent = resultArray;
}
// TexCoord goes from 0 to 3 to match GLTFHelpers.BuildMeshAttributes
for (int i = 0; i < 4; i++)
{
if (attributeAccessors.ContainsKey(SemanticProperties.TexCoord(i)))
{
NumericArray resultArray = attributeAccessors[SemanticProperties.TexCoord(i)].AccessorContent;
resultArray.AsTexcoords = GLTFUnityHelpers.FlipTexCoordArrayV(resultArray.AsTexcoords);
attributeAccessors[SemanticProperties.TexCoord(i)].AccessorContent = resultArray;
}
}
if (attributeAccessors.ContainsKey(SemanticProperties.TANGENT))
{
NumericArray resultArray = attributeAccessors[SemanticProperties.TANGENT].AccessorContent;
resultArray.AsTangents = GLTFUnityHelpers.FlipVectorArrayHandedness(resultArray.AsTangents);
attributeAccessors[SemanticProperties.TANGENT].AccessorContent = resultArray;
}

_assetCache.MeshCache[meshID][primitiveIndex].MeshAttributes = attributeAccessors;
}
}
Expand Down
Loading

0 comments on commit 0682767

Please sign in to comment.