-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/[Ll]ibrary/ | ||
/[Tt]emp/ | ||
/[Oo]bj/ | ||
/[Bb]uild/ | ||
|
||
# Autogenerated VS/MD solution and project files | ||
*.csproj | ||
*.unityproj | ||
*.sln | ||
*.suo | ||
*.tmp | ||
*.user | ||
*.userprefs | ||
*.pidb | ||
*.booproj | ||
|
||
# Unity3D generated meta files | ||
*.pidb.meta | ||
|
||
# Unity3D Generated File On Crash Reports | ||
sysinfo.txt | ||
|
||
README.md.meta | ||
LICENSE.md.meta | ||
CONTRIBUTING.md.meta |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Thor Brigsted | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using UnityEditor.Experimental.AssetImporters; | ||
using UnityEngine; | ||
|
||
namespace Siccity.GLTFUtility { | ||
[ScriptedImporter(1, "gltf")] | ||
public class GLTFImporter : ScriptedImporter { | ||
|
||
public override void OnImportAsset(AssetImportContext ctx) { | ||
GLTFObject gltfObject = JsonUtility.FromJson<GLTFObject>(File.ReadAllText(ctx.assetPath)); | ||
string directoryRoot = Directory.GetParent(ctx.assetPath).ToString() + "/"; | ||
|
||
// Read buffers | ||
for (int i = 0; i < gltfObject.buffers.Count; i++) { | ||
gltfObject.buffers[i].Read(directoryRoot); | ||
} | ||
|
||
// Get root node indices from scenes | ||
int[] rootNodes = gltfObject.scenes.SelectMany(x => x.nodes).ToArray(); | ||
|
||
if (rootNodes.Length != 1) { | ||
Debug.LogError("Only one root node is currently supported"); | ||
return; | ||
} | ||
|
||
// Add meshes | ||
for (int i = 0; i < gltfObject.meshes.Count; i++) { | ||
ctx.AddSubAsset(gltfObject.meshes[i].name, gltfObject.meshes[i].GetMesh(gltfObject)); | ||
} | ||
|
||
// Parse root node | ||
GameObject root = gltfObject.nodes[0].Create(gltfObject, null); | ||
ctx.SetMainAsset("main obj", root); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Siccity.GLTFUtility { | ||
public static class Extensions { | ||
|
||
public static T[] SubArray<T>(this T[] data, int index, int length) { | ||
T[] result = new T[length]; | ||
Array.Copy(data, index, result, 0, length); | ||
return result; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace Siccity.GLTFUtility { | ||
/// <summary> Reads data from BufferViews </summary> | ||
[Serializable] | ||
public class GLTFAccessor { | ||
public int bufferView = -1; | ||
public int byteOffset = -1; | ||
public string type; | ||
public GLType componentType = GLType.UNSET; | ||
public int count = -1; | ||
public float[] min; | ||
public float[] max; | ||
public Sparse sparse; | ||
public Indices indices; | ||
|
||
public Vector4[] ReadVec4(GLTFObject gLTFObject) { | ||
if (type != "VEC4") { | ||
Debug.LogError("Type mismatch! Expected VEC4 got " + type); | ||
return new Vector4[count]; | ||
} | ||
if (componentType != GLType.FLOAT) { | ||
Debug.LogError("Non-float componentType not supported. Got " + (int) componentType); | ||
return new Vector4[count]; | ||
} | ||
|
||
Vector4[] verts = new Vector4[count]; | ||
byte[] bytes = gLTFObject.bufferViews[bufferView].GetBytes(gLTFObject); | ||
int componentSize = GetComponentSize(); | ||
for (int i = 0; i < count; i++) { | ||
int startIndex = i * componentSize; | ||
verts[i].x = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
verts[i].y = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
verts[i].z = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
verts[i].w = System.BitConverter.ToSingle(bytes, startIndex); | ||
} | ||
return verts; | ||
} | ||
|
||
public Vector3[] ReadVec3(GLTFObject gLTFObject) { | ||
if (type != "VEC3") { | ||
Debug.LogError("Type mismatch! Expected VEC3 got " + type); | ||
return new Vector3[count]; | ||
} | ||
if (componentType != GLType.FLOAT) { | ||
Debug.LogError("Non-float componentType not supported. Got " + (int) componentType); | ||
return new Vector3[count]; | ||
} | ||
|
||
Vector3[] verts = new Vector3[count]; | ||
byte[] bytes = gLTFObject.bufferViews[bufferView].GetBytes(gLTFObject); | ||
int componentSize = GetComponentSize(); | ||
for (int i = 0; i < count; i++) { | ||
int startIndex = i * componentSize; | ||
verts[i].x = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
verts[i].y = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
verts[i].z = System.BitConverter.ToSingle(bytes, startIndex); | ||
} | ||
return verts; | ||
} | ||
|
||
public Vector2[] ReadVec2(GLTFObject gLTFObject) { | ||
if (type != "VEC2") { | ||
Debug.LogError("Type mismatch! Expected VEC2 got " + type); | ||
return new Vector2[count]; | ||
} | ||
if (componentType != GLType.FLOAT) { | ||
Debug.LogError("Non-float componentType not supported. Got " + (int) componentType); | ||
return new Vector2[count]; | ||
} | ||
|
||
Vector2[] verts = new Vector2[count]; | ||
byte[] bytes = gLTFObject.bufferViews[bufferView].GetBytes(gLTFObject); | ||
int componentSize = GetComponentSize(); | ||
for (int i = 0; i < count; i++) { | ||
int startIndex = i * componentSize; | ||
verts[i].x = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
verts[i].y = System.BitConverter.ToSingle(bytes, startIndex); | ||
startIndex += GetComponentTypeSize(componentType); | ||
} | ||
return verts; | ||
} | ||
|
||
public int[] ReadInt(GLTFObject gLTFObject) { | ||
if (type != "SCALAR") { | ||
Debug.LogError("Type mismatch! Expected SCALAR got " + type); | ||
return new int[count]; | ||
} | ||
if (componentType != GLType.UNSIGNED_SHORT) { | ||
Debug.LogError("Non-ushort componentType not supported. Got " + (int) componentType); | ||
return new int[count]; | ||
} | ||
|
||
int[] ints = new int[count]; | ||
byte[] bytes = gLTFObject.bufferViews[bufferView].GetBytes(gLTFObject); | ||
int componentSize = GetComponentSize(); | ||
for (int i = 0; i < count; i++) { | ||
int startIndex = i * componentSize; | ||
ints[i] = System.BitConverter.ToUInt16(bytes, startIndex); | ||
} | ||
return ints; | ||
} | ||
|
||
/// <summary> Get the size of the attribute type, in bytes </summary> | ||
public int GetComponentSize() { | ||
return GetComponentNumber(type) * GetComponentTypeSize(componentType); | ||
} | ||
|
||
public static int GetComponentTypeSize(GLType componentType) { | ||
switch (componentType) { | ||
case GLType.BYTE: | ||
return 1; | ||
case GLType.UNSIGNED_BYTE: | ||
return 1; | ||
case GLType.SHORT: | ||
return 2; | ||
case GLType.UNSIGNED_SHORT: | ||
return 2; | ||
case GLType.FLOAT: | ||
return 4; | ||
default: | ||
Debug.LogError("componentType " + (int) componentType + " not supported!"); | ||
return 0; | ||
} | ||
} | ||
|
||
public static int GetComponentNumber(string type) { | ||
switch (type) { | ||
case "SCALAR": | ||
return 1; | ||
case "VEC2": | ||
return 2; | ||
case "VEC3": | ||
return 3; | ||
case "VEC4": | ||
return 4; | ||
case "MAT2": | ||
return 4; | ||
case "MAT3": | ||
return 9; | ||
case "MAT4": | ||
return 16; | ||
default: | ||
Debug.LogError("type " + type + " not supported!"); | ||
return 0; | ||
} | ||
} | ||
|
||
[Serializable] | ||
public class Sparse { | ||
public int count = -1; | ||
public Values values; | ||
|
||
[Serializable] | ||
public class Values { | ||
public int bufferView = -1; | ||
} | ||
} | ||
|
||
[Serializable] | ||
public class Indices { | ||
public int bufferView = -1; | ||
public int componentType = -1; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using UnityEngine; | ||
|
||
namespace Siccity.GLTFUtility { | ||
/// <summary> Contains raw binary data </summary> | ||
[Serializable] | ||
public class GLTFBuffer { | ||
public int byteLength = -1; | ||
public string uri; | ||
|
||
public byte[] cache; | ||
|
||
public void Read(string directoryRoot) { | ||
if (cache == null) cache = File.ReadAllBytes(directoryRoot + uri); | ||
} | ||
|
||
public byte[] GetBytes() { | ||
if (cache == null) Debug.LogError("Need to call Read(directoryRoot) on GLTFBuffer before GetBytes()"); | ||
return cache; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.