Skip to content

Commit

Permalink
cleanup: allow turning optimized binary writer on/off via flag; upper…
Browse files Browse the repository at this point in the history
…case SwitchHandedness
  • Loading branch information
hybridherbst committed May 9, 2022
1 parent 154bc83 commit c8349b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -561,21 +561,21 @@ public static Matrix4x4[] ToUnityMatrix4x4(this GLTF.Math.Matrix4x4[] inMatrixAr
return outMatrixArr;
}

public static Vector4 switchHandedness(this Vector4 input)
public static Vector4 SwitchHandedness(this Vector4 input)
{
return new Vector4(-input.x, input.y, input.z, -input.w);
}


public static Quaternion switchHandedness(this Quaternion input)
public static Quaternion SwitchHandedness(this Quaternion input)
{
return new Quaternion(input.x, input.y, -input.z, -input.w);
}

public static Matrix4x4 switchHandedness(this Matrix4x4 matrix)
public static Matrix4x4 SwitchHandedness(this Matrix4x4 matrix)
{
Vector3 position = matrix.GetColumn(3).switchHandedness();
Quaternion rotation = Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)).switchHandedness();
Vector3 position = matrix.GetColumn(3).SwitchHandedness();
Quaternion rotation = Quaternion.LookRotation(matrix.GetColumn(2), matrix.GetColumn(1)).SwitchHandedness();
Vector3 scale = new Vector3(matrix.GetColumn(0).magnitude, matrix.GetColumn(1).magnitude, matrix.GetColumn(2).magnitude);

float epsilon = 0.00001f;
Expand Down
79 changes: 50 additions & 29 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define ANIMATION_SUPPORTED
#endif

#define USE_FAST_BINARY_WRITER

using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -124,7 +126,11 @@ private struct ImageInfo
private GLTFRoot _root;
private BufferId _bufferId;
private GLTFBuffer _buffer;
#if USE_FAST_BINARY_WRITER
private FastBinaryWriter _bufferWriter;
#else
private BinaryWriter _bufferWriter;
#endif
private List<ImageInfo> _imageInfos;
private List<Texture> _textures;
private Dictionary<Material, int> _materials;
Expand Down Expand Up @@ -2896,7 +2902,14 @@ private AccessorId ExportAccessor(float[] arr)
exportAccessorBufferWriteMarker.Begin();
accessor.ComponentType = GLTFComponentType.Float;

#if USE_FAST_BINARY_WRITER
_bufferWriter.Write(arr);
#else
foreach (var v in arr)
{
_bufferWriter.Write((float)v);
}
#endif
exportAccessorBufferWriteMarker.End();

accessor.Min = new List<double> { min };
Expand Down Expand Up @@ -3170,13 +3183,16 @@ private AccessorId ExportAccessor(Vector3[] arr)
uint byteOffset = CalculateAlignment((uint)_bufferWriter.BaseStream.Position, 4);

exportAccessorBufferWriteMarker.Begin();
#if USE_FAST_BINARY_WRITER
_bufferWriter.Write(arr);
// foreach (var vec in arr)
// {
// _bufferWriter.Write(vec.x);
// _bufferWriter.Write(vec.y);
// _bufferWriter.Write(vec.z);
// }
#else
foreach (var vec in arr)
{
_bufferWriter.Write(vec.x);
_bufferWriter.Write(vec.y);
_bufferWriter.Write(vec.z);
}
#endif
exportAccessorBufferWriteMarker.End();

uint byteLength = CalculateAlignment((uint)_bufferWriter.BaseStream.Position - byteOffset, 4);
Expand Down Expand Up @@ -3423,14 +3439,17 @@ private AccessorId ExportAccessor(Vector4[] arr)
uint byteOffset = CalculateAlignment((uint)_bufferWriter.BaseStream.Position, 4);

exportAccessorBufferWriteMarker.Begin();
#if USE_FAST_BINARY_WRITER
_bufferWriter.Write(arr);
// foreach (var vec in arr)
// {
// _bufferWriter.Write(vec.x);
// _bufferWriter.Write(vec.y);
// _bufferWriter.Write(vec.z);
// _bufferWriter.Write(vec.w);
// }
#else
foreach (var vec in arr)
{
_bufferWriter.Write(vec.x);
_bufferWriter.Write(vec.y);
_bufferWriter.Write(vec.z);
_bufferWriter.Write(vec.w);
}
#endif
exportAccessorBufferWriteMarker.End();

uint byteLength = CalculateAlignment((uint)_bufferWriter.BaseStream.Position - byteOffset, 4);
Expand Down Expand Up @@ -4650,7 +4669,7 @@ void ConvertToAnimationPointer(object animatedObject, string propertyName, Anima

AnimationSampler Rsampler = new AnimationSampler();
Rsampler.Input = timeAccessor; // Float, for time
Rsampler.Output = ExportAccessor(rotations, true); // Vec4 for
Rsampler.Output = ExportAccessorSwitchHandedness(rotations); // Vec4 for rotations
Rsampler.Output.Value.BufferView.Value.ByteStride = 0;
Rchannel.Sampler = new AnimationSamplerId
{
Expand Down Expand Up @@ -5136,7 +5155,7 @@ private AccessorId ExportAccessorUint(Vector4[] arr)
}

// This is used for Quaternions / Rotations
private AccessorId ExportAccessor(Vector4[] arr, bool switchHandedness = false)
private AccessorId ExportAccessorSwitchHandedness(Vector4[] arr)
{
exportAccessorMarker.Begin();
exportAccessorVector4ArrayMarker.Begin();
Expand All @@ -5154,8 +5173,8 @@ private AccessorId ExportAccessor(Vector4[] arr, bool switchHandedness = false)
accessor.Type = GLTFAccessorAttributeType.VEC4;

var a0 = arr[0];
a0 = switchHandedness ? a0.switchHandedness() : a0;
a0 = a0.normalized;
a0 = a0.SwitchHandedness();
a0.Normalize();
float minX = a0.x;
float minY = a0.y;
float minZ = a0.z;
Expand All @@ -5168,8 +5187,8 @@ private AccessorId ExportAccessor(Vector4[] arr, bool switchHandedness = false)
for (var i = 1; i < count; i++)
{
var cur = arr[i];
cur = switchHandedness ? cur.switchHandedness() : cur;
cur = cur.normalized;
cur = cur.SwitchHandedness();
cur.Normalize();

if (cur.x < minX)
{
Expand Down Expand Up @@ -5212,17 +5231,19 @@ private AccessorId ExportAccessor(Vector4[] arr, bool switchHandedness = false)
uint byteOffset = CalculateAlignment((uint)_bufferWriter.BaseStream.Position, 4);

exportAccessorBufferWriteMarker.Begin();

#if USE_FAST_BINARY_WRITER
_bufferWriter.WriteNormalizedSwitchHandedness(arr);
// foreach (var vec in arr)
// {
// Vector4 vect = switchHandedness ? vec.switchHandedness() : vec;
// vect = vect.normalized;
// _bufferWriter.Write(vect.x);
// _bufferWriter.Write(vect.y);
// _bufferWriter.Write(vect.z);
// _bufferWriter.Write(vect.w);
// }
#else
foreach (var vec in arr)
{
Vector4 vect = vec.SwitchHandedness();
vect.Normalize();
_bufferWriter.Write(vect.x);
_bufferWriter.Write(vect.y);
_bufferWriter.Write(vect.z);
_bufferWriter.Write(vect.w);
}
#endif
exportAccessorBufferWriteMarker.End();

uint byteLength = CalculateAlignment((uint)_bufferWriter.BaseStream.Position - byteOffset, 4);
Expand Down

0 comments on commit c8349b7

Please sign in to comment.