Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from ousttrue/sparse_accessor
Browse files Browse the repository at this point in the history
Sparse accessor
  • Loading branch information
ousttrue authored Jun 7, 2018
2 parents cc58099 + d162fc6 commit 3fd8fc2
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 141 deletions.
76 changes: 71 additions & 5 deletions Core/Scripts/Extensions/glTFExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
Expand Down Expand Up @@ -101,31 +102,96 @@ static int GetAccessorElementCount<T>()
}
}

public static int ExtendBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex, T[] array,
public static int ExtendBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex, T[] array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
return gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, new ArraySegment<T>(array), target);
}

public static int ExtendBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex, ArraySegment<T> array,
glBufferTarget target=glBufferTarget.NONE) where T : struct
public static int ExtendBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex,
ArraySegment<T> array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
if (array.Count == 0)
{
return -1;
}
var viewIndex = ExtendBufferAndGetViewIndex(gltf, bufferIndex, array, target);
var accessorIndex = gltf.accessors.Count;
gltf.accessors.Add(new glTFAccessor
{
bufferView = viewIndex,
byteOffset = 0,
componentType = GetComponentType<T>(),
type = GetAccessorType<T>(),
count = array.Count,
});
return accessorIndex;
}

public static int ExtendBufferAndGetViewIndex<T>(this glTF gltf, int bufferIndex,
T[] array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
return ExtendBufferAndGetViewIndex(gltf, bufferIndex, new ArraySegment<T>(array), target);
}

public static int ExtendBufferAndGetViewIndex<T>(this glTF gltf, int bufferIndex,
ArraySegment<T> array,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
if (array.Count == 0)
{
return -1;
}
var view = gltf.buffers[bufferIndex].Append(array, target);
var viewIndex = gltf.bufferViews.Count;
gltf.bufferViews.Add(view);
return viewIndex;
}

public static int ExtendSparseBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex,
int accessorCount,
T[] sparseValues, int[] sparseIndices, int sparseViewIndex,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
return ExtendSparseBufferAndGetAccessorIndex(gltf, bufferIndex,
accessorCount,
new ArraySegment<T>(sparseValues), sparseIndices, sparseViewIndex,
target);
}

public static int ExtendSparseBufferAndGetAccessorIndex<T>(this glTF gltf, int bufferIndex,
int accessorCount,
ArraySegment<T> sparseValues, int[] sparseIndices, int sparseIndicesViewIndex,
glBufferTarget target = glBufferTarget.NONE) where T : struct
{
if (sparseValues.Count == 0)
{
return -1;
}
var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(gltf, bufferIndex, sparseValues, target);
var accessorIndex = gltf.accessors.Count;
gltf.accessors.Add(new glTFAccessor
{
bufferView = viewIndex,
byteOffset = 0,
componentType = GetComponentType<T>(),
type = GetAccessorType<T>(),
count = array.Count,
count = accessorCount,

sparse = new glTFSparse
{
count=sparseIndices.Length,
indices = new glTFSparseIndices
{
bufferView = sparseIndicesViewIndex,
componentType = glComponentType.UNSIGNED_INT
},
values = new glTFSparseValues
{
bufferView = sparseValuesViewIndex,
}
}
});
return accessorIndex;
}
Expand Down
80 changes: 57 additions & 23 deletions Core/Scripts/Format/glTF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ public int AddBufferView(glTFBufferView view)

T[] GetAttrib<T>(glTFAccessor accessor, glTFBufferView view) where T : struct
{
var attrib = new T[accessor.count];
return GetAttrib<T>(accessor.count, accessor.byteOffset, view);
}
T[] GetAttrib<T>(int count, int byteOffset, glTFBufferView view) where T : struct
{
var attrib = new T[count];
//
var segment = buffers[view.buffer].GetBytes();
var bytes = new ArraySegment<Byte>(segment.Array, segment.Offset + view.byteOffset + accessor.byteOffset, accessor.count * view.byteStride);
var bytes = new ArraySegment<Byte>(segment.Array, segment.Offset + view.byteOffset + byteOffset, count * view.byteStride);
bytes.MarshalCoyTo(attrib);
return attrib;
}
Expand All @@ -91,9 +95,8 @@ public ArraySegment<Byte> GetViewBytes(int bufferView)
return new ArraySegment<byte>(segment.Array, segment.Offset + view.byteOffset, view.byteLength);
}

IEnumerable<int> _GetIndices(int index, out int count)
IEnumerable<int> _GetIndices(glTFAccessor accessor, out int count)
{
var accessor = accessors[index];
count = accessor.count;
var view = bufferViews[accessor.bufferView];
switch ((glComponentType)accessor.componentType)
Expand All @@ -115,15 +118,37 @@ IEnumerable<int> _GetIndices(int index, out int count)
}
throw new NotImplementedException("GetIndices: unknown componenttype: " + accessor.componentType);
}
public int[] GetIndices(int index, Func<int, int> mod = null)

IEnumerable<int> _GetIndices(glTFBufferView view, int count, int byteOffset, glComponentType componentType)
{
switch (componentType)
{
case glComponentType.UNSIGNED_BYTE:
{
return GetAttrib<Byte>(count, byteOffset, view).Select(x => (int)(x));
}

case glComponentType.UNSIGNED_SHORT:
{
return GetAttrib<UInt16>(count, byteOffset, view).Select(x => (int)(x));
}

case glComponentType.UNSIGNED_INT:
{
return GetAttrib<UInt32>(count, byteOffset, view).Select(x => (int)(x));
}
}
throw new NotImplementedException("GetIndices: unknown componenttype: " + componentType);
}

public int[] GetIndices(int accessorIndex)
{
int count;
var result = _GetIndices(index, out count);
var result = _GetIndices(accessors[accessorIndex], out count);
var indices = new int[count];

// flip triangles
var it = result.GetEnumerator();
if (mod == null)
{
for (int i = 0; i < count; i += 3)
{
Expand All @@ -132,29 +157,38 @@ public int[] GetIndices(int index, Func<int, int> mod = null)
it.MoveNext(); indices[i] = it.Current;
}
}
else
{
for (int i = 0; i < count; i += 3)
{
it.MoveNext(); indices[i + 2] = mod(it.Current);
it.MoveNext(); indices[i + 1] = mod(it.Current);
it.MoveNext(); indices[i] = mod(it.Current);
}
}

return indices;
}

public T[] GetArrayFromAccessor<T>(int index, Func<T, T> mod = null) where T : struct
public T[] GetArrayFromAccessor<T>(int accessorIndex) where T : struct
{
var vertexAccessor = accessors[index];
var view = bufferViews[vertexAccessor.bufferView];
var result = GetAttrib<T>(vertexAccessor, view);
if (mod != null)
var vertexAccessor = accessors[accessorIndex];

if (vertexAccessor.count <= 0) return new T[] { };

var result = (vertexAccessor.bufferView != -1)
? GetAttrib<T>(vertexAccessor, bufferViews[vertexAccessor.bufferView])
: new T[vertexAccessor.count]
;

var sparse = vertexAccessor.sparse;
if (sparse !=null && sparse.count > 0)
{
for (int i = 0; i < result.Length; ++i)
// override sparse values
var indices = _GetIndices(bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType);
var values = GetAttrib<T>(sparse.count, sparse.values.byteOffset, bufferViews[sparse.values.bufferView]);

if (sparse.count != values.Length)
{
int a = 0;
}

var it = indices.GetEnumerator();
for(int i=0; i<sparse.count; ++i)
{
result[i] = mod(result[i]);
it.MoveNext();
result[it.Current] = values[i];
}
}
return result;
Expand Down
60 changes: 53 additions & 7 deletions Core/Scripts/Format/glTFBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,63 @@ public string ToJson()
}

[Serializable]
public class glTFAccessor : IJsonSerializable
public class glTFSparseIndices : JsonSerializableBase
{
public int bufferView;
public int bufferView = -1;
public int byteOffset;
public glComponentType componentType;

protected override void SerializeMembers(JsonFormatter f)
{
f.KeyValue(() => bufferView);
f.KeyValue(() => byteOffset);
f.Key("componentType"); f.Value((int)componentType);
}
}

[Serializable]
public class glTFSparseValues : JsonSerializableBase
{
public int bufferView = -1;
public int byteOffset;

protected override void SerializeMembers(JsonFormatter f)
{
f.KeyValue(() => bufferView);
f.KeyValue(() => byteOffset);
}
}

[Serializable]
public class glTFSparse : JsonSerializableBase
{
public int count;
public glTFSparseIndices indices;
public glTFSparseValues values;

protected override void SerializeMembers(JsonFormatter f)
{
f.KeyValue(() => count);
f.KeyValue(() => indices);
f.KeyValue(() => values);
}
}

[Serializable]
public class glTFAccessor : JsonSerializableBase
{
public int bufferView = -1;
public int byteOffset;
public string type;
public glComponentType componentType;
public int count;
public float[] max;
public float[] min;

public string ToJson()
public glTFSparse sparse;

protected override void SerializeMembers(JsonFormatter f)
{
var f = new JsonFormatter();
f.BeginMap();
f.KeyValue(() => bufferView);
f.KeyValue(() => byteOffset);
f.KeyValue(() => type);
Expand All @@ -116,8 +159,11 @@ public string ToJson()
{
f.KeyValue(() => min);
}
f.EndMap();
return f.ToString();

if (sparse != null && sparse.count > 0)
{
f.KeyValue(() => sparse);
}
}
}
}
14 changes: 11 additions & 3 deletions Core/Scripts/IO/ImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ public interface IShaderStore

class ShaderStore : IShaderStore
{
string m_defaultShaderName;
Shader m_default;
Shader Default
{
get
{
if (m_default == null)
{
m_default = Shader.Find("Standard");
m_default = Shader.Find(m_defaultShaderName);
}
return m_default;
}
Expand All @@ -45,11 +46,16 @@ Shader VColor
}
}

public ShaderStore()
public ShaderStore() : this("Standard")
{

}

public ShaderStore(string defaultShaderName)
{
m_defaultShaderName = defaultShaderName;
}

public Shader GetShader(ImporterContext context, int materialIndex)
{
if (context.HasVertexColor(materialIndex))
Expand All @@ -61,8 +67,10 @@ public Shader GetShader(ImporterContext context, int materialIndex)
}
}


public delegate Material CreateMaterialFunc(ImporterContext ctx, int i);


public class ImporterContext
{
#region Source
Expand Down Expand Up @@ -207,7 +215,7 @@ void RestoreOlderVersionValues()

public bool HasVertexColor(int materialIndex)
{
if(materialIndex<0 || materialIndex >= GLTF.materials.Count)
if (materialIndex < 0 || materialIndex >= GLTF.materials.Count)
{
return false;
}
Expand Down
Loading

0 comments on commit 3fd8fc2

Please sign in to comment.