How to find an average value of the texture channel in material editor ?

Hello,

I have a question. Is it possible to find an average value of the texture channel in the material editor ?

Example of what I’m trying to get:
Avarage.jpg

Possible to do with a HLSL custom node. Note that because it’s a Custom node, it won’t necessarily compile for all platforms. PC should work, but if it’s PS4 or Mobile, you’ll have to write the code in the related format.

What you could do is work out the size of the input texture, then continuously add the colour at each pixel in the texture. At the end, divide the colour by however many pixels you have (Texsize ^ 2). The downside is that it wouldn’t be particularly fast, since it has to do a for-loop over every pixel (which might be a lot of them!) and do so every frame. Anything more than a 8*8 texture and you’re probably starting to look at trouble (since that’s 64 iterations of the loop).

What’s the original problem you’re facing?

@TheJamsh Is there a way to look at the mip textures when averaging? You could just look at the lowest mip setting and sample that somehow…

Hmm… depends on whether you can access that lowest Mip at runtime in the Shader… it might be that by the time the shader is run, the GPU has already decided which textures to use for the Samplers.

You could write a custom HLSL sampler though that would grab the lowest MIP (pretty sure that’s possible in HLSL). It’s still slightly dependant upon the size of the texture though, but definitely a good optimization. The default ‘Simple Average’ mips would basically do a lot of the precomputation for you…

I’m working on the strategy game. I have created a shader that calculates influence zones based on scene objects.
Map.jpg
Now I need to find the way to calculate the volume of each zone. It does not have to be extremely precise.
aeb95238fb7dcdb187e4e2e9fa97f9f455bc360a.jpeg

You can easily access the lower mipmaps in a shader. A texture sample has a “MipValueMode” which, if set to “MipLevel”, allows you to specify a specific mipmap. If you set this to the number of available mipmaps for that texture (or higher, so you can also set it to some arbitrary high number to always get the highest regardless of texture size) you effectively get the average value of the texture.

Problem is that based on your last post it seems that you calculate the zones directly in the shader, which would mean that you do not have a texture at all. Is that correct? (If not, disregard the next part and just use the mipmaps)

If you are indeed generating the zones in the shader I can think of three main options:

  • Take a multiple sample approach by evaluating the shader code at various offsets.
  • First render to a texture and then use that texture instead of your shader logic.
  • Calculate the average analytically.

The first two options will quickly get expensive. However, as you already know the math for the zone calculation you should be able to analytically solve this. Might involve some tricky math depending on your shader, but it will most likely be far more efficient.

That is correct, if you are generating your shapes via code, you will either need to do your sample gathering inside of the code that does that, or to capture a RenderTarget using a scenecapture and then look up values from that texture.