##
__White Noise __

Many of you may know what white noise is and how it can be used in code.But there are a spectrum of 'Coloured Noises' as well these include ones like brown noise, pink, blue and violet noise all these have slightly different properties and can be very useful in creating different effects. More details on coloured noises can be found HERE.

Comparison of noiseless and noisy image |

Creating white noise boils down to the random function used.

While writing C++ code or C# code we have access to good pseudo-random functions from their standard libraries... but while writing code in shaders we usually have to create our own pseudo-random number generator.

One really good way of making one is by using the frac( in Unity & HLSL ) or fract( in GLSL ) function which returns the fractional part of a number.

```
float random( float2 point )
{
return frac(sin(dot(point.xy,float2(someXvalue,someYvalue)))*someFloatWithManyDecimals);
}
```

Why are we using this 'special arrangement' of functions to make our pseudo-random number generator?__The__:- gives values based on how close the directions of 2 vectors are to each other.

**dot**product__The__:- gives us value from -1.0 to 1.0 based on the input from the dot product.

**sin**function__The__:- gives us the fractional part of the entire thing, which will be our random number.

**frac**functionAfter tweaking some of these values you should be getting a pretty decent random number generator.

We will be making an effect that looks like this. It's a bit exaggerated for effect.☺.

An example of grain filter shader |

```
Properties
{
_MainTex("Texture", 2D)="white"
_NoiseScale("Noise Scale",Float) = 50.0
_Strength("Noise Strength",Float) = 1.0
}
```

What are they? :```
sampler2D _MainTex; //Texture Used
float _NoiseScale; //How big each noise block is
float _Strength; //How prevalent it is in final image
```

We will look at the random function used ( by me ).```
float random( float2 p )
{
return frac(sin(dot(p.xy ,float2( _Time.y,6.115)))*53.8856);
}
```

I have used _Time.y as a parameter to change the noise each frame.
Now the fragment shader:

```
fixed4 frag (v2f i) : SV_Target
{
float2 normUV = i.uv;
normUV *= _NoiseScale; // Scale the coordinate system by _NoiseScale amount
float2 ipos = floor(normUV); // get the integer coords
float rand = random(ipos); // Assign a random value based on the integer coord
fixed4 col = fixed4(rand,rand,rand,1.0);
col = lerp( tex2D(_MainTex,i.uv),col,_Strength ); /*Interpolating between the noise and
texture image by _Strength Value*/
return col;
}
```

The entire source code of shader is HEREIf you like programming shaders make sure you check these out : Shader Tutorials

Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.

If you have any questions that you might have about shaders or unity development in general don't be shy and leave a message on my facebook page or down in the comments.

## Comments

## Post a Comment