Skip to content

Commit 1c405c8

Browse files
author
Ruo-Ping Dong
authored
Bring back root reference in grid sensor (#5300) (#5313)
1 parent 9e03966 commit 1c405c8

File tree

7 files changed

+99
-24
lines changed

7 files changed

+99
-24
lines changed

Diff for: Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/PushBlockCollabAreaGrid.prefab

+51-3
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,16 @@ PrefabInstance:
22612261
m_Modification:
22622262
m_TransformParent: {fileID: 8188317207052398481}
22632263
m_Modifications:
2264+
- target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967,
2265+
type: 3}
2266+
propertyPath: m_RootReference
2267+
value:
2268+
objectReference: {fileID: 8190299122290044757}
2269+
- target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967,
2270+
type: 3}
2271+
propertyPath: m_AgentGameObject
2272+
value:
2273+
objectReference: {fileID: 8190299122290044757}
22642274
- target: {fileID: 2598450485826216109, guid: ac01d0f42c5e1463e943632a60d99967,
22652275
type: 3}
22662276
propertyPath: m_Model
@@ -2336,7 +2346,7 @@ MonoBehaviour:
23362346
type: 3}
23372347
m_PrefabInstance: {fileID: 6067781793364901444}
23382348
m_PrefabAsset: {fileID: 0}
2339-
m_GameObject: {fileID: 0}
2349+
m_GameObject: {fileID: 8190299122290044757}
23402350
m_Enabled: 1
23412351
m_EditorHideFlags: 0
23422352
m_Script: {fileID: 11500000, guid: d94a85eca2e074578943301959c555ba, type: 3}
@@ -2348,13 +2358,29 @@ Transform:
23482358
type: 3}
23492359
m_PrefabInstance: {fileID: 6067781793364901444}
23502360
m_PrefabAsset: {fileID: 0}
2361+
--- !u!1 &8190299122290044757 stripped
2362+
GameObject:
2363+
m_CorrespondingSourceObject: {fileID: 2710286047221272849, guid: ac01d0f42c5e1463e943632a60d99967,
2364+
type: 3}
2365+
m_PrefabInstance: {fileID: 6067781793364901444}
2366+
m_PrefabAsset: {fileID: 0}
23512367
--- !u!1001 &6565363751102736699
23522368
PrefabInstance:
23532369
m_ObjectHideFlags: 0
23542370
serializedVersion: 2
23552371
m_Modification:
23562372
m_TransformParent: {fileID: 8188317207052398481}
23572373
m_Modifications:
2374+
- target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967,
2375+
type: 3}
2376+
propertyPath: m_RootReference
2377+
value:
2378+
objectReference: {fileID: 9115291448867436586}
2379+
- target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967,
2380+
type: 3}
2381+
propertyPath: m_AgentGameObject
2382+
value:
2383+
objectReference: {fileID: 9115291448867436586}
23582384
- target: {fileID: 2598450485826216109, guid: ac01d0f42c5e1463e943632a60d99967,
23592385
type: 3}
23602386
propertyPath: m_Model
@@ -2435,7 +2461,7 @@ MonoBehaviour:
24352461
type: 3}
24362462
m_PrefabInstance: {fileID: 6565363751102736699}
24372463
m_PrefabAsset: {fileID: 0}
2438-
m_GameObject: {fileID: 0}
2464+
m_GameObject: {fileID: 9115291448867436586}
24392465
m_Enabled: 1
24402466
m_EditorHideFlags: 0
24412467
m_Script: {fileID: 11500000, guid: d94a85eca2e074578943301959c555ba, type: 3}
@@ -2447,13 +2473,29 @@ Transform:
24472473
type: 3}
24482474
m_PrefabInstance: {fileID: 6565363751102736699}
24492475
m_PrefabAsset: {fileID: 0}
2476+
--- !u!1 &9115291448867436586 stripped
2477+
GameObject:
2478+
m_CorrespondingSourceObject: {fileID: 2710286047221272849, guid: ac01d0f42c5e1463e943632a60d99967,
2479+
type: 3}
2480+
m_PrefabInstance: {fileID: 6565363751102736699}
2481+
m_PrefabAsset: {fileID: 0}
24502482
--- !u!1001 &6716844123244810954
24512483
PrefabInstance:
24522484
m_ObjectHideFlags: 0
24532485
serializedVersion: 2
24542486
m_Modification:
24552487
m_TransformParent: {fileID: 8188317207052398481}
24562488
m_Modifications:
2489+
- target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967,
2490+
type: 3}
2491+
propertyPath: m_RootReference
2492+
value:
2493+
objectReference: {fileID: 8695281997955662811}
2494+
- target: {fileID: 1548337883655231979, guid: ac01d0f42c5e1463e943632a60d99967,
2495+
type: 3}
2496+
propertyPath: m_AgentGameObject
2497+
value:
2498+
objectReference: {fileID: 8695281997955662811}
24572499
- target: {fileID: 2598450485826216109, guid: ac01d0f42c5e1463e943632a60d99967,
24582500
type: 3}
24592501
propertyPath: m_Model
@@ -2534,7 +2576,7 @@ MonoBehaviour:
25342576
type: 3}
25352577
m_PrefabInstance: {fileID: 6716844123244810954}
25362578
m_PrefabAsset: {fileID: 0}
2537-
m_GameObject: {fileID: 0}
2579+
m_GameObject: {fileID: 8695281997955662811}
25382580
m_Enabled: 1
25392581
m_EditorHideFlags: 0
25402582
m_Script: {fileID: 11500000, guid: d94a85eca2e074578943301959c555ba, type: 3}
@@ -2546,3 +2588,9 @@ Transform:
25462588
type: 3}
25472589
m_PrefabInstance: {fileID: 6716844123244810954}
25482590
m_PrefabAsset: {fileID: 0}
2591+
--- !u!1 &8695281997955662811 stripped
2592+
GameObject:
2593+
m_CorrespondingSourceObject: {fileID: 2710286047221272849, guid: ac01d0f42c5e1463e943632a60d99967,
2594+
type: 3}
2595+
m_PrefabInstance: {fileID: 6716844123244810954}
2596+
m_PrefabAsset: {fileID: 0}

Diff for: Project/Assets/ML-Agents/Examples/PushBlock/Scenes/PushBlockCollab.unity

+5
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ PrefabInstance:
816816
m_Modification:
817817
m_TransformParent: {fileID: 0}
818818
m_Modifications:
819+
- target: {fileID: 4704531522807670703, guid: f5bbed44a6ea747a687fbbb738eb1730,
820+
type: 3}
821+
propertyPath: m_ShowGizmos
822+
value: 0
823+
objectReference: {fileID: 0}
819824
- target: {fileID: 8188317207052398481, guid: f5bbed44a6ea747a687fbbb738eb1730,
820825
type: 3}
821826
propertyPath: m_RootOrder

Diff for: com.unity.ml-agents/Editor/GridSensorComponentEditor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public override void OnInspectorGUI()
3737
gridSize.vector3IntValue = new Vector3Int(newGridSize.x, 1, newGridSize.z);
3838
}
3939
EditorGUI.EndDisabledGroup();
40+
EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_AgentGameObject)), true);
4041
EditorGUILayout.PropertyField(so.FindProperty(nameof(GridSensorComponent.m_RotateWithAgent)), true);
4142

4243
EditorGUI.BeginDisabledGroup(!EditorUtilities.CanUpdateModelProperties());

Diff for: com.unity.ml-agents/Runtime/Sensors/BoxOverlapChecker.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ internal class BoxOverlapChecker
99
Vector3Int m_GridSize;
1010
bool m_RotateWithAgent;
1111
LayerMask m_ColliderMask;
12-
GameObject m_RootReference;
12+
GameObject m_CenterObject;
13+
GameObject m_AgentGameObject;
1314
string[] m_DetectableTags;
1415
int m_InitialColliderBufferSize;
1516
int m_MaxColliderBufferSize;
@@ -32,7 +33,8 @@ public BoxOverlapChecker(
3233
Vector3Int gridSize,
3334
bool rotateWithAgent,
3435
LayerMask colliderMask,
35-
GameObject rootReference,
36+
GameObject centerObject,
37+
GameObject agentGameObject,
3638
string[] detectableTags,
3739
int initialColliderBufferSize,
3840
int maxColliderBufferSize)
@@ -41,7 +43,8 @@ public BoxOverlapChecker(
4143
m_GridSize = gridSize;
4244
m_RotateWithAgent = rotateWithAgent;
4345
m_ColliderMask = colliderMask;
44-
m_RootReference = rootReference;
46+
m_CenterObject = centerObject;
47+
m_AgentGameObject = agentGameObject;
4548
m_DetectableTags = detectableTags;
4649
m_InitialColliderBufferSize = initialColliderBufferSize;
4750
m_MaxColliderBufferSize = maxColliderBufferSize;
@@ -95,17 +98,17 @@ internal Vector3 GetCellGlobalPosition(int cellIndex)
9598
{
9699
if (m_RotateWithAgent)
97100
{
98-
return m_RootReference.transform.TransformPoint(m_CellLocalPositions[cellIndex]);
101+
return m_CenterObject.transform.TransformPoint(m_CellLocalPositions[cellIndex]);
99102
}
100103
else
101104
{
102-
return m_CellLocalPositions[cellIndex] + m_RootReference.transform.position;
105+
return m_CellLocalPositions[cellIndex] + m_CenterObject.transform.position;
103106
}
104107
}
105108

106109
internal Quaternion GetGridRotation()
107110
{
108-
return m_RotateWithAgent ? m_RootReference.transform.rotation : Quaternion.identity;
111+
return m_RotateWithAgent ? m_CenterObject.transform.rotation : Quaternion.identity;
109112
}
110113

111114
/// <summary>
@@ -191,13 +194,13 @@ void ParseCollidersClosest(Collider[] foundColliders, int numFound, int cellInde
191194
var currentColliderGo = foundColliders[i].gameObject;
192195

193196
// Continue if the current collider go is the root reference
194-
if (ReferenceEquals(currentColliderGo, m_RootReference))
197+
if (ReferenceEquals(currentColliderGo, m_AgentGameObject))
195198
{
196199
continue;
197200
}
198201

199202
var closestColliderPoint = foundColliders[i].ClosestPointOnBounds(cellCenter);
200-
var currentDistanceSquared = (closestColliderPoint - m_RootReference.transform.position).sqrMagnitude;
203+
var currentDistanceSquared = (closestColliderPoint - m_CenterObject.transform.position).sqrMagnitude;
201204

202205
if (currentDistanceSquared >= minDistanceSquared)
203206
{
@@ -235,7 +238,7 @@ void ParseCollidersAll(Collider[] foundColliders, int numFound, int cellIndex, V
235238
for (int i = 0; i < numFound; i++)
236239
{
237240
var currentColliderGo = foundColliders[i].gameObject;
238-
if (!ReferenceEquals(currentColliderGo, m_RootReference))
241+
if (!ReferenceEquals(currentColliderGo, m_AgentGameObject))
239242
{
240243
detectedAction.Invoke(currentColliderGo, cellIndex);
241244
}

Diff for: com.unity.ml-agents/Runtime/Sensors/GridSensorComponent.cs

+13
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ public bool RotateWithAgent
7272
set { m_RotateWithAgent = value; }
7373
}
7474

75+
[HideInInspector, SerializeField]
76+
internal GameObject m_AgentGameObject;
77+
/// <summary>
78+
/// The reference of the root of the agent. This is used to disambiguate objects with
79+
/// the same tag as the agent. Defaults to current GameObject.
80+
/// </summary>
81+
public GameObject AgentGameObject
82+
{
83+
get { return (m_AgentGameObject == null ? gameObject : m_AgentGameObject); }
84+
set { m_AgentGameObject = value; }
85+
}
86+
7587
[HideInInspector, SerializeField]
7688
internal string[] m_DetectableTags;
7789
/// <summary>
@@ -191,6 +203,7 @@ public override ISensor[] CreateSensors()
191203
m_RotateWithAgent,
192204
m_ColliderMask,
193205
gameObject,
206+
AgentGameObject,
194207
m_DetectableTags,
195208
m_InitialColliderBufferSize,
196209
m_MaxColliderBufferSize

Diff for: com.unity.ml-agents/Tests/Runtime/Sensor/BoxOverlapCheckerTests.cs

+17-11
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public TestBoxOverlapChecker(
1414
Vector3Int gridSize,
1515
bool rotateWithAgent,
1616
LayerMask colliderMask,
17-
GameObject rootReference,
17+
GameObject centerObject,
18+
GameObject agentGameObject,
1819
string[] detectableTags,
1920
int initialColliderBufferSize,
2021
int maxColliderBufferSize
@@ -23,7 +24,8 @@ int maxColliderBufferSize
2324
gridSize,
2425
rotateWithAgent,
2526
colliderMask,
26-
rootReference,
27+
centerObject,
28+
agentGameObject,
2729
detectableTags,
2830
initialColliderBufferSize,
2931
maxColliderBufferSize)
@@ -53,7 +55,8 @@ public static TestBoxOverlapChecker CreateChecker(
5355
int gridSizeX = 10,
5456
int gridSizeZ = 10,
5557
bool rotateWithAgent = true,
56-
GameObject rootReference = null,
58+
GameObject centerObject = null,
59+
GameObject agentGameObject = null,
5760
string[] detectableTags = null,
5861
int initialColliderBufferSize = 4,
5962
int maxColliderBufferSize = 500)
@@ -63,7 +66,8 @@ public static TestBoxOverlapChecker CreateChecker(
6366
new Vector3Int(gridSizeX, 1, gridSizeZ),
6467
rotateWithAgent,
6568
LayerMask.GetMask("Default"),
66-
rootReference,
69+
centerObject,
70+
agentGameObject,
6771
detectableTags,
6872
initialColliderBufferSize,
6973
maxColliderBufferSize);
@@ -77,7 +81,7 @@ public void TestCellLocalPosition()
7781
{
7882
var testGo = new GameObject("test");
7983
testGo.transform.position = Vector3.zero;
80-
var boxOverlapSquare = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, rootReference: testGo);
84+
var boxOverlapSquare = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, agentGameObject: testGo);
8185

8286
var localPos = boxOverlapSquare.CellLocalPositions;
8387
Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f), localPos[0]);
@@ -88,7 +92,7 @@ public void TestCellLocalPosition()
8892

8993
var testGo2 = new GameObject("test");
9094
testGo2.transform.position = new Vector3(3.5f, 8f, 17f); // random, should have no effect on local positions
91-
var boxOverlapRect = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, rootReference: testGo);
95+
var boxOverlapRect = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, agentGameObject: testGo);
9296

9397
localPos = boxOverlapRect.CellLocalPositions;
9498
Assert.AreEqual(new Vector3(-2f, 0, -7f), localPos[0]);
@@ -104,7 +108,7 @@ public void TestCellGlobalPositionNoRotate()
104108
var testGo = new GameObject("test");
105109
var position = new Vector3(3.5f, 8f, 17f);
106110
testGo.transform.position = position;
107-
var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, rootReference: testGo);
111+
var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 10, gridSizeZ: 10, rotateWithAgent: false, agentGameObject: testGo, centerObject: testGo);
108112

109113
Assert.AreEqual(new Vector3(-4.5f, 0, -4.5f) + position, boxOverlap.GetCellGlobalPosition(0));
110114
Assert.AreEqual(new Vector3(-4.5f, 0, 4.5f) + position, boxOverlap.GetCellGlobalPosition(9));
@@ -126,7 +130,7 @@ public void TestCellGlobalPositionRotate()
126130
var testGo = new GameObject("test");
127131
var position = new Vector3(15f, 6f, 13f);
128132
testGo.transform.position = position;
129-
var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, rootReference: testGo);
133+
var boxOverlap = TestBoxOverlapChecker.CreateChecker(gridSizeX: 5, gridSizeZ: 15, rotateWithAgent: true, agentGameObject: testGo, centerObject: testGo);
130134

131135
Assert.AreEqual(new Vector3(-2f, 0, -7f) + position, boxOverlap.GetCellGlobalPosition(0));
132136
Assert.AreEqual(new Vector3(-2f, 0, 7f) + position, boxOverlap.GetCellGlobalPosition(14));
@@ -150,7 +154,7 @@ public void TestBufferResize()
150154
var testGo = new GameObject("test");
151155
testGo.transform.position = Vector3.zero;
152156
testObjects.Add(testGo);
153-
var boxOverlap = TestBoxOverlapChecker.CreateChecker(rootReference: testGo, initialColliderBufferSize: 2, maxColliderBufferSize: 5);
157+
var boxOverlap = TestBoxOverlapChecker.CreateChecker(agentGameObject: testGo, centerObject: testGo, initialColliderBufferSize: 2, maxColliderBufferSize: 5);
154158
boxOverlap.Update();
155159
Assert.AreEqual(2, boxOverlap.ColliderBuffer.Length);
156160

@@ -193,7 +197,8 @@ public void TestParseCollidersClosest()
193197
cellScaleZ: 10f,
194198
gridSizeX: 2,
195199
gridSizeZ: 2,
196-
rootReference: testGo,
200+
agentGameObject: testGo,
201+
centerObject: testGo,
197202
detectableTags: new [] { tag1 });
198203
var helper = new VerifyParseCollidersHelper();
199204
boxOverlap.GridOverlapDetectedClosest += helper.DetectedAction;
@@ -229,7 +234,8 @@ public void TestParseCollidersAll()
229234
cellScaleZ: 10f,
230235
gridSizeX: 2,
231236
gridSizeZ: 2,
232-
rootReference: testGo,
237+
agentGameObject: testGo,
238+
centerObject: testGo,
233239
detectableTags: new [] { tag1 });
234240
var helper = new VerifyParseCollidersHelper();
235241
boxOverlap.GridOverlapDetectedAll += helper.DetectedAction;

Diff for: docs/Migrating.md

-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ will not work in newer version. Some errors might show up when loading the old s
117117
You'll need to remove the old sensor and create a new GridSensor.
118118
* These parameters names have changed but still refer to the same concept in the sensor: `GridNumSide` -> `GridSize`,
119119
`RotateToAgent` -> `RotateWithAgent`, `ObserveMask` -> `ColliderMask`, `DetectableObjects` -> `DetectableTags`
120-
* `RootReference` is removed and the sensor component's GameObject will always be ignored for hit results.
121120
* `DepthType` (`ChanelBase`/`ChannelHot`) option and `ChannelDepth` are removed. Now the default is
122121
one-hot encoding for detected tag. If you were using original GridSensor without overriding any method,
123122
switching to new GridSensor will produce similar effect for training although the actual observations

0 commit comments

Comments
 (0)