浏览代码

Get grid sensor working again.

/develop/input-actuator-tanks
Christopher Goy 4 年前
当前提交
20c5c293
共有 6 个文件被更改,包括 109 次插入93 次删除
  1. 8
      ML-Agents-Input-Example/Assets/Samples/Input System/1.0.2/Tanks Demo/Scenes/NewInput.unity
  2. 1
      ML-Agents-Input-Example/Assets/Samples/Input System/1.0.2/Tanks Demo/Scripts/Managers/TankManager.cs
  3. 6
      ML-Agents-Input-Example/Packages/manifest.json
  4. 33
      ML-Agents-Input-Example/Packages/packages-lock.json
  5. 4
      ML-Agents-Input-Example/ProjectSettings/ProjectVersion.txt
  6. 150
      com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs

8
ML-Agents-Input-Example/Assets/Samples/Input System/1.0.2/Tanks Demo/Scenes/NewInput.unity


m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &416936944
GameObject:

objectReference: {fileID: 0}
- target: {fileID: 488946, guid: 312c52d27be2d2d42acea3ccebc4694b, type: 3}
propertyPath: m_RootOrder
value: 3
value: 1
objectReference: {fileID: 0}
- target: {fileID: 488946, guid: 312c52d27be2d2d42acea3ccebc4694b, type: 3}
propertyPath: m_LocalPosition.x

coloredPlayerText:
instance: {fileID: 0}
wins: 0
isHeuristic: 1
isHeuristic: 0
- playerColor: {r: 0.8980392, g: 0.18039216, b: 0.15686275, a: 0}
spawnPoint: {fileID: 2083660209}
playerNumber: 2

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

1
ML-Agents-Input-Example/Assets/Samples/Input System/1.0.2/Tanks Demo/Scripts/Managers/TankManager.cs


var bp = instance.GetComponent<BehaviorParameters>();
bp.BehaviorType = BehaviorType.HeuristicOnly;
Reset(instance.transform);
instance.GetComponent<InputActuatorComponent>().UpdateDeviceBinding(true);
}
// Set the player numbers to be consistent across the scripts.

6
ML-Agents-Input-Example/Packages/manifest.json


"dependencies": {
"com.unity.collab-proxy": "1.2.16",
"com.unity.ide.rider": "1.1.4",
"com.unity.ide.vscode": "1.2.1",
"com.unity.ide.vscode": "1.2.3",
"com.unity.test-framework": "1.1.14",
"com.unity.test-framework": "1.1.22",
"com.unity.timeline": "1.2.15",
"com.unity.timeline": "1.2.18",
"com.unity.ugui": "1.0.0",
"com.unity.modules.ai": "1.0.0",
"com.unity.modules.androidjni": "1.0.0",

33
ML-Agents-Input-Example/Packages/packages-lock.json


{
"dependencies": {
"com.unity.barracuda": {
"version": "1.3.0-preview",
"version": "1.3.2-preview",
"depth": 1,
"source": "registry",
"dependencies": {

"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.3.4",
"depth": 2,
"version": "1.4.6",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1"

},
"com.unity.ext.nunit": {
"version": "1.0.6",
"depth": 2,
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"

"url": "https://packages.unity.com"
},
"com.unity.ide.vscode": {
"version": "1.2.1",
"version": "1.2.3",
"depth": 0,
"source": "registry",
"dependencies": {},

},
"com.unity.mathematics": {
"version": "1.2.1",
"depth": 3,
"depth": 2,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"

"depth": 0,
"source": "local",
"dependencies": {
"com.unity.barracuda": "1.3.0-preview",
"com.unity.barracuda": "1.3.2-preview",
"com.unity.modules.physics2d": "1.0.0",
"com.unity.modules.unityanalytics": "1.0.0"
"com.unity.modules.physics2d": "1.0.0"
}
},
"com.unity.ml-agents.extensions": {

"dependencies": {
"com.unity.ml-agents": "1.7.2-preview"
"com.unity.ml-agents": "1.9.0-preview",
"com.unity.burst": "1.4.6"
"version": "1.1.14",
"version": "1.1.22",
"com.unity.ext.nunit": "1.0.0",
"com.unity.ext.nunit": "1.0.6",
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0"
},

"url": "https://packages.unity.com"
},
"com.unity.timeline": {
"version": "1.2.15",
"version": "1.2.18",
"dependencies": {},
"dependencies": {
"com.unity.modules.director": "1.0.0",
"com.unity.modules.animation": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.particlesystem": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.ugui": {

4
ML-Agents-Input-Example/ProjectSettings/ProjectVersion.txt


m_EditorVersion: 2019.4.7f1
m_EditorVersionWithRevision: 2019.4.7f1 (e992b1a16e65)
m_EditorVersion: 2019.4.21f1
m_EditorVersionWithRevision: 2019.4.21f1 (b76dac84db26)

150
com.unity.ml-agents.extensions/Runtime/Sensors/GridSensor.cs


using Unity.Jobs;
using UnityEngine.Jobs;
using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;
#if UNITY_EDITOR
using UnityEditor;
#endif

var sphereRadiusX = (CellScaleX * GridNumSideX) / Mathf.Sqrt(2);
var sphereRadiusZ = (CellScaleZ * GridNumSideZ) / Mathf.Sqrt(2);
InverseSphereRadius = 1.0f / Mathf.Max(sphereRadiusX, sphereRadiusZ);
if (ChannelOffsets.IsCreated)
{
ChannelOffsets.Dispose();
}
DisposeNativeArray(ChannelOffsets);
ChannelOffsets = new NativeArray<int>(ChannelDepth.Length, Allocator.Persistent);
DiffNumSideZX = (GridNumSideZ - GridNumSideX);
OffsetGridNumSide = (GridNumSideZ - 1f) / 2f;

if (m_PerceptionBuffer.IsCreated)
{
m_boxcastJobHandle.Complete();
m_PerceptionBuffer.Dispose();
DisposeNativeArray(m_PerceptionBuffer);
m_PerceptionBuffer = new NativeArray<float>(NumberOfObservations, Allocator.Persistent);
if (gridDepthType == GridDepthType.ChannelHot)
{

NumImages += 1;
m_ChannelBuffer = new float[ChannelDepth.Length];
if (CellActivity.IsCreated)
{
CellActivity.Dispose();
}
DisposeNativeArray(CellActivity);
void OnEnable()
{
Start();
}
/// <summary>
/// Calls the initialization methods. Creates the data storing properties used to send the data
/// Establishes

InitDepthType();
InitCellPoints();
InitPerceptionBuffer();
if (m_Hits.IsCreated)
{
m_Hits.Dispose();
}
DisposeNativeArray(m_Hits);
if (m_HitIndexes.IsCreated)
{
m_HitIndexes.Dispose();
}
DisposeNativeArray(m_HitIndexes);
if (m_BoxcastCommands.IsCreated)
{
m_BoxcastCommands.Dispose();
}
DisposeNativeArray(m_BoxcastCommands);
m_BoxcastCommands = new NativeArray<BoxcastCommand>(NumCells, Allocator.Persistent);
for (var i = 0; i < DetectableObjects.Length; i++)

void OnDisable()
{
m_Hits.Dispose();
m_HitIndexes.Dispose();
m_BoxcastCommands.Dispose();
m_PerceptionBuffer.Dispose();
CellActivity.Dispose();
CellPoints.Dispose();
m_ChannelHotDefaultPerceptionBuffer.Dispose();
ChannelOffsets.Dispose();
}
void OnDestroy()
{
DisposeNativeArray(m_Hits);
DisposeNativeArray(m_HitIndexes);
DisposeNativeArray(m_BoxcastCommands);
DisposeNativeArray(CellActivity);
DisposeNativeArray(CellPoints);
DisposeNativeArray(m_ChannelHotDefaultPerceptionBuffer);
DisposeNativeArray(ChannelOffsets);
}
static void DisposeNativeArray<TD>(NativeArray<TD> array)
where TD : struct
{
if (array.IsCreated)
{
array.Dispose();
}
}
/// <summary>Gets the shape of the grid observation</summary>

{
var cellCenter = Pos + CellPoints[index];
var rotation = Quaternion.identity;
Commands[index] = new BoxcastCommand(new Vector3(cellCenter.x, cellCenter.y - 2.0f, cellCenter.z),
Commands[index] = new BoxcastCommand(new Vector3(cellCenter.x, cellCenter.y + 2.0f, cellCenter.z),
Vector3.up,
2.0f,
Vector3.down,
10.0f,
ObserveMask);
}

{
var cellCenter = Mat.MultiplyPoint(CellPoints[index]);
var rotation = Rotation;
Commands[index] = new BoxcastCommand(new Vector3(cellCenter.x, cellCenter.y - 2.0f, cellCenter.z),
Commands[index] = new BoxcastCommand(new Vector3(cellCenter.x, cellCenter.y + 7.0f, cellCenter.z),
Vector3.up,
2.0f,
Vector3.down,
10.0f,
ObserveMask);
}

Profiler.BeginSample("GridSensor.Perceive");
{
m_boxcastJobHandle.Complete();
JobHandle clearHandle;
if (gridDepthType == GridDepthType.ChannelHot)
{

CellActivity = CellActivity,
DebugDefault = DebugDefaultColor
};
clearHandle = JobHandle.CombineDependencies(clearHandle, gizmoJob.Schedule(NumCells, NumCells / 12));
var gizmoHandle = gizmoJob.Schedule(NumCells, NumCells / 12);
clearHandle = JobHandle.CombineDependencies(clearHandle, gizmoHandle);
}
var t = transform;

/// <param name="cellCenter">The center position of the cell</param>
protected virtual void ParseColliders(Collider[] foundColliders, int numFound, int cellIndex, Vector3 cellCenter)
{
Profiler.BeginSample("GridSensor.ParseColliders");
GameObject closestColliderGo = null;
var minDistanceSquared = float.MaxValue;
var detectableIndex = -1;
for (var i = 0; i < numFound; i++)
{
if (ReferenceEquals(foundColliders[i], null))
{
continue;
}
var currentColliderGo = foundColliders[i].gameObject;
// Continue if the current collider go is the root reference
if (ReferenceEquals(currentColliderGo, rootReference))
continue;
var closestColliderPoint = foundColliders[i].ClosestPointOnBounds(cellCenter);
var currentDistanceSquared = (closestColliderPoint - rootReference.transform.position).sqrMagnitude;
Profiler.EndSample();
// Checks if our colliders contain a detectable object
if (m_DetectableObjectToIndex.TryGetValue(currentColliderGo.tag, out detectableIndex) && currentDistanceSquared < minDistanceSquared)
{
minDistanceSquared = currentDistanceSquared;
closestColliderGo = currentColliderGo;
}
}
if (!ReferenceEquals(closestColliderGo, null))
LoadObjectData(closestColliderGo, cellIndex, detectableIndex, (float)Math.Sqrt(minDistanceSquared) * InverseSphereRadius);
Profiler.EndSample();
// Profiler.BeginSample("GridSensor.ParseColliders");
// GameObject closestColliderGo = null;
// var minDistanceSquared = float.MaxValue;
//
// var detectableIndex = -1;
// for (var i = 0; i < numFound; i++)
// {
// if (ReferenceEquals(foundColliders[i], null))
// {
// continue;
// }
// var currentColliderGo = foundColliders[i].gameObject;
//
// // Continue if the current collider go is the root reference
// if (ReferenceEquals(currentColliderGo, rootReference))
// continue;
//
// var closestColliderPoint = foundColliders[i].ClosestPointOnBounds(cellCenter);
// var currentDistanceSquared = (closestColliderPoint - rootReference.transform.position).sqrMagnitude;
// Profiler.EndSample();
//
// // Checks if our colliders contain a detectable object
// if (m_DetectableObjectToIndex.TryGetValue(currentColliderGo.tag, out detectableIndex) && currentDistanceSquared < minDistanceSquared)
// {
// }
// for (var ii = 0; ii < DetectableObjects.Length; ii++)
// {
// if (currentColliderGo.CompareTag(DetectableObjects[ii]))
// {
// minDistanceSquared = currentDistanceSquared;
// closestColliderGo = currentColliderGo;
// break;
// }
// }
// }
//
// if (!ReferenceEquals(closestColliderGo, null))
// LoadObjectData(closestColliderGo, cellIndex, detectableIndex, (float)Math.Sqrt(minDistanceSquared) * InverseSphereRadius);
// Profiler.EndSample();
}
/// <summary>

UpdateBufferFromJob();
var scale = new Vector3(CellScaleX, CellScaleY, CellScaleZ);
var offset = new Vector3(0, GizmoYOffset, 0);
var offset = new Vector3(0, 7.0f, 0);
var oldGizmoMatrix = Gizmos.matrix;
for (var i = 0; i < NumCells; i++)
{

正在加载...
取消
保存