mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-09 20:28:07 -05:00
GLTF to Gltf
This commit is contained in:
parent
8d80c32a11
commit
bccad49a27
|
|
@ -7,7 +7,7 @@ namespace UniGLTF
|
|||
{
|
||||
public class ExportingGltfData
|
||||
{
|
||||
public glTF GLTF { get; } = new glTF();
|
||||
public glTF Gltf { get; } = new glTF();
|
||||
|
||||
protected IBytesBuffer _buffer;
|
||||
/// <summary>
|
||||
|
|
@ -23,7 +23,7 @@ namespace UniGLTF
|
|||
}
|
||||
|
||||
// buffers[0] is export target
|
||||
GLTF.buffers.Add(new glTFBuffer());
|
||||
Gltf.buffers.Add(new glTFBuffer());
|
||||
_buffer = new ArrayByteBuffer(new byte[reserved]);
|
||||
}
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ namespace UniGLTF
|
|||
public glTFBufferView ExtendBufferAndGetView<T>(ArraySegment<T> segment, glBufferTarget target) where T : struct
|
||||
{
|
||||
var view = _buffer.Extend(segment, target);
|
||||
GLTF.buffers[0].byteLength = _buffer.Bytes.Count;
|
||||
Gltf.buffers[0].byteLength = _buffer.Bytes.Count;
|
||||
return view;
|
||||
}
|
||||
|
||||
|
|
@ -44,8 +44,8 @@ namespace UniGLTF
|
|||
return -1;
|
||||
}
|
||||
var view = ExtendBufferAndGetView(array, target);
|
||||
var viewIndex = GLTF.bufferViews.Count;
|
||||
GLTF.bufferViews.Add(view);
|
||||
var viewIndex = Gltf.bufferViews.Count;
|
||||
Gltf.bufferViews.Add(view);
|
||||
return viewIndex;
|
||||
}
|
||||
|
||||
|
|
@ -67,10 +67,10 @@ namespace UniGLTF
|
|||
var viewIndex = ExtendBufferAndGetViewIndex(array, target);
|
||||
|
||||
// index buffer's byteStride is unnecessary
|
||||
GLTF.bufferViews[viewIndex].byteStride = 0;
|
||||
Gltf.bufferViews[viewIndex].byteStride = 0;
|
||||
|
||||
var accessorIndex = GLTF.accessors.Count;
|
||||
GLTF.accessors.Add(new glTFAccessor
|
||||
var accessorIndex = Gltf.accessors.Count;
|
||||
Gltf.accessors.Add(new glTFAccessor
|
||||
{
|
||||
bufferView = viewIndex,
|
||||
byteOffset = 0,
|
||||
|
|
@ -111,8 +111,8 @@ namespace UniGLTF
|
|||
return -1;
|
||||
}
|
||||
var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(sparseValues, target);
|
||||
var accessorIndex = GLTF.accessors.Count;
|
||||
GLTF.accessors.Add(new glTFAccessor
|
||||
var accessorIndex = Gltf.accessors.Count;
|
||||
Gltf.accessors.Add(new glTFAccessor
|
||||
{
|
||||
byteOffset = 0,
|
||||
componentType = glTFExtensions.GetComponentType<T>(),
|
||||
|
|
@ -144,8 +144,8 @@ namespace UniGLTF
|
|||
public int AppendToBuffer(ArraySegment<byte> segment)
|
||||
{
|
||||
var gltfBufferView = _buffer.Extend(segment);
|
||||
var viewIndex = GLTF.bufferViews.Count;
|
||||
GLTF.bufferViews.Add(gltfBufferView);
|
||||
var viewIndex = Gltf.bufferViews.Count;
|
||||
Gltf.bufferViews.Add(gltfBufferView);
|
||||
return viewIndex;
|
||||
}
|
||||
|
||||
|
|
@ -159,7 +159,7 @@ namespace UniGLTF
|
|||
public byte[] ToGlbBytes()
|
||||
{
|
||||
var f = new JsonFormatter();
|
||||
GltfSerializer.Serialize(f, GLTF);
|
||||
GltfSerializer.Serialize(f, Gltf);
|
||||
|
||||
var json = f.ToString().ParseAsJson().ToString(" ");
|
||||
|
||||
|
|
@ -188,10 +188,10 @@ namespace UniGLTF
|
|||
public (string, glTFBuffer) ToGltf(string gltfPath)
|
||||
{
|
||||
// fix buffer path
|
||||
if (GLTF.buffers.Count == 1)
|
||||
if (Gltf.buffers.Count == 1)
|
||||
{
|
||||
var withoutExt = Path.GetFileNameWithoutExtension(gltfPath);
|
||||
GLTF.buffers[0].uri = $"{withoutExt}.bin";
|
||||
Gltf.buffers[0].uri = $"{withoutExt}.bin";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -199,12 +199,12 @@ namespace UniGLTF
|
|||
}
|
||||
|
||||
var f = new JsonFormatter();
|
||||
GltfSerializer.Serialize(f, GLTF);
|
||||
GltfSerializer.Serialize(f, Gltf);
|
||||
var json = f.ToString().ParseAsJson().ToString(" ");
|
||||
|
||||
json = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(json);
|
||||
|
||||
return (json, GLTF.buffers[0]);
|
||||
return (json, Gltf.buffers[0]);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ namespace UniGLTF
|
|||
|
||||
public static GltfData CreateFromExportForTest(ExportingGltfData data)
|
||||
{
|
||||
return CreateFromGltfDataForTest(data.GLTF, data.BinBytes);
|
||||
return CreateFromGltfDataForTest(data.Gltf, data.BinBytes);
|
||||
}
|
||||
|
||||
public static GltfData CreateFromGltfDataForTest(glTF gltf, ArraySegment<byte> bytes)
|
||||
|
|
|
|||
|
|
@ -74,8 +74,8 @@ namespace UniGLTF
|
|||
{
|
||||
// position
|
||||
var positionAccessorIndex = data.ExtendBufferAndGetAccessorIndex(positions, glBufferTarget.ARRAY_BUFFER);
|
||||
data.GLTF.accessors[positionAccessorIndex].min = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray();
|
||||
data.GLTF.accessors[positionAccessorIndex].max = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray();
|
||||
data.Gltf.accessors[positionAccessorIndex].min = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Min(a.x, b.x), Math.Min(a.y, b.y), Mathf.Min(a.z, b.z))).ToArray();
|
||||
data.Gltf.accessors[positionAccessorIndex].max = positions.Aggregate(positions[0], (a, b) => new Vector3(Mathf.Max(a.x, b.x), Math.Max(a.y, b.y), Mathf.Max(a.z, b.z))).ToArray();
|
||||
|
||||
// normal
|
||||
var normalAccessorIndex = -1;
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ namespace UniGLTF
|
|||
var indicesAccessorIndex = data.ExtendBufferAndGetAccessorIndex(indices.Select(x => (uint)m_vertexIndexMap[x]).ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER);
|
||||
var positions = m_positions.ToArray();
|
||||
var positionAccessorIndex = data.ExtendBufferAndGetAccessorIndex(positions, glBufferTarget.ARRAY_BUFFER);
|
||||
AccessorsBounds.UpdatePositionAccessorsBounds(data.GLTF.accessors[positionAccessorIndex], positions);
|
||||
AccessorsBounds.UpdatePositionAccessorsBounds(data.Gltf.accessors[positionAccessorIndex], positions);
|
||||
|
||||
var normals = m_normals.ToArray();
|
||||
var normalAccessorIndex = data.ExtendBufferAndGetAccessorIndex(normals, glBufferTarget.ARRAY_BUFFER);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ namespace UniGLTF
|
|||
var materials = unityMesh.Materials;
|
||||
var positions = mesh.vertices.Select(axisInverter.InvertVector3).ToArray();
|
||||
var positionAccessorIndex = data.ExtendBufferAndGetAccessorIndex(positions, glBufferTarget.ARRAY_BUFFER);
|
||||
AccessorsBounds.UpdatePositionAccessorsBounds(data.GLTF.accessors[positionAccessorIndex], positions);
|
||||
AccessorsBounds.UpdatePositionAccessorsBounds(data.Gltf.accessors[positionAccessorIndex], positions);
|
||||
|
||||
var normalAccessorIndex = data.ExtendBufferAndGetAccessorIndex(mesh.normals.Select(y => axisInverter.InvertVector3(y.normalized)).ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ namespace UniGLTF
|
|||
var viewIndex = data.ExtendBufferAndGetViewIndex(bytesWithMime.bytes);
|
||||
|
||||
// add image
|
||||
var imageIndex = data.GLTF.images.Count;
|
||||
data.GLTF.images.Add(new glTFImage
|
||||
var imageIndex = data.Gltf.images.Count;
|
||||
data.Gltf.images.Add(new glTFImage
|
||||
{
|
||||
name = TextureImportName.RemoveSuffix(texture.name),
|
||||
bufferView = viewIndex,
|
||||
|
|
@ -39,13 +39,13 @@ namespace UniGLTF
|
|||
});
|
||||
|
||||
// add sampler
|
||||
var samplerIndex = data.GLTF.samplers.Count;
|
||||
var samplerIndex = data.Gltf.samplers.Count;
|
||||
var sampler = TextureSamplerUtil.Export(texture);
|
||||
data.GLTF.samplers.Add(sampler);
|
||||
data.Gltf.samplers.Add(sampler);
|
||||
|
||||
// add texture
|
||||
var textureIndex = data.GLTF.textures.Count;
|
||||
data.GLTF.textures.Add(new glTFTexture
|
||||
var textureIndex = data.Gltf.textures.Count;
|
||||
data.Gltf.textures.Add(new glTFTexture
|
||||
{
|
||||
sampler = samplerIndex,
|
||||
source = imageIndex,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ namespace UniGLTF
|
|||
{
|
||||
protected ExportingGltfData _data;
|
||||
|
||||
protected glTF _gltf => _data.GLTF;
|
||||
protected glTF _gltf => _data.Gltf;
|
||||
|
||||
public GameObject Copy
|
||||
{
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ namespace UniGLTF
|
|||
exporter.Prepare(root);
|
||||
exporter.Export(new EditorTextureSerializer());
|
||||
}
|
||||
var gltf = data.GLTF;
|
||||
var gltf = data.Gltf;
|
||||
Assert.AreEqual(1, gltf.images.Count);
|
||||
var exportedImage = gltf.images[0];
|
||||
Assert.AreEqual("image/png", exportedImage.mimeType);
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace UniGLTF
|
|||
public void TextureNameUniqueness()
|
||||
{
|
||||
var data = new ExportingGltfData();
|
||||
var gltf = data.GLTF;
|
||||
var gltf = data.Gltf;
|
||||
gltf.asset.version = "2.0";
|
||||
gltf.textures.Add(new glTFTexture
|
||||
{
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ namespace UniGLTF
|
|||
: MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings)
|
||||
;
|
||||
|
||||
using (var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes))
|
||||
using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes))
|
||||
{
|
||||
|
||||
{
|
||||
|
|
@ -190,7 +190,7 @@ namespace UniGLTF
|
|||
: MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings)
|
||||
;
|
||||
|
||||
using (var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes))
|
||||
using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes))
|
||||
{
|
||||
{
|
||||
var indices = parsed.GetIndices(gltfMesh.primitives[0].indices);
|
||||
|
|
|
|||
|
|
@ -109,9 +109,9 @@ namespace UniGLTF
|
|||
exporter.Export(new EditorTextureSerializer());
|
||||
|
||||
// remove empty buffer
|
||||
data.GLTF.buffers.Clear();
|
||||
data.Gltf.buffers.Clear();
|
||||
|
||||
json = data.GLTF.ToJson();
|
||||
json = data.Gltf.ToJson();
|
||||
}
|
||||
|
||||
// parse
|
||||
|
|
@ -141,7 +141,7 @@ namespace UniGLTF
|
|||
data.ExtendBufferAndGetView(bytes, glBufferTarget.NONE);
|
||||
}
|
||||
|
||||
Assert.AreEqual(values.Count, data.GLTF.buffers[0].byteLength);
|
||||
Assert.AreEqual(values.Count, data.Gltf.buffers[0].byteLength);
|
||||
Assert.True(Enumerable.SequenceEqual(values, data.BinBytes.ToArray()));
|
||||
}
|
||||
|
||||
|
|
@ -299,7 +299,7 @@ namespace UniGLTF
|
|||
exporter.Export(new EditorTextureSerializer());
|
||||
}
|
||||
|
||||
var expected = data.GLTF.ToJson().ParseAsJson();
|
||||
var expected = data.Gltf.ToJson().ParseAsJson();
|
||||
expected.AddKey(Utf8String.From("meshes"));
|
||||
expected.AddValue(default(ArraySegment<byte>), ValueNodeType.Array);
|
||||
expected["meshes"].AddValue(default(ArraySegment<byte>), ValueNodeType.Object);
|
||||
|
|
@ -333,7 +333,7 @@ namespace UniGLTF
|
|||
primitive["targets"][1].AddKey(Utf8String.From("TANGENT"));
|
||||
primitive["targets"][1].AddValue(Utf8String.From("0").Bytes, ValueNodeType.Integer);
|
||||
|
||||
data.GLTF.meshes.Add(new glTFMesh("test")
|
||||
data.Gltf.meshes.Add(new glTFMesh("test")
|
||||
{
|
||||
primitives = new List<glTFPrimitives>
|
||||
{
|
||||
|
|
@ -361,7 +361,7 @@ namespace UniGLTF
|
|||
}
|
||||
}
|
||||
});
|
||||
var actual = data.GLTF.ToJson().ParseAsJson();
|
||||
var actual = data.Gltf.ToJson().ParseAsJson();
|
||||
|
||||
Assert.AreEqual(expected, actual);
|
||||
}
|
||||
|
|
@ -528,7 +528,7 @@ namespace UniGLTF
|
|||
|
||||
// export
|
||||
var data = new ExportingGltfData();
|
||||
var gltf = data.GLTF;
|
||||
var gltf = data.Gltf;
|
||||
var json = default(string);
|
||||
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
|
||||
{
|
||||
|
|
@ -606,7 +606,7 @@ namespace UniGLTF
|
|||
|
||||
// export
|
||||
var data = new ExportingGltfData();
|
||||
var gltf = data.GLTF;
|
||||
var gltf = data.Gltf;
|
||||
string json;
|
||||
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
|
||||
{
|
||||
|
|
@ -669,7 +669,7 @@ namespace UniGLTF
|
|||
|
||||
// export
|
||||
var data = new ExportingGltfData();
|
||||
var gltf = data.GLTF;
|
||||
var gltf = data.Gltf;
|
||||
string json;
|
||||
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -169,7 +169,7 @@ namespace VRM.Samples
|
|||
|
||||
// TODO: Check contents in JSON
|
||||
/*var exportJson = */
|
||||
JsonParser.Parse(vrm.GLTF.ToJson());
|
||||
JsonParser.Parse(vrm.Gltf.ToJson());
|
||||
|
||||
// TODO: Check contents in JSON
|
||||
/*var newExportedJson = */
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ namespace UniVRM10
|
|||
Action<ArraySegment<byte>, glTFAccessor> minMax = null,
|
||||
int offset = 0, int count = 0)
|
||||
{
|
||||
var gltf = data.GLTF;
|
||||
var gltf = data.Gltf;
|
||||
var accessorIndex = gltf.accessors.Count;
|
||||
var accessor = self.CreateGltfAccessor(viewIndex, count, offset * self.Stride);
|
||||
if (minMax != null)
|
||||
|
|
@ -119,7 +119,7 @@ namespace UniVRM10
|
|||
var sparseIndexView = data.AppendToBuffer(sparseIndexBin);
|
||||
var sparseValueView = data.AppendToBuffer(sparseValueBin);
|
||||
|
||||
var accessorIndex = data.GLTF.accessors.Count;
|
||||
var accessorIndex = data.Gltf.accessors.Count;
|
||||
var accessor = new glTFAccessor
|
||||
{
|
||||
componentType = (glComponentType)self.ComponentType,
|
||||
|
|
@ -144,7 +144,7 @@ namespace UniVRM10
|
|||
{
|
||||
minMax(sparseValueBin, accessor);
|
||||
}
|
||||
data.GLTF.accessors.Add(accessor);
|
||||
data.Gltf.accessors.Add(accessor);
|
||||
return accessorIndex;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,12 +36,12 @@ namespace UniVRM10
|
|||
throw new ArgumentException(nameof(textureSerializer));
|
||||
}
|
||||
|
||||
Storage.GLTF.extensionsUsed.Add(glTF_KHR_texture_transform.ExtensionName);
|
||||
Storage.GLTF.extensionsUsed.Add(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm.ExtensionName);
|
||||
Storage.GLTF.extensionsUsed.Add(glTF_KHR_materials_unlit.ExtensionName);
|
||||
Storage.GLTF.extensionsUsed.Add(UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon.ExtensionName);
|
||||
Storage.GLTF.extensionsUsed.Add(UniGLTF.Extensions.VRMC_springBone.VRMC_springBone.ExtensionName);
|
||||
Storage.GLTF.extensionsUsed.Add(UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint.ExtensionName);
|
||||
Storage.Gltf.extensionsUsed.Add(glTF_KHR_texture_transform.ExtensionName);
|
||||
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm.ExtensionName);
|
||||
Storage.Gltf.extensionsUsed.Add(glTF_KHR_materials_unlit.ExtensionName);
|
||||
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon.ExtensionName);
|
||||
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_springBone.VRMC_springBone.ExtensionName);
|
||||
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint.ExtensionName);
|
||||
|
||||
m_textureSerializer = textureSerializer;
|
||||
m_textureExporter = new TextureExporter(m_textureSerializer);
|
||||
|
|
@ -166,36 +166,36 @@ namespace UniVRM10
|
|||
|
||||
public void Export(GameObject root, Model model, ModelExporter converter, ExportArgs option, VRM10ObjectMeta vrmMeta = null)
|
||||
{
|
||||
Storage.GLTF.asset = ExportAsset(model);
|
||||
Storage.Gltf.asset = ExportAsset(model);
|
||||
|
||||
Storage.Reserve(CalcReserveBytes(model));
|
||||
|
||||
foreach (var material in ExportMaterials(model, m_textureExporter, m_settings))
|
||||
{
|
||||
Storage.GLTF.materials.Add(material);
|
||||
Storage.Gltf.materials.Add(material);
|
||||
}
|
||||
|
||||
foreach (var mesh in ExportMeshes(model.MeshGroups, model.Materials, Storage, option))
|
||||
{
|
||||
Storage.GLTF.meshes.Add(mesh);
|
||||
Storage.Gltf.meshes.Add(mesh);
|
||||
}
|
||||
|
||||
foreach (var (node, skin) in ExportNodes(model.Nodes, model.MeshGroups, Storage, option))
|
||||
{
|
||||
Storage.GLTF.nodes.Add(node);
|
||||
Storage.Gltf.nodes.Add(node);
|
||||
if (skin != null)
|
||||
{
|
||||
var skinIndex = Storage.GLTF.skins.Count;
|
||||
Storage.GLTF.skins.Add(skin);
|
||||
var skinIndex = Storage.Gltf.skins.Count;
|
||||
Storage.Gltf.skins.Add(skin);
|
||||
node.skin = skinIndex;
|
||||
}
|
||||
}
|
||||
Storage.GLTF.scenes.Add(new gltfScene()
|
||||
Storage.Gltf.scenes.Add(new gltfScene()
|
||||
{
|
||||
nodes = model.Root.Children.Select(child => model.Nodes.IndexOfThrow(child)).ToArray()
|
||||
});
|
||||
|
||||
var (vrm, vrmSpringBone, thumbnailTextureIndex) = ExportVrm(root, model, converter, vrmMeta, Storage.GLTF.nodes, m_textureExporter);
|
||||
var (vrm, vrmSpringBone, thumbnailTextureIndex) = ExportVrm(root, model, converter, vrmMeta, Storage.Gltf.nodes, m_textureExporter);
|
||||
|
||||
// Extension で Texture が増える場合があるので最後に呼ぶ
|
||||
var exportedTextures = m_textureExporter.Export();
|
||||
|
|
@ -207,18 +207,18 @@ namespace UniVRM10
|
|||
|
||||
if (thumbnailTextureIndex.HasValue)
|
||||
{
|
||||
vrm.Meta.ThumbnailImage = Storage.GLTF.textures[thumbnailTextureIndex.Value].source;
|
||||
vrm.Meta.ThumbnailImage = Storage.Gltf.textures[thumbnailTextureIndex.Value].source;
|
||||
}
|
||||
|
||||
UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref Storage.GLTF.extensions, vrm);
|
||||
UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrm);
|
||||
|
||||
if (vrmSpringBone != null)
|
||||
{
|
||||
UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref Storage.GLTF.extensions, vrmSpringBone);
|
||||
UniGLTF.Extensions.VRMC_springBone.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrmSpringBone);
|
||||
}
|
||||
|
||||
// Fix Duplicated name
|
||||
gltfExporter.FixName(Storage.GLTF);
|
||||
gltfExporter.FixName(Storage.Gltf);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user