• 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 $$anonymous$$ · Jan 25, 2019 at 05:54 PM · shader programmingpixeldepth-buffer

Is there a way to sum all the pixels of a texture together in a shader?

I'm doing a post process effect that needs to sum the pixel values together. For example, I have a pass that gets the depth values and renders to a RenderTexture. If I want to sum those values together and find the average depth, how could I go about doing that?

I tried making a separate (1x1) RenderTexture and adding the values from the DepthTexture to it, but no luck. I also tried downsampling with Graphics.Blit(), but from what I understand it uses bilinear interpolation to get a weighted average, which is not what I need.

Any help would be appreciated!

Comment
Add comment · Show 2
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
avatar image Fluffy_Kaeloky · Jan 25, 2019 at 08:14 PM 0
Share

I've had a similar issue with a game jam I made, where I wanted a spotlight to have the average color of a TV. While my solution isn't exactly what you ask for (It executes on the CPU), it does what you are asking in a timely manner (Albeit it is still very intensive. Datas goes from CPU to GPU real fast, the other way around is another matter entirely ...).

 using System.Threading.Tasks;
 using Unity.Collections;
 using UnityEngine;
 using UnityEngine.Rendering;
 
 public class TVSpotlight : $$anonymous$$onoBehaviour
 {
     public RenderTexture renderTexture = null;
 
     public List<Light> lights = null;
 
     private Queue<AsyncGPUReadbackRequest> requests = new Queue<AsyncGPUReadbackRequest>();
 
     private Color32[] color32Buffer;
 
     private void Awake()
     {
         color32Buffer = new Color32[renderTexture.width * renderTexture.height];
     }
 
     private void LateUpdate()
     {
         if (requests.Count < 8)
         {
             requests.Enqueue(AsyncGPUReadback.Request(renderTexture, 0, (AsyncGPUReadbackRequest req) =>
             {
                 if (req.hasError)
                 {
                     Debug.Log("GPU readback error detected.");
                     requests.Dequeue();
                     return;
                 }
                 else if (req.done)
                 {
                     req.GetData<Color32>().CopyTo(color32Buffer);
 
                     int averageR = 0;
                     int averageG = 0;
                     int averageB = 0;
                     int count = 0;
 
                     for (int i = 0; i < color32Buffer.Length; ++i)
                     {
                         averageR += color32Buffer[i].r;
                         averageG += color32Buffer[i].g;
                         averageB += color32Buffer[i].b;
                         ++count;
                     }
 
                     averageR /= count;
                     averageG /= count;
                     averageB /= count;
 
                     lights.ForEach(x => x.color = new Color32((byte)averageR, (byte)averageG, (byte)averageB, 255));
                 }
 
                 requests.Dequeue();
             }));
         }
     }
 }
 

I believe it can be adapted to your use case (Setting a render texture ins$$anonymous$$d of the light color).

This is the kind of render I was able to get using this :

alt text

It is also worth noting that the ASyncGPUReadbackRequest has a very slight lag, but small enough to be nearly invisible.

I hope it will be able to help you !

capturereafdxas.jpg (413.6 kB)
avatar image $$anonymous$$ · Feb 01, 2019 at 02:42 PM 0
Share

Thanks for your idea! I didn't know about the AsyncGPU functions. There's a noticeable hiccup when I first implemented it, but it's a good start!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by $$anonymous$$ · Feb 01, 2019 at 02:45 PM

I ended up blitting to a lower resolution texture and reading from the CPU on those pixels. The AsyncGPU solution Fluffy proposed has some hiccups but I think it's something I can work with as well.

Comment
Add comment · Share
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

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

101 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

Related Questions

Modifying the depth buffer of an image effect? 0 Answers

Post-processing shader using 3D pixel coordinates to dictate colour,Postprocessor shader based upon pixel 3D location and distance from object,Genuinely challenging post-processing shader... 1 Answer

Post-processing shader using 3D pixel coordinates to dictate colour 1 Answer

Get the color value of a pixel in a texture or send data array 0 Answers

Is a high rendering order and Zwrite Off enough to ensure that an object is always visible? 1 Answer


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