diff --git a/Assets/SceneLoader/Prefabs/SimScene.prefab b/Assets/SceneLoader/Prefabs/SimScene.prefab index 73d5337..9f11fa8 100644 --- a/Assets/SceneLoader/Prefabs/SimScene.prefab +++ b/Assets/SceneLoader/Prefabs/SimScene.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 2085763002845073359} - component: {fileID: 1817450283232457565} - component: {fileID: 5552327243606421420} + - component: {fileID: 2865823669208674190} - component: {fileID: 7136934837751640320} m_Layer: 0 m_Name: SimScene @@ -59,6 +60,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 43b973fe0a32ff894963acb23af7fd0e, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &2865823669208674190 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1715612398501171564} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9b9354a0ff8c2bc459f4fb7b784dcebf, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!114 &7136934837751640320 MonoBehaviour: m_ObjectHideFlags: 0 @@ -72,4 +85,6 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: lightIntensity: 0.5 + lightHeight: 10 + lightOffset: 10 lightColor: {r: 1, g: 1, b: 1, a: 1} diff --git a/Assets/SceneLoader/Scripts/DeformObjectsController.cs b/Assets/SceneLoader/Scripts/DeformObjectsController.cs new file mode 100644 index 0000000..b278413 --- /dev/null +++ b/Assets/SceneLoader/Scripts/DeformObjectsController.cs @@ -0,0 +1,81 @@ +using IRXR.Node; +using IRXR.SceneLoader; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using UnityEngine; + +[RequireComponent(typeof(SimLoader))] +public class DeformObjectsController : MonoBehaviour +{ + public Dictionary> _objectsMeshes; + private Subscriber _subscriber; + + void Start() + { + gameObject.GetComponent().OnSceneLoaded += StartSubscription; + gameObject.GetComponent().OnSceneCleared += StopSubscription; + _subscriber = new Subscriber("DeformUpdate", SubscribeCallback); + } + + public void StartSubscription() + { + _objectsMeshes = gameObject.GetComponent().GetObjectMeshes(); + Debug.Log("Start Update Deform"); + _subscriber.StartSubscription(); + } + + public void StopSubscription() + { + _subscriber.Unsubscribe(); + } + + public void SubscribeCallback(byte[] streamMsg) + { + // Decode update message + // Structure: + // + // L: Length of update string containing all deform meshes [ 4 bytes ] + // S: Update string, semicolon seperated list of prims contained in thisupdate [ ? bytes ] + // N: Number of verticies for each mesh in update string [ num_meshes x 4 bytes] + // V: Verticies for each mesh [ ? bytes for each mesh ] + // + // | L | S ... S | N ... N | V ... V | + // + + ReadOnlySpan msg = new ReadOnlySpan(streamMsg); + Int32 updateListEndPos = BitConverter.ToInt32(msg.Slice(0, sizeof(Int32))); // L + + if (updateListEndPos == 0) + return; + + string updateListContents = Encoding.UTF8.GetString(streamMsg.Skip(sizeof(Int32)).Take(updateListEndPos).ToArray()); // S + + if (updateListContents[updateListContents.Length - 1] == ';') + updateListContents = updateListContents.Remove(updateListContents.Length - 1); + + string[] updateList = updateListContents.Split(';'); + + Int32[] meshVertSizes = MemoryMarshal.Cast(msg.Slice(updateListEndPos + sizeof(Int32), updateList.Length * sizeof(Int32))).ToArray(); // N + + int currentPos = updateListEndPos + (updateList.Length + 1) * sizeof(Int32); + for (int i = 0; i < updateList.Length; i++) + { + Vector3[] updatedVerticies = MemoryMarshal.Cast(msg.Slice(currentPos, meshVertSizes[i] * sizeof(float))).ToArray(); // V + List meshFilters; + if (_objectsMeshes.TryGetValue(updateList[i], out meshFilters)) + { + if (meshFilters.Count > 0) + { + // first entry should be complete mesh, even if sub components exist + MeshFilter meshFilter = meshFilters.First(); + meshFilter.mesh.vertices = updatedVerticies; + meshFilter.mesh.RecalculateNormals(); + } + } + currentPos += meshVertSizes[i] * sizeof(float); + } + } +} \ No newline at end of file diff --git a/Assets/SceneLoader/Scripts/DeformObjectsController.cs.meta b/Assets/SceneLoader/Scripts/DeformObjectsController.cs.meta new file mode 100644 index 0000000..018b19b --- /dev/null +++ b/Assets/SceneLoader/Scripts/DeformObjectsController.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 9b9354a0ff8c2bc459f4fb7b784dcebf \ No newline at end of file diff --git a/Assets/SceneLoader/Scripts/SimLoader.cs b/Assets/SceneLoader/Scripts/SimLoader.cs index 49b44e5..1914a45 100644 --- a/Assets/SceneLoader/Scripts/SimLoader.cs +++ b/Assets/SceneLoader/Scripts/SimLoader.cs @@ -21,6 +21,7 @@ public class SimLoader : MonoBehaviour private GameObject _simSceneObj; private SimScene _simScene; private Dictionary _simObjTrans = new(); + private Dictionary> _simObjMeshes = new(); private Dictionary>> _pendingMesh = new(); private Dictionary>> _pendingTexture = new(); // Services @@ -130,6 +131,7 @@ GameObject CreateObject(Transform root, SimBody body) { GameObject VisualContainer = new GameObject($"{body.name}_Visuals"); VisualContainer.transform.SetParent(bodyRoot.transform, false); + _simObjMeshes.Add(body.name, new()); foreach (SimVisual visual in body.visuals) { GameObject visualObj; @@ -144,6 +146,7 @@ GameObject CreateObject(Transform root, SimBody body) _pendingMesh[simMesh.hash] = new List>(); } _pendingMesh[simMesh.hash].Add(new(simMesh, visualObj.GetComponent())); + _simObjMeshes[body.name].Add(visualObj.GetComponent()); break; } case "CUBE": @@ -279,5 +282,9 @@ public GameObject GetSimObject() return _simSceneObj; } + public Dictionary> GetObjectMeshes() + { + return _simObjMeshes; + } } } \ No newline at end of file