- Home /
RenderTexture for computer shader is being null on material
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<Camera>();
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: Then I double click this RenderTexture in script Inspector,show rendertexture's inspector,and the result will be right:
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612235620im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do int textures work in ComputeShaders? 2 Answers
Black Screen in Android when calling compute shader dispatch in Update 0 Answers
RenderTexture transparency working in editor becomes solid color in build 1 Answer
Do uninitialized RenderTexture areas lead to wrong texturing (tiled RenderTexture problem)? 1 Answer
How to put many RenderTextures into Compute Shader 0 Answers