浏览代码

Fix for memory leak

/hotfix-v0.9.2a
Arthur Juliani 7 年前
当前提交
9dd4a81b
共有 1 个文件被更改,包括 34 次插入18 次删除
  1. 52
      unity-environment/Assets/ML-Agents/Scripts/Agent.cs

52
unity-environment/Assets/ML-Agents/Scripts/Agent.cs


/// their own experience.
int stepCount;
// Flag to signify that an agent has been reset but the fact that it is
// done has not been communicated (required for On Demand Decisions).
/// Flag to signify that an agent has been reset but the fact that it is
/// done has not been communicated (required for On Demand Decisions).
// Flag to signify that an agent is done and should not reset until
// the fact that it is done has been communicated.
/// Flag to signify that an agent is done and should not reset until
/// the fact that it is done has been communicated.
bool terminate;
/// Unique identifier each agent receives at initialization. It is used

/// Array of Texture2D used to render to from render buffer before
/// transforming into float tensor.
Texture2D[] textureArray;
textureArray = new Texture2D[agentParameters.agentCameras.Count];
for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++)
{
textureArray[i] = new Texture2D(brain.brainParameters.cameraResolutions[i].width,
brain.brainParameters.cameraResolutions[i].height, TextureFormat.RGB24, false);
}
id = gameObject.GetInstanceID();
Academy academy = Object.FindObjectOfType<Academy>() as Academy;
OnEnableHelper(academy);

info.visualObservations.Add(ObservationToTexture(
agentParameters.agentCameras[i],
param.cameraResolutions[i].width,
param.cameraResolutions[i].height));
param.cameraResolutions[i].height,
textureArray[i]));
}
info.reward = reward;

/// Converts a camera and correspinding resolution to a 2D texture.
/// </summary>
/// <returns>The 2D texture.</returns>
/// <param name="camera">Camera.</param>
/// <param name="obsCamera">Camera.</param>
public static Texture2D ObservationToTexture(Camera camera, int width, int height)
/// <param name="texture2D">Texture2D to render to.</param>
public static Texture2D ObservationToTexture(Camera obsCamera, int width, int height, Texture2D texture2D)
Rect oldRec = camera.rect;
camera.rect = new Rect(0f, 0f, 1f, 1f);
Rect oldRec = obsCamera.rect;
obsCamera.rect = new Rect(0f, 0f, 1f, 1f);
var depth = 24;
var format = RenderTextureFormat.Default;
var readWrite = RenderTextureReadWrite.Default;

var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
if (width != texture2D.width || height != texture2D.height)
{
texture2D = new Texture2D(width, height, TextureFormat.RGB24, false);
}
var prevCameraRT = camera.targetTexture;
var prevCameraRT = obsCamera.targetTexture;
camera.targetTexture = tempRT;
obsCamera.targetTexture = tempRT;
camera.Render();
obsCamera.Render();
tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
tex.Apply();
camera.targetTexture = prevCameraRT;
camera.rect = oldRec;
texture2D.ReadPixels(new Rect(0, 0, texture2D.width, texture2D.height), 0, 0);
texture2D.Apply();
obsCamera.targetTexture = prevCameraRT;
obsCamera.rect = oldRec;
return tex;
return texture2D;
}
}
}
正在加载...
取消
保存