mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-07-14 23:24:09 -05:00
ScriptedImporterImpl.cs
This commit is contained in:
parent
2ff647ec57
commit
07b2bd0aa9
|
|
@ -1,5 +1,3 @@
|
|||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Experimental.AssetImporters;
|
||||
using UnityEngine;
|
||||
|
||||
|
|
@ -12,100 +10,16 @@ namespace UniGLTF
|
|||
[SerializeField]
|
||||
Axises m_reverseAxis = default;
|
||||
|
||||
const string MaterialDirName = "Materials";
|
||||
|
||||
public override void OnImportAsset(AssetImportContext ctx)
|
||||
{
|
||||
Debug.Log("OnImportAsset to " + ctx.assetPath);
|
||||
|
||||
try
|
||||
{
|
||||
// Parse
|
||||
var parser = new GltfParser();
|
||||
parser.ParsePath(ctx.assetPath);
|
||||
|
||||
// Build Unity Model
|
||||
var externalObjectMap = GetExternalObjectMap()
|
||||
.Select(kv => (kv.Key.name, kv.Value))
|
||||
;
|
||||
|
||||
var context = new ImporterContext(parser, null, externalObjectMap);
|
||||
context.InvertAxis = m_reverseAxis;
|
||||
context.Load();
|
||||
context.ShowMeshes();
|
||||
|
||||
// Texture
|
||||
foreach (var info in context.TextureFactory.Textures)
|
||||
{
|
||||
if (!info.IsUsed)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!info.IsExternal)
|
||||
{
|
||||
var texture = info.Texture;
|
||||
ctx.AddObjectToAsset(texture.name, texture);
|
||||
}
|
||||
}
|
||||
|
||||
// Material
|
||||
foreach (var info in context.MaterialFactory.Materials)
|
||||
{
|
||||
if (!info.UseExternal)
|
||||
{
|
||||
var material = info.Asset;
|
||||
ctx.AddObjectToAsset(material.name, material);
|
||||
}
|
||||
}
|
||||
|
||||
// Mesh
|
||||
foreach (var mesh in context.Meshes.Select(x => x.Mesh))
|
||||
{
|
||||
ctx.AddObjectToAsset(mesh.name, mesh);
|
||||
}
|
||||
|
||||
// Animation
|
||||
foreach (var clip in context.AnimationClips)
|
||||
{
|
||||
ctx.AddObjectToAsset(clip.name, clip);
|
||||
}
|
||||
|
||||
// Root
|
||||
ctx.AddObjectToAsset(context.Root.name, context.Root);
|
||||
ctx.SetMainObject(context.Root);
|
||||
ScriptedImporterImpl.Import(this, ctx, m_reverseAxis);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void ExtractMaterialsAndTextures()
|
||||
{
|
||||
if (string.IsNullOrEmpty(assetPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TextureExtractor.ExtractTextures(assetPath,
|
||||
this.GetSubAssets<UnityEngine.Texture2D>(this.assetPath).ToArray(),
|
||||
externalObject =>
|
||||
{
|
||||
this.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject);
|
||||
},
|
||||
() =>
|
||||
{
|
||||
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
|
||||
this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
|
||||
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
|
||||
});
|
||||
}
|
||||
|
||||
public void SetExternalUnityObject<T>(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object
|
||||
{
|
||||
this.AddRemap(sourceAssetIdentifier, obj);
|
||||
AssetDatabase.WriteImportSettingsIfDirty(this.assetPath);
|
||||
AssetDatabase.ImportAsset(this.assetPath, ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,5 +82,35 @@ namespace UniGLTF
|
|||
ExtractFromAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false);
|
||||
}
|
||||
}
|
||||
|
||||
const string MaterialDirName = "Materials";
|
||||
|
||||
public static void ExtractMaterialsAndTextures(this ScriptedImporter self)
|
||||
{
|
||||
if (string.IsNullOrEmpty(self.assetPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TextureExtractor.ExtractTextures(self.assetPath,
|
||||
self.GetSubAssets<UnityEngine.Texture2D>(self.assetPath).ToArray(),
|
||||
externalObject =>
|
||||
{
|
||||
self.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(UnityEngine.Texture2D), externalObject.name), externalObject);
|
||||
},
|
||||
() =>
|
||||
{
|
||||
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
|
||||
self.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
|
||||
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
|
||||
});
|
||||
}
|
||||
|
||||
public static void SetExternalUnityObject<T>(this ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object
|
||||
{
|
||||
self.AddRemap(sourceAssetIdentifier, obj);
|
||||
AssetDatabase.WriteImportSettingsIfDirty(self.assetPath);
|
||||
AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
|
||||
using System.Linq;
|
||||
using UnityEditor.Experimental.AssetImporters;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public static class ScriptedImporterImpl
|
||||
{
|
||||
public static void Import(ScriptedImporter scriptedImporter, AssetImportContext importerContext, Axises reverseAxis)
|
||||
{
|
||||
#if VRM_DEVELOP
|
||||
Debug.Log("OnImportAsset to " + importerContext.assetPath);
|
||||
#endif
|
||||
|
||||
//
|
||||
// Parse(parse glb, parser gltf json)
|
||||
//
|
||||
var parser = new GltfParser();
|
||||
parser.ParsePath(importerContext.assetPath);
|
||||
|
||||
//
|
||||
// Import(create unity objects)
|
||||
//
|
||||
var context = new ImporterContext(parser, null,
|
||||
scriptedImporter.GetExternalObjectMap()
|
||||
.Select(kv => (kv.Key.name, kv.Value))
|
||||
);
|
||||
context.InvertAxis = reverseAxis;
|
||||
context.Load();
|
||||
context.ShowMeshes();
|
||||
|
||||
//
|
||||
// SubAsset
|
||||
//
|
||||
|
||||
// Texture
|
||||
foreach (var info in context.TextureFactory.Textures)
|
||||
{
|
||||
if (info.IsSubAsset)
|
||||
{
|
||||
var texture = info.Texture;
|
||||
importerContext.AddObjectToAsset(texture.name, texture);
|
||||
}
|
||||
}
|
||||
|
||||
// Material
|
||||
foreach (var info in context.MaterialFactory.Materials)
|
||||
{
|
||||
if (info.IsSubAsset)
|
||||
{
|
||||
var material = info.Asset;
|
||||
importerContext.AddObjectToAsset(material.name, material);
|
||||
}
|
||||
}
|
||||
|
||||
// Mesh
|
||||
foreach (var mesh in context.Meshes.Select(x => x.Mesh))
|
||||
{
|
||||
// all mesh is subasset
|
||||
importerContext.AddObjectToAsset(mesh.name, mesh);
|
||||
}
|
||||
|
||||
// Animation
|
||||
foreach (var clip in context.AnimationClips)
|
||||
{
|
||||
// all animation is subasset
|
||||
importerContext.AddObjectToAsset(clip.name, clip);
|
||||
}
|
||||
|
||||
// Root GameObject is main object
|
||||
importerContext.AddObjectToAsset(context.Root.name, context.Root);
|
||||
importerContext.SetMainObject(context.Root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 85bf14b48ed135743828ee49a830b1c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -64,6 +64,8 @@ namespace UniGLTF
|
|||
public readonly Material Asset;
|
||||
public readonly bool UseExternal;
|
||||
|
||||
public bool IsSubAsset => !UseExternal;
|
||||
|
||||
public MaterialLoadInfo(Material asset, bool useExternal)
|
||||
{
|
||||
Asset = asset;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ namespace UniGLTF
|
|||
public bool IsUsed => Flags.HasFlag(TextureLoadFlags.Used);
|
||||
public bool IsExternal => Flags.HasFlag(TextureLoadFlags.External);
|
||||
|
||||
public bool IsSubAsset => IsUsed && !IsExternal;
|
||||
|
||||
public TextureLoadInfo(Texture2D texture, bool used, bool isExternal)
|
||||
{
|
||||
Texture = texture;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user