Skip to content

Commit

Permalink
fix: add empty transforms to the list of objects for making instances (
Browse files Browse the repository at this point in the history
…#903) (#906)

* Ensure parent objects are added when an object is created in a hierarchy with empty transforms.

* Fix for compiler error in yamato

* Refactoring to make the code more complicated

* Using existing method instead of new one

(cherry picked from commit 81304e7)

Co-authored-by: schinkowski <[email protected]>
  • Loading branch information
sindharta-tanuwijaya and schinkowski authored Apr 6, 2023
1 parent 16b29b9 commit efdbdf8
Showing 1 changed file with 87 additions and 16 deletions.
103 changes: 87 additions & 16 deletions Runtime/Scripts/BaseMeshSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1746,13 +1746,98 @@ private EntityRecord UpdatePointsEntity(PointsData data, MeshSyncPlayerConfig co
return rec;
}

static Transform FindOrCreateByPath(Transform parent, string path, Action<string> parentCreationCallback, bool worldPositionStays = true) {
string[] names = path.Split('/');
if (names.Length <= 0)
return null;

//if parent is null, search from root
Transform t = parent;
int tokenStartIdx = 0;
if (null == t) {
string rootGameObjectName = names[0];
t = FilmInternalUtilities.GameObjectUtility.FindFirstRoot(rootGameObjectName);
if (null == t) {
GameObject go = new GameObject(rootGameObjectName);
t = go.GetComponent<Transform>();
}

tokenStartIdx = 1;
}

static Transform FindOrCreateChild(Transform t, string childName, out bool didCreate, bool worldPositionStays = true) {
Transform childT = t.Find(childName);
if (null != childT) {
didCreate = false;
return childT;
}

GameObject go = new GameObject(childName);
childT = go.transform;
childT.SetParent(t, worldPositionStays);
didCreate = true;
return childT;
}

//loop over hierarchy of names and generate parents that don't exist
int nameLength = names.Length;
List<string> processedParents = new List<string>();
for (int i = tokenStartIdx; i < nameLength; ++i) {
string nameToken = names[i];
if (string.IsNullOrEmpty(nameToken))
continue;

processedParents.Add(nameToken);

t = FindOrCreateChild(t, nameToken, out var didCreate, worldPositionStays);
if (i < nameLength - 1 && didCreate) {
var parentPath = String.Join("/", processedParents);
if (!parentPath.StartsWith("/")) {
parentPath = $"/{parentPath}";
}

parentCreationCallback?.Invoke(parentPath);
}

if (null == t)
return null;
}

return t;
}

void AddClientObject(string path, out EntityRecord rec) {
if (m_clientObjects.TryGetValue(path, out rec))
if (rec.go == null) {
m_clientObjects.Remove(path);
rec = null;
}

if (rec == null) {
var trans = FindOrCreateByPath(m_rootObject, path,
delegate(string parentPath) {
EntityRecord parentRec = null;
AddClientObject(parentPath, out parentRec);
if (parentRec.dataType == EntityType.Unknown)
parentRec.dataType = EntityType.Transform;
},
false);

rec = new EntityRecord {
go = trans.gameObject,
trans = trans,
recved = true
};
m_clientObjects.Add(path, rec);
}
}

private EntityRecord UpdateTransformEntity(TransformData data, MeshSyncPlayerConfig config) {
string path = data.path;
int hostID = data.hostID;
if (path.Length == 0)
return null;

Transform trans = null;
EntityRecord rec = null;
if (hostID != Lib.invalidID) {
if (m_hostObjects.TryGetValue(hostID, out rec))
Expand All @@ -1765,21 +1850,7 @@ private EntityRecord UpdateTransformEntity(TransformData data, MeshSyncPlayerCon
return null;
}
else {
if (m_clientObjects.TryGetValue(path, out rec))
if (rec.go == null) {
m_clientObjects.Remove(path);
rec = null;
}

if (rec == null) {
trans = FilmInternalUtilities.GameObjectUtility.FindOrCreateByPath(m_rootObject, path, false);
rec = new EntityRecord {
go = trans.gameObject,
trans = trans,
recved = true
};
m_clientObjects.Add(path, rec);
}
AddClientObject(path, out rec);
}

return UpdateTransformEntity(data, config, rec);
Expand Down

0 comments on commit efdbdf8

Please sign in to comment.