Skip to content

Commit

Permalink
Merge pull request #23 from from2001/develop
Browse files Browse the repository at this point in the history
Vision OS Support (Shader replacement)
  • Loading branch information
from2001 authored Dec 5, 2023
2 parents e7df6f2 + 68a6d66 commit e824f80
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ These samples can be imported.

![InstallSamples](https://github.com/from2001/VRM_VisualScriptingNodes/assets/387880/31c42fde-8b71-46e5-a4d5-a488015ca379)

## Vision OS Support

Shaders are replaced with `Universal Render Pipeline/Unlit` shader

## ToDo

Implement features of [VRM-1.0 APIs](https://vrm-c.github.io/UniVRM/ja/vrm1/api_update.html#expression)

- ~~Load~~
- ~~VisionOS Support (Partially supported)~~
- Improve Shader replacement for VisionOS Support
- Expression
- LookAt
- Gaze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using System.Collections;
using VisualScriptingNodes;

namespace VrmVisualScriptingNodes
{
Expand Down Expand Up @@ -35,16 +36,19 @@ protected override void Definition()

VrmURL = ValueInput<string>("VRM URL", "");
result = ValueOutput<GameObject>("Game Object", (flow) => resultValue);

}

private IEnumerator Enter(Flow flow)
{
string url = flow.GetValue<string>(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;

if (Utils.IsVisionOS()) Utils.ChangeShadersWithTexture(resultValue, "Universal Render Pipeline/Unlit", "_MainTex", "_BaseMap");

yield return outputTrigger;
}

Expand All @@ -62,6 +66,7 @@ private async UniTask<Vrm10Instance> LoadVrm(string URL)
{
VrmBytes = request.downloadHandler.data;
}

Vrm10Instance vrmInstance = await Vrm10.LoadBytesAsync(
VrmBytes,
canLoadVrm0X: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,92 @@
using System;
using UnityEngine.Networking;
using System.Runtime.CompilerServices;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using Unity.VisualScripting;
using System.Collections.Generic;

namespace from2001
namespace VisualScriptingNodes
{
public class Utils : MonoBehaviour
{
static int addTest(int a, int b)
/// <summary>
/// Return true if current OS is VisionOS.
/// </summary>
/// <returns></returns>
public static bool IsVisionOS()
{
return a + b;
//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;
}
}

return false;
}

/// <summary>
/// Return all GameObjects in scene.
/// </summary>
/// <returns></returns>
public static List<GameObject> GetAllObjectsInScene()
{
List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject obj in UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects())
{
objectsInScene.Add(obj);
GetChildObjects(obj, ref objectsInScene);
}
return objectsInScene;

static void GetChildObjects(GameObject obj, ref List<GameObject> objectsInScene)
{
foreach (Transform child in obj.transform)
{
objectsInScene.Add(child.gameObject);
GetChildObjects(child.gameObject, ref objectsInScene);
}
}
}



/// <summary>
/// Change all shaders of GameObject to new one.
/// This method is intended to be used for VRM and glTF models on VisionOS.
/// </summary>
/// <param name="targetObject"></param>
/// <param name="newShader">"Universal Render Pipeline/Unlit", "Universal Render Pipeline/Lit", etc.</param>
/// <param name="texturePropertyName_old">Set "_MainTex" for Built-in shader</param>
/// <param name="texturePropertyName_new">Set "_BaseMap" for URP Shader</param>
public static void ChangeShadersWithTexture(GameObject targetObject, string newShader = "Universal Render Pipeline/Unlit", string texturePropertyName_old = "_MainTex", string texturePropertyName_new = "_BaseMap")
{
SkinnedMeshRenderer[] skinedMeshrenderers = targetObject.GetComponentsInChildren<SkinnedMeshRenderer>();
foreach (SkinnedMeshRenderer renderer in skinedMeshrenderers) foreach (Material mat in renderer.materials) ChangeShader(mat, newShader);

MeshRenderer[] meshrenderers = targetObject.GetComponentsInChildren<MeshRenderer>();
foreach (MeshRenderer renderer in meshrenderers) foreach (Material mat in renderer.materials) ChangeShader(mat, newShader);

void ChangeShader(Material mat, string shaderName)
{
if (mat.HasProperty(texturePropertyName_old))
{
Texture texture = mat.GetTexture(texturePropertyName_old);
mat.shader = Shader.Find(shaderName);
mat.SetTexture(texturePropertyName_new, texture);
}
else
{
mat.shader = Shader.Find(newShader);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.from2001.vrm-visualscripting-nodes",
"version": "0.1.3",
"version": "0.1.4",
"displayName": "VRM Visual Scripting Nodes",
"description": "Unity Visual Scripting node library for VRM",
"unity": "2021.3",
Expand Down

0 comments on commit e824f80

Please sign in to comment.