Hi,guys
I write a compute shader for a rendertexure and set this rendertexture to be a global texture( Shader.SetGlobalTexture()),but I find that if I do not select it in editor,it will be null on material,this is my script:
public ComputeShader computeShader;
public int texSize = 256;
[Range(5,256)]
public float renderRange = 5;
public RenderTexture rendTex;
public RenderTexture computeTex;
Camera topCamera;
int kernelHandle;
// Start is called before the first frame update
void Start()
{
Init();
}
// Update is called once per frame
void Update()
{
RunShader();
}
private void OnDisable()
{
rendTex.Release();
computeTex.Release();
}
private void Init()
{
topCamera = this.GetComponent();
kernelHandle = computeShader.FindKernel("CSMain");
rendTex = new RenderTexture(texSize, texSize, 16,RenderTextureFormat.Depth);
computeTex = new RenderTexture(texSize, texSize, 0);
computeTex.enableRandomWrite = true;
computeTex.name = "TopRenderTexture";
//rendTex.Create();
//computeTex.Create();
if (topCamera != null)
{
topCamera.targetTexture = rendTex;
topCamera.enabled = false;
topCamera.orthographicSize = renderRange;
}
computeShader.SetTexture(kernelHandle, "Result", computeTex);
computeShader.SetTexture(kernelHandle, "TopDepthTex", rendTex);
Shader.SetGlobalTexture("_TopRenderTexture", computeTex);
}
void RunShader()
{
UpdateTopCamera();
topCamera.Render();
computeShader.Dispatch(kernelHandle, texSize / 8, texSize / 8, 1);
}
void UpdateTopCamera()
{
if (topCamera != null)
{
topCamera.orthographicSize = renderRange;
}
}
The compute shader is working perfect,but when I run this script,the "TopRenderTexture"is null at first on material:
![alt text][1]
Then I double click this RenderTexture in script Inspector,show rendertexture's inspector,and the result will be right:
![alt text][2]
It seems like this rendertexture must be rendered on my editor window,then it's pointer will correctly connect to the materials?How can I fix it?Thanks for answering.
[1]: /storage/temp/160712-tim图片20200530060214.png
[2]: /storage/temp/160714-tim图片20200530060627.png
↧