# Procedural square shape in Cycles (with similar effect to Gaussian blur)

## Desired result:

Basically what I want to achieve is procedural square shape with ability to blur edges with similar effect to Gaussian blur.

I'm aware of Cycles limitations regarding blur nodes, and also I'm familiar with method to blur texture by noise. This case could be different as I want to use this shape further in my node setup.

## What I have so far:

Results:

Node setup for the first example:

## Overview and problems:

1. This is desired result for "blur" effect. Problem with this one is if I change Color Ramp to Constant shape has still rounded corners. Also with bigger scale (Scale - 2 and Color Ramp Black position - 1) it looks like this:

Sadly shapes on edges aren't tiles, after scaling (and applying) mesh they are just white stripes.

Another problem is control over size of this shape.

1. This is just ok result. Nothing to add. Maybe only painful control.
2. This is Box blur effect, only upside of it is that it could be achieved only by changing Color Ramp interpolation to Linear from Constant (pt. 2).

The biggest problem here is that despite I can have the shape/effect I want, control over them (and the fact that they are separated setups) is horrible. For example - scaling from really small square to big one touching edges involves changing Scale in every Mapping node and also correcting Color Ramp.

## Blend file:

Here's an alternative solution using maths nodes to calculate distance from the square and the Gaussian distribution function.

The key here is for any point in the shader to calculate the closest point to the square. The square is defined by 4 boundary values - X-Min, X-Max, Y-Min, Y-Max.

To calculate the closest point that is still within the square we can simply use the Max and Min functions. ie,

closest-X = Min( Max(X, X-Min), X-Max)
closest-Y = Min( Max(Y, Y-Min), Y-Max)


To calculate the distance to this point (ie, the closest distance to the square) we can use Pythagoras

distance^2 = X^2 + Y^2


ie,

distance = sqrt(X^2 + Y^2)


For the Gaussian falloff we use the equation :

 intensity = 1 / (2^dist)


Bringing it all together produces the following material (note that I've omitted the 'sqrt' of the distance calculation - this gives a more pronounced falloff based on the square of the distance, rather than the plain distance) :

Adjusting the input nodes (X-Min, X-Max, Y-Min, Y-Max, Focus) will adjust the size and 'blurriness' of the square. Note that the Gaussian blur starts from the edge of the square - so the square is always maximum intensity with the falloff starting at its boundary. This means that the blur will always make it appear larger - adjust the boundary values to compensate whilst adjusting the Focus if this is not desired.

To tile multiple squares you can simply use a Mapping node and Modulo nodes to repeat the range multiple times :

Animating the mapping and various inputs can produce the following result :

Blend file attached

This can be used to generate procedural tiles as shown by this sample image provided by @cgslav :

• Wow! I didn't wanted this to be tiled texture but now I can't stop playing with it. Couple nodes more and I was able to create tiles with bumped mortar, check here: imgur.com/ENIJW3s Now only figure out different gradient per tile and this will be complete solution for making tiles Substance Designer style :) – cgslav Sep 5 '17 at 7:37
• Nice - that's really effective as tiles. Adding variation per tile shouldn't be too complicated - would just need to 'Round' the coordinates to integers before feeding into a Noise texture. I'll try and update the answer with that later. – Rich Sedman Sep 5 '17 at 8:26

Here is a working node graph. I don't know if it mimics exactly a gaussian filter, but it looks better than the screenshots you posted.

Forgive me for the messy graph, I guess you will organize all this with groups and so on ;-).

It looks like this:

Node setup: (you have input for U/V coordinates of the top left corner of the square, the width/height of the square, and the amount of blur)

"feather interval" node group: (note that with the maximum and minimum nodes, the input a and b can be in any order) (note also that I put a max node on the feather to avoid dividing by zero or negative values)

Basically "feather interval" is 1D function similar to a gate function (wikipedia article) with smooth corners. Than I combine 2 intervals for U and V by multiplying them.