cvr-props/Assets/automata/Diffuse/diffuse.shader

85 lines
1.6 KiB
GLSL

Shader "CrispyPin/diffuse"
{
Properties
{
[NoScaleOffset]
_LastFrame ("Render texture", 2D) = "white" {}
[NoScaleOffset]
_Noise ("Noise", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define WIDTH 256
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
texture2D _LastFrame;
sampler2D _Noise;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float3 source_pixel(uint2 p, float2 uv, float2 rand_offset) {
const uint dir = tex2D(_Noise, uv + _Time.xy + rand_offset).r * 4.0;
if (dir == 0) {
p.x += 1;
} else if (dir == 1) {
p.x -= 1;
} else if (dir == 2) {
p.y += 1;
} else if (dir == 3) {
p.y -= 1;
}
return _LastFrame[p];
}
float4 frag (v2f i) : SV_Target {
if(_ProjectionParams.z > 1) discard;
const uint2 p = i.uv * WIDTH;
float red = _LastFrame[p].r - (1./255.);
float green = _LastFrame[p].g - (1./255.);
float blue = _LastFrame[p].b - (1./255.);
if (red <= 0) {
red = source_pixel(p, i.uv, float2(0.34, 0.89)).r;
}
if (green <= 0) {
green = source_pixel(p, i.uv, float2(0.43, 0.19)).g;
}
if (blue <= 0) {
blue = source_pixel(p, i.uv, float2(0.03, 0.39)).b;
}
return float4(red, green, blue, 1);
}
ENDCG
}
}
}