revival/game/Assets/Shaders/glitch.shader

196 lines
6.9 KiB
Plaintext
Raw Normal View History

2021-04-24 03:06:03 +02:00
// This shader draws a texture on the mesh.
Shader "KernelPanic/glitch"
{
// The _BaseMap variable is visible in the Material's Inspector, as a field
// called Base Map.
Properties
2021-04-25 01:59:37 +02:00
{
2021-04-24 03:06:03 +02:00
[HDR] _BaseColor("Base Color", Color) = (1,1,1,1)
[HDR] _NoiseColor("Noise Color", Color) = (1,1,1,1)
2021-04-25 01:59:37 +02:00
[HDR] _GlitchColor("Glitch Color", Color) = (1,1,1,1)
2021-04-24 03:06:03 +02:00
_BaseMap("Base Map", 2D) = "white"
_NoiseMap1("Noise 1", 2D) = "black"
_GlitchTex("Glitch Tex", 2D) = "black"
2021-04-25 01:59:37 +02:00
[Range(0,1)] _Distortion("Distortion", Float) = 0
_Intensity("Intensity", Float) = 1
_TrailScatter("Trail Scatter", Float) = 0.02
_VertShake("Vertex Shake", Float) = 0.05
2021-04-25 06:16:35 +02:00
_VertFloat("Vertex Float", Float) = 0.1
2021-04-24 03:06:03 +02:00
}
SubShader
{
2021-04-25 01:59:37 +02:00
Tags
{
"RenderType" = "Opaque"
2021-04-24 03:06:03 +02:00
"Queue" = "Geometry"
2021-04-25 01:59:37 +02:00
"RenderPipeline" = "UniversalRenderPipeline"
2021-04-24 03:06:03 +02:00
}
Pass
{
2021-04-26 00:44:43 +02:00
Stencil
{
Ref 1
Comp always
Pass replace
}
2021-04-24 03:06:03 +02:00
Cull Off
Zwrite On
2021-04-25 01:59:37 +02:00
ZTest LEqual
2021-04-24 03:06:03 +02:00
2021-04-25 01:59:37 +02:00
Blend SrcAlpha OneMinusSrcAlpha
2021-04-24 03:06:03 +02:00
HLSLPROGRAM
// #pragma target 5.0
// #pragma only_renderers d3d11
2021-04-25 01:59:37 +02:00
2021-04-24 03:06:03 +02:00
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Assets/Shaders/random.cginc"
struct Attributes
{
2021-04-25 01:59:37 +02:00
float4 positionOS : POSITION;
2021-04-24 03:06:03 +02:00
// The uv variable contains the UV coordinate on the texture for the
// given vertex.
2021-04-25 01:59:37 +02:00
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
2021-04-24 03:06:03 +02:00
};
struct Varyings
{
2021-04-25 01:59:37 +02:00
float4 positionHCS : SV_POSITION;
2021-04-24 03:06:03 +02:00
// The uv variable contains the UV coordinate on the texture for the
// given vertex.
2021-04-25 01:59:37 +02:00
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
float4 screenPos : TEXCOORD1;
2021-04-24 03:06:03 +02:00
};
// This macro declares _BaseMap as a Texture2D object.
TEXTURE2D(_BaseMap);
TEXTURE2D(_NoiseMap1);
TEXTURE2D(_GlitchTex);
TEXTURE2D(_CameraDepthTexture);
2021-04-25 01:59:37 +02:00
2021-04-24 03:06:03 +02:00
// This macro declares the sampler for the _BaseMap texture.
SAMPLER(sampler_BaseMap);
SAMPLER(sampler_NoiseMap1);
SAMPLER(sampler_GlitchTex);
SAMPLER(sampler_CameraDepthTexture);
CBUFFER_START(UnityPerMaterial)
2021-04-25 01:59:37 +02:00
// The following line declares the _BaseMap_ST variable, so that you
// can use the _BaseMap variable in the fragment shader. The _ST
// suffix is necessary for the tiling and offset function to work.
float4 _BaseMap_ST;
float4 _NoiseMap_ST;
2021-04-24 03:06:03 +02:00
2021-04-26 00:44:43 +02:00
float _VertFloat;
float _VertShake;
float _Distortion;
float _Intensity;
float _TrailScatter;
float4 _NoiseColor;
float4 _BaseColor;
float4 _GlitchColor;
2021-04-25 01:59:37 +02:00
CBUFFER_END
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float4, _Positions)
UNITY_INSTANCING_BUFFER_END(Props)
2021-04-24 03:06:03 +02:00
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.normal = TransformObjectToWorldDir(IN.normal);
// OUT.normal = IN.normal;
2021-04-25 01:59:37 +02:00
float4 poscs = TransformObjectToHClip(IN.positionOS);
2021-04-26 00:44:43 +02:00
float4 n = SAMPLE_TEXTURE2D_LOD(_NoiseMap1, sampler_NoiseMap1, hash42(poscs+_Time), 0);
2021-04-25 01:59:37 +02:00
float3 pos = IN.positionOS + (n-0.5) * _VertShake;
float4 p = UNITY_ACCESS_INSTANCED_PROP(Props, _Positions);
float t = sin(p.y*_Time.w + p.x*6.28);
float3 wpos = TransformObjectToWorld(pos);
2021-04-25 06:16:35 +02:00
wpos += float3(0,t*p.z*_VertFloat,0);
2021-04-25 01:59:37 +02:00
pos = TransformWorldToObject(wpos);
OUT.positionHCS = TransformObjectToHClip(pos);
2021-04-24 03:06:03 +02:00
// The TRANSFORM_TEX macro performs the tiling and offset
// transformation.
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
OUT.screenPos = ComputeScreenPos(OUT.positionHCS);
2021-04-25 01:59:37 +02:00
2021-04-24 03:06:03 +02:00
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
2021-04-25 01:59:37 +02:00
float2 suv = IN.screenPos.xy / IN.screenPos.w;
float d = SAMPLE_TEXTURE2D(_CameraDepthTexture, sampler_CameraDepthTexture, suv);
2021-04-24 03:06:03 +02:00
2021-04-25 01:59:37 +02:00
// float4 p = float4(.42069, .6969, .1337, .8008135);
float4 p = UNITY_ACCESS_INSTANCED_PROP(Props, _Positions);
2021-04-24 03:06:03 +02:00
2021-04-25 01:59:37 +02:00
float t = (p.x*sin(_Time.y)+p.w*cos(_Time.x)) * 0.5 + 0.5;
// sample regular color
float4 c = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, (IN.uv +lerp(p.xy,p.zw,0.2+0.6*t))*0.5);
// c = lerp(c, _BaseColor, 0.5);
2021-04-24 03:06:03 +02:00
2021-04-26 00:44:43 +02:00
float4 n = SAMPLE_TEXTURE2D(_NoiseMap1, sampler_NoiseMap1, (IN.uv +lerp(p.xy,p.zw,0.9-30*t))*190);
2021-04-24 03:06:03 +02:00
n *= _NoiseColor;
2021-04-25 01:59:37 +02:00
// sample blended glitch
float4 g = SAMPLE_TEXTURE2D(_GlitchTex, sampler_GlitchTex,
suv + _TrailScatter*n*hash42(IN.uv));
2021-04-26 00:44:43 +02:00
// g *= _GlitchColor;
2021-04-25 01:59:37 +02:00
// g = 1 - g;
// sample color distorted by noise
float distortion = _Distortion + (t-0.5) * _Intensity;
float4 cd = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv + n * distortion * t);
// float4 cn = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv + nuv * _GlitchColor);
2021-04-24 03:06:03 +02:00
// c *= _BaseColor;
2021-04-25 01:59:37 +02:00
// cn = lerp(cn,(1-cn),_Distortion);
// cn += n;
// float4 color = lerp(c, cn, n);
2021-04-24 03:06:03 +02:00
// cheeky bit of top shading
2021-04-25 01:59:37 +02:00
float un = dot(float3(0,1,0), IN.normal); // dot of up and normal
2021-04-26 00:44:43 +02:00
c = lerp(c,_BaseColor,un*(1-d));
2021-04-25 01:59:37 +02:00
// color += (1-abs(un)) *_BaseColor;
2021-04-24 03:06:03 +02:00
2021-04-26 00:44:43 +02:00
c = lerp(n, c, g);
// c = min(c, float4(1, 1, 1, 1));
2021-04-25 01:59:37 +02:00
2021-04-26 00:44:43 +02:00
c += n*_NoiseColor;
2021-04-25 01:59:37 +02:00
// c = lerp(n,c,0.5);
// c = lerp(c, _BaseColor, _BaseColor.a);
2021-04-26 00:44:43 +02:00
return c * _BaseColor;
2021-04-24 03:06:03 +02:00
}
ENDHLSL
}
2021-04-25 01:59:37 +02:00
UsePass "Universal Render Pipeline/Lit/DepthOnly"
2021-04-24 03:06:03 +02:00
}
2021-04-25 01:59:37 +02:00
}