void OnValidate() { if (m_renderPipeline == UniGLTF.RenderPipelineTypes.UniversalRenderPipeline) { if (Shader.Find(UniGLTF.GltfPbrUrpMaterialImporter.ShaderName) == null) { Debug.LogWarning("URP is not installed. Force to BuiltinRenderPipeline"); m_renderPipeline = UniGLTF.RenderPipelineTypes.BuiltinRenderPipeline; } } }
static IMaterialDescriptorGenerator GetMaterialDescriptorGenerator(RenderPipelineTypes renderPipeline) { switch (renderPipeline) { case RenderPipelineTypes.BuiltinRenderPipeline: return(new Vrm10MaterialDescriptorGenerator()); case RenderPipelineTypes.UniversalRenderPipeline: return(new Vrm10UrpMaterialDescriptorGenerator()); default: throw new NotImplementedException(); } }
/// <summary> /// /// </summary> /// <param name="scriptedImporter"></param> /// <param name="context"></param> /// <param name="doMigrate">vrm0 だった場合に vrm1 化する</param> /// <param name="renderPipeline"></param> /// <param name="doNormalize">normalize する</param> public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, bool doMigrate, RenderPipelineTypes renderPipeline, bool doNormalize) { #if VRM_DEVELOP Debug.Log("OnImportAsset to " + scriptedImporter.assetPath); #endif // 1st parse as vrm1 using (var data = new GlbFileParser(scriptedImporter.assetPath).Parse()) { var vrm1Data = Vrm10Data.Parse(data); if (vrm1Data != null) { // successfully parsed vrm-1.0 Process(vrm1Data, scriptedImporter, context, renderPipeline, doNormalize); } if (!doMigrate) { return; } // try migration... MigrationData migration; using (var migrated = Vrm10Data.Migrate(data, out vrm1Data, out migration)) { if (vrm1Data != null) { Process(vrm1Data, scriptedImporter, context, renderPipeline, doNormalize); } } // fail to migrate... if (migration != null) { Debug.LogWarning(migration.Message); } return; } }
static void Process(Vrm10Data result, ScriptedImporter scriptedImporter, AssetImportContext context, RenderPipelineTypes renderPipeline, bool doNormalize) { // // Import(create unity objects) // var extractedObjects = scriptedImporter.GetExternalObjectMap() .Where(kv => kv.Value != null) .ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Key.name), kv => kv.Value); var materialGenerator = GetMaterialDescriptorGenerator(renderPipeline); using (var loader = new Vrm10Importer(result, extractedObjects, materialGenerator: materialGenerator, doNormalize: doNormalize)) { // settings TextureImporters foreach (var textureInfo in loader.TextureDescriptorGenerator.Get().GetEnumerable()) { VRMShaders.TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalTextures); } var loaded = loader.Load(); loaded.ShowMeshes(); loaded.TransferOwnership((key, o) => { context.AddObjectToAsset(key.Name, o); }); var root = loaded.Root; GameObject.DestroyImmediate(loaded); context.AddObjectToAsset(root.name, root); context.SetMainObject(root); } }
/// <summary> /// /// </summary> /// <param name="scriptedImporter"></param> /// <param name="context"></param> /// <param name="migrateToVrm1">vrm0 だった場合に vrm1 化する</param> /// <param name="renderPipeline"></param> /// <param name="doNormalize">normalize する</param> public static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, bool migrateToVrm1, RenderPipelineTypes renderPipeline, bool doNormalize) { #if VRM_DEVELOP Debug.Log("OnImportAsset to " + scriptedImporter.assetPath); #endif using (var data = Vrm10Data.ParseOrMigrate(scriptedImporter.assetPath, migrateToVrm1, out Vrm10Data result, out MigrationData migration)) { if (result == null) { // fail to parse vrm1 return; } // // Import(create unity objects) // var extractedObjects = scriptedImporter.GetExternalObjectMap() .Where(kv => kv.Value != null) .ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Key.name), kv => kv.Value); var materialGenerator = GetMaterialDescriptorGenerator(renderPipeline); using (var loader = new Vrm10Importer(result, extractedObjects, materialGenerator: materialGenerator, doNormalize: doNormalize)) { // settings TextureImporters foreach (var textureInfo in loader.TextureDescriptorGenerator.Get().GetEnumerable()) { VRMShaders.TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalTextures); } var loaded = loader.Load(); loaded.ShowMeshes(); loaded.TransferOwnership((key, o) => { context.AddObjectToAsset(key.Name, o); }); var root = loaded.Root; GameObject.DestroyImmediate(loaded); context.AddObjectToAsset(root.name, root); context.SetMainObject(root); } } }
/// <summary> /// glb をパースして、UnityObject化、さらにAsset化する /// </summary> /// <param name="scriptedImporter"></param> /// <param name="context"></param> /// <param name="reverseAxis"></param> protected static void Import(ScriptedImporter scriptedImporter, AssetImportContext context, Axes reverseAxis, RenderPipelineTypes renderPipeline) { #if VRM_DEVELOP Debug.Log("OnImportAsset to " + scriptedImporter.assetPath); #endif // // Import(create unity objects) // // 2 回目以降の Asset Import において、 Importer の設定で Extract した UnityEngine.Object が入る var extractedObjects = scriptedImporter.GetExternalObjectMap() .Where(x => x.Value != null) .ToDictionary(kv => new SubAssetKey(kv.Value.GetType(), kv.Key.name), kv => kv.Value); IMaterialDescriptorGenerator materialGenerator = GetMaterialGenerator(renderPipeline); using (var data = new AutoGltfFileParser(scriptedImporter.assetPath).Parse()) using (var loader = new ImporterContext(data, extractedObjects, materialGenerator: materialGenerator)) { // Configure TextureImporter to Extracted Textures. foreach (var textureInfo in loader.TextureDescriptorGenerator.Get().GetEnumerable()) { TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalTextures); } loader.InvertAxis = reverseAxis; var loaded = loader.Load(); loaded.ShowMeshes(); loaded.TransferOwnership((k, o) => { context.AddObjectToAsset(k.Name, o); }); var root = loaded.Root; GameObject.DestroyImmediate(loaded); context.AddObjectToAsset(root.name, root); context.SetMainObject(root); } }