From 1c405c8a23f0274bdb4b7e50a49b882575f06454 Mon Sep 17 00:00:00 2001 From: Ruo-Ping Dong Date: Fri, 23 Apr 2021 22:00:55 -0700 Subject: [PATCH] Bring back root reference in grid sensor (#5300) (#5313) --- .../Prefabs/PushBlockCollabAreaGrid.prefab | 54 +++++++++++++++++-- .../PushBlock/Scenes/PushBlockCollab.unity | 5 ++ .../Editor/GridSensorComponentEditor.cs | 1 + .../Runtime/Sensors/BoxOverlapChecker.cs | 21 ++++---- .../Runtime/Sensors/GridSensorComponent.cs | 13 +++++ .../Runtime/Sensor/BoxOverlapCheckerTests.cs | 28 ++++++---- docs/Migrating.md | 1 - 7 files changed, 99 insertions(+), 24 deletions(-) diff --git a/Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockCollabAreaGrid.prefab b/Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockCollabAreaGrid.prefab index c0bbee555e..6f681e1e5a 100644 --- a/Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockCollabAreaGrid.prefab +++ b/Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockCollabAreaGrid.prefab @@ -2261,6 +2261,16 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 8188317207052398481} m_Modifications: + - target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + propertyPath: m_RootReference + value: + objectReference: {fileID: 8190299122290044757} + - target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + propertyPath: m_AgentGameObject + value: + objectReference: {fileID: 8190299122290044757} - target: {fileID: 2598450485826216109, guid: ac01d0f42c5e1463e943632a60d99967, type: 3} propertyPath: m_Model @@ -2336,7 +2346,7 @@ MonoBehaviour: type: 3} m_PrefabInstance: {fileID: 6067781793364901444} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} + m_GameObject: {fileID: 8190299122290044757} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: d94a85eca2e074578943301959c555ba, type: 3} @@ -2348,6 +2358,12 @@ Transform: type: 3} m_PrefabInstance: {fileID: 6067781793364901444} m_PrefabAsset: {fileID: 0} +--- !u!1 &8190299122290044757 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 2710286047221272849, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + m_PrefabInstance: {fileID: 6067781793364901444} + m_PrefabAsset: {fileID: 0} --- !u!1001 &6565363751102736699 PrefabInstance: m_ObjectHideFlags: 0 @@ -2355,6 +2371,16 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 8188317207052398481} m_Modifications: + - target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + propertyPath: m_RootReference + value: + objectReference: {fileID: 9115291448867436586} + - target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + propertyPath: m_AgentGameObject + value: + objectReference: {fileID: 9115291448867436586} - target: {fileID: 2598450485826216109, guid: ac01d0f42c5e1463e943632a60d99967, type: 3} propertyPath: m_Model @@ -2435,7 +2461,7 @@ MonoBehaviour: type: 3} m_PrefabInstance: {fileID: 6565363751102736699} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} + m_GameObject: {fileID: 9115291448867436586} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: d94a85eca2e074578943301959c555ba, type: 3} @@ -2447,6 +2473,12 @@ Transform: type: 3} m_PrefabInstance: {fileID: 6565363751102736699} m_PrefabAsset: {fileID: 0} +--- !u!1 &9115291448867436586 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 2710286047221272849, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + m_PrefabInstance: {fileID: 6565363751102736699} + m_PrefabAsset: {fileID: 0} --- !u!1001 &6716844123244810954 PrefabInstance: m_ObjectHideFlags: 0 @@ -2454,6 +2486,16 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 8188317207052398481} m_Modifications: + - target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + propertyPath: m_RootReference + value: + objectReference: {fileID: 8695281997955662811} + - target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + propertyPath: m_AgentGameObject + value: + objectReference: {fileID: 8695281997955662811} - target: {fileID: 2598450485826216109, guid: ac01d0f42c5e1463e943632a60d99967, type: 3} propertyPath: m_Model @@ -2534,7 +2576,7 @@ MonoBehaviour: type: 3} m_PrefabInstance: {fileID: 6716844123244810954} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} + m_GameObject: {fileID: 8695281997955662811} m_Enabled: 1 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: d94a85eca2e074578943301959c555ba, type: 3} @@ -2546,3 +2588,9 @@ Transform: type: 3} m_PrefabInstance: {fileID: 6716844123244810954} m_PrefabAsset: {fileID: 0} +--- !u!1 &8695281997955662811 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 2710286047221272849, guid: ac01d0f42c5e1463e943632a60d99967, + type: 3} + m_PrefabInstance: {fileID: 6716844123244810954} + m_PrefabAsset: {fileID: 0} diff --git a/Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity b/Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity index f6a12a0448..7450774ee4 100644 --- a/Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity +++ b/Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity @@ -816,6 +816,11 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: 4704531522807670703, guid: f5bbed44a6ea747a687fbbb738eb1730, + type: 3} + propertyPath: m_ShowGizmos + value: 0 + objectReference: {fileID: 0} - target: {fileID: 8188317207052398481, guid: f5bbed44a6ea747a687fbbb738eb1730, type: 3} propertyPath: m_RootOrder diff --git a/com.unity.ml-agents/Editor/GridSensorComponentEditor.cs b/com.unity.ml-agents/Editor/GridSensorComponentEditor.cs index 02e89b4696..fa9208e274 100644 --- a/com.unity.ml-agents/Editor/GridSensorComponentEditor.cs +++ b/com.unity.ml-agents/Editor/GridSensorComponentEditor.cs @@ -37,6 +37,7 @@ public override void OnInspectorGUI() gridSize.vector3IntValue = new Vector3Int(newGridSize.x, 1, newGridSize.z); } EditorGUI.EndDisabledGroup(); + EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_AgentGameObject)), true); EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_RotateWithAgent)), true); EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties()); diff --git a/com.unity.ml-agents/Runtime/Sensors/BoxOverlapChecker.cs b/com.unity.ml-agents/Runtime/Sensors/BoxOverlapChecker.cs index 93029722b2..f3183975ee 100644 --- a/com.unity.ml-agents/Runtime/Sensors/BoxOverlapChecker.cs +++ b/com.unity.ml-agents/Runtime/Sensors/BoxOverlapChecker.cs @@ -9,7 +9,8 @@ internal class BoxOverlapChecker Vector3Int m_GridSize; bool m_RotateWithAgent; LayerMask m_ColliderMask; - GameObject m_RootReference; + GameObject m_CenterObject; + GameObject m_AgentGameObject; string[] m_DetectableTags; int m_InitialColliderBufferSize; int m_MaxColliderBufferSize; @@ -32,7 +33,8 @@ public BoxOverlapChecker( Vector3Int gridSize, bool rotateWithAgent, LayerMask colliderMask, - GameObject rootReference, + GameObject centerObject, + GameObject agentGameObject, string[] detectableTags, int initialColliderBufferSize, int maxColliderBufferSize) @@ -41,7 +43,8 @@ public BoxOverlapChecker( m_GridSize = gridSize; m_RotateWithAgent = rotateWithAgent; m_ColliderMask = colliderMask; - m_RootReference = rootReference; + m_CenterObject = centerObject; + m_AgentGameObject = agentGameObject; m_DetectableTags = detectableTags; m_InitialColliderBufferSize = initialColliderBufferSize; m_MaxColliderBufferSize = maxColliderBufferSize; @@ -95,17 +98,17 @@ internal Vector3 GetCellGlobalPosition(int cellIndex) { if (m_RotateWithAgent) { - return m_RootReference.transform.TransformPoint(m_CellLocalPositions[cellIndex]); + return m_CenterObject.transform.TransformPoint(m_CellLocalPositions[cellIndex]); } else { - return m_CellLocalPositions[cellIndex] + m_RootReference.transform.position; + return m_CellLocalPositions[cellIndex] + m_CenterObject.transform.position; } } internal Quaternion GetGridRotation() { - return m_RotateWithAgent ? m_RootReference.transform.rotation : Quaternion.identity; + return m_RotateWithAgent ? m_CenterObject.transform.rotation : Quaternion.identity; } /// @@ -191,13 +194,13 @@ void ParseCollidersClosest(Collider[] foundColliders, int numFound, int cellInde var currentColliderGo = foundColliders[i].gameObject; // Continue if the current collider go is the root reference - if (ReferenceEquals(currentColliderGo, m_RootReference)) + if (ReferenceEquals(currentColliderGo, m_AgentGameObject)) { continue; } var closestColliderPoint = foundColliders[i].ClosestPointOnBounds(cellCenter); - var currentDistanceSquared = (closestColliderPoint - m_RootReference.transform.position).sqrMagnitude; + var currentDistanceSquared = (closestColliderPoint - m_CenterObject.transform.position).sqrMagnitude; if (currentDistanceSquared >= minDistanceSquared) { @@ -235,7 +238,7 @@ void ParseCollidersAll(Collider[] foundColliders, int numFound, int cellIndex, V for (int i = 0; i < numFound; i++) { var currentColliderGo = foundColliders[i].gameObject; - if (!ReferenceEquals(currentColliderGo, m_RootReference)) + if (!ReferenceEquals(currentColliderGo, m_AgentGameObject)) { detectedAction.Invoke(currentColliderGo, cellIndex); } diff --git a/com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs b/com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs index 6cf3628b09..6efe32c66e 100644 --- a/com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs +++ b/com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs @@ -72,6 +72,18 @@ public bool RotateWithAgent set { m_RotateWithAgent = value; } } + [HideInInspector, SerializeField] + internal GameObject m_AgentGameObject; + /// + /// The reference of the root of the agent. This is used to disambiguate objects with + /// the same tag as the agent. Defaults to current GameObject. + /// + public GameObject AgentGameObject + { + get { return (m_AgentGameObject == null ? gameObject : m_AgentGameObject); } + set { m_AgentGameObject = value; } + } + [HideInInspector, SerializeField] internal string[] m_DetectableTags; /// @@ -191,6 +203,7 @@ public override ISensor[] CreateSensors() m_RotateWithAgent, m_ColliderMask, gameObject, + AgentGameObject, m_DetectableTags, m_InitialColliderBufferSize, m_MaxColliderBufferSize diff --git a/com.unity.ml-agents/Tests/Runtime/Sensor/BoxOverlapCheckerTests.cs b/com.unity.ml-agents/Tests/Runtime/Sensor/BoxOverlapCheckerTests.cs index 7884414434..2c00494f1e 100644 --- a/com.unity.ml-agents/Tests/Runtime/Sensor/BoxOverlapCheckerTests.cs +++ b/com.unity.ml-agents/Tests/Runtime/Sensor/BoxOverlapCheckerTests.cs @@ -14,7 +14,8 @@ public TestBoxOverlapChecker( Vector3Int gridSize, bool rotateWithAgent, LayerMask colliderMask, - GameObject rootReference, + GameObject centerObject, + GameObject agentGameObject, string[] detectableTags, int initialColliderBufferSize, int maxColliderBufferSize @@ -23,7 +24,8 @@ int maxColliderBufferSize gridSize, rotateWithAgent, colliderMask, - rootReference, + centerObject, + agentGameObject, detectableTags, initialColliderBufferSize, maxColliderBufferSize) @@ -53,7 +55,8 @@ public static TestBoxOverlapChecker CreateChecker( int gridSizeX = 10, int gridSizeZ = 10, bool rotateWithAgent = true, - GameObject rootReference = null, + GameObject centerObject = null, + GameObject agentGameObject = null, string[] detectableTags = null, int initialColliderBufferSize = 4, int maxColliderBufferSize = 500) @@ -63,7 +66,8 @@ public static TestBoxOverlapChecker CreateChecker( new Vector3Int(gridSizeX, 1, gridSizeZ), rotateWithAgent, LayerMask.GetMask("Default"), - rootReference, + centerObject, + agentGameObject, detectableTags, initialColliderBufferSize, maxColliderBufferSize); @@ -77,7 +81,7 @@ public void TestCellLocalPosition() { var testGo = new GameObject("test"); testGo.transform.position = Vector3.zero; - var boxOverlapSquare = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, rootReference: testGo); + var boxOverlapSquare = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, agentGameObject: testGo); var localPos = boxOverlapSquare.CellLocalPositions; Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f), localPos[0]); @@ -88,7 +92,7 @@ public void TestCellLocalPosition() var testGo2 = new GameObject("test"); testGo2.transform.position = new Vector3(3.5f, 8f, 17f); // random, should have no effect on local positions - var boxOverlapRect = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, rootReference: testGo); + var boxOverlapRect = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, agentGameObject: testGo); localPos = boxOverlapRect.CellLocalPositions; Assert.AreEqual(new Vector3(-2f, 0, -7f), localPos[0]); @@ -104,7 +108,7 @@ public void TestCellGlobalPositionNoRotate() var testGo = new GameObject("test"); var position = new Vector3(3.5f, 8f, 17f); testGo.transform.position = position; - var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, rootReference: testGo); + var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, agentGameObject: testGo, centerObject: testGo); Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f) + position, boxOverlap.GetCellGlobalPosition(0)); Assert.AreEqual(new Vector3(-4.5f, 0, 4.5f) + position, boxOverlap.GetCellGlobalPosition(9)); @@ -126,7 +130,7 @@ public void TestCellGlobalPositionRotate() var testGo = new GameObject("test"); var position = new Vector3(15f, 6f, 13f); testGo.transform.position = position; - var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, rootReference: testGo); + var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, agentGameObject: testGo, centerObject: testGo); Assert.AreEqual(new Vector3(-2f, 0, -7f) + position, boxOverlap.GetCellGlobalPosition(0)); Assert.AreEqual(new Vector3(-2f, 0, 7f) + position, boxOverlap.GetCellGlobalPosition(14)); @@ -150,7 +154,7 @@ public void TestBufferResize() var testGo = new GameObject("test"); testGo.transform.position = Vector3.zero; testObjects.Add(testGo); - var boxOverlap = TestBoxOverlapChecker.CreateChecker(rootReference: testGo, initialColliderBufferSize: 2, maxColliderBufferSize: 5); + var boxOverlap = TestBoxOverlapChecker.CreateChecker(agentGameObject: testGo, centerObject: testGo, initialColliderBufferSize: 2, maxColliderBufferSize: 5); boxOverlap.Update(); Assert.AreEqual(2, boxOverlap.ColliderBuffer.Length); @@ -193,7 +197,8 @@ public void TestParseCollidersClosest() cellScaleZ: 10f, gridSizeX: 2, gridSizeZ: 2, - rootReference: testGo, + agentGameObject: testGo, + centerObject: testGo, detectableTags: new [] { tag1 }); var helper = new VerifyParseCollidersHelper(); boxOverlap.GridOverlapDetectedClosest += helper.DetectedAction; @@ -229,7 +234,8 @@ public void TestParseCollidersAll() cellScaleZ: 10f, gridSizeX: 2, gridSizeZ: 2, - rootReference: testGo, + agentGameObject: testGo, + centerObject: testGo, detectableTags: new [] { tag1 }); var helper = new VerifyParseCollidersHelper(); boxOverlap.GridOverlapDetectedAll += helper.DetectedAction; diff --git a/docs/Migrating.md b/docs/Migrating.md index 7d26042509..8397d2aeb3 100644 --- a/docs/Migrating.md +++ b/docs/Migrating.md @@ -117,7 +117,6 @@ will not work in newer version. Some errors might show up when loading the old s You'll need to remove the old sensor and create a new GridSensor. * These parameters names have changed but still refer to the same concept in the sensor: `GridNumSide` -> `GridSize`, `RotateToAgent` -> `RotateWithAgent`, `ObserveMask` -> `ColliderMask`, `DetectableObjects` -> `DetectableTags` -* `RootReference` is removed and the sensor component's GameObject will always be ignored for hit results. * `DepthType` (`ChanelBase`/`ChannelHot`) option and `ChannelDepth` are removed. Now the default is one-hot encoding for detected tag. If you were using original GridSensor without overriding any method, switching to new GridSensor will produce similar effect for training although the actual observations