【发布时间】:2018-10-30 23:29:39
【问题描述】:
我有一个统一的场景,我在其中使用着色器和渲染纹理进行一些计算。
为此,我有一个脚本来实例化渲染纹理和我需要的材质,然后将材质应用到现有渲染纹理并将结果写入实例化的渲染纹理中。
这基本上是我所拥有的:
public RenderTexture dstTex;
public RenderTexture srcTex1;
public RenderTexture srcTex2;
public Shader myComputeShader;
public Material myMaterial;
void Awake()
{
//Create result render Texture
dstTex = new RenderTexture(textureSizeX, textureSizeY, 16, RenderTextureFormat.ARGB32);
dstTex.Create();
//Create material
myMaterial = new Material(myComputeShader);
myMaterial.SetTexture("_srcTex2", srcTex2);
}
void Update()
{
//Apply material to srcTex1 and write result in dstTex
Graphics.Blit(srcTex1, dstTex, myMaterial);
}
现在这工作正常。 我的问题是,当我重新加载我的场景或加载另一个使用相同脚本的场景时,dstTex 没有重新初始化。
我的脚本实际上是用当前测量的高度图 (srcTex) 更新房间的高度图 (dstTex)。所以 srcTex 值会随着时间而变化,而 dstTex 会随着时间而被填充。
然后,当我重新加载我的场景时,我希望 dstTex 再次全黑,因为它再次被 dstTex = new RenderTexture(textureSizeX, textureSizeY, 16, RenderTextureFormat.ARGB32); 实例化。但事实并非如此,之前场景的值还在纹理中。
我尝试在脚本中添加以下行来解决此问题,但没有奏效:
void OnDestroy()
{ srcTex1.Release();
srcTex2.Release();
dstTex.Release();
srcTex1.DiscardContents();
srcTex2.DiscardContents();
dstTex.DiscardContents();
}
知道如何从内存中删除这些纹理吗?
【问题讨论】:
-
您谈到了
dstTex,但甚至没有在您的代码中的任何地方声明...... -
是的对不起它实际上也是一个公共变量,我在我的帖子中添加了它。我不得不在这里写一个更简单的代码来解释我的问题,因为我的代码中有很多其他不相关的东西。
标签: c# unity3d memory textures render-to-texture