From 588ecf81cd3fdcabf474718c2ee22ce866f052a5 Mon Sep 17 00:00:00 2001 From: Masahiro Yamaguchi Date: Tue, 5 Dec 2023 19:27:27 +0900 Subject: [PATCH] Temporary commit --- .../Runtime/Nodes/LoadVRM.cs | 8 ++- .../Runtime/Utils/Utils.cs | 49 ++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Nodes/LoadVRM.cs b/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Nodes/LoadVRM.cs index 1807bc2..5c6e510 100644 --- a/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Nodes/LoadVRM.cs +++ b/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Nodes/LoadVRM.cs @@ -6,6 +6,7 @@ using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; using System.Collections; +using VisualScriptingNodes; namespace VrmVisualScriptingNodes { @@ -35,16 +36,19 @@ protected override void Definition() VrmURL = ValueInput("VRM URL", ""); result = ValueOutput("Game Object", (flow) => resultValue); - + } private IEnumerator Enter(Flow flow) { string url = flow.GetValue(VrmURL); Vrm10Instance vrmInstance = null; - UniTask.Create(async () => {vrmInstance = await LoadVrm(url);}).Forget(); + UniTask.Create(async () => { vrmInstance = await LoadVrm(url); }).Forget(); yield return new WaitUntil(() => vrmInstance); resultValue = vrmInstance.gameObject; + + Utils.ReplaceErrorShaders(resultValue, Utils.GetShaderForErrorReplacement()); + yield return outputTrigger; } diff --git a/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Utils/Utils.cs b/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Utils/Utils.cs index 7a06f6f..fb24dc0 100644 --- a/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Utils/Utils.cs +++ b/VRM_VisualScriptingNodes/Packages/com.from2001.vrm-visualscripting-nodes/Runtime/Utils/Utils.cs @@ -2,14 +2,59 @@ using System; using UnityEngine.Networking; using System.Runtime.CompilerServices; +using UnityEngine.Rendering; +using UnityEngine.Rendering.Universal; -namespace from2001 +namespace VisualScriptingNodes { public class Utils : MonoBehaviour { - static int addTest(int a, int b) + static public int addTest(int a, int b) { return a + b; } + + + + public static Shader GetShaderForErrorReplacement(string shaderType = "Unlit") + { + Shader retShader = null; + retShader = shaderType switch + { + "Unlit" => Shader.Find(GraphicsSettings.currentRenderPipeline is UniversalRenderPipelineAsset + ? "Universal Render Pipeline/Unlit" : "Unlit/Texture"), + "Lit" => Shader.Find(GraphicsSettings.currentRenderPipeline is UniversalRenderPipelineAsset + ? "Universal Render Pipeline/Lit" : "Standard"), + _ => Shader.Find(GraphicsSettings.currentRenderPipeline is UniversalRenderPipelineAsset + ? "Universal Render Pipeline/Unlit" : "Unlit/Texture"), + }; + return retShader; + } + + + // GameObjectとその子孫のマテリアルのシェーダーを再帰的に置き換えるヘルパーメソッド + public static void ReplaceErrorShaders(GameObject obj, Shader replacementShader) + { + if (obj.TryGetComponent(out var renderer)) + { + foreach (Material mat in renderer.materials) + { + //if (mat.shader == null || Shader.Find(mat.shader.name) == null) + //{ + mat.shader = replacementShader; + //} + } + } + + // 子GameObjectに対しても同じ処理を行う + foreach (Transform child in obj.transform) + { + ReplaceErrorShaders(child.gameObject, replacementShader); + } + } + + + + } } \ No newline at end of file