Skip to content
This repository was archived by the owner on Oct 28, 2025. It is now read-only.
Open
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
15 changes: 15 additions & 0 deletions Assets/SceneLoader/Prefabs/SimScene.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
81 changes: 81 additions & 0 deletions Assets/SceneLoader/Scripts/DeformObjectsController.cs
Original file line number Diff line number Diff line change
@@ -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<string, List<MeshFilter>> _objectsMeshes;
private Subscriber<byte[]> _subscriber;

void Start()
{
gameObject.GetComponent<SimLoader>().OnSceneLoaded += StartSubscription;
gameObject.GetComponent<SimLoader>().OnSceneCleared += StopSubscription;
_subscriber = new Subscriber<byte[]>("DeformUpdate", SubscribeCallback);
}

public void StartSubscription()
{
_objectsMeshes = gameObject.GetComponent<SimLoader>().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<byte> msg = new ReadOnlySpan<byte>(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<byte, Int32>(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<byte, Vector3>(msg.Slice(currentPos, meshVertSizes[i] * sizeof(float))).ToArray(); // V
List<MeshFilter> 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);
}
}
}
2 changes: 2 additions & 0 deletions Assets/SceneLoader/Scripts/DeformObjectsController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Assets/SceneLoader/Scripts/SimLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SimLoader : MonoBehaviour
private GameObject _simSceneObj;
private SimScene _simScene;
private Dictionary<string, Transform> _simObjTrans = new();
private Dictionary<string, List<MeshFilter>> _simObjMeshes = new();
private Dictionary<string, List<Tuple<SimMesh, MeshFilter>>> _pendingMesh = new();
private Dictionary<string, List<Tuple<SimTexture, Material>>> _pendingTexture = new();
// Services
Expand Down Expand Up @@ -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;
Expand All @@ -144,6 +146,7 @@ GameObject CreateObject(Transform root, SimBody body)
_pendingMesh[simMesh.hash] = new List<Tuple<SimMesh, MeshFilter>>();
}
_pendingMesh[simMesh.hash].Add(new(simMesh, visualObj.GetComponent<MeshFilter>()));
_simObjMeshes[body.name].Add(visualObj.GetComponent<MeshFilter>());
break;
}
case "CUBE":
Expand Down Expand Up @@ -279,5 +282,9 @@ public GameObject GetSimObject()
return _simSceneObj;
}

public Dictionary<string, List<MeshFilter>> GetObjectMeshes()
{
return _simObjMeshes;
}
}
}