From e68cc2d343b2d98738b3e053b804a48348d62ffe Mon Sep 17 00:00:00 2001 From: Winson Han Date: Fri, 9 Aug 2024 16:36:17 -0700 Subject: [PATCH 1/7] Update TestThirdPartyCameraAndMainCamera.cs --- .../TestThirdPartyCameraAndMainCamera.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs index 5ac54dd841..3159e13a64 100644 --- a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs +++ b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs @@ -191,6 +191,49 @@ public IEnumerator TestUpdateThirdPartyCamera() Assert.AreEqual(result, true); } + [UnityTest] + public IEnumerator TestGetVisibleObjects() + { + Dictionary action = new Dictionary(); + + action["action"] = "Initialize"; + action["fieldOfView"] = 90f; + action["snapToGrid"] = true; + yield return step(action); + + action.Clear(); + + action["action"] = "AddThirdPartyCamera"; + action["position"] = new Vector3(-0.67f, 1.315f, 0.46f); + action["rotation"] = new Vector3(0, 180, 0); + action["orthographic"] = true; + action["orthographicSize"] = 5; + action["parent"] = "world"; + action["agentPositionRelativeCoordinates"] = false; + yield return step(action); + + action.Clear(); + + action["action"] = "GetVisibleObjects"; + action["thirdPartyCameraIndex"] = 0; + action["visibilityScheme"] = "Distance"; + yield return step(action); + + //Debug.Log($"action return: {actionReturn}"); + List visibleObjects = (List) actionReturn; + + //check for expected object at first few elements + //also check for total count of visible objects to be the expected amount + foreach(string obj in visibleObjects) + { + Debug.Log(obj); + } + + //test with objectId filter now + + + } + [UnityTest] public IEnumerator TestUpdateMainCamera() { From e0bd9bb544d3955ca0c444cf1d56eac40ea07f53 Mon Sep 17 00:00:00 2001 From: Winson Han Date: Mon, 12 Aug 2024 15:27:34 -0700 Subject: [PATCH 2/7] removing Collider based visibility scheme, update GetVisibleObjects this commit removes all references and use of the collider based visibility scheme used to query sim objects from cameras. Moving forward this scheme will not be supported, and all visibility checks will now be completely distance-based from the camera (ie: objects will only be potentially visible if they are within maxVisibleDistance from the camera based purely by raycast distance) Additionally, updating the function to query objects from a camera to be called `GetVisibleObjectsFromCamera` since the old name `GetVisibleObjects` was too easily confused with other object-visibility helper functions. The old`GetVisibleObjects` action will remain but will have a deprecation warning message thrown for those who may still use it. Finally, added unit tests for`GetVisibleObjectsFromCamera` --- unity/Assets/Scripts/AgentManager.cs | 6 +- .../Assets/Scripts/BaseFPSAgentController.cs | 405 ++++-------------- unity/Assets/Scripts/DebugInputField.cs | 20 +- unity/Assets/Scripts/FpinAgentController.cs | 2 +- .../Assets/UnitTests/Procedural/Movement.meta | 8 + .../TestThirdPartyCameraAndMainCamera.cs | 36 +- 6 files changed, 137 insertions(+), 340 deletions(-) create mode 100644 unity/Assets/UnitTests/Procedural/Movement.meta diff --git a/unity/Assets/Scripts/AgentManager.cs b/unity/Assets/Scripts/AgentManager.cs index 96653a9767..913629a7ca 100644 --- a/unity/Assets/Scripts/AgentManager.cs +++ b/unity/Assets/Scripts/AgentManager.cs @@ -2944,7 +2944,7 @@ public class ServerAction // default time for objects to wait before returning actionFinished() if an action put them in motion public float TimeToWaitForObjectsToComeToRest = 10.0f; public float scale; - public string visibilityScheme = VisibilityScheme.Collider.ToString(); + public string visibilityScheme = VisibilityScheme.Distance.ToString(); public bool fastActionEmit = true; // this allows us to chain the dispatch between two separate @@ -2997,7 +2997,7 @@ public SimObjType ReceptableSimObjType() public static VisibilityScheme GetVisibilitySchemeFromString(string visibilityScheme) { - VisibilityScheme result = VisibilityScheme.Collider; + VisibilityScheme result = VisibilityScheme.Distance; try { result = (VisibilityScheme)Enum.Parse(typeof(VisibilityScheme), visibilityScheme, true); @@ -3122,7 +3122,7 @@ public enum ServerActionErrorCode public enum VisibilityScheme { - Collider, + Collider, //deprecated, scheme is now Distance based only Distance } diff --git a/unity/Assets/Scripts/BaseFPSAgentController.cs b/unity/Assets/Scripts/BaseFPSAgentController.cs index 48e04bd1ea..cb40043b91 100644 --- a/unity/Assets/Scripts/BaseFPSAgentController.cs +++ b/unity/Assets/Scripts/BaseFPSAgentController.cs @@ -220,7 +220,7 @@ public ImageSynthesis ImageSynthesis // outbound object filter private SimObjPhysics[] simObjFilter = null; - protected VisibilityScheme visibilityScheme = VisibilityScheme.Collider; + protected VisibilityScheme visibilityScheme = VisibilityScheme.Distance; protected HashSet collidersDisabledForVisbilityCheck = new HashSet(); private Dictionary> originalLightingValues = null; @@ -5178,24 +5178,12 @@ protected SimObjPhysics[] GetAllVisibleSimObjPhysics( { SimObjPhysics[] interactable; - if (this.visibilityScheme == VisibilityScheme.Collider) - { - return GetAllVisibleSimObjPhysicsCollider( - camera, - maxDistance, - filterSimObjs, - out interactable - ); - } - else - { - return GetAllVisibleSimObjPhysicsDistance( - camera, - maxDistance, - filterSimObjs, - out interactable - ); - } + return GetAllVisibleSimObjPhysicsDistance( + camera, + maxDistance, + filterSimObjs, + out interactable + ); } protected SimObjPhysics[] GetAllVisibleSimObjPhysics( @@ -5205,61 +5193,79 @@ protected SimObjPhysics[] GetAllVisibleSimObjPhysics( IEnumerable filterSimObjs = null ) { - if (this.visibilityScheme == VisibilityScheme.Collider) - { - return GetAllVisibleSimObjPhysicsCollider( - camera, - maxDistance, - filterSimObjs, - out interactable - ); - } - else - { - return GetAllVisibleSimObjPhysicsDistance( - camera, - maxDistance, - filterSimObjs, - out interactable - ); - } + return GetAllVisibleSimObjPhysicsDistance( + camera, + maxDistance, + filterSimObjs, + out interactable + ); } - protected VisibilityScheme getVisibilityScheme(string visibilityScheme = null) + //return a list of visible sim objects from a specific camera + // maxDistance defaults to whatever initialized by agent's maxVisibleDistance + // thirdPartyCameraId defaults to use main camera + // filterObjectIds defaults to null, which means all objects are considered + // note: visible objects are returned but this does not guarantee the objects are interactable (ex: something behind glass) + public void GetVisibleObjectsFromCamera( + float? maxDistance = null, //max distance from the camera origin to consider objects visible + int? thirdPartyCameraId = null, //leave null to query main camera, otherwise pass in the index of the third party camera + List filterObjectIds = null //only return objects with these ids + ) { - VisibilityScheme visSchemeEnum; - if (visibilityScheme != null) + //if thirdPartyCameraId not specified, default to main camera + Camera camera = m_Camera; + if (thirdPartyCameraId.HasValue) { - visibilityScheme = visibilityScheme.ToLower(); - - if ( - visibilityScheme - == Enum.GetName(typeof(VisibilityScheme), VisibilityScheme.Collider).ToLower() - ) - { - visSchemeEnum = VisibilityScheme.Collider; - } - else if ( - visibilityScheme - == Enum.GetName(typeof(VisibilityScheme), VisibilityScheme.Distance).ToLower() - ) - { - visSchemeEnum = VisibilityScheme.Distance; - } - else + camera = agentManager.thirdPartyCameras[thirdPartyCameraId.Value]; + if (this.visibilityScheme != VisibilityScheme.Distance) { throw new System.NotImplementedException( - $"Visibility scheme {visibilityScheme} is not implemented. Must be 'distance' or 'collider'." + $"Visibility scheme {this.visibilityScheme} is not implemented for third party cameras. Default visibility scheme should be 'Distance'." ); - } + } } - else + + //only check visibility for objects with these ids otherwise check them all + List filterSimObjs = null; + if (filterObjectIds != null) { - visSchemeEnum = this.visibilityScheme; + foreach (string objectId in filterObjectIds) + { + if (!physicsSceneManager.ObjectIdToSimObjPhysics.ContainsKey(objectId)) + { + throw new ArgumentException( + $"Object with id {objectId} does not exist in scene." + ); + } + } + filterSimObjs = filterObjectIds + .Select(objectId => physicsSceneManager.ObjectIdToSimObjPhysics[objectId]) + .ToList(); } - return visSchemeEnum; + + SimObjPhysics[] interactable; + SimObjPhysics[] visible = GetAllVisibleSimObjPhysicsDistance( + camera: camera, + maxDistance: maxDistance.GetValueOrDefault(this.maxVisibleDistance), + filterSimObjs: filterSimObjs, + interactable: out interactable + ); + +// #if UNITY_EDITOR +// foreach (SimObjPhysics sop in visible) +// { +// Debug.Log("Visible: " + sop.name); +// } +// #endif + + actionFinishedEmit(true, visible.Select(sop => sop.ObjectID).ToList()); } + //old version of GetVisibleSimObjectsFromCamera + [ObsoleteAttribute( + message: "This action is deprecated. Call GetVisibleObjectsFromCamera instead.", + error: false + )] public void GetVisibleObjects( float? maxDistance = null, string visibilityScheme = null, @@ -5267,19 +5273,19 @@ public void GetVisibleObjects( List objectIds = null ) { - VisibilityScheme visSchemeEnum = getVisibilityScheme(visibilityScheme); - - Camera camera; + Camera camera; //which camera are we checking visibility from? if (thirdPartyCameraIndex.HasValue) { camera = agentManager.thirdPartyCameras[thirdPartyCameraIndex.Value]; - if (visSchemeEnum != VisibilityScheme.Distance) + if (this.visibilityScheme != VisibilityScheme.Distance) { throw new System.NotImplementedException( - $"Visibility scheme {visSchemeEnum} is not implemented for third party cameras. Must be 'distance'." + $"Visibility scheme {this.visibilityScheme} is not implemented for third party cameras. Default visibility scheme should be 'Distance'." ); } } + + //can also be used to query main camera else { camera = m_Camera; @@ -5304,30 +5310,14 @@ public void GetVisibleObjects( SimObjPhysics[] interactable; SimObjPhysics[] visible; - if (visSchemeEnum == VisibilityScheme.Collider) - { - visible = GetAllVisibleSimObjPhysicsCollider( - camera: camera, - maxDistance: maxDistance.GetValueOrDefault(this.maxVisibleDistance), // lgtm [cs/dereferenced-value-may-be-null] - filterSimObjs: filterSimObjs, - interactable: out interactable - ); - } - else if (visSchemeEnum == VisibilityScheme.Distance) - { - visible = GetAllVisibleSimObjPhysicsDistance( - camera: camera, - maxDistance: maxDistance.GetValueOrDefault(this.maxVisibleDistance), // lgtm [cs/dereferenced-value-may-be-null] - filterSimObjs: filterSimObjs, - interactable: out interactable - ); - } - else - { - throw new System.NotImplementedException( - $"Visibility scheme {visSchemeEnum} is not implemented. Must be 'distance' or 'collider'." - ); - } + + visible = GetAllVisibleSimObjPhysicsDistance( + camera: camera, + maxDistance: maxDistance.GetValueOrDefault(this.maxVisibleDistance), // lgtm [cs/dereferenced-value-may-be-null] + filterSimObjs: filterSimObjs, + interactable: out interactable + ); + #if UNITY_EDITOR foreach (SimObjPhysics sop in visible) { @@ -5362,23 +5352,6 @@ public void GetObjaverseAnnotations() actionFinishedEmit(true, annotations); } - [ObsoleteAttribute( - message: "This action is deprecated. Call GetVisibleObjects instead.", - error: false - )] - public void ObjectsVisibleFromThirdPartyCamera( - int thirdPartyCameraIndex, - float? maxDistance = null, - string visibilityScheme = null - ) - { - GetVisibleObjects( - maxDistance: maxDistance, - visibilityScheme: visibilityScheme, - thirdPartyCameraIndex: thirdPartyCameraIndex - ); - } - // this is a faster version of the visibility check, but is not entirely // consistent with the collider based method. In particular, if an object // is within range of the maxVisibleDistance, but obscurred only within this @@ -5417,222 +5390,6 @@ out SimObjPhysics[] interactable return visible.ToArray(); } - private SimObjPhysics[] GetAllVisibleSimObjPhysicsCollider( - Camera camera, - float maxDistance, - IEnumerable filterSimObjs, - out SimObjPhysics[] interactable - ) - { - HashSet currentlyVisibleItems = new HashSet(); - HashSet interactableItems = new HashSet(); - -#if UNITY_EDITOR - foreach ( - KeyValuePair< - string, - SimObjPhysics - > pair in physicsSceneManager.ObjectIdToSimObjPhysics - ) - { - // Set all objects to not be visible - pair.Value.debugIsVisible = false; - pair.Value.debugIsInteractable = false; - } -#endif - - HashSet filter = null; - if (filterSimObjs != null) - { - filter = new HashSet(filterSimObjs); - if (filter.Count == 0) - { - interactable = interactableItems.ToArray(); - return currentlyVisibleItems.ToArray(); - } - } - - Vector3 agentCameraPos = camera.transform.position; - - // get all sim objects in range around us that have colliders in layer 8 (visible), ignoring objects in the SimObjInvisible layer - // this will make it so the receptacle trigger boxes don't occlude the objects within them. - CapsuleCollider agentCapsuleCollider = GetComponent(); - Vector3 point0, - point1; - float radius; - agentCapsuleCollider.ToWorldSpaceCapsule(out point0, out point1, out radius); - if (point0.y <= point1.y) - { - point1.y += maxDistance; - } - else - { - point0.y += maxDistance; - } - - // Turn off the colliders corresponding to this agent - // and any invisible agents. - updateAllAgentCollidersForVisibilityCheck(false); - - HashSet<(SimObjPhysics, bool)> sopAndIncInvisibleTuples = - new HashSet<(SimObjPhysics, bool)>(); - - // Find all nearby colliders corresponding to visible components and grab - // their corresponding SimObjPhysics component - Collider[] collidersInView = Physics.OverlapCapsule( - point0, - point1, - maxDistance, - LayerMask.GetMask( - "SimObjVisible", - "Procedural1", - "Procedural2", - "Procedural3", - "Procedural0" - ), - QueryTriggerInteraction.Collide - ); - if (collidersInView != null) - { - foreach (Collider c in collidersInView) - { - SimObjPhysics sop = ancestorSimObjPhysics(c.gameObject); - if (sop != null) - { - sopAndIncInvisibleTuples.Add((sop, false)); - } - } - } - - // Check against anything in the invisible layers that we actually want to have occlude things in this round. - // normally receptacle trigger boxes must be ignored from the visibility check otherwise objects inside them will be occluded, but - // this additional check will allow us to see inside of receptacle objects like cabinets/fridges by checking for that interior - // receptacle trigger box. Oh boy! - Collider[] invisibleCollidersInView = Physics.OverlapCapsule( - point0, - point1, - maxDistance, - LayerMask.GetMask("SimObjInvisible"), - QueryTriggerInteraction.Collide - ); - if (invisibleCollidersInView != null) - { - foreach (Collider c in invisibleCollidersInView) - { - if (c.tag == "Receptacle") - { - SimObjPhysics sop = c.GetComponentInParent(); - if (sop != null) - { - sopAndIncInvisibleTuples.Add((sop, true)); - } - } - } - } - - // We have to explicitly add the items held by the arm as their - // rigidbodies are set to not detect collisions - if (Arm != null && Arm.gameObject.activeSelf) - { - foreach (SimObjPhysics sop in Arm.heldObjects.Keys) - { - sopAndIncInvisibleTuples.Add((sop, false)); - } - } - else if (SArm != null && SArm.gameObject.activeSelf) - { - foreach (SimObjPhysics sop in SArm.heldObjects.Keys) - { - sopAndIncInvisibleTuples.Add((sop, false)); - } - } - - if (sopAndIncInvisibleTuples.Count != 0) - { - foreach ((SimObjPhysics, bool) sopAndIncInvisible in sopAndIncInvisibleTuples) - { - SimObjPhysics sop = sopAndIncInvisible.Item1; - bool includeInvisible = sopAndIncInvisible.Item2; - - // now we have a reference to our sim object - if (sop != null && (filter == null || filter.Contains(sop))) - { - // check against all visibility points, accumulate count. If at least one point is visible, set object to visible - if (sop.VisibilityPoints != null && sop.VisibilityPoints.Length > 0) - { - Transform[] visPoints = sop.VisibilityPoints; - VisibilityCheck visCheck = new VisibilityCheck(); - - foreach (Transform point in visPoints) - { - // if this particular point is in view... - // if we see at least one vis point, the object is "visible" - visCheck |= CheckIfVisibilityPointInViewport( - sop, - point, - camera, - includeInvisible - ); - if (visCheck.visible && visCheck.interactable) - { -#if !UNITY_EDITOR - // If we're in the unity editor then don't break on finding a visible - // point as we want to draw lines to each visible point. - break; -#endif - } - } - -#if UNITY_EDITOR - sop.debugIsVisible = visCheck.visible; - sop.debugIsInteractable = visCheck.interactable; -#endif - if (visCheck.visible && !currentlyVisibleItems.Contains(sop)) - { - currentlyVisibleItems.Add(sop); - } - - if (visCheck.interactable && !interactableItems.Contains(sop)) - { - interactableItems.Add(sop); - } - } - else - { - Debug.Log( - "Error! Set at least 1 visibility point on SimObjPhysics " - + sop - + "." - ); - } - } - } - } - - // Turn back on the colliders corresponding to this agent and invisible agents. - updateAllAgentCollidersForVisibilityCheck(true); - - // populate array of visible items in order by distance - List currentlyVisibleItemsToList = currentlyVisibleItems.ToList(); - List interactableItemsToList = interactableItems.ToList(); - - interactableItemsToList.Sort( - (x, y) => - Vector3 - .Distance(x.transform.position, agentCameraPos) - .CompareTo(Vector3.Distance(y.transform.position, agentCameraPos)) - ); - currentlyVisibleItemsToList.Sort( - (x, y) => - Vector3 - .Distance(x.transform.position, agentCameraPos) - .CompareTo(Vector3.Distance(y.transform.position, agentCameraPos)) - ); - - interactable = interactableItemsToList.ToArray(); - return currentlyVisibleItemsToList.ToArray(); - } - protected virtual LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) { string[] layers = new string[] diff --git a/unity/Assets/Scripts/DebugInputField.cs b/unity/Assets/Scripts/DebugInputField.cs index 0eb115f664..d2ccd304de 100644 --- a/unity/Assets/Scripts/DebugInputField.cs +++ b/unity/Assets/Scripts/DebugInputField.cs @@ -2926,10 +2926,10 @@ IEnumerator executeBatch(JArray jActions) Dictionary action = new Dictionary() { ["action"] = "AddThirdPartyCamera", - ["position"] = new Vector3(1, 1, 1), - ["rotation"] = new Vector3(10, 20, 30), - ["parent"] = "agent", - ["agentPositionRelativeCoordinates"] = true + ["position"] = new Vector3(-0.67f, 1.315f, 0.46f), + ["rotation"] = new Vector3(0, 180, 0), + ["parent"] = "world", + ["agentPositionRelativeCoordinates"] = false }; CurrentActiveController() @@ -2937,6 +2937,18 @@ IEnumerator executeBatch(JArray jActions) break; } + case "gvo": + { + Dictionary action = new Dictionary() + { + ["action"] = "GetVisibleObjects", + ["thirdPartyCameraIndex"] = 0, + }; + + CurrentActiveController().ProcessControlCommand(action); + break; + } + case "utpc": { Dictionary action = new Dictionary() diff --git a/unity/Assets/Scripts/FpinAgentController.cs b/unity/Assets/Scripts/FpinAgentController.cs index f35e3696f7..bffbda8a8a 100644 --- a/unity/Assets/Scripts/FpinAgentController.cs +++ b/unity/Assets/Scripts/FpinAgentController.cs @@ -66,7 +66,7 @@ public class BackwardsCompatibleInitializeParams public float maxVisibleDistance = 1.5f; public float visibilityDistance; public float TimeToWaitForObjectsToComeToRest = 10.0f; - public string visibilityScheme = VisibilityScheme.Collider.ToString(); + public string visibilityScheme = VisibilityScheme.Distance.ToString(); } public class FpinAgentController : PhysicsRemoteFPSAgentController diff --git a/unity/Assets/UnitTests/Procedural/Movement.meta b/unity/Assets/UnitTests/Procedural/Movement.meta new file mode 100644 index 0000000000..77ee3f4cd3 --- /dev/null +++ b/unity/Assets/UnitTests/Procedural/Movement.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 65017e063124b4536b7ef7572fc6c275 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs index 3159e13a64..99854954b5 100644 --- a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs +++ b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs @@ -192,7 +192,7 @@ public IEnumerator TestUpdateThirdPartyCamera() } [UnityTest] - public IEnumerator TestGetVisibleObjects() + public IEnumerator TestGetVisibleObjectsFromCamera() { Dictionary action = new Dictionary(); @@ -214,24 +214,44 @@ public IEnumerator TestGetVisibleObjects() action.Clear(); - action["action"] = "GetVisibleObjects"; - action["thirdPartyCameraIndex"] = 0; - action["visibilityScheme"] = "Distance"; + action["action"] = "GetVisibleObjectsFromCamera"; + action["thirdPartyCameraId"] = 0; yield return step(action); - //Debug.Log($"action return: {actionReturn}"); List visibleObjects = (List) actionReturn; - - //check for expected object at first few elements - //also check for total count of visible objects to be the expected amount + #if UNITY_EDITOR foreach(string obj in visibleObjects) { Debug.Log(obj); + } + #endif + + //check for expected object at first few elements + //also check for total count of visible objects to be the expected amount + Assert.AreEqual(visibleObjects.Count, 16); + Assert.AreEqual(visibleObjects[0], "Apple|-00.47|+01.15|+00.48"); //test with objectId filter now + action.Clear(); + action["action"] = "GetVisibleObjectsFromCamera"; + action["thirdPartyCameraId"] = 0; + action["filterObjectIds"] = new List { "Apple|-00.47|+01.15|+00.48" }; + yield return step(action); + + visibleObjects.Clear(); + visibleObjects = (List) actionReturn; + #if UNITY_EDITOR + foreach(string obj in visibleObjects) + { + Debug.Log(obj); + + } + #endif + Assert.AreEqual(visibleObjects.Count, 1); + Assert.AreEqual(visibleObjects[0], "Apple|-00.47|+01.15|+00.48"); } [UnityTest] From f4cd5daf954a4f52bf5ea141a1d8eedaa01b48cc Mon Sep 17 00:00:00 2001 From: Winson Han Date: Mon, 12 Aug 2024 16:47:52 -0700 Subject: [PATCH 3/7] Update DebugInputField.cs --- unity/Assets/Scripts/DebugInputField.cs | 127 ------------------------ 1 file changed, 127 deletions(-) diff --git a/unity/Assets/Scripts/DebugInputField.cs b/unity/Assets/Scripts/DebugInputField.cs index d2c139ef00..b24b716259 100644 --- a/unity/Assets/Scripts/DebugInputField.cs +++ b/unity/Assets/Scripts/DebugInputField.cs @@ -2763,39 +2763,6 @@ IEnumerator executeBatch(JArray jActions) { break; } - case "initsynth": - { - Dictionary action = new Dictionary(); - - action["renderNormalsImage"] = true; - action["renderDepthImage"] = true; - action["renderSemanticSegmentation"] = true; - action["renderInstanceSegmentation"] = true; - action["renderFlowImage"] = true; - - // action.ssao = "default"; - - action["action"] = "Initialize"; - ActionDispatcher.Dispatch(AManager, new DynamicServerAction(action)); - break; - } - - case "atpc": - { - Dictionary action = new Dictionary() - { - ["action"] = "AddThirdPartyCamera", - ["position"] = new Vector3(-0.67f, 1.315f, 0.46f), - ["rotation"] = new Vector3(0, 180, 0), - ["parent"] = "world", - ["agentPositionRelativeCoordinates"] = false - }; - - CurrentActiveController() - .ProcessControlCommand(new DynamicServerAction(action), AManager); - break; - } - case "gvo": { Dictionary action = new Dictionary() @@ -2808,100 +2775,6 @@ IEnumerator executeBatch(JArray jActions) { break; } - case "utpc": - { - Dictionary action = new Dictionary() - { - ["action"] = "UpdateThirdPartyCamera", - ["position"] = new Vector3(2, 2, 2), - ["rotation"] = new Vector3(15, 25, 35), - ["thirdPartyCameraId"] = 1, - ["parent"] = "agent", - ["agentPositionRelativeCoordinates"] = true - }; - - CurrentActiveController() - .ProcessControlCommand(new DynamicServerAction(action), AManager); - break; - } - - case "umc": - { - Dictionary action = new Dictionary() - { - ["action"] = "UpdateMainCamera", - ["position"] = new Vector3(0.5f, 0.5f, 0.5f), - ["rotation"] = new Vector3(15, 25, 35), - ["fieldOfView"] = 120f, - //["agentPositionRelativeCoordinates"] = false - }; - - CurrentActiveController() - .ProcessControlCommand(new DynamicServerAction(action), AManager); - break; - } - - case "debugmaincamera": - { - CurrentActiveController().generateMetadataWrapper(); - break; - } - case "to": - { - ServerAction action = new ServerAction(); - action.action = "TeleportObject"; - action.objectId = splitcommand[1]; - action.x = float.Parse(splitcommand[2]); - action.y = float.Parse(splitcommand[3]); - action.z = float.Parse(splitcommand[4]); - CurrentActiveController().ProcessControlCommand(action); - break; - } - case "daoot": - { - ServerAction action = new ServerAction(); - action.action = "DisableAllObjectsOfType"; - action.objectId = splitcommand[1]; - CurrentActiveController().ProcessControlCommand(action); - break; - } - case "ctlq": - { - ServerAction action = new ServerAction(); - action.action = "ChangeQuality"; - action.quality = "Very Low"; - CurrentActiveController().ProcessControlCommand(action); - break; - } - case "roco": - { - Dictionary action = new Dictionary(); - action["action"] = "RandomlyOpenCloseObjects"; - action["randomSeed"] = (new System.Random()).Next(1, 1000000); - CurrentActiveController().ProcessControlCommand(action); - break; - } - case "crouch": - { - ExecuteAction("Crouch"); - break; - } - case "stand": - { - ExecuteAction("Stand"); - break; - } - - case "remove": - { - Dictionary action = new Dictionary(); - action["action"] = "RemoveFromScene"; - - if (splitcommand.Length == 2) - { - action["objectId"] = splitcommand[1]; - } - case "putxy": { Dictionary action = new Dictionary(); action["action"] = "PutObject"; From 5bb0c6386c9ed76877ebcfc87ffbdfa79d38d4af Mon Sep 17 00:00:00 2001 From: Winson Han Date: Mon, 12 Aug 2024 16:49:22 -0700 Subject: [PATCH 4/7] formatting for collider scheme removal changes --- unity/Assets/Scripts/AgentManager.cs | 9 +- .../Assets/Scripts/BaseFPSAgentController.cs | 51 +-- unity/Assets/Scripts/DebugInputField.cs | 22 +- unity/Assets/Scripts/FpinAgentController.cs | 419 ++++++------------ .../TestThirdPartyCameraAndMainCamera.cs | 18 +- 5 files changed, 176 insertions(+), 343 deletions(-) diff --git a/unity/Assets/Scripts/AgentManager.cs b/unity/Assets/Scripts/AgentManager.cs index ab7aa9b2f6..f4163ac819 100644 --- a/unity/Assets/Scripts/AgentManager.cs +++ b/unity/Assets/Scripts/AgentManager.cs @@ -2677,11 +2677,9 @@ public SimObjType ReceptableSimObjType() { return (SimObjType)Enum.Parse(typeof(SimObjType), receptacleObjectType); } - public static VisibilityScheme GetVisibilitySchemeFromString(string visibilityScheme) - { + public static VisibilityScheme GetVisibilitySchemeFromString(string visibilityScheme) { VisibilityScheme result = VisibilityScheme.Distance; - try - { + try { result = (VisibilityScheme)Enum.Parse(typeof(VisibilityScheme), visibilityScheme, true); } // including this pragma so the "ex variable declared but not used" warning stops yelling @@ -2789,8 +2787,7 @@ public enum ServerActionErrorCode { UnhandledException } -public enum VisibilityScheme -{ +public enum VisibilityScheme { Collider, //deprecated, scheme is now Distance based only Distance } diff --git a/unity/Assets/Scripts/BaseFPSAgentController.cs b/unity/Assets/Scripts/BaseFPSAgentController.cs index 5e1c994107..e5e64d81f0 100644 --- a/unity/Assets/Scripts/BaseFPSAgentController.cs +++ b/unity/Assets/Scripts/BaseFPSAgentController.cs @@ -4537,8 +4537,7 @@ protected SimObjPhysics[] GetAllVisibleSimObjPhysics( float maxDistance, out SimObjPhysics[] interactable, IEnumerable filterSimObjs = null - ) - { + ) { return GetAllVisibleSimObjPhysicsDistance( camera, maxDistance, @@ -4556,29 +4555,23 @@ public void GetVisibleObjectsFromCamera( float? maxDistance = null, //max distance from the camera origin to consider objects visible int? thirdPartyCameraId = null, //leave null to query main camera, otherwise pass in the index of the third party camera List filterObjectIds = null //only return objects with these ids - ) - { + ) { //if thirdPartyCameraId not specified, default to main camera Camera camera = m_Camera; - if (thirdPartyCameraId.HasValue) - { + if (thirdPartyCameraId.HasValue) { camera = agentManager.thirdPartyCameras[thirdPartyCameraId.Value]; - if (this.visibilityScheme != VisibilityScheme.Distance) - { + if (this.visibilityScheme != VisibilityScheme.Distance) { throw new System.NotImplementedException( $"Visibility scheme {this.visibilityScheme} is not implemented for third party cameras. Default visibility scheme should be 'Distance'." ); - } + } } //only check visibility for objects with these ids otherwise check them all List filterSimObjs = null; - if (filterObjectIds != null) - { - foreach (string objectId in filterObjectIds) - { - if (!physicsSceneManager.ObjectIdToSimObjPhysics.ContainsKey(objectId)) - { + if (filterObjectIds != null) { + foreach (string objectId in filterObjectIds) { + if (!physicsSceneManager.ObjectIdToSimObjPhysics.ContainsKey(objectId)) { throw new ArgumentException( $"Object with id {objectId} does not exist in scene." ); @@ -4597,12 +4590,12 @@ public void GetVisibleObjectsFromCamera( interactable: out interactable ); -// #if UNITY_EDITOR -// foreach (SimObjPhysics sop in visible) -// { -// Debug.Log("Visible: " + sop.name); -// } -// #endif + // #if UNITY_EDITOR + // foreach (SimObjPhysics sop in visible) + // { + // Debug.Log("Visible: " + sop.name); + // } + // #endif actionFinishedEmit(true, visible.Select(sop => sop.ObjectID).ToList()); } @@ -4617,23 +4610,18 @@ public void GetVisibleObjects( string visibilityScheme = null, int? thirdPartyCameraIndex = null, List objectIds = null - ) - { + ) { Camera camera; //which camera are we checking visibility from? - if (thirdPartyCameraIndex.HasValue) - { + if (thirdPartyCameraIndex.HasValue) { camera = agentManager.thirdPartyCameras[thirdPartyCameraIndex.Value]; - if (this.visibilityScheme != VisibilityScheme.Distance) - { + if (this.visibilityScheme != VisibilityScheme.Distance) { throw new System.NotImplementedException( $"Visibility scheme {this.visibilityScheme} is not implemented for third party cameras. Default visibility scheme should be 'Distance'." ); } } - //can also be used to query main camera - else - { + else { camera = m_Camera; } @@ -4725,8 +4713,7 @@ out SimObjPhysics[] interactable return visible.ToArray(); } - protected virtual LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) - { + protected virtual LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) { string[] layers = new string[] { "SimObjVisible", diff --git a/unity/Assets/Scripts/DebugInputField.cs b/unity/Assets/Scripts/DebugInputField.cs index b24b716259..b789b2bacb 100644 --- a/unity/Assets/Scripts/DebugInputField.cs +++ b/unity/Assets/Scripts/DebugInputField.cs @@ -2760,20 +2760,18 @@ IEnumerator executeBatch(JArray jActions) { action["objectId"] = splitcommand[1]; } - break; - } + break; + } - case "gvo": - { - Dictionary action = new Dictionary() - { - ["action"] = "GetVisibleObjects", - ["thirdPartyCameraIndex"] = 0, - }; + case "gvo": { + Dictionary action = new Dictionary() { + ["action"] = "GetVisibleObjects", + ["thirdPartyCameraIndex"] = 0, + }; - CurrentActiveController().ProcessControlCommand(action); - break; - } + CurrentActiveController().ProcessControlCommand(action); + break; + } case "putxy": { Dictionary action = new Dictionary(); diff --git a/unity/Assets/Scripts/FpinAgentController.cs b/unity/Assets/Scripts/FpinAgentController.cs index bffbda8a8a..aee1812096 100644 --- a/unity/Assets/Scripts/FpinAgentController.cs +++ b/unity/Assets/Scripts/FpinAgentController.cs @@ -11,10 +11,8 @@ using UnityEngine.Rendering.PostProcessing; using UnityEngine.UIElements; -namespace UnityStandardAssets.Characters.FirstPerson -{ - public class BoxBounds - { +namespace UnityStandardAssets.Characters.FirstPerson { + public class BoxBounds { public Vector3 worldCenter; public Vector3 agentRelativeCenter; public Vector3 size; @@ -22,8 +20,7 @@ public class BoxBounds [Serializable] [MessagePackObject(keyAsPropertyName: true)] - public class LoadInUnityProceduralAsset - { + public class LoadInUnityProceduralAsset { public string id; public string dir; public string extension = ".msgpack.gz"; @@ -33,8 +30,7 @@ public class LoadInUnityProceduralAsset #nullable enable [Serializable] [MessagePackObject(keyAsPropertyName: true)] - public class BodyAsset - { + public class BodyAsset { public string? assetId = null; public LoadInUnityProceduralAsset? dynamicAsset = null; public ProceduralAsset? asset = null; @@ -44,8 +40,7 @@ public class BodyAsset [Serializable] [MessagePackObject(keyAsPropertyName: true)] - public class BackwardsCompatibleInitializeParams - { + public class BackwardsCompatibleInitializeParams { // Make what parameters it uses explicit public float maxUpwardLookAngle = 0.0f; public float maxDownwardLookAngle = 0.0f; @@ -69,8 +64,7 @@ public class BackwardsCompatibleInitializeParams public string visibilityScheme = VisibilityScheme.Distance.ToString(); } - public class FpinAgentController : PhysicsRemoteFPSAgentController - { + public class FpinAgentController : PhysicsRemoteFPSAgentController { private static readonly Vector3 agentSpawnOffset = new Vector3(100.0f, 100.0f, 100.0f); private FpinMovableContinuous fpinMovable; public BoxCollider spawnedBoxCollider = null; @@ -79,12 +73,9 @@ public class FpinAgentController : PhysicsRemoteFPSAgentController private Transform topMeshTransform = null; private Bounds? agentBounds = null; public BoxBounds boxBounds = null; - public BoxBounds BoxBounds - { - get - { - if (spawnedBoxCollider != null) - { + public BoxBounds BoxBounds { + get { + if (spawnedBoxCollider != null) { BoxBounds currentBounds = new BoxBounds(); currentBounds.worldCenter = spawnedBoxCollider.transform.TransformPoint( @@ -100,9 +91,7 @@ public BoxBounds BoxBounds // Debug.Log($"world center: {boxBounds.worldCenter}"); // Debug.Log($"size: {boxBounds.size}"); // Debug.Log($"agentRelativeCenter: {boxBounds.agentRelativeCenter}"); - } - else - { + } else { // Debug.Log("why is it nullll"); return null; } @@ -117,8 +106,7 @@ public BoxBounds BoxBounds public FpinAgentController(BaseAgentComponent baseAgentComponent, AgentManager agentManager) : base(baseAgentComponent, agentManager) { } - public void Start() - { + public void Start() { //put stuff we need here when we need it maybe } @@ -129,8 +117,7 @@ public override RaycastHit[] CastBodyTrayectory( float moveMagnitude, int layerMask, CapsuleData cachedCapsule - ) - { + ) { Vector3 startPositionBoxCenter = startPosition + this.transform.TransformDirection(this.boxBounds.agentRelativeCenter); @@ -148,16 +135,14 @@ CapsuleData cachedCapsule //since the size returned by the box collider only reflects its size in local space DISREGARDING ANY TRANSFORM SCALES //IN ITS PARENTS OR ITSELF here we go - public Vector3 GetTrueSizeOfBoxCollider(BoxCollider collider) - { + public Vector3 GetTrueSizeOfBoxCollider(BoxCollider collider) { Vector3 trueSize = collider.size; // get the transform of the collider itself Transform currentTransform = collider.transform; // Apply the scale from the collider's transform and all parent transforms - while (currentTransform != null) - { + while (currentTransform != null) { trueSize.x *= currentTransform.localScale.x; trueSize.y *= currentTransform.localScale.y; trueSize.z *= currentTransform.localScale.z; @@ -170,29 +155,24 @@ public Vector3 GetTrueSizeOfBoxCollider(BoxCollider collider) } //override so we can access to fpin specific stuff - public override MetadataWrapper generateMetadataWrapper() - { + public override MetadataWrapper generateMetadataWrapper() { //get all the usual stuff from base agent's implementation MetadataWrapper metaWrap = base.generateMetadataWrapper(); //here's the fpin specific stuff - if (boxBounds != null) - { + if (boxBounds != null) { //get from BoxBounds as box world center will update as agent moves so we can't cache it metaWrap.agent.fpinColliderSize = BoxBounds.size; metaWrap.agent.fpinColliderWorldCenter = BoxBounds.worldCenter; metaWrap.agent.fpinColliderAgentRelativeCenter = BoxBounds.agentRelativeCenter; - } - else - { + } else { metaWrap.agent.fpinColliderSize = new Vector3(0, 0, 0); } return metaWrap; } - public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) - { + public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) { float minX = agentManager.SceneBounds.min.x; float minZ = agentManager.SceneBounds.min.z; float maxX = agentManager.SceneBounds.max.x; @@ -203,10 +183,8 @@ public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) int n = (int)Mathf.Ceil(Mathf.Sqrt(sampleCount)); List initPoints = new List(); - for (int i = 0; i < n; i++) - { - for (int j = 0; j < n; j++) - { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { initPoints.Add( new Vector3( Mathf.Lerp(minX, maxX, (i + 0.5f) / n), @@ -219,17 +197,14 @@ public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) initPoints.Shuffle_(); List pointsOnMesh = new List(); - for (int i = 0; i < initPoints.Count; i++) - { - if (pointsOnMesh.Count >= sampleCount) - { + for (int i = 0; i < initPoints.Count; i++) { + if (pointsOnMesh.Count >= sampleCount) { break; } NavMeshHit hit; Vector3 randomPoint = initPoints[i]; - if (NavMesh.SamplePosition(randomPoint, out hit, maxDistance, NavMesh.AllAreas)) - { + if (NavMesh.SamplePosition(randomPoint, out hit, maxDistance, NavMesh.AllAreas)) { # if UNITY_EDITOR Debug.DrawLine( hit.position, @@ -247,17 +222,14 @@ public List SamplePointsOnNavMesh(int sampleCount, float maxDistance) return pointsOnMesh; } - public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) - { + public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) { List pointsOnMesh = SamplePointsOnNavMesh(n, maxDistance: maxDistance); - if (pointsOnMesh.Count == 0) - { + if (pointsOnMesh.Count == 0) { throw new InvalidOperationException("No points on the navmesh"); } Bounds b = UtilityFunctions.CreateEmptyBounds(); - foreach (Collider c in GetComponentsInChildren()) - { + foreach (Collider c in GetComponentsInChildren()) { b.Encapsulate(c.bounds); } @@ -266,10 +238,8 @@ public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) //Debug.Log($"yOffset is: {yOffset}"); bool success = false; - foreach (Vector3 point in pointsOnMesh) - { - try - { + foreach (Vector3 point in pointsOnMesh) { + try { //Debug.Log($"what is the point we are trying from the pointsOnMesh? {point:F8}"); teleportFull( position: point + new Vector3(0, yOffset, 0), @@ -279,9 +249,7 @@ public void RandomlyPlaceAgentOnNavMesh(int n = 200, float maxDistance = 0.1f) ); success = true; break; - } - catch (InvalidOperationException) - { + } catch (InvalidOperationException) { continue; } } @@ -296,8 +264,7 @@ public void spawnAgentBoxCollider( Bounds agentBounds, bool useVisibleColliderBase = false, bool spawnCollidersWithoutMesh = false - ) - { + ) { //create colliders based on the agent bounds var col = new GameObject("fpinCollider", typeof(BoxCollider)); col.layer = LayerMask.NameToLayer("Agent"); @@ -332,27 +299,22 @@ public void spawnAgentBoxCollider( //helper function to remove the currently generated agent box collider //make sure to follow this up with a subsequent generation so BoxBounds isn't left null - public void destroyAgentBoxCollider() - { + public void destroyAgentBoxCollider() { GameObject visibleBox = GameObject.Find("VisibleBox"); - if (spawnedBoxCollider != null) - { + if (spawnedBoxCollider != null) { UnityEngine.Object.DestroyImmediate(spawnedBoxCollider.transform.gameObject); spawnedBoxCollider = null; } - if (spawnedTriggerBoxCollider != null) - { + if (spawnedTriggerBoxCollider != null) { UnityEngine.Object.DestroyImmediate(spawnedTriggerBoxCollider.transform.gameObject); spawnedTriggerBoxCollider = null; } - if (visibleBox != null) - { + if (visibleBox != null) { UnityEngine.Object.DestroyImmediate(visibleBox); } #if UNITY_EDITOR GameObject visualizedBoxCollider = GameObject.Find("VisualizedBoxCollider"); - if (visualizedBoxCollider != null) - { + if (visualizedBoxCollider != null) { UnityEngine.Object.DestroyImmediate(visualizedBoxCollider); } #endif @@ -368,36 +330,30 @@ private Transform CopyMeshChildrenRecursive( Transform sourceTransform, Transform targetTransform, bool isTopMost = true - ) - { + ) { Transform thisTransform = null; - foreach (Transform child in sourceTransform) - { + foreach (Transform child in sourceTransform) { GameObject copiedChild = null; // Check if the child has a MeshFilter component MeshFilter meshFilter = child.GetComponent(); - if (meshFilter != null) - { + if (meshFilter != null) { copiedChild = CopyMeshToTarget(child, targetTransform); } // Process children only if necessary (i.e., they contain MeshFilters) - if (HasMeshInChildrenOrSelf(child)) - { + if (HasMeshInChildrenOrSelf(child)) { Transform parentForChildren = (copiedChild != null) ? copiedChild.transform : CreateContainerForHierarchy(child, targetTransform).transform; CopyMeshChildrenRecursive(child, parentForChildren, false); - if (isTopMost) - { + if (isTopMost) { thisTransform = parentForChildren; } } } - if (isTopMost) - { + if (isTopMost) { // Set up intermediate object for scaling the mesh in agent-space (since the top-level mesh could be rotated, potentially introducing a complex matrix of scales) GameObject agentScaleObject = new GameObject("AgentScaleObject"); agentScaleObject.transform.position = thisTransform.position; @@ -409,8 +365,7 @@ private Transform CopyMeshChildrenRecursive( return null; } - private GameObject CopyMeshToTarget(Transform child, Transform targetParent) - { + private GameObject CopyMeshToTarget(Transform child, Transform targetParent) { // Create a new GameObject and copy components GameObject copiedChild = new GameObject(child.name); copiedChild.transform.SetParent(targetParent); @@ -420,8 +375,7 @@ private GameObject CopyMeshToTarget(Transform child, Transform targetParent) copiedMeshFilter.mesh = meshFilter.mesh; MeshRenderer sourceMeshRenderer = child.GetComponent(); - if (sourceMeshRenderer != null) - { + if (sourceMeshRenderer != null) { MeshRenderer copiedMeshRenderer = copiedChild.AddComponent(); copiedMeshRenderer.sharedMaterials = sourceMeshRenderer.sharedMaterials; } @@ -433,26 +387,21 @@ private GameObject CopyMeshToTarget(Transform child, Transform targetParent) return copiedChild; } - private bool HasMeshInChildrenOrSelf(Transform transform) - { - foreach (Transform child in transform) - { - if (child.GetComponent() != null || HasMeshInChildrenOrSelf(child)) - { + private bool HasMeshInChildrenOrSelf(Transform transform) { + foreach (Transform child in transform) { + if (child.GetComponent() != null || HasMeshInChildrenOrSelf(child)) { return true; } } - if (transform.GetComponent() != null) - { + if (transform.GetComponent() != null) { return true; } return false; } - private GameObject CreateContainerForHierarchy(Transform child, Transform targetParent) - { + private GameObject CreateContainerForHierarchy(Transform child, Transform targetParent) { GameObject container = new GameObject(child.name + "_Container"); container.transform.SetParent(targetParent); container.transform.localPosition = child.localPosition; @@ -464,26 +413,22 @@ private GameObject CreateContainerForHierarchy(Transform child, Transform target private HashSet TransformedMeshRendererVertices( MeshRenderer mr, bool returnFirstVertexOnly = false - ) - { + ) { MeshFilter mf = mr.gameObject.GetComponent(); Matrix4x4 localToWorld = mr.transform.localToWorldMatrix; HashSet vertices = new HashSet(mf.sharedMesh.vertices); HashSet transformedVertices = new HashSet(); - foreach (Vector3 vertex in vertices) - { + foreach (Vector3 vertex in vertices) { transformedVertices.Add(localToWorld.MultiplyPoint3x4(vertex)); } return transformedVertices; } - public ActionFinished GetBoxBounds() - { + public ActionFinished GetBoxBounds() { return new ActionFinished() { success = true, actionReturn = this.BoxBounds }; } - public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) - { + public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) { // Move the point to the pivot's origin Vector3 dir = point - pivot; // Rotate it @@ -495,31 +440,23 @@ public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angl public ActionFinished BackwardsCompatibleInitialize( BackwardsCompatibleInitializeParams args - ) - { + ) { Debug.Log("RUNNING BackCompatInitialize from FpinAgentController.cs"); // limit camera from looking too far down/up //default max are 30 up and 60 down, different agent types may overwrite this - if (Mathf.Approximately(args.maxUpwardLookAngle, 0.0f)) - { + if (Mathf.Approximately(args.maxUpwardLookAngle, 0.0f)) { this.maxUpwardLookAngle = 30f; - } - else - { + } else { this.maxUpwardLookAngle = args.maxUpwardLookAngle; } - if (Mathf.Approximately(args.maxDownwardLookAngle, 0.0f)) - { + if (Mathf.Approximately(args.maxDownwardLookAngle, 0.0f)) { this.maxDownwardLookAngle = 60f; - } - else - { + } else { this.maxDownwardLookAngle = args.maxDownwardLookAngle; } - if (args.antiAliasing != null) - { + if (args.antiAliasing != null) { agentManager.updateAntiAliasing( postProcessLayer: m_Camera.gameObject.GetComponentInChildren(), antiAliasing: args.antiAliasing @@ -527,77 +464,62 @@ BackwardsCompatibleInitializeParams args } // m_Camera.GetComponent().SwitchRenderersToHide(this.VisibilityCapsule); - if (args.gridSize == 0) - { + if (args.gridSize == 0) { args.gridSize = 0.25f; } // note: this overrides the default FOV values set in InitializeBody() - if (args.fieldOfView > 0 && args.fieldOfView < 180) - { + if (args.fieldOfView > 0 && args.fieldOfView < 180) { m_Camera.fieldOfView = args.fieldOfView; - } - else if (args.fieldOfView < 0 || args.fieldOfView >= 180) - { + } else if (args.fieldOfView < 0 || args.fieldOfView >= 180) { errorMessage = "fov must be set to (0, 180) noninclusive."; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.cameraNearPlane > 0) - { + if (args.cameraNearPlane > 0) { m_Camera.nearClipPlane = args.cameraNearPlane; } - if (args.cameraFarPlane > 0) - { + if (args.cameraFarPlane > 0) { m_Camera.farClipPlane = args.cameraFarPlane; } - if (args.timeScale > 0) - { - if (Time.timeScale != args.timeScale) - { + if (args.timeScale > 0) { + if (Time.timeScale != args.timeScale) { Time.timeScale = args.timeScale; } - } - else - { + } else { errorMessage = "Time scale must be > 0"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.rotateStepDegrees <= 0.0) - { + if (args.rotateStepDegrees <= 0.0) { errorMessage = "rotateStepDegrees must be a non-zero, non-negative float"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } // default is 90 defined in the ServerAction class, specify whatever you want the default to be - if (args.rotateStepDegrees > 0.0) - { + if (args.rotateStepDegrees > 0.0) { this.rotateStepDegrees = args.rotateStepDegrees; } - if (args.snapToGrid && !ValidRotateStepDegreesWithSnapToGrid(args.rotateStepDegrees)) - { + if (args.snapToGrid && !ValidRotateStepDegreesWithSnapToGrid(args.rotateStepDegrees)) { errorMessage = $"Invalid values 'rotateStepDegrees': ${args.rotateStepDegrees} and 'snapToGrid':${args.snapToGrid}. 'snapToGrid': 'True' is not supported when 'rotateStepDegrees' is different from grid rotation steps of 0, 90, 180, 270 or 360."; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.maxDownwardLookAngle < 0) - { + if (args.maxDownwardLookAngle < 0) { errorMessage = "maxDownwardLookAngle must be a non-negative float"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); } - if (args.maxUpwardLookAngle < 0) - { + if (args.maxUpwardLookAngle < 0) { errorMessage = "maxUpwardLookAngle must be a non-negative float"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); @@ -610,21 +532,18 @@ BackwardsCompatibleInitializeParams args || args.renderSemanticSegmentation || args.renderInstanceSegmentation || args.renderNormalsImage - ) - { + ) { this.updateImageSynthesis(true); } - if (args.visibilityDistance > 0.0f) - { + if (args.visibilityDistance > 0.0f) { this.maxVisibleDistance = args.visibilityDistance; } var navmeshAgent = this.GetComponentInChildren(); var collider = this.GetComponent(); - if (collider != null && navmeshAgent != null) - { + if (collider != null && navmeshAgent != null) { navmeshAgent.radius = collider.radius; navmeshAgent.height = collider.height; navmeshAgent.transform.localPosition = new Vector3( @@ -636,14 +555,11 @@ BackwardsCompatibleInitializeParams args // navmeshAgent.radius = - if (args.gridSize <= 0 || args.gridSize > 5) - { + if (args.gridSize <= 0 || args.gridSize > 5) { errorMessage = "grid size must be in the range (0,5]"; Debug.Log(errorMessage); return new ActionFinished(success: false, errorMessage: errorMessage); - } - else - { + } else { gridSize = args.gridSize; // Don't know what this was for @@ -666,8 +582,7 @@ BackwardsCompatibleInitializeParams args return new ActionFinished( success: true, - actionReturn: new InitializeReturn - { + actionReturn: new InitializeReturn { cameraNearPlane = m_Camera.nearClipPlane, cameraFarPlane = m_Camera.farClipPlane } @@ -682,8 +597,7 @@ public ActionFinished Initialize( Vector3? colliderScaleRatio = null, bool useAbsoluteSize = false, bool useVisibleColliderBase = false - ) - { + ) { this.visibilityScheme = VisibilityScheme.Distance; var actionFinished = this.InitializeBody( bodyAsset: bodyAsset, @@ -705,16 +619,14 @@ public ActionFinished InitializeBody( Vector3? colliderScaleRatio = null, bool useAbsoluteSize = false, bool useVisibleColliderBase = false - ) - { + ) { // if using no source body mesh, we default to using absolute size via the colliderScaleRatio // since a non absolute size doesn't make sense if we have no default mesh size to base the scale // ratio on Vector3 meshScaleRatio = colliderScaleRatio.GetValueOrDefault(Vector3.one); bool noMesh = false; - if (bodyAsset == null) - { + if (bodyAsset == null) { useAbsoluteSize = true; noMesh = true; } @@ -729,21 +641,18 @@ public ActionFinished InitializeBody( //remove any old copied meshes or generated colliders from previous fpin agent now destroyAgentBoxCollider(); - if (fpinVisibilityCapsule != null) - { + if (fpinVisibilityCapsule != null) { UnityEngine.Object.DestroyImmediate(fpinVisibilityCapsule); } var spawnAssetActionFinished = new ActionFinished(); Bounds meshBoundsWorld = new Bounds(this.transform.position, Vector3.zero); - if (bodyAsset != null) - { + if (bodyAsset != null) { //spawn in a default mesh in an out-of-the-way location (currently 200,200,200) to base the new bounds on spawnAssetActionFinished = spawnBodyAsset(bodyAsset, out GameObject spawnedMesh); // Return early if spawn failed - if (!spawnAssetActionFinished.success) - { + if (!spawnAssetActionFinished.success) { return spawnAssetActionFinished; } @@ -759,18 +668,13 @@ public ActionFinished InitializeBody( // so we'll take the bounds-center of the first meshRenderer MeshRenderer[] meshRenderers = topMeshTransform.gameObject.GetComponentsInChildren(); - foreach (MeshRenderer mr in meshRenderers) - { + foreach (MeshRenderer mr in meshRenderers) { // No need to run TransformedMeshRendererVertices if the meshRenderer's GameObject isn't rotated - if (mr.transform.eulerAngles.magnitude < 1e-4f) - { + if (mr.transform.eulerAngles.magnitude < 1e-4f) { meshBoundsWorld.Encapsulate(mr.bounds); - } - else - { + } else { HashSet vertices = TransformedMeshRendererVertices(mr); - foreach (Vector3 vertex in vertices) - { + foreach (Vector3 vertex in vertices) { meshBoundsWorld.Encapsulate(vertex); } } @@ -781,17 +685,14 @@ public ActionFinished InitializeBody( topMeshTransform.position = meshBoundsWorld.center; topMeshTransform.GetChild(0).position = currentTopMeshTransformChildPos; - if (useAbsoluteSize) - { + if (useAbsoluteSize) { topMeshTransform.localScale = new Vector3( meshScaleRatio.x / meshBoundsWorld.size.x, meshScaleRatio.y / meshBoundsWorld.size.y, meshScaleRatio.z / meshBoundsWorld.size.z ); meshBoundsWorld.size = meshScaleRatio; - } - else - { + } else { topMeshTransform.localScale = meshScaleRatio; meshBoundsWorld.size = new Vector3( meshScaleRatio.x * meshBoundsWorld.size.x, @@ -810,18 +711,14 @@ public ActionFinished InitializeBody( this.transform.position + Vector3.up * meshBoundsWorld.extents.y; // remove the spawned mesh cause we are done with it - foreach (var sop in spawnedMesh.GetComponentsInChildren()) - { + foreach (var sop in spawnedMesh.GetComponentsInChildren()) { agentManager.physicsSceneManager.RemoveFromObjectsInScene(sop); } - if (spawnedMesh.activeInHierarchy) - { + if (spawnedMesh.activeInHierarchy) { UnityEngine.Object.DestroyImmediate(spawnedMesh); } - } - else - { + } else { meshBoundsWorld = new Bounds( this.transform.position + (Vector3.up * meshScaleRatio.y / 2), meshScaleRatio @@ -836,8 +733,7 @@ public ActionFinished InitializeBody( // set reference to the meshes so the base agent and fpin agent are happy VisibilityCapsule = fpinVisibilityCapsule = viscap; - if (topMeshTransform != null) - { + if (topMeshTransform != null) { topMeshTransform.SetParent(viscap.transform); } @@ -853,8 +749,7 @@ public ActionFinished InitializeBody( ); // spawn the visible collider base if we need to - if (useVisibleColliderBase) - { + if (useVisibleColliderBase) { GameObject visibleBase = GameObject.CreatePrimitive(PrimitiveType.Cube); visibleBase.name = "visibleBase"; visibleBase.GetComponent().enabled = false; @@ -966,8 +861,7 @@ public ActionFinished InitializeBody( originalRotation, checkBoxLayerMask ) - ) - { + ) { this.transform.position = originalPosition; this.transform.rotation = originalRotation; string error = @@ -989,10 +883,8 @@ public ActionFinished InitializeBody( fpinMovable = new FpinMovableContinuous(this.GetComponentInParent()); // we had a body asset used, so actionFinished returns info related to that - if (bodyAsset != null) - { - return new ActionFinished(spawnAssetActionFinished) - { + if (bodyAsset != null) { + return new ActionFinished(spawnAssetActionFinished) { // TODO: change to a proper class once metadata return is defined actionReturn = new Dictionary() { @@ -1006,11 +898,8 @@ spawnAssetActionFinished.actionReturn as ObjectSphereBounds { "cameraFarPlane", m_Camera.farClipPlane } } }; - } - else - { - return new ActionFinished() - { + } else { + return new ActionFinished() { // TODO: change to a proper class once metadata return is defined actionReturn = new Dictionary() { @@ -1022,18 +911,14 @@ spawnAssetActionFinished.actionReturn as ObjectSphereBounds } } - private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawnedMesh) - { - if (bodyAsset == null) - { + private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawnedMesh) { + if (bodyAsset == null) { throw new ArgumentNullException("bodyAsset is null"); - } - else if ( - bodyAsset.assetId == null - && bodyAsset.dynamicAsset == null - && bodyAsset.asset == null - ) - { + } else if ( + bodyAsset.assetId == null + && bodyAsset.dynamicAsset == null + && bodyAsset.asset == null + ) { throw new ArgumentNullException( "`bodyAsset.assetId`, `bodyAsset.dynamicAsset` or `bodyAsset.asset` must be provided all are null." ); @@ -1047,8 +932,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne if ( (bodyAsset.dynamicAsset != null || bodyAsset.asset != null) && bodyAsset.assetId == null - ) - { + ) { var id = bodyAsset.dynamicAsset != null ? bodyAsset.dynamicAsset.id @@ -1056,24 +940,20 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne var assetMap = ProceduralTools.getAssetMap(); // Check if asset is in AssetDatabase already - if (assetMap.ContainsKey(id)) - { + if (assetMap.ContainsKey(id)) { Debug.Log("------- Already contains key"); bodyAsset.assetId = id; } } - if (bodyAsset.assetId != null) - { + if (bodyAsset.assetId != null) { actionFinished = SpawnAsset( bodyAsset.assetId, "agentMesh", new Vector3(200f, 200f, 200f) ); spawnedMesh = GameObject.Find("agentMesh"); - } - else if (bodyAsset.dynamicAsset != null) - { + } else if (bodyAsset.dynamicAsset != null) { actionFinished = this.CreateRuntimeAsset( id: bodyAsset.dynamicAsset.id, dir: bodyAsset.dynamicAsset.dir, @@ -1082,9 +962,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne serializable: true ); spawnedMesh = GameObject.Find("mesh"); - } - else if (bodyAsset.asset != null) - { + } else if (bodyAsset.asset != null) { bodyAsset.asset.serializable = true; actionFinished = this.CreateRuntimeAsset(asset: bodyAsset.asset); } @@ -1092,8 +970,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne if ( bodyAsset.assetId == null && (bodyAsset.dynamicAsset != null || bodyAsset.asset != null) - ) - { + ) { var id = bodyAsset.dynamicAsset != null ? bodyAsset.dynamicAsset.id @@ -1101,8 +978,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne Debug.Log( $"-- checks {bodyAsset.assetId == null} {bodyAsset.dynamicAsset != null} {bodyAsset.asset != null} " ); - if (!actionFinished.success || actionFinished.actionReturn == null) - { + if (!actionFinished.success || actionFinished.actionReturn == null) { return new ActionFinished( success: false, errorMessage: $"Could not create asset `{bodyAsset.dynamicAsset}` error: {actionFinished.errorMessage}" @@ -1115,8 +991,7 @@ private ActionFinished spawnBodyAsset(BodyAsset bodyAsset, out GameObject spawne return actionFinished; } - protected override LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) - { + protected override LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisible = false) { // No agent because camera can be in the path of colliders string[] layers = new string[] { @@ -1126,8 +1001,7 @@ protected override LayerMask GetVisibilityRaycastLayerMask(bool withSimObjInvisi "Procedural3", "Procedural0" //, "Agent" }; - if (withSimObjInvisible) - { + if (withSimObjInvisible) { layers = layers.Append("SimObjInvisible").ToArray(); } return LayerMask.GetMask(layers); @@ -1140,8 +1014,7 @@ public override void TeleportFull( float? horizon = null, bool? standing = null, bool forceAction = false - ) - { + ) { teleportFull( position: position, rotation: rotation, @@ -1155,8 +1028,7 @@ protected override void teleportFull( Vector3? rotation, float? horizon, bool forceAction - ) - { + ) { //Debug.Log($"what even is the position passed in at the start? {position:F8}"); if ( rotation.HasValue @@ -1164,8 +1036,7 @@ bool forceAction !Mathf.Approximately(rotation.Value.x, 0f) || !Mathf.Approximately(rotation.Value.z, 0f) ) - ) - { + ) { throw new ArgumentOutOfRangeException( "No agents currently can change in pitch or roll. So, you must set rotation(x=0, y=yaw, z=0)." + $" You gave {rotation.Value.ToString("F6")}." @@ -1177,8 +1048,7 @@ bool forceAction !forceAction && horizon.HasValue && (horizon.Value > maxDownwardLookAngle || horizon.Value < -maxUpwardLookAngle) - ) - { + ) { throw new ArgumentOutOfRangeException( $"Each horizon must be in [{-maxUpwardLookAngle}:{maxDownwardLookAngle}]. You gave {horizon}." ); @@ -1188,15 +1058,13 @@ bool forceAction !forceAction && position.HasValue && !agentManager.SceneBounds.Contains(position.Value) - ) - { + ) { throw new ArgumentOutOfRangeException( $"Teleport position {position.Value.ToString("F6")} out of scene bounds! Ignore this by setting forceAction=true." ); } - if (!forceAction && position.HasValue && !isPositionOnGrid(position.Value)) - { + if (!forceAction && position.HasValue && !isPositionOnGrid(position.Value)) { throw new ArgumentOutOfRangeException( $"Teleport position {position.Value.ToString("F6")} is not on the grid of size {gridSize}." ); @@ -1225,15 +1093,13 @@ bool forceAction targetPosition: position.GetValueOrDefault(transform.position) ); - if (!forceAction) - { + if (!forceAction) { if ( isAgentCapsuleColliding( collidersToIgnore: collidersToIgnoreDuringMovement, includeErrorMessage: true ) - ) - { + ) { transform.position = oldPosition; transform.rotation = oldRotation; autoSyncTransforms(); @@ -1247,8 +1113,7 @@ bool forceAction collidersToIgnore: collidersToIgnoreDuringMovement, includeErrorMessage: true ) - ) - { + ) { transform.position = oldPosition; transform.rotation = oldRotation; autoSyncTransforms(); @@ -1260,11 +1125,9 @@ bool forceAction actionFinished(success: true); } - protected override void assertTeleportedNearGround(Vector3? targetPosition) - { + protected override void assertTeleportedNearGround(Vector3? targetPosition) { // position should not change if it's null. - if (targetPosition == null) - { + if (targetPosition == null) { return; } @@ -1279,8 +1142,7 @@ protected override void assertTeleportedNearGround(Vector3? targetPosition) autoSyncTransforms(); // perhaps like y=2 was specified, with an agent's standing height of 0.9 - if (Mathf.Abs(transform.position.y - pos.y) > 1.0f) - { + if (Mathf.Abs(transform.position.y - pos.y) > 1.0f) { throw new InvalidOperationException( "After teleporting and adjusting agent position to floor, there was too large a change." + " This may be due to the target teleport coordinates causing the agent to fall through the floor." @@ -1294,10 +1156,8 @@ public IEnumerator MoveAgent( float ahead = 0, float right = 0, float speed = 1 - ) - { - if (ahead == 0 && right == 0) - { + ) { + if (ahead == 0 && right == 0) { throw new ArgumentException("Must specify ahead or right!"); } Vector3 direction = new Vector3(x: right, y: 0, z: ahead); @@ -1325,8 +1185,7 @@ public IEnumerator MoveAhead( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( ahead: moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1338,8 +1197,7 @@ public IEnumerator MoveBack( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( ahead: -moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1351,8 +1209,7 @@ public IEnumerator MoveRight( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( right: moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1364,8 +1221,7 @@ public IEnumerator MoveLeft( float? moveMagnitude = null, float speed = 1, bool returnToStart = true - ) - { + ) { return MoveAgent( right: -moveMagnitude.GetValueOrDefault(gridSize), speed: speed, @@ -1377,8 +1233,7 @@ public IEnumerator RotateRight( float? degrees = null, float speed = 1.0f, bool returnToStart = true - ) - { + ) { return RotateAgent( degrees: degrees.GetValueOrDefault(rotateStepDegrees), speed: speed, @@ -1390,8 +1245,7 @@ public IEnumerator RotateLeft( float? degrees = null, float speed = 1.0f, bool returnToStart = true - ) - { + ) { return RotateAgent( degrees: -degrees.GetValueOrDefault(rotateStepDegrees), speed: speed, @@ -1403,8 +1257,7 @@ public virtual IEnumerator RotateAgent( float degrees, float speed = 1.0f, bool returnToStart = true - ) - { + ) { CollisionListener collisionListener = fpinMovable.collisionListener; collisionListener.Reset(); diff --git a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs index 99854954b5..686c1ae06d 100644 --- a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs +++ b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs @@ -218,14 +218,13 @@ public IEnumerator TestGetVisibleObjectsFromCamera() action["thirdPartyCameraId"] = 0; yield return step(action); - List visibleObjects = (List) actionReturn; - #if UNITY_EDITOR - foreach(string obj in visibleObjects) + List visibleObjects = (List)actionReturn; +#if UNITY_EDITOR + foreach (string obj in visibleObjects) { Debug.Log(obj); - } - #endif +#endif //check for expected object at first few elements //also check for total count of visible objects to be the expected amount @@ -241,14 +240,13 @@ public IEnumerator TestGetVisibleObjectsFromCamera() yield return step(action); visibleObjects.Clear(); - visibleObjects = (List) actionReturn; - #if UNITY_EDITOR - foreach(string obj in visibleObjects) + visibleObjects = (List)actionReturn; +#if UNITY_EDITOR + foreach (string obj in visibleObjects) { Debug.Log(obj); - } - #endif +#endif Assert.AreEqual(visibleObjects.Count, 1); Assert.AreEqual(visibleObjects[0], "Apple|-00.47|+01.15|+00.48"); From 88c496c25018f856f386b12ba0dfaec9d4334bbc Mon Sep 17 00:00:00 2001 From: Winson Han Date: Mon, 19 Aug 2024 17:10:20 -0700 Subject: [PATCH 5/7] fixes for PR --- unity/Assets/Scripts/AgentManager.cs | 9 +++++++- .../Assets/Scripts/BaseFPSAgentController.cs | 22 ++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/unity/Assets/Scripts/AgentManager.cs b/unity/Assets/Scripts/AgentManager.cs index f4163ac819..c59cec89b9 100644 --- a/unity/Assets/Scripts/AgentManager.cs +++ b/unity/Assets/Scripts/AgentManager.cs @@ -352,6 +352,13 @@ public void Initialize(ServerAction action) { action.alwaysReturnVisibleRange; } + //deprecation check for visibilityScheme + if(action.visibilityScheme != VisibilityScheme.Distance.ToString()) { + throw new ArgumentException( + $"Invalid visibilityScheme {action.visibilityScheme}. Must be 'Distance'." + ); + } + //if multi agent requested, add duplicates of primary agent now addAgents(action); this.agents[0].m_Camera.depth = 9999; @@ -2687,7 +2694,7 @@ public static VisibilityScheme GetVisibilitySchemeFromString(string visibilitySc catch (ArgumentException ex) { #pragma warning restore 0168 Debug.LogError( - "Error parsing visibilityScheme: '" + visibilityScheme + "' defaulting to Collider" + "Error parsing visibilityScheme: '" + visibilityScheme + "' defaulting to Distance Based" ); } diff --git a/unity/Assets/Scripts/BaseFPSAgentController.cs b/unity/Assets/Scripts/BaseFPSAgentController.cs index ae6617f01a..a9090d501f 100644 --- a/unity/Assets/Scripts/BaseFPSAgentController.cs +++ b/unity/Assets/Scripts/BaseFPSAgentController.cs @@ -4560,11 +4560,12 @@ public void GetVisibleObjectsFromCamera( Camera camera = m_Camera; if (thirdPartyCameraId.HasValue) { camera = agentManager.thirdPartyCameras[thirdPartyCameraId.Value]; - if (this.visibilityScheme != VisibilityScheme.Distance) { - throw new System.NotImplementedException( - $"Visibility scheme {this.visibilityScheme} is not implemented for third party cameras. Default visibility scheme should be 'Distance'." - ); - } + } + + if (this.visibilityScheme != VisibilityScheme.Distance) { + throw new System.NotImplementedException( + $"Visibility scheme {this.visibilityScheme} is not compatible. Visibility scheme should be 'Distance'." + ); } //only check visibility for objects with these ids otherwise check them all @@ -4614,17 +4615,18 @@ public void GetVisibleObjects( Camera camera; //which camera are we checking visibility from? if (thirdPartyCameraIndex.HasValue) { camera = agentManager.thirdPartyCameras[thirdPartyCameraIndex.Value]; - if (this.visibilityScheme != VisibilityScheme.Distance) { - throw new System.NotImplementedException( - $"Visibility scheme {this.visibilityScheme} is not implemented for third party cameras. Default visibility scheme should be 'Distance'." - ); - } } //can also be used to query main camera else { camera = m_Camera; } + if (this.visibilityScheme != VisibilityScheme.Distance) { + throw new System.NotImplementedException( + $"Visibility scheme {this.visibilityScheme} is not compatible. Visibility scheme should be 'Distance'." + ); + } + List filterSimObjs = null; if (objectIds != null) { foreach (string objectId in objectIds) { From 93a137fbd9f1f8bd8a6fa51e81873d02747c07c6 Mon Sep 17 00:00:00 2001 From: Winson Han Date: Mon, 19 Aug 2024 17:37:24 -0700 Subject: [PATCH 6/7] ran formatting --- unity/Assets/Scripts/AgentManager.cs | 6 ++++-- unity/Assets/Scripts/FpinAgentController.cs | 10 +++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/unity/Assets/Scripts/AgentManager.cs b/unity/Assets/Scripts/AgentManager.cs index c59cec89b9..9e0efc8904 100644 --- a/unity/Assets/Scripts/AgentManager.cs +++ b/unity/Assets/Scripts/AgentManager.cs @@ -353,7 +353,7 @@ public void Initialize(ServerAction action) { } //deprecation check for visibilityScheme - if(action.visibilityScheme != VisibilityScheme.Distance.ToString()) { + if (action.visibilityScheme != VisibilityScheme.Distance.ToString()) { throw new ArgumentException( $"Invalid visibilityScheme {action.visibilityScheme}. Must be 'Distance'." ); @@ -2694,7 +2694,9 @@ public static VisibilityScheme GetVisibilitySchemeFromString(string visibilitySc catch (ArgumentException ex) { #pragma warning restore 0168 Debug.LogError( - "Error parsing visibilityScheme: '" + visibilityScheme + "' defaulting to Distance Based" + "Error parsing visibilityScheme: '" + + visibilityScheme + + "' defaulting to Distance Based" ); } diff --git a/unity/Assets/Scripts/FpinAgentController.cs b/unity/Assets/Scripts/FpinAgentController.cs index e7410e89e8..ffbf503269 100644 --- a/unity/Assets/Scripts/FpinAgentController.cs +++ b/unity/Assets/Scripts/FpinAgentController.cs @@ -883,10 +883,8 @@ public ActionFinished InitializeBody( fpinMovable = new FpinMovableContinuous(this.GetComponentInParent()); // we had a body asset used, so actionFinished returns info related to that - if (bodyAsset != null) - { - return new ActionFinished(spawnAssetActionFinished) - { + if (bodyAsset != null) { + return new ActionFinished(spawnAssetActionFinished) { success = true, // TODO: change to a proper class once metadata return is defined actionReturn = new Dictionary() @@ -901,9 +899,7 @@ spawnAssetActionFinished.actionReturn as ObjectSphereBounds { "cameraFarPlane", m_Camera.farClipPlane } } }; - } - else - { + } else { return new ActionFinished( success: true, // TODO: change to a proper class once metadata return is defined From 37cce20189ce4b2dcbbe88cf59cceecafd746991 Mon Sep 17 00:00:00 2001 From: Winson Han Date: Wed, 21 Aug 2024 10:53:52 -0700 Subject: [PATCH 7/7] removing visibilityScheme param from GetVisibleObjects... ... to ensure an error is thrown because the local visibilityScheme value is set to the only valid enum at initialization now also adding a main camera unit test in GetVisibleObjectsFromCamera as previously only the created ThirdPartyCamera was being tested for expected returns --- .../Assets/Scripts/BaseFPSAgentController.cs | 15 +---------- .../TestThirdPartyCameraAndMainCamera.cs | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/unity/Assets/Scripts/BaseFPSAgentController.cs b/unity/Assets/Scripts/BaseFPSAgentController.cs index a9090d501f..a2ef43c4fc 100644 --- a/unity/Assets/Scripts/BaseFPSAgentController.cs +++ b/unity/Assets/Scripts/BaseFPSAgentController.cs @@ -4562,12 +4562,6 @@ public void GetVisibleObjectsFromCamera( camera = agentManager.thirdPartyCameras[thirdPartyCameraId.Value]; } - if (this.visibilityScheme != VisibilityScheme.Distance) { - throw new System.NotImplementedException( - $"Visibility scheme {this.visibilityScheme} is not compatible. Visibility scheme should be 'Distance'." - ); - } - //only check visibility for objects with these ids otherwise check them all List filterSimObjs = null; if (filterObjectIds != null) { @@ -4608,7 +4602,6 @@ public void GetVisibleObjectsFromCamera( )] public void GetVisibleObjects( float? maxDistance = null, - string visibilityScheme = null, int? thirdPartyCameraIndex = null, List objectIds = null ) { @@ -4621,12 +4614,6 @@ public void GetVisibleObjects( camera = m_Camera; } - if (this.visibilityScheme != VisibilityScheme.Distance) { - throw new System.NotImplementedException( - $"Visibility scheme {this.visibilityScheme} is not compatible. Visibility scheme should be 'Distance'." - ); - } - List filterSimObjs = null; if (objectIds != null) { foreach (string objectId in objectIds) { @@ -4646,7 +4633,7 @@ public void GetVisibleObjects( visible = GetAllVisibleSimObjPhysicsDistance( camera: camera, - maxDistance: maxDistance.GetValueOrDefault(this.maxVisibleDistance), // lgtm [cs/dereferenced-value-may-be-null] + maxDistance: maxDistance.GetValueOrDefault(this.maxVisibleDistance), filterSimObjs: filterSimObjs, interactable: out interactable ); diff --git a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs index 686c1ae06d..38a70f6897 100644 --- a/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs +++ b/unity/Assets/UnitTests/TestThirdPartyCameraAndMainCamera.cs @@ -214,6 +214,7 @@ public IEnumerator TestGetVisibleObjectsFromCamera() action.Clear(); + //testing third party camera return action["action"] = "GetVisibleObjectsFromCamera"; action["thirdPartyCameraId"] = 0; yield return step(action); @@ -241,7 +242,10 @@ public IEnumerator TestGetVisibleObjectsFromCamera() visibleObjects.Clear(); visibleObjects = (List)actionReturn; + #if UNITY_EDITOR + Debug.Log($"Checking Visible Objects from ThirdPartyCamera Index 0"); + Debug.Log($"Total Visible Objects: {visibleObjects.Count}"); foreach (string obj in visibleObjects) { Debug.Log(obj); @@ -250,6 +254,28 @@ public IEnumerator TestGetVisibleObjectsFromCamera() Assert.AreEqual(visibleObjects.Count, 1); Assert.AreEqual(visibleObjects[0], "Apple|-00.47|+01.15|+00.48"); + + //Test main camera return + action.Clear(); + + action["action"] = "GetVisibleObjectsFromCamera"; + action["thirdPartyCameraId"] = null; //null ID queries main camera instead + yield return step(action); + + visibleObjects.Clear(); + visibleObjects = (List)actionReturn; + +#if UNITY_EDITOR + Debug.Log($"Checking Visible Objects from Main Camera"); + Debug.Log($"Total Visible Objects: {visibleObjects.Count}"); + foreach (string obj in visibleObjects) + { + Debug.Log(obj); + } +#endif + + Assert.AreEqual(visibleObjects.Count, 4); + Assert.AreEqual(visibleObjects[0], "Cabinet|-01.85|+02.02|+00.38"); } [UnityTest]