@@ -236,22 +236,32 @@ public abstract class Agent : MonoBehaviour
236236 /// their own experience.
237237 int stepCount ;
238238
239- // Flag to signify that an agent has been reset but the fact that it is
240- // done has not been communicated (required for On Demand Decisions).
239+ /// Flag to signify that an agent has been reset but the fact that it is
240+ /// done has not been communicated (required for On Demand Decisions).
241241 bool hasAlreadyReset ;
242242
243- // Flag to signify that an agent is done and should not reset until
244- // the fact that it is done has been communicated.
243+ /// Flag to signify that an agent is done and should not reset until
244+ /// the fact that it is done has been communicated.
245245 bool terminate ;
246246
247247 /// Unique identifier each agent receives at initialization. It is used
248248 /// to separate between different agents in the environment.
249249 int id ;
250250
251+ /// Array of Texture2D used to render to from render buffer before
252+ /// transforming into float tensor.
253+ Texture2D [ ] textureArray ;
254+
251255 /// Monobehavior function that is called when the attached GameObject
252256 /// becomes enabled or active.
253257 void OnEnable ( )
254258 {
259+ textureArray = new Texture2D [ agentParameters . agentCameras . Count ] ;
260+ for ( int i = 0 ; i < brain . brainParameters . cameraResolutions . Length ; i ++ )
261+ {
262+ textureArray [ i ] = new Texture2D ( brain . brainParameters . cameraResolutions [ i ] . width ,
263+ brain . brainParameters . cameraResolutions [ i ] . height , TextureFormat . RGB24 , false ) ;
264+ }
255265 id = gameObject . GetInstanceID ( ) ;
256266 Academy academy = Object . FindObjectOfType < Academy > ( ) as Academy ;
257267 OnEnableHelper ( academy ) ;
@@ -561,10 +571,12 @@ void SendInfoToBrain()
561571
562572 for ( int i = 0 ; i < brain . brainParameters . cameraResolutions . Length ; i ++ )
563573 {
564- info . visualObservations . Add ( ObservationToTexture (
574+ ObservationToTexture (
565575 agentParameters . agentCameras [ i ] ,
566576 param . cameraResolutions [ i ] . width ,
567- param . cameraResolutions [ i ] . height ) ) ;
577+ param . cameraResolutions [ i ] . height ,
578+ ref textureArray [ i ] ) ;
579+ info . visualObservations . Add ( textureArray [ i ] ) ;
568580 }
569581
570582 info . reward = reward ;
@@ -926,37 +938,41 @@ void MakeRequests(int academyStepCounter)
926938 /// Converts a camera and correspinding resolution to a 2D texture.
927939 /// </summary>
928940 /// <returns>The 2D texture.</returns>
929- /// <param name="camera ">Camera.</param>
941+ /// <param name="obsCamera ">Camera.</param>
930942 /// <param name="width">Width of resulting 2D texture.</param>
931943 /// <param name="height">Height of resulting 2D texture.</param>
932- public static Texture2D ObservationToTexture ( Camera camera , int width , int height )
944+ /// <param name="texture2D">Texture2D to render to.</param>
945+ public static void ObservationToTexture ( Camera obsCamera , int width , int height , ref Texture2D texture2D )
933946 {
934- Rect oldRec = camera . rect ;
935- camera . rect = new Rect ( 0f , 0f , 1f , 1f ) ;
947+ Rect oldRec = obsCamera . rect ;
948+ obsCamera . rect = new Rect ( 0f , 0f , 1f , 1f ) ;
936949 var depth = 24 ;
937950 var format = RenderTextureFormat . Default ;
938951 var readWrite = RenderTextureReadWrite . Default ;
939952
940953 var tempRT =
941954 RenderTexture . GetTemporary ( width , height , depth , format , readWrite ) ;
942- var tex = new Texture2D ( width , height , TextureFormat . RGB24 , false ) ;
955+
956+ if ( width != texture2D . width || height != texture2D . height )
957+ {
958+ texture2D . Resize ( width , height ) ;
959+ }
943960
944961 var prevActiveRT = RenderTexture . active ;
945- var prevCameraRT = camera . targetTexture ;
962+ var prevCameraRT = obsCamera . targetTexture ;
946963
947964 // render to offscreen texture (readonly from CPU side)
948965 RenderTexture . active = tempRT ;
949- camera . targetTexture = tempRT ;
966+ obsCamera . targetTexture = tempRT ;
950967
951- camera . Render ( ) ;
968+ obsCamera . Render ( ) ;
952969
953- tex . ReadPixels ( new Rect ( 0 , 0 , tex . width , tex . height ) , 0 , 0 ) ;
954- tex . Apply ( ) ;
955- camera . targetTexture = prevCameraRT ;
956- camera . rect = oldRec ;
970+ texture2D . ReadPixels ( new Rect ( 0 , 0 , texture2D . width , texture2D . height ) , 0 , 0 ) ;
971+ texture2D . Apply ( ) ;
972+ obsCamera . targetTexture = prevCameraRT ;
973+ obsCamera . rect = oldRec ;
957974 RenderTexture . active = prevActiveRT ;
958975 RenderTexture . ReleaseTemporary ( tempRT ) ;
959- return tex ;
960976 }
961977 }
962978}
0 commit comments