• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ggedmond · Jun 01, 2020 at 06:17 AM · rendertexturecompute shader

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: alt text Then I double click this RenderTexture in script Inspector,show rendertexture's inspector,and the result will be right: alt text 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Black Screen in Android when calling compute shader dispatch in Update 0 Answers

Runtime generated RenderTexture as Displacement map weird behavior 1 Answer

RenderTextureFormat.R8 0 Answers

Do uninitialized RenderTexture areas lead to wrong texturing (tiled RenderTexture problem)? 1 Answer

How to put many RenderTextures into Compute Shader 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges