Skip to content

Commit

Permalink
Make GLTFUtility shaders work in URP and HDRP. Don't create a new Uni…
Browse files Browse the repository at this point in the history
…ty game object when one exists already. (#52)

* update code and shadergraphs of GLTFUtility to support rendering in URP/HDRP

* dont create game object or download assets for objects that already exist in unity scene and bug fixes for hdrp/urp
  • Loading branch information
EdwardLu2018 authored Jan 24, 2023
1 parent 729dba4 commit 8ec7515
Show file tree
Hide file tree
Showing 8 changed files with 1,349 additions and 59 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

ARENA-unity notable changes. Started 2021-11-30 (version 0.0.1).

## [0.6.0](https://github.com/arenaxr/arena-unity/compare/v0.5.1...v0.6.0) (2022-11-21)
## [0.6.1](https://github.com/arenaxr/arena-unity/compare/v0.6.0...v0.6.1) (2022-12-22)

### Features

* **glTFUtility:** add support for URP/HDRP
* **object:** don't create (or download assests for) GameObjects that already exist in the Unity scene.

## [0.6.0](https://github.com/arenaxr/arena-unity/compare/v0.5.1...v0.6.0) (2022-11-21)

### Features

Expand Down
71 changes: 51 additions & 20 deletions Runtime/ArenaClientScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Rendering;

namespace ArenaUnity
{
Expand Down Expand Up @@ -87,14 +88,22 @@ protected override void Awake()
static readonly string[] msgUriTags = { "url", "src", "overrideSrc", "detailedUrl", "headModelPath" };
static readonly string[] gltfUriTags = { "uri" };
static readonly string[] skipMimeClasses = { "video", "audio" };
static readonly string[] requiredShaders = {
static readonly string[] requiredShadersStandardRP = {
"Standard",
"Unlit/Color",
"GLTFUtility/Standard (Metallic)",
"GLTFUtility/Standard Transparent (Metallic)",
"GLTFUtility/Standard (Specular)",
"GLTFUtility/Standard Transparent (Specular)",
};
static readonly string[] requiredShadersURPHDRP = {
// "Standard",
// "Unlit/Color",
"GLTFUtility/URP/Standard (Metallic)",
"GLTFUtility/URP/Standard Transparent (Metallic)",
"GLTFUtility/URP/Standard (Specular)",
"GLTFUtility/URP/Standard Transparent (Specular)",
};
static public string HandLeftPath = "static/models/hands/valve_index_left.gltf";
static public string HandRightPath = "static/models/hands/valve_index_right.gltf";

Expand Down Expand Up @@ -127,6 +136,11 @@ protected override void Start()
{
importPath = Path.Combine(appFilesPath, "Assets", "ArenaUnity", "import");

var requiredShaders = requiredShadersStandardRP;
// check if URP or HDR; different shaders are required
if (GraphicsSettings.renderPipelineAsset)
requiredShaders = requiredShadersURPHDRP;

// ensure shaders are in project
foreach (string shader in requiredShaders)
{
Expand Down Expand Up @@ -295,15 +309,20 @@ private IEnumerator SceneLoadPersist()
string object_id = (string)msg.object_id;
string msg_type = (string)msg.type;
float ttl = isElement(msg.ttl) ? (float)msg.ttl : 0f;

if (!arenaObjs.ContainsKey(object_id)) // do not duplicate, local project object takes priority
{
IEnumerable<string> uris = ExtractAssetUris(msg.attributes, msgUriTags);
foreach (var uri in uris)
// there isnt already an object in the scene created by the user with the same object_id
if (GameObject.Find((string)(msg.object_id)) == null)
{
if (!string.IsNullOrWhiteSpace(uri))
IEnumerable<string> uris = ExtractAssetUris(msg.attributes, msgUriTags);
foreach (var uri in uris)
{
cd = new CoroutineWithData(this, DownloadAssets(msg_type, uri));
yield return cd.coroutine;
if (!string.IsNullOrWhiteSpace(uri))
{
cd = new CoroutineWithData(this, DownloadAssets(msg_type, uri));
yield return cd.coroutine;
}
}
}
CreateUpdateObject(object_id, msg_type, persist, ttl, msg.attributes);
Expand Down Expand Up @@ -496,8 +515,14 @@ private void CreateUpdateObject(string object_id, string storeType, bool persist
#if !UNITY_EDITOR
Debug.Log($"Loading object '{object_id}'..."); // show new objects in log
#endif
gobj = new GameObject();
gobj.name = object_id;
// check if theres already an object in unity, if so dont make a new one
gobj = GameObject.Find((string)object_id);
if (gobj == null)
{
gobj = new GameObject();
gobj.name = object_id;
}

arenaObjs.Add(object_id, gobj);
aobj = gobj.AddComponent(typeof(ArenaObject)) as ArenaObject;
aobj.Created = true;
Expand Down Expand Up @@ -952,31 +977,37 @@ private IEnumerator ProcessArenaMessage(dynamic msg, object menuCommand = null)
{
case "create":
case "update":
IEnumerable<string> uris = ExtractAssetUris(msg.data, msgUriTags);
foreach (var uri in uris)
string object_id = (string)msg.object_id;
string msg_type = (string)msg.type;
float ttl = isElement(msg.ttl) ? (float)msg.ttl : 0f;
bool persist = Convert.ToBoolean(msg.persist);
string displayName = (string)msg.displayName;

// there isnt already an object in the scene created by the user with the same object_id
if (GameObject.Find((string)object_id) == null)
{
if (!string.IsNullOrWhiteSpace(uri))
IEnumerable<string> uris = ExtractAssetUris(msg.data, msgUriTags);
foreach (var uri in uris)
{
cd = new CoroutineWithData(this, DownloadAssets((string)msg.type, uri));
yield return cd.coroutine;
if (!string.IsNullOrWhiteSpace(uri))
{
cd = new CoroutineWithData(this, DownloadAssets(msg_type, uri));
yield return cd.coroutine;
}
}
}

switch ((string)msg.data.object_type)
{
case "handLeft":
cd = new CoroutineWithData(this, DownloadAssets((string)msg.type, HandLeftPath));
cd = new CoroutineWithData(this, DownloadAssets(msg_type, HandLeftPath));
yield return cd.coroutine;
break;
case "handRight":
cd = new CoroutineWithData(this, DownloadAssets((string)msg.type, HandRightPath));
cd = new CoroutineWithData(this, DownloadAssets(msg_type, HandRightPath));
yield return cd.coroutine;
break;
}
string object_id = (string)msg.object_id;
string msg_type = (string)msg.type;
float ttl = isElement(msg.ttl) ? (float)msg.ttl : 0f;
bool persist = Convert.ToBoolean(msg.persist);
string displayName = (string)msg.displayName;
CreateUpdateObject(object_id, msg_type, persist, ttl, msg.data, displayName, menuCommand);
break;
case "delete":
Expand Down
20 changes: 15 additions & 5 deletions Runtime/ArenaUnity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace ArenaUnity
/// </summary>
public static class ArenaUnity
{
private static string ColorPropertyName = (!GraphicsSettings.renderPipelineAsset ? "_Color" : "_BaseColor");

private static float ArenaFloat(float n) { return (float)Math.Round(n, 3); }

// time
Expand Down Expand Up @@ -50,6 +52,7 @@ public static string ToArenaObjectType(GameObject gobj)
objectType = meshFilter.sharedMesh.name.ToLower();
return objectType;
}

public static void ToUnityMesh(dynamic indata, ref GameObject gobj)
{
dynamic data;
Expand Down Expand Up @@ -730,7 +733,7 @@ public static void ToArenaMaterial(GameObject obj, ref dynamic data)
data.material.shader = "standard";
//data.url = ToArenaTexture(mat);
//data.material.repeat = ArenaFloat(mat.mainTextureScale.x);
if (mat.HasProperty("_Color"))
if (mat.HasProperty(ColorPropertyName))
data.material.color = ToArenaColor(mat.color);
//data.material.side = "double";
}
Expand All @@ -750,19 +753,26 @@ public static void ToUnityMaterial(dynamic data, ref GameObject gobj)
}
// object material
var material = renderer.material;
if (GraphicsSettings.renderPipelineAsset)
{
if (GraphicsSettings.renderPipelineAsset.GetType().ToString().Contains("HDRenderPipelineAsset"))
material.shader = Shader.Find("HDRP/Lit");
else
material.shader = Shader.Find("Universal Render Pipeline/Lit");
}
// legacy color overrides material color in the arena
if (data.color != null) // support legacy arena color
material.SetColor("_Color", ToUnityColor((string)data.color));
material.SetColor(ColorPropertyName, ToUnityColor((string)data.color));
else if (data.material != null && data.material.color != null)
material.SetColor("_Color", ToUnityColor((string)data.material.color));
material.SetColor(ColorPropertyName, ToUnityColor((string)data.material.color));
if (data.material != null)
{
if (data.material.shader != null)
material.shader.name = (string)data.material.shader == "flat" ? "Unlit/Color" : "Standard";
if (data.material.opacity != null)
{
Color c = material.GetColor("_Color");
material.SetColor("_Color", new Color(c.r, c.g, c.b, (float)data.material.opacity));
Color c = material.GetColor(ColorPropertyName);
material.SetColor(ColorPropertyName, new Color(c.r, c.g, c.b, (float)data.material.opacity));
}
if (data.material.transparent != null)
{
Expand Down
Loading

0 comments on commit 8ec7515

Please sign in to comment.