# How can I loop through pixels in the material editor?

I am using Unreal Engine 4, and I want to try to procedurally generate a grass texture. For now, I am using a noise block. Now I want to loop through every 16 pixels or whatever (4x4 squares), take the average color of those pixels, and make that all one "pixel". I don't see any for loops, loops, etc. in the pallete though. Can you not do this with the material editor? Thanks.

• This sounds a bit like mipmapping. Could you read from a deeper mip level to get this kind of local averaging? May 30, 2017 at 18:47

You can do this but it just makes your nodes messy! It's a bit of a messy approach but I'm not aware of any nodes that gives us quick access to surrounding pixels:

1. Create const floats ( name them wRatio and hRatio ) and set their values to:
wRatio : 1.0/texture width
hRatio: 1.0/texture height

If you want to hit pixel center offset the texture ( or add it to w/hRatio ) by half of wRatio and hRatio.

1. Create a TexCoord node

2. Create a BreakOutFloatComponent and attach the TexCoord output to it.

3. With our new data we now need to Create 9 new (3x3, you can do 16 but do you really want to?) 2d Vectors. The ouput is set by their offset based on the wRatio and hRation. for example {-wRatio,-hRatio} is the top left pixel of the 3x3 setup. Combine the BreakOutFloatComponent with your ratios using add math nodes. Make sure to negate any ratio first by multiplying by -1 using a math multiply node to get the correct coordinate. R output with wRatio and G with hRatio.

4. Use the append node to combine the new u and v coordinates into a new vector 2d. Repeat this for every offset.

5. Use each result to sample from the texture as uv input. For this create 9 Texture Sample nodes and connect each offset respectively.

6. Add all of the color outputs together with the math add node and then divide them by 9 with the math divide node. This is your average color.

Here is the setup for average of 2 lookups

You can see that this will be a nightmare if you go up in sample count.

Alternatively you can also use a custom expression : https://docs.unrealengine.com/en-us/Engine/Rendering/Materials/ExpressionReference/Custom

Requires you to write some hlsl but the syntax for such function is actually very easy. The blur example points you in the right direction.

I don't think this can be done through material nodes. However you can use the Custom node to write your own GLSL code, or write a custom GLSL shader.

• You probably meant HLSL Sep 30, 2019 at 12:23