|
using System; |
|
using UnityEngine; |
|
|
|
namespace Unity.MLAgents.Sensors |
|
{ |
|
|
|
|
|
|
|
public class RenderTextureSensor : ISensor, IBuiltInSensor, IDisposable |
|
{ |
|
RenderTexture m_RenderTexture; |
|
bool m_Grayscale; |
|
string m_Name; |
|
private ObservationSpec m_ObservationSpec; |
|
SensorCompressionType m_CompressionType; |
|
Texture2D m_Texture; |
|
|
|
|
|
|
|
|
|
public SensorCompressionType CompressionType |
|
{ |
|
get { return m_CompressionType; } |
|
set { m_CompressionType = value; } |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public RenderTextureSensor( |
|
RenderTexture renderTexture, bool grayscale, string name, SensorCompressionType compressionType) |
|
{ |
|
m_RenderTexture = renderTexture; |
|
var width = renderTexture != null ? renderTexture.width : 0; |
|
var height = renderTexture != null ? renderTexture.height : 0; |
|
m_Grayscale = grayscale; |
|
m_Name = name; |
|
m_ObservationSpec = ObservationSpec.Visual(height, width, grayscale ? 1 : 3); |
|
m_CompressionType = compressionType; |
|
m_Texture = new Texture2D(width, height, TextureFormat.RGB24, false); |
|
} |
|
|
|
|
|
public string GetName() |
|
{ |
|
return m_Name; |
|
} |
|
|
|
|
|
public ObservationSpec GetObservationSpec() |
|
{ |
|
return m_ObservationSpec; |
|
} |
|
|
|
|
|
public byte[] GetCompressedObservation() |
|
{ |
|
using (TimerStack.Instance.Scoped("RenderTextureSensor.GetCompressedObservation")) |
|
{ |
|
ObservationToTexture(m_RenderTexture, m_Texture); |
|
|
|
var compressed = m_Texture.EncodeToPNG(); |
|
return compressed; |
|
} |
|
} |
|
|
|
|
|
public int Write(ObservationWriter writer) |
|
{ |
|
using (TimerStack.Instance.Scoped("RenderTextureSensor.Write")) |
|
{ |
|
ObservationToTexture(m_RenderTexture, m_Texture); |
|
var numWritten = writer.WriteTexture(m_Texture, m_Grayscale); |
|
return numWritten; |
|
} |
|
} |
|
|
|
|
|
public void Update() { } |
|
|
|
|
|
public void Reset() { } |
|
|
|
|
|
public CompressionSpec GetCompressionSpec() |
|
{ |
|
return new CompressionSpec(m_CompressionType); |
|
} |
|
|
|
|
|
public BuiltInSensorType GetBuiltInSensorType() |
|
{ |
|
return BuiltInSensorType.RenderTextureSensor; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ObservationToTexture(RenderTexture obsTexture, Texture2D texture2D) |
|
{ |
|
var prevActiveRt = RenderTexture.active; |
|
RenderTexture.active = obsTexture; |
|
|
|
texture2D.ReadPixels(new Rect(0, 0, texture2D.width, texture2D.height), 0, 0); |
|
texture2D.Apply(); |
|
RenderTexture.active = prevActiveRt; |
|
} |
|
|
|
|
|
|
|
|
|
public void Dispose() |
|
{ |
|
if (!ReferenceEquals(null, m_Texture)) |
|
{ |
|
Utilities.DestroyTexture(m_Texture); |
|
m_Texture = null; |
|
} |
|
} |
|
} |
|
} |
|
|