using UnityEngine;
using UnityEngine.Serialization;
namespace Unity.MLAgents.Sensors
{
///
/// A SensorComponent that creates a .
///
[AddComponentMenu("ML Agents/Camera Sensor", (int)MenuGroup.Sensors)]
public class CameraSensorComponent : SensorComponent
{
[HideInInspector, SerializeField, FormerlySerializedAs("camera")]
Camera m_Camera;
CameraSensor m_Sensor;
///
/// Camera object that provides the data to the sensor.
///
public Camera Camera
{
get { return m_Camera; }
set { m_Camera = value; UpdateSensor(); }
}
[HideInInspector, SerializeField, FormerlySerializedAs("sensorName")]
string m_SensorName = "CameraSensor";
///
/// Name of the generated object.
/// Note that changing this at runtime does not affect how the Agent sorts the sensors.
///
public string SensorName
{
get { return m_SensorName; }
set { m_SensorName = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("width")]
int m_Width = 84;
///
/// Width of the generated observation.
/// Note that changing this after the sensor is created has no effect.
///
public int Width
{
get { return m_Width; }
set { m_Width = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("height")]
int m_Height = 84;
///
/// Height of the generated observation.
/// Note that changing this after the sensor is created has no effect.
///
public int Height
{
get { return m_Height; }
set { m_Height = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("grayscale")]
bool m_Grayscale;
///
/// Whether to generate grayscale images or color.
/// Note that changing this after the sensor is created has no effect.
///
public bool Grayscale
{
get { return m_Grayscale; }
set { m_Grayscale = value; }
}
[HideInInspector, SerializeField]
ObservationType m_ObservationType;
///
/// The type of the observation.
///
public ObservationType ObservationType
{
get { return m_ObservationType; }
set { m_ObservationType = value; UpdateSensor(); }
}
[HideInInspector, SerializeField]
[Range(1, 50)]
[Tooltip("Number of camera frames that will be stacked before being fed to the neural network.")]
int m_ObservationStacks = 1;
[HideInInspector, SerializeField, FormerlySerializedAs("compression")]
SensorCompressionType m_Compression = SensorCompressionType.PNG;
///
/// The compression type to use for the sensor.
///
public SensorCompressionType CompressionType
{
get { return m_Compression; }
set { m_Compression = value; UpdateSensor(); }
}
///
/// Whether to stack previous observations. Using 1 means no previous observations.
/// Note that changing this after the sensor is created has no effect.
///
public int ObservationStacks
{
get { return m_ObservationStacks; }
set { m_ObservationStacks = value; }
}
///
/// Creates the
///
/// The created object for this component.
public override ISensor[] CreateSensors()
{
m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, Grayscale, m_SensorName, m_Compression, m_ObservationType);
if (ObservationStacks != 1)
{
return new ISensor[] { new StackingSensor(m_Sensor, ObservationStacks) };
}
return new ISensor[] { m_Sensor };
}
///
/// Update fields that are safe to change on the Sensor at runtime.
///
internal void UpdateSensor()
{
if (m_Sensor != null)
{
m_Sensor.Camera = m_Camera;
m_Sensor.CompressionType = m_Compression;
}
}
}
}