271 lines
9.5 KiB
Plaintext
271 lines
9.5 KiB
Plaintext
Shader "Unlit/ObjectPostProcess"
|
|
{
|
|
Properties
|
|
{
|
|
_MainTex ("Texture", 2D) = "white" {}
|
|
_NoiseTexture ("Noise Texture", 2D) = "white" {}
|
|
_NoisePow("Noise power", Range(0.5, 10)) = 2
|
|
_NoiseScale("Noise Scale", Range(0.5, 100)) = 10
|
|
_CutoutTh("Cutout Th", Range(0, 1)) = 0
|
|
_CutOutEdgeWidth("Cutout Edge Width", Range(0.0001, 0.2)) = 0.001
|
|
_EdgeColor1 ("Edge Color1", Color) = (0.3,0.4,1,1)
|
|
_EdgeColor1 ("Edge Color1", Color) = (0.1,0.2,0.7,1)
|
|
|
|
_Delta ("Line Thickness", Range(0.0005, 0.0025)) = 0.001
|
|
|
|
[Header(Wave)]
|
|
_WaveDistance ("Distance from the Camera", float) = 10
|
|
_WaveTrail ("Length of the trail", Range(0,5)) = 1
|
|
_WaveColor ("Color", Color) = (1,0,0,1)
|
|
}
|
|
SubShader
|
|
{
|
|
Tags { "RenderType"="Transparent" }
|
|
LOD 100
|
|
|
|
Pass
|
|
{
|
|
Name "Test Filter"
|
|
HLSLPROGRAM
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
|
|
|
|
TEXTURE2D(_CameraDepthTexture);
|
|
SAMPLER(sampler_CameraDepthTexture);
|
|
|
|
TEXTURE2D(_CameraOpaqueTexture);
|
|
SAMPLER(sampler_CameraOpaqueTexture);
|
|
|
|
half _Delta;
|
|
int _PosterizationCount;
|
|
//variables to control the wave
|
|
half _WaveDistance;
|
|
half _WaveTrail;
|
|
half4 _WaveColor;
|
|
half _NoisePow;
|
|
half _NoiseScale;
|
|
half _CutoutTh;
|
|
half _CutOutEdgeWidth;
|
|
half3 _EdgeColor1;
|
|
half3 _EdgeColor2;
|
|
/*
|
|
TEXTURE2D(_MainTex);
|
|
SAMPLER(sampler_MainTex);
|
|
TEXTURE2D(_NoiseTexture);
|
|
SAMPLER(sampler_NoiseTexture);
|
|
*/
|
|
sampler2D _MainTex;
|
|
sampler2D _NoiseTexture;
|
|
float4 _MainTex_ST;
|
|
//float4 _Time;
|
|
|
|
|
|
|
|
float3 SampleScreen(float2 uv)
|
|
{
|
|
return SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture, uv).x;
|
|
}
|
|
|
|
float SampleDepth(float2 uv)
|
|
{
|
|
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
|
|
return SAMPLE_TEXTURE2D_ARRAY(_CameraDepthTexture, sampler_CameraDepthTexture, uv, unity_StereoEyeIndex).r;
|
|
#else
|
|
return SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv);
|
|
#endif
|
|
}
|
|
|
|
float sobel (float2 uv)
|
|
{
|
|
float2 delta = float2(_Delta, _Delta);
|
|
|
|
float hr = 0;
|
|
float vt = 0;
|
|
|
|
hr += SampleDepth(uv + float2(-1.0, -1.0) * delta) * 1.0;
|
|
hr += SampleDepth(uv + float2( 1.0, -1.0) * delta) * -1.0;
|
|
hr += SampleDepth(uv + float2(-1.0, 0.0) * delta) * 2.0;
|
|
hr += SampleDepth(uv + float2( 1.0, 0.0) * delta) * -2.0;
|
|
hr += SampleDepth(uv + float2(-1.0, 1.0) * delta) * 1.0;
|
|
hr += SampleDepth(uv + float2( 1.0, 1.0) * delta) * -1.0;
|
|
|
|
vt += SampleDepth(uv + float2(-1.0, -1.0) * delta) * 1.0;
|
|
vt += SampleDepth(uv + float2( 0.0, -1.0) * delta) * 2.0;
|
|
vt += SampleDepth(uv + float2( 1.0, -1.0) * delta) * 1.0;
|
|
vt += SampleDepth(uv + float2(-1.0, 1.0) * delta) * -1.0;
|
|
vt += SampleDepth(uv + float2( 0.0, 1.0) * delta) * -2.0;
|
|
vt += SampleDepth(uv + float2( 1.0, 1.0) * delta) * -1.0;
|
|
|
|
return sqrt(hr * hr + vt * vt);
|
|
}
|
|
|
|
float sobel2 (float2 uv)
|
|
{
|
|
float2 delta = float2(_Delta, _Delta);
|
|
|
|
float hr = 0;
|
|
float vt = 0;
|
|
|
|
hr += SampleScreen(uv + float2(-1.0, -1.0) * delta) * 1.0;
|
|
hr += SampleScreen(uv + float2( 1.0, -1.0) * delta) * -1.0;
|
|
hr += SampleScreen(uv + float2(-1.0, 0.0) * delta) * 2.0;
|
|
hr += SampleScreen(uv + float2( 1.0, 0.0) * delta) * -2.0;
|
|
hr += SampleScreen(uv + float2(-1.0, 1.0) * delta) * 1.0;
|
|
hr += SampleScreen(uv + float2( 1.0, 1.0) * delta) * -1.0;
|
|
|
|
vt += SampleScreen(uv + float2(-1.0, -1.0) * delta) * 1.0;
|
|
vt += SampleScreen(uv + float2( 0.0, -1.0) * delta) * 2.0;
|
|
vt += SampleScreen(uv + float2( 1.0, -1.0) * delta) * 1.0;
|
|
vt += SampleScreen(uv + float2(-1.0, 1.0) * delta) * -1.0;
|
|
vt += SampleScreen(uv + float2( 0.0, 1.0) * delta) * -2.0;
|
|
vt += SampleScreen(uv + float2( 1.0, 1.0) * delta) * -1.0;
|
|
|
|
return sqrt(hr * hr + vt * vt);
|
|
}
|
|
|
|
float simpleEdge (float2 uv)
|
|
{
|
|
float2 delta = float2(_Delta, _Delta);
|
|
|
|
float hr = 0;
|
|
float vt = 0;
|
|
|
|
hr += SampleScreen(uv + float2(-1.0, 0.0) * delta) * 2.0;
|
|
hr += SampleScreen(uv + float2( 1.0, 0.0) * delta) * -2.0;
|
|
|
|
vt += SampleScreen(uv + float2( 0.0, -1.0) * delta) * 2.0;
|
|
vt += SampleScreen(uv + float2( 0.0, 1.0) * delta) * -2.0;
|
|
|
|
return sqrt(hr * hr + vt * vt);
|
|
}
|
|
|
|
half AlphaCutOut(half2 uv, out half3 emissive)
|
|
{
|
|
half2 shift = half2(frac(_Time.x),frac(_Time.x*2));
|
|
half noise = tex2D(_NoiseTexture, uv * _NoiseScale + shift).r;
|
|
emissive = half3(0.0, 0.0, 0.0);
|
|
|
|
|
|
|
|
noise = pow(noise, _NoisePow);
|
|
//uv = abs(uv - 0.5);
|
|
//noise = noise * pow(1 - uv.x - uv.y,3);
|
|
|
|
noise = noise * pow(1 - distance(uv, float2(0.5, 0.5)), 3);
|
|
noise += _CutoutTh;
|
|
|
|
|
|
|
|
if (noise > 1.0)
|
|
{
|
|
noise = 1.0;
|
|
}
|
|
else if ((noise + _CutOutEdgeWidth ) > 1.0)
|
|
{
|
|
noise = (noise + _CutOutEdgeWidth - 1.0)/_CutOutEdgeWidth;
|
|
emissive = lerp(_EdgeColor1, _EdgeColor2, noise);
|
|
}
|
|
else {
|
|
noise = 0.0;
|
|
}
|
|
|
|
return noise;
|
|
}
|
|
|
|
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float2 uv : TEXCOORD0;
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float2 uv : TEXCOORD0;
|
|
float4 vertex : SV_POSITION;
|
|
float4 posNDC : TEXCOORD1;
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
};
|
|
|
|
struct v2f
|
|
{
|
|
float2 uv : TEXCOORD0;
|
|
float4 vertex : SV_POSITION;
|
|
};
|
|
|
|
|
|
|
|
Varyings vert(Attributes input)
|
|
{
|
|
Varyings output = (Varyings)0;
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
|
output.vertex = vertexInput.positionCS;
|
|
output.posNDC = vertexInput.positionNDC;
|
|
output.uv = input.uv;
|
|
//output.uv = ComputeScreenPos(output.vertex);
|
|
return output;
|
|
}
|
|
|
|
float4 frag (Varyings i) : SV_Target
|
|
{
|
|
// sample the texture
|
|
half2 uv = i.posNDC.xy/i.posNDC.w;
|
|
|
|
float4 col = float4(SampleScreen(i.posNDC.xy/i.posNDC.w),0);
|
|
//float4 col = SAMPLE_TEXTURE2D_LOD(_CameraOpaqueTexture,sampler_CameraOpaqueTexture, i.posNDC.xy/i.posNDC.w,0);
|
|
float lum = dot(col.xyz, float3(0.28, 0.58, 0.14));
|
|
col.xyz = lerp(col.xyz, lum.xxx, 0.5);
|
|
//col.xyz = (1.0 - lum.xxx)*0.5;
|
|
//col.xyz = (1.0 - saturate(col)).xyz * 0.5;
|
|
|
|
uv.x += sin(_Time.x * 40 + uv.y*40)*0.002;
|
|
uv.y += cos(_Time.x * 50 + uv.x*50)*0.0015;
|
|
//float s = saturate(sobel2(uv));
|
|
float s = saturate(simpleEdge(uv));
|
|
|
|
float depth = SampleDepth(uv);
|
|
depth = Linear01Depth(depth, _ZBufferParams);
|
|
depth = depth * _ProjectionParams.z;
|
|
|
|
|
|
float dist = fmod(_Time.w*5, 30);
|
|
|
|
|
|
|
|
float waveFront = step(depth, dist);
|
|
float waveTrail = smoothstep(dist - _WaveTrail, dist, depth);
|
|
float wave = waveFront * waveTrail;
|
|
|
|
|
|
//image with sobel
|
|
//return half4(s.xxx, 1);
|
|
//return half4(wave.xxx,1);
|
|
col.xyz = s.xxx + _WaveColor * wave;
|
|
|
|
//uv = abs(i.uv * 2 - 1.0);
|
|
//half a = uv.x * uv.y;
|
|
uv = abs(uv - 0.5);
|
|
//uv = abs(i.uv - 0.5);
|
|
half a = uv.x + uv.y;
|
|
a = pow(a,3);
|
|
|
|
//just alpha
|
|
//col.xyz = col.aaa;
|
|
half3 emissive;
|
|
a = AlphaCutOut(i.uv, emissive);
|
|
if (a < 0.5)
|
|
discard;
|
|
col.xyz += emissive;
|
|
return col;
|
|
}
|
|
ENDHLSL
|
|
|
|
}
|
|
}
|
|
}
|