Skip to content

RaycastPerceptionComponent custom editor #3484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.ml-agents/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `DemonstrationRecorder` now has an optional path for the demonstrations. This will default to `Application.dataPath` if not set.
- `DemonstrationStore` was changed to accept a `Stream` for its constructor, and was renamed to `DemonstrationWriter`
- The method `GetStepCount()` on the Agent class has been replaced with the property getter `StepCount`
- `RayPerceptionSensorComponent` and related classes now display the debug gizmos whenever the Agent is selected (not just Play mode).
- Most fields on `RayPerceptionSensorComponent` can now be changed while the editor is in Play mode. The exceptions to this are fields that affect the number of observations.

### Bugfixes
- Fixed an issue which caused self-play training sessions to consume a lot of memory. (#3451)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using UnityEngine;
using UnityEditor;
using Barracuda;

namespace MLAgents
{
internal class RayPerceptionSensorComponentBaseEditor : Editor
{
bool m_RequireSensorUpdate;

protected void OnRayPerceptionInspectorGUI(bool is3d)
{
var so = serializedObject;
so.Update();

// Drawing the RayPerceptionSensorComponent
EditorGUI.BeginChangeCheck();
EditorGUI.indentLevel++;

EditorGUILayout.PropertyField(so.FindProperty("m_SensorName"), true);

// Because the number of rays and the tags affect the observation shape,
// they are not editable during play mode.
EditorGUI.BeginDisabledGroup(Application.isPlaying);
{
EditorGUILayout.PropertyField(so.FindProperty("m_DetectableTags"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_RaysPerDirection"), true);
}
EditorGUI.EndDisabledGroup();

EditorGUILayout.PropertyField(so.FindProperty("m_MaxRayDegrees"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_SphereCastRadius"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_RayLength"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_RayLayerMask"), true);

// Because the number of observation stacks affects the observation shape,
// it is not editable during play mode.
EditorGUI.BeginDisabledGroup(Application.isPlaying);
{
EditorGUILayout.PropertyField(so.FindProperty("m_ObservationStacks"), true);
}
EditorGUI.EndDisabledGroup();

if (is3d)
{
EditorGUILayout.PropertyField(so.FindProperty("m_StartVerticalOffset"), true);
EditorGUILayout.PropertyField(so.FindProperty("m_EndVerticalOffset"), true);
}

EditorGUILayout.PropertyField(so.FindProperty("rayHitColor"), true);
EditorGUILayout.PropertyField(so.FindProperty("rayMissColor"), true);

EditorGUI.indentLevel--;
if (EditorGUI.EndChangeCheck())
{
m_RequireSensorUpdate = true;
}

UpdateSensorIfDirty();
so.ApplyModifiedProperties();
}


void UpdateSensorIfDirty()
{
if (m_RequireSensorUpdate)
{
var sensorComponent = serializedObject.targetObject as RayPerceptionSensorComponentBase;
sensorComponent?.UpdateSensor();
m_RequireSensorUpdate = false;
}
}
}

[CustomEditor(typeof(RayPerceptionSensorComponent2D))]
[CanEditMultipleObjects]
internal class RayPerceptionSensorComponent2DEditor : RayPerceptionSensorComponentBaseEditor
{
public override void OnInspectorGUI()
{
OnRayPerceptionInspectorGUI(false);
}
}

[CustomEditor(typeof(RayPerceptionSensorComponent3D))]
[CanEditMultipleObjects]
internal class RayPerceptionSensorComponent3DEditor : RayPerceptionSensorComponentBaseEditor
{
public override void OnInspectorGUI()
{
OnRayPerceptionInspectorGUI(true);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion com.unity.ml-agents/Runtime/Sensor/RayPerceptionSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ public RayPerceptionSensor(string name, RayPerceptionInput rayInput)
}
}

internal void SetRayPerceptionInput(RayPerceptionInput input)
{
// TODO make sure that number of rays and tags don't change
m_RayPerceptionInput = input;
}

public int Write(WriteAdapter adapter)
{
using (TimerStack.Instance.Scoped("RayPerceptionSensor.Perceive"))
Expand Down Expand Up @@ -331,7 +337,7 @@ public static RayPerceptionOutput PerceiveStatic(RayPerceptionInput input)
/// <param name="rayIndex"></param>
/// <param name="debugRayOut"></param>
/// <returns></returns>
static RayPerceptionOutput.RayOutput PerceiveSingleRay(
internal static RayPerceptionOutput.RayOutput PerceiveSingleRay(
RayPerceptionInput input,
int rayIndex,
out DebugDisplayInfo.RayInfo debugRayOut
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
using System;
using UnityEngine;
using UnityEngine.Serialization;

namespace MLAgents
{
[AddComponentMenu("ML Agents/Ray Perception Sensor 3D", (int)MenuGroup.Sensors)]
public class RayPerceptionSensorComponent3D : RayPerceptionSensorComponentBase
{
[Header("3D Properties", order = 100)]
[HideInInspector]
[SerializeField]
[FormerlySerializedAs("startVerticalOffset")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: attributes should go on the same line if they fit.

[Range(-10f, 10f)]
[Tooltip("Ray start is offset up or down by this amount.")]
public float startVerticalOffset;
float m_StartVerticalOffset;
public float startVerticalOffset
{
get => m_StartVerticalOffset;
set { m_StartVerticalOffset = value; UpdateSensor(); }
}

[HideInInspector]
[SerializeField]
[FormerlySerializedAs("endVerticalOffset")]
[Range(-10f, 10f)]
[Tooltip("Ray end is offset up or down by this amount.")]
public float endVerticalOffset;
float m_EndVerticalOffset;
public float endVerticalOffset
{
get => m_EndVerticalOffset;
set { m_EndVerticalOffset = value; UpdateSensor(); }
}

public override RayPerceptionCastType GetCastType()
{
Expand Down
Loading