Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion VRM_VisualScriptingNodes/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ crashlytics-build.properties
# For this specific project
/[Aa]ssets/temp/*
/[Aa]ssets/temp.meta

/[Aa]ssets/_temp/*
/[Aa]ssets/_temp.meta
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ private IEnumerator Enter(Flow flow)
yield return new WaitUntil(() => vrmInstance);
resultValue = vrmInstance.gameObject;

if (Utils.IsVisionOS()) Utils.ChangeShadersWithTexture(resultValue, "Universal Render Pipeline/Unlit", "_MainTex", "_BaseMap");
// Change shaders of VRM to Unlit if platform is VisionOS
if (Utils.IsVisionOS()) Utils.ChangeMtoon10ShaderToUnlitOfGameobject(resultValue);

yield return outputTrigger;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using UnityEngine;
using System;
using UnityEngine.Networking;
using System.Runtime.CompilerServices;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using Unity.VisualScripting;
using System.Collections.Generic;

Expand All @@ -19,18 +14,12 @@ public static bool IsVisionOS()
{
//When operatingSystem is "visionOS", return true
if (SystemInfo.operatingSystem.Contains("visionOS")) return true;

//When VolumeCamera exists in scene, return true
foreach (GameObject obj in GetAllObjectsInScene())
{
Component[] components = obj.GetComponents<Component>();
foreach (Component component in components)
{
if (component.GetType().Name=="VolumeCamera") return true;
}
}

if (Application.platform == RuntimePlatform.VisionOS) return true;
#if UNITY_VISIONOS
return true;
#else
return false;
#endif
}

/// <summary>
Expand All @@ -57,8 +46,6 @@ static void GetChildObjects(GameObject obj, ref List<GameObject> objectsInScene)
}
}



/// <summary>
/// Change all shaders of GameObject to new one.
/// This method is intended to be used for VRM and glTF models on VisionOS.
Expand Down Expand Up @@ -89,5 +76,98 @@ void ChangeShader(Material mat, string shaderName)
}
}
}

/// <summary>
/// Change all URP/MToon10 shaders of GameObject to URP/Unlit.
/// This method is intended to be used for VRM models on VisionOS.
/// </summary>
/// <param name="targetObject"></param>
public static void ChangeMtoon10ShaderToUnlitOfGameobject(GameObject targetObject)
{
ChangeToUnlitMaterialsRecursive(targetObject.transform);
static void ChangeToUnlitMaterialsRecursive(Transform target)
{
if (target.TryGetComponent<Renderer>(out var renderer))
{
var materialsCopy = renderer.materials;
for (int i = 0; i < materialsCopy.Length; i++)
{
materialsCopy[i] = GetUnlitMaterialMadeByMToon10Shader(materialsCopy[i]);
}
renderer.materials = materialsCopy;
}
foreach (Transform child in target)
{
ChangeToUnlitMaterialsRecursive(child);
}
}
}

/// <summary>
/// Change URP/MToon10 shader to URP/Unlit shader.
/// </summary>
/// <param name="mat_original"></param>
static Material GetUnlitMaterialMadeByMToon10Shader(Material mat_original)
{
if (mat_original.shader.name != "VRM10/Universal Render Pipeline/MToon10")
{
return mat_original; // Exit if the shader is not MToon10
}

// Create a temporary copy of the material to extract properties
Material tempMat = new(mat_original);

// Create a new material with the Unlit shader
Material newMat = new(Shader.Find("Universal Render Pipeline/Unlit"));

// Handle Surface Type (Opaque/Transparent)
bool isTransparent = tempMat.GetFloat("_AlphaMode") > 0 || tempMat.GetFloat("_TransparentWithZWrite") > 0;

if (isTransparent)
{
// Set surface type to Transparent and enable Alpha Clipping
newMat.SetFloat("_Surface", 1.0f); // Set to Transparent
newMat.SetFloat("_AlphaClip", 1.0f); // Enable Alpha Clipping

// Set blend modes for transparent materials
newMat.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.SrcAlpha);
newMat.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
newMat.SetFloat("_ZWrite", 0.0f); // Typically, ZWrite is off for transparent materials
}
else
{
// Set surface type to Opaque and disable Alpha Clipping
newMat.SetFloat("_Surface", 0.0f); // Set to Opaque
newMat.SetFloat("_AlphaClip", 0.0f); // Disable Alpha Clipping
}

// Transfer texture and color properties
if (tempMat.HasProperty("_MainTex") && newMat.HasProperty("_BaseMap"))
{
newMat.SetTexture("_BaseMap", tempMat.GetTexture("_MainTex"));
}
if (tempMat.HasProperty("_Color") && newMat.HasProperty("_BaseColor"))
{
newMat.SetColor("_BaseColor", tempMat.GetColor("_Color"));
}

// Transfer alpha cutoff property
if (tempMat.HasProperty("_Cutoff"))
{
newMat.SetFloat("_Cutoff", tempMat.GetFloat("_Cutoff"));
}

// Dispose of the temporary material
DestroyImmediate(tempMat);

// Additional blend mode settings can be adjusted here if needed

// Set shader again (I don't know why this is necessary, but it doesn't work without it)
newMat.shader = Shader.Find("Universal Render Pipeline/Unlit");

return newMat;
}


}
}