Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Unit Scale" option for import settings #251

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Scripts/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ private static GameObject LoadInternal(this GLTFObject gltfObject, string filepa
materialTask.RunSynchronously();
GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, bufferViewTask, materialTask, importSettings);
meshTask.RunSynchronously();
GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask);
GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask, importSettings.unitScale);
skinTask.RunSynchronously();
GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras);
GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras, importSettings.unitScale);
nodeTask.RunSynchronously();
GLTFAnimation.ImportResult[] animationResult = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result, importSettings);
if (animationResult != null) animations = animationResult.Select(x => x.clip).ToArray();
Expand Down Expand Up @@ -311,9 +311,9 @@ private static IEnumerator LoadAsync(string json, string filepath, byte[] bytefi
importTasks.Add(materialTask);
GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, bufferViewTask, materialTask, importSettings);
importTasks.Add(meshTask);
GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask);
GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask, importSettings.unitScale);
importTasks.Add(skinTask);
GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras);
GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras, importSettings.unitScale);
importTasks.Add(nodeTask);

// Ignite
Expand Down
2 changes: 2 additions & 0 deletions Scripts/Settings/ImportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class ImportSettings {
[Range(1, 64)]
public float packMargin = 4;

public float unitScale = 1.0f;

[Tooltip("Script used to process extra data.")]
public GLTFExtrasProcessor extrasProcessor;
}
Expand Down
6 changes: 3 additions & 3 deletions Scripts/Spec/GLTFAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public ImportResult Import(GLTFAccessor.ImportResult[] accessors, GLTFNode.Impor
AnimationCurve posY = new AnimationCurve();
AnimationCurve posZ = new AnimationCurve();
for (int k = 0; k < keyframeInput.Length; k++) {
posX.AddKey(CreateKeyframe(k, keyframeInput, pos, x => -x.x, interpolationMode));
posY.AddKey(CreateKeyframe(k, keyframeInput, pos, x => x.y, interpolationMode));
posZ.AddKey(CreateKeyframe(k, keyframeInput, pos, x => x.z, interpolationMode));
posX.AddKey(CreateKeyframe(k, keyframeInput, pos, x => -x.x*importSettings.unitScale, interpolationMode));
posY.AddKey(CreateKeyframe(k, keyframeInput, pos, x => x.y*importSettings.unitScale, interpolationMode));
posZ.AddKey(CreateKeyframe(k, keyframeInput, pos, x => x.z*importSettings.unitScale, interpolationMode));
}
result.clip.SetCurve(relativePath, typeof(Transform), "localPosition.x", posX);
result.clip.SetCurve(relativePath, typeof(Transform), "localPosition.y", posY);
Expand Down
22 changes: 11 additions & 11 deletions Scripts/Spec/GLTFMesh.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -63,7 +63,7 @@ private class BlendShape {
public int prim;
}

public MeshData(GLTFMesh gltfMesh, GLTFAccessor.ImportResult[] accessors, GLTFBufferView.ImportResult[] bufferViews) {
public MeshData(GLTFMesh gltfMesh, GLTFAccessor.ImportResult[] accessors, GLTFBufferView.ImportResult[] bufferViews, float importScale = 1.0f) {
name = gltfMesh.name;
if (gltfMesh.primitives.Count == 0) {
Debug.LogWarning("0 primitives in mesh");
Expand Down Expand Up @@ -100,7 +100,7 @@ public MeshData(GLTFMesh gltfMesh, GLTFAccessor.ImportResult[] accessors, GLTFBu
int vertCount = verts.Count();
submeshTris.Add(asyncMesh.tris.Reverse().Select(x => x + vertCount).ToList());

verts.AddRange(asyncMesh.verts.Select(x => new Vector3(-x.x, x.y, x.z)));
verts.AddRange(asyncMesh.verts.Select(x => new Vector3(-x.x*importScale, x.y*importScale, x.z*importScale)));

if (asyncMesh.norms != null) {
normals.AddRange(asyncMesh.norms.Select(v => { v.x = -v.x; return v; }));
Expand Down Expand Up @@ -136,13 +136,13 @@ public MeshData(GLTFMesh gltfMesh, GLTFAccessor.ImportResult[] accessors, GLTFBu
else {
int vertStartIndex = verts.Count;
submeshVertexStart.Add(vertStartIndex);

// Verts - (X points left in GLTF)
if (primitive.attributes.POSITION.HasValue) {
IEnumerable<Vector3> newVerts = accessors[primitive.attributes.POSITION.Value].ReadVec3(true).Select(v => { v.x = -v.x; return v; });
IEnumerable<Vector3> newVerts = accessors[primitive.attributes.POSITION.Value].ReadVec3(true).Select(v => { v.x = -v.x; return v*importScale; });
verts.AddRange(newVerts);
}

submeshVertexCount.Add(verts.Count - vertStartIndex);

int vertCount = verts.Count;
Expand Down Expand Up @@ -222,7 +222,7 @@ public MeshData(GLTFMesh gltfMesh, GLTFAccessor.ImportResult[] accessors, GLTFBu
if (primitive.targets != null) {
for (int k = 0; k < primitive.targets.Count; k++) {
BlendShape blendShape = new BlendShape();
blendShape.pos = GetMorphWeights(primitive.targets[k].POSITION, submeshVertexStart[i], finalVertCount, accessors);
blendShape.pos = GetMorphWeights(primitive.targets[k].POSITION, submeshVertexStart[i], finalVertCount, accessors, importScale);
blendShape.norm = GetMorphWeights(primitive.targets[k].NORMAL, submeshVertexStart[i], finalVertCount, accessors);
blendShape.tan = GetMorphWeights(primitive.targets[k].TANGENT, submeshVertexStart[i], finalVertCount, accessors);
blendShape.startvert = submeshVertexStart[i];
Expand All @@ -237,13 +237,13 @@ public MeshData(GLTFMesh gltfMesh, GLTFAccessor.ImportResult[] accessors, GLTFBu
}
}

private Vector3[] GetMorphWeights(int? accessor, int vertStartIndex, int vertCount, GLTFAccessor.ImportResult[] accessors) {
private Vector3[] GetMorphWeights(int? accessor, int vertStartIndex, int vertCount, GLTFAccessor.ImportResult[] accessors, float importScale = 1.0f) {
if (accessor.HasValue) {
if (accessors[accessor.Value] == null) {
Debug.LogWarning("Accessor is null");
return new Vector3[vertCount];
}
Vector3[] accessorData = accessors[accessor.Value].ReadVec3(true).Select(v => { v.x = -v.x; return v; }).ToArray();
Vector3[] accessorData = accessors[accessor.Value].ReadVec3(true).Select(v => { v.x = -v.x; v.x*=importScale; v.y*=importScale; v.z*=importScale; return v; }).ToArray();
if (accessorData.Length != vertCount) {
Vector3[] resized = new Vector3[vertCount];
Array.Copy(accessorData, 0, resized, vertStartIndex, accessorData.Length);
Expand Down Expand Up @@ -415,7 +415,7 @@ public ImportTask(List<GLTFMesh> meshes, GLTFAccessor.ImportTask accessorTask, G

meshData = new MeshData[meshes.Count];
for (int i = 0; i < meshData.Length; i++) {
meshData[i] = new MeshData(meshes[i], accessorTask.Result, bufferViewTask.Result);
meshData[i] = new MeshData(meshes[i], accessorTask.Result, bufferViewTask.Result, importSettings.unitScale);
}
});
}
Expand Down Expand Up @@ -491,4 +491,4 @@ public static ExportResult Export(Mesh mesh) {
}
#endregion
}
}
}
11 changes: 7 additions & 4 deletions Scripts/Spec/GLTFNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public class ImportResult {
}

/// <summary> Set local position, rotation and scale </summary>
public void ApplyTRS(Transform transform) {
public void ApplyTRS(Transform transform, float importScale = 1.0f) {
if(matrix!=Matrix4x4.identity)
matrix.UnpackTRS(ref translation, ref rotation, ref scale);
transform.localPosition = translation;
transform.localPosition = translation * importScale;
transform.localRotation = rotation;
transform.localScale = scale;
}
Expand All @@ -57,11 +57,14 @@ public class ImportTask : Importer.ImportTask<ImportResult[]> {
GLTFSkin.ImportTask skinTask;
List<GLTFCamera> cameras;

public ImportTask(List<GLTFNode> nodes, GLTFMesh.ImportTask meshTask, GLTFSkin.ImportTask skinTask, List<GLTFCamera> cameras) : base(meshTask, skinTask) {
float importScale;

public ImportTask(List<GLTFNode> nodes, GLTFMesh.ImportTask meshTask, GLTFSkin.ImportTask skinTask, List<GLTFCamera> cameras, float importScale=1.0f) : base(meshTask, skinTask) {
this.nodes = nodes;
this.meshTask = meshTask;
this.skinTask = skinTask;
this.cameras = cameras;
this.importScale = importScale;
//task = new Task(() => { });
}

Expand Down Expand Up @@ -96,7 +99,7 @@ public override IEnumerator OnCoroutine(Action<float> onProgress = null) {
}
// Apply TRS
for (int i = 0; i < Result.Length; i++) {
nodes[i].ApplyTRS(Result[i].transform);
nodes[i].ApplyTRS(Result[i].transform, this.importScale);
}
// Setup components
for (int i = 0; i < Result.Length; i++) {
Expand Down
12 changes: 7 additions & 5 deletions Scripts/Spec/GLTFSkin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ [Preserve] public class GLTFSkin {
public int? skeleton;
public string name;

public float importScale;

public class ImportResult {
public Matrix4x4[] inverseBindMatrices;
public int[] joints;
Expand Down Expand Up @@ -53,10 +55,10 @@ public SkinnedMeshRenderer SetupSkinnedRenderer(GameObject go, Mesh mesh, GLTFNo
}
}

public ImportResult Import(GLTFAccessor.ImportResult[] accessors) {
public ImportResult Import(GLTFAccessor.ImportResult[] accessors, float importScale=1.0f) {
ImportResult result = new ImportResult();
result.joints = joints;

this.importScale = importScale;
// Inverse bind matrices
if (inverseBindMatrices.HasValue) {
result.inverseBindMatrices = accessors[inverseBindMatrices.Value].ReadMatrix4x4();
Expand All @@ -76,7 +78,7 @@ public ImportResult Import(GLTFAccessor.ImportResult[] accessors) {
Vector4 row2 = m.GetRow(2);
row2.x = -row2.x;
Vector4 row3 = m.GetRow(3);
row3.x = -row3.x;
row3 = Vector4.Scale(row3, new Vector4(-importScale, importScale, importScale, 1.0f));
m.SetColumn(0, row0);
m.SetColumn(1, row1);
m.SetColumn(2, row2);
Expand All @@ -88,13 +90,13 @@ public ImportResult Import(GLTFAccessor.ImportResult[] accessors) {
}

public class ImportTask : Importer.ImportTask<ImportResult[]> {
public ImportTask(List<GLTFSkin> skins, GLTFAccessor.ImportTask accessorTask) : base(accessorTask) {
public ImportTask(List<GLTFSkin> skins, GLTFAccessor.ImportTask accessorTask, float importScale=1.0f) : base(accessorTask) {
task = new Task(() => {
if (skins == null) return;

Result = new ImportResult[skins.Count];
for (int i = 0; i < Result.Length; i++) {
Result[i] = skins[i].Import(accessorTask.Result);
Result[i] = skins[i].Import(accessorTask.Result, importScale);
}
});
}
Expand Down