- Home /
Average Colour of Texture in Surface Shader
Hello there,
I have a basic cube object with a RenderTexture assigned to it that acts as a monitor. I would like the monitor to light up the nearby area based on what the screen shows, so my first thought was to simply get the average colour of the RenderTexture via the shader i am using and get that from a script.
I ended up using a public colour property named _AvgColour and at the end of the surf function i called
_AvgColour.rgb+=(_AvgColour.rgb+c.rgb)*0.5f;
where c is the colour of the current texel. Unfortunately that doesn't seem to modify the _AvgColour property at all. How else would i be able to calculate the average colour of the camera's RenderTexture each frame?
this is a good question for the shader forum. I think that the colour in the surface shader is called albedo. you can probably do it in a fragment shader, and if performance is not an issue you can send the texture to the CPU, I believe it might be possible with a render texture.
Answer by reefwirrax · Sep 30, 2013 at 08:24 AM
I don't think a surface shader will give you enough control in order to loop through every pixel and to get the average, and it's a task that will take potentially thousands of additions which is probably too much calculation for the shader anyway. if you use vertex and fragment shader, it would be easy.
It should be an additional 4-ish(?) math operations per pixel, doesn't sound all that unrealistic.
It's because you can't do it the way you're intending to. You would need at least two passes to do it this way, and even then, I'm pretty sure you can't save values between pixels like this. What you really need to do is calculate the colour and then use it directly, which is much more calculation.
This was more or less the kind of answer i was looking for. If values can't be saved across pixels, i may as well do it on the CPU.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers
Color white does nothing consider mobile... really? 1 Answer
Transparency cube shader? 1 Answer
Shader: get nearest point light's color, direction and general custom lighting questions 0 Answers