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

vrm1のローダー更新 #837

Merged
merged 48 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6116a6c
rename
ousttrue Mar 29, 2021
5758146
remove IVrmStorage
ousttrue Mar 29, 2021
ef6e161
remove IVrmExporter
ousttrue Mar 29, 2021
0601347
remove stride
ousttrue Mar 29, 2021
8a07b1c
use UniGLTF.IBytesBuffer
ousttrue Mar 29, 2021
d4777e1
Vrm10Storage(GltfParser)
ousttrue Mar 29, 2021
8629f4d
VrmLoader.CreateVrmModel use GltfParser
ousttrue Mar 29, 2021
fec118d
WIP UniGLTF with VrmLib.Model
ousttrue Mar 29, 2021
1c4b0e6
WIP Vrm10MToonMaterialImporter
ousttrue Mar 30, 2021
7fce365
rename. remove IExternalUnityObject
ousttrue Mar 30, 2021
323a816
WIP RuntimeUnityBuilder
ousttrue Mar 30, 2021
f8745ee
comment
ousttrue Mar 30, 2021
75a12cb
remove non used
ousttrue Mar 30, 2021
8184536
WIP ExportVrm
ousttrue Mar 30, 2021
a8039d4
WIP LoadVrm
ousttrue Mar 30, 2021
c789364
remove MaterialAdapter for import
ousttrue Mar 30, 2021
2336a28
WIP vrm, material
ousttrue Mar 30, 2021
0a11bd1
WIP Model.Vrm
ousttrue Mar 30, 2021
ce4d393
Submesh.Material to int
ousttrue Mar 30, 2021
c8d90c1
remove Material and VRM from VrmLib
ousttrue Mar 30, 2021
ad62835
remove Texture and Material from VRM10(use VRMShaders)
ousttrue Mar 30, 2021
a15b497
use Vrm10MToonMaterialImporter
ousttrue Mar 30, 2021
bf73c96
Migration
ousttrue Mar 30, 2021
2d5d3bb
comment
ousttrue Mar 30, 2021
9a1f85c
MigrateToVrm1 switch
ousttrue Mar 30, 2021
ce6090f
LoadGeometryAsync
ousttrue Mar 30, 2021
9a29eac
humanoid
ousttrue Mar 30, 2021
d57cf4c
expression
ousttrue Mar 30, 2021
339b2f6
MorphTargetBinding
ousttrue Mar 30, 2021
0b65eeb
MaterialColorBind, TextureTransformBind
ousttrue Mar 30, 2021
82ef39e
remove VRM10Controller.ModelAsset
ousttrue Mar 31, 2021
fd7f09f
remove RuntimeUnityBuilder.Asset
ousttrue Mar 31, 2021
4b9a140
ModelAsset を縮小
ousttrue Mar 31, 2021
0249e77
remove dependency to ImporterContext from IAnimationImporter
ousttrue Apr 1, 2021
94bb7ef
Remove dependency to ImporterContext from MeshImporter
ousttrue Apr 1, 2021
6244f6a
Remove dependency to ImporterContext from NodeImporter
ousttrue Apr 1, 2021
5df1576
implement build meta
ousttrue Apr 1, 2021
c3fc779
implement LookAt
ousttrue Apr 1, 2021
009fcab
implement firstPerson
ousttrue Apr 1, 2021
5723540
implement SpringBone
ousttrue Apr 1, 2021
ca559b5
implement Constraint
ousttrue Apr 1, 2021
b3b7712
param.Sampler.WrapModes != null
ousttrue Apr 1, 2021
dbc6d45
Nodes
ousttrue Apr 1, 2021
315c90c
MigrationVrm.Migrate
ousttrue Apr 1, 2021
c94a43e
implement MigrationMToon
ousttrue Apr 1, 2021
99091a5
fix test
ousttrue Apr 1, 2021
8784f78
implement MToon
ousttrue Apr 1, 2021
fa25c0f
コード整理
ousttrue Apr 1, 2021
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
10 changes: 5 additions & 5 deletions Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public void Dispose()
static bool s_foldMaterials = true;
static bool s_foldTextures = true;

public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser)
public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures)
{
var canExtract = !importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D);
using (new TmpGuiEnable(canExtract))
{
if (GUILayout.Button("Extract Materials And Textures ..."))
{
ExtractMaterialsAndTextures(importer, parser);
ExtractMaterialsAndTextures(importer, parser, enumTextures);
}
}

Expand All @@ -57,7 +57,7 @@ public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser)
s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures");
if (s_foldTextures)
{
var names = GltfTextureEnumerator.Enumerate(parser)
var names = enumTextures(parser)
.Select(x =>
{
if (x.TextureType != TextureImportTypes.StandardMap && !string.IsNullOrEmpty(x.Uri))
Expand Down Expand Up @@ -122,7 +122,7 @@ public static void SetExternalUnityObject<T>(this ScriptedImporter self, UnityEd
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
}

static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser)
static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures)
{
if (string.IsNullOrEmpty(self.assetPath))
{
Expand All @@ -143,7 +143,7 @@ static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser
var assetPath = UnityPath.FromFullpath(parser.TargetPath);
var dirName = $"{assetPath.FileNameWithoutExtension}.Textures";
TextureExtractor.ExtractTextures(parser, assetPath.Parent.Child(dirName),
GltfTextureEnumerator.Enumerate,
enumTextures,
self.GetSubAssets<UnityEngine.Texture2D>(self.assetPath).ToArray(),
addRemap,
onCompleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void OnInspectorGUI()
break;

case Tabs.Materials:
EditorMaterial.OnGUIMaterial(m_importer, m_parser);
EditorMaterial.OnGUIMaterial(m_importer, m_parser, GltfTextureEnumerator.EnumerateAllTexturesDistinct);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override void OnInspectorGUI()
break;

case Tabs.Materials:
EditorMaterial.OnGUIMaterial(m_importer, m_parser);
EditorMaterial.OnGUIMaterial(m_importer, m_parser, GltfTextureEnumerator.EnumerateAllTexturesDistinct);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
Expand Down Expand Up @@ -37,32 +36,28 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
// Import(create unity objects)
//
var externalObjectMap = scriptedImporter.GetExternalObjectMap().Select(kv => (kv.Value.name, kv.Value)).ToArray();

var externalTextures = EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent).ToArray();

using (var loaded = new ImporterContext(parser, externalObjectMap.Concat(externalTextures)))
using (var loader = new ImporterContext(parser, externalObjectMap.Concat(externalTextures)))
{
// settings TextureImporters
foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser))
foreach (var textureInfo in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser))
{
TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap);
TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalMap);
}

loaded.InvertAxis = reverseAxis;
loaded.Load();
loaded.ShowMeshes();
loader.InvertAxis = reverseAxis;
loader.Load();
loader.ShowMeshes();

loaded.TransferOwnership(o =>
loader.TransferOwnership(o =>
{
#if VRM_DEVELOP
Debug.Log($"[{o.GetType().Name}] {o.name} will not destroy");
#endif

context.AddObjectToAsset(o.name, o);
if (o is GameObject)
{
// Root GameObject is main object
context.SetMainObject(loaded.Root);
context.SetMainObject(loader.Root);
}

return true;
Expand All @@ -74,7 +69,7 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
GltfParser parser, UnityPath dir)
{
var used = new HashSet<Texture2D>();
foreach (var texParam in GltfTextureEnumerator.Enumerate(parser))
foreach (var texParam in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser))
{
switch (texParam.TextureType)
{
Expand All @@ -96,8 +91,10 @@ public static void Import(ScriptedImporter scriptedImporter, AssetImportContext
{
// exclude. skip
}
else{
if(used.Add(asset)){
else
{
if (used.Add(asset))
{
yield return (asset.name, asset);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static DirectoryInfo SafeCreateDirectory(string path)
/// <param name="dirName"></param>
/// <param name="onCompleted"></param>
public static void ExtractTextures(GltfParser parser, UnityPath textureDirectory,
TextureEnumerator textureEnumerator, Texture2D[] subAssets, Action<Texture2D> addRemap,
EnumerateAllTexturesDistinctFunc textureEnumerator, Texture2D[] subAssets, Action<Texture2D> addRemap,
Action<IEnumerable<UnityPath>> onCompleted = null)
{
var extractor = new TextureExtractor(parser, textureDirectory, subAssets);
Expand Down
46 changes: 46 additions & 0 deletions Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,51 @@ public static (string, List<glTFBuffer>) ToGltf(this glTF self, string gltfPath)
self.RemoveUnusedExtensions(json);
return (json, self.buffers);
}

public static bool IsGeneratedUniGLTFAndOlderThan(string generatorVersion, int major, int minor)
{
if (string.IsNullOrEmpty(generatorVersion)) return false;
if (generatorVersion == "UniGLTF") return true;
if (!generatorVersion.FastStartsWith("UniGLTF-")) return false;

try
{
var splitted = generatorVersion.Substring(8).Split('.');
var generatorMajor = int.Parse(splitted[0]);
var generatorMinor = int.Parse(splitted[1]);

if (generatorMajor < major)
{
return true;
}
else if (generatorMajor > major)
{
return false;
}
else
{
if (generatorMinor >= minor)
{
return false;
}
else
{
return true;
}
}
}
catch (Exception ex)
{
Debug.LogWarningFormat("{0}: {1}", generatorVersion, ex);
return false;
}
}

public static bool IsGeneratedUniGLTFAndOlder(this glTF gltf, int major, int minor)
{
if (gltf == null) return false;
if (gltf.asset == null) return false;
return IsGeneratedUniGLTFAndOlderThan(gltf.asset.generator, major, minor);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;


namespace UniGLTF
Expand Down Expand Up @@ -27,9 +26,11 @@ public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) wh
throw new NotImplementedException();
}

public ArraySegment<byte> GetBytes()
public void ExtendCapacity(int capacity)
{
return m_bytes;
throw new NotImplementedException();
}

public ArraySegment<byte> Bytes => m_bytes;
}
}
5 changes: 3 additions & 2 deletions Assets/UniGLTF/Runtime/UniGLTF/Format/IBytesBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace UniGLTF
public interface IBytesBuffer
{
string Uri { get; }
ArraySegment<Byte> GetBytes();
glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct;
ArraySegment<Byte> Bytes { get; }
glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target = glBufferTarget.NONE) where T : struct;
void ExtendCapacity(int capacity);
}
}
13 changes: 7 additions & 6 deletions Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ namespace UniGLTF
[Serializable]
public class glTFBuffer
{
IBytesBuffer Storage;
IBytesBuffer m_buffer;
public IBytesBuffer Buffer => m_buffer;

public void OpenStorage(IStorage storage)
{
Storage = new ArraySegmentByteBuffer(storage.Get(uri));
m_buffer = new ArraySegmentByteBuffer(storage.Get(uri));
}

public glTFBuffer()
Expand All @@ -20,7 +21,7 @@ public glTFBuffer()

public glTFBuffer(IBytesBuffer storage)
{
Storage = storage;
m_buffer = storage;
}

public string uri;
Expand All @@ -39,14 +40,14 @@ public glTFBufferView Append<T>(T[] array, glBufferTarget target) where T : stru
}
public glTFBufferView Append<T>(ArraySegment<T> segment, glBufferTarget target) where T : struct
{
var view = Storage.Extend(segment, target);
byteLength = Storage.GetBytes().Count;
var view = m_buffer.Extend(segment, target);
byteLength = m_buffer.Bytes.Count;
return view;
}

public ArraySegment<Byte> GetBytes()
{
return Storage.GetBytes();
return m_buffer.Bytes;
}
}

Expand Down
98 changes: 98 additions & 0 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/ArrayByteBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Runtime.InteropServices;


namespace UniGLTF
{
/// <summary>
/// for exporter
/// </summary>
public class ArrayByteBuffer : IBytesBuffer
{
public string Uri
{
get;
private set;
}

Byte[] m_bytes;
int m_used;

public ArrayByteBuffer(Byte[] bytes = null)
{
Uri = "";
m_bytes = bytes;
}

public glTFBufferView Extend<T>(ArraySegment<T> array, glBufferTarget target) where T : struct
{
using (var pin = Pin.Create(array))
{
var elementSize = Marshal.SizeOf(typeof(T));
var view = Extend(pin.Ptr, array.Count * elementSize, elementSize, target);
return view;
}
}

public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarget target)
{
var tmp = m_bytes;
// alignment
var padding = m_used % stride == 0 ? 0 : stride - m_used % stride;

if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length)
{
// recreate buffer
m_bytes = new Byte[m_used + padding + bytesLength];
if (m_used > 0)
{
Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used);
}
}
if (m_used + padding + bytesLength > m_bytes.Length)
{
throw new ArgumentOutOfRangeException();
}

Marshal.Copy(p, m_bytes, m_used + padding, bytesLength);
var result = new glTFBufferView
{
buffer = 0,
byteLength = bytesLength,
byteOffset = m_used + padding,
byteStride = stride,
target = target,
};
m_used = m_used + padding + bytesLength;
return result;
}

public void ExtendCapacity(int capacity)
{
if (m_bytes != null && capacity < m_bytes.Length)
{
return;
}

var newBuffer = new byte[capacity];
if (m_bytes != null && m_used > 0)
{
Buffer.BlockCopy(m_bytes, 0, newBuffer, 0, m_used);
}
m_bytes = newBuffer;
}

public ArraySegment<byte> Bytes
{
get
{
if (m_bytes == null)
{
return new ArraySegment<byte>();
}

return new ArraySegment<byte>(m_bytes, 0, m_used);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading