• 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 goncarm79 · Nov 05, 2021 at 07:03 PM · shader programming

I need to send a render texture from camera to a GPU

Hi! I recently started working on a game in which you need to stay in a lighted area. I decided to create a computational shader that will be responsible for calculating the illumination of each frame, and then getting the value using a script. I have a slight problem... My texture is black from the GPU side of a code and I don't know why!!!

Here's GPU compute shader:

 #pragma kernel CSMain
 //Result texture will be accessed from the script to display on a screen as a visualization
 RWTexture2D Result;
 //Texture that is captured from the screen and set from the script; all the values are reading from it
 Texture2D TextureToRead;
 //Buffer for storing a value of illuminance of all pixels in TextureToRead texture
 RWBuffer NumBuffer;
 
 //A function that was taken somewhere from the internet to calculate the illuminance or brightness of a color
 float brightness(float4 color)
 {
     return sqrt(0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b) * color.a;
 }
 
 [numthreads(1,1,1)]
 void CSMain (uint3 id : SV_DispatchThreadID)
 {
         //Color sapling from the texture
     float4 col = TextureToRead[id.xy];
         //Calculating the brightness/illuminance value
     float illuminance = brightness(col);
         //The result is black
     Result[id.xy] = col;
 }

And CPU C# code:

 //Shader
 public ComputeShader shader;
 //Camera that renders a texture
 public Camera renderCamera;
 //That texture
 public RenderTexture sourceTexture;
 //Result texture
 public RenderTexture texture;
 //Value that will be taken by some other scripts
 public float screenIlluminance = 0;
 
 void Start()
 {
     CalculateIlluminance();
 }
 
 //Method for calculating; can be invoked every frame
 public void CalculateIlluminance()
 {
     //Creating texture if it doesn't exists
     if (texture == null)
     {
         texture = new RenderTexture(256, 256, 24);
         texture.enableRandomWrite = true;
         texture.Create();
     }
 
     //Creating buffer to take the value of  illuminance
     ComputeBuffer numBuffer = new ComputeBuffer(1, sizeof(unit), 
     ComputeBufferType.Structured);
     uint[] numData = new uint[1] { 0 };
     numBuffer.SetData(numData);
     //Finding kernel
     int kernelHandle = shader.FindKernel("CSMain");
 
     //Send variables to the shader
     shader.SetBuffer(kernelHandle, "NumBuffer", numBuffer);
     shader.SetTexture(kernelHandle, "TextureToRead", sourceTexture);
     shader.SetTexture(kernelHandle, "Result", texture);
     //Executing it
     shader.Dispatch(kernelHandle, texture.width, texture.height, 1);
 
     //Getting data back
     uint[] newNumData = new uint[1];
     numBuffer.GetData(newNumData);
         
     uint illNumValue = newNumData[0];
     screenIlluminance = illNumValue;
 
     //Deleting all the data from the buffer (not important in this particular situation)
     numBuffer.Dispose();
 }
 void OnRenderImage(RenderTexture src, RenderTexture dest)
 {
     //The method that is called for visualizing the resulting texture to the screen
     //Results to a black nothing
     Graphics.Blit(texture, dest);
 }
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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

134 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove terrain default texture 0 Answers

Rendering volumetric objects and maintaining correct depth 1 Answer

Billboard shader transform/move object to the center of the screen 1 Answer

Can I make a lasting variable in Shader Graph?, 0 Answers

How would I join two gradients together in shader graph? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges