Skip to content

Instantly share code, notes, and snippets.

@josephbk117
Last active September 18, 2022 03:37
Show Gist options
  • Save josephbk117/884900c161705738d1b2fc4be38c5910 to your computer and use it in GitHub Desktop.
Save josephbk117/884900c161705738d1b2fc4be38c5910 to your computer and use it in GitHub Desktop.
White noise ( grain filter ) shader in unity
Shader "BitShiftProductions/Noises"
{
Properties
{
_MainTex("Texture", 2D)="white"
_NoiseScale("Noise Scale",Float) = 5.0
_Strength("Noise Strength",Float) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float _NoiseScale;
float _Strength;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float random( float2 p )
{
return frac(sin(dot(p.xy,float2(_Time.y,65.115)))*2773.8856);
}
fixed4 frag (v2f i) : SV_Target
{
float2 normUV = i.uv;
normUV *= _NoiseScale; // Scale the coordinate system by 10
float2 ipos = floor(normUV); // get the integer coords
// Assign a random value based on the integer coord
float rand = random(ipos);
fixed4 col = fixed4(rand,rand,rand,1.0);
col = lerp( tex2D(_MainTex,i.uv),col,_Strength);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment