Skip to content

Instantly share code, notes, and snippets.

@downthecrop
Created June 9, 2024 15:17
Show Gist options
  • Save downthecrop/c4f4bb35b0954911a603691b6ac0c32e to your computer and use it in GitHub Desktop.
Save downthecrop/c4f4bb35b0954911a603691b6ac0c32e to your computer and use it in GitHub Desktop.
URP
Shader "Xffect/displacement/screen"
{
Properties
{
_DispMap("Displacement Map (RG)", 2D) = "white" {}
_MaskTex("Mask (R)", 2D) = "white" {}
_DispScrollSpeedX("Map Scroll Speed X", Float) = 0
_DispScrollSpeedY("Map Scroll Speed Y", Float) = 0
_StrengthX("Displacement Strength X", Float) = 1
_StrengthY("Displacement Strength Y", Float) = -1
}
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_DispMap); // Declare the texture
SAMPLER(sampler_DispMap); // This is automatically defined, no need to manually declare
TEXTURE2D(_MaskTex); // Declare the mask texture
SAMPLER(sampler_MaskTex); // This is automatically defined, no need to manually declare
TEXTURE2D(_GrabTexture); // Declare the grab texture, assuming you set this in your engine
SAMPLER(sampler_GrabTexture); // This is automatically defined, no need to manually declare
CBUFFER_START(ShaderProperties)
float _DispScrollSpeedX;
float _DispScrollSpeedY;
float _StrengthX;
float _StrengthY;
CBUFFER_END
struct Attributes
{
float4 positionOS : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 screenUV : TEXCOORD1;
};
Varyings MainVert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.color = IN.color;
OUT.texcoord = IN.texcoord;
float4 screenPos = ComputeScreenPos(OUT.positionCS);
OUT.screenUV = screenPos.xy / screenPos.w;
return OUT;
}
float4 MainFrag(Varyings IN) : SV_Target
{
float2 displacement = SAMPLE_TEXTURE2D(_DispMap, sampler_DispMap, IN.texcoord).rg;
float2 scrolledUV = IN.texcoord + _Time.yy * float2(_DispScrollSpeedX, _DispScrollSpeedY);
displacement = float2(displacement.r - 0.5, displacement.g - 0.5) * 2.0;
displacement *= float2(_StrengthX, _StrengthY);
float4 color = SAMPLE_TEXTURE2D(_GrabTexture, sampler_GrabTexture, IN.screenUV + displacement);
color.a *= SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, IN.texcoord).r;
return color * IN.color;
}
ENDHLSL
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent+99" }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Pass
{
Name "BASE"
HLSLPROGRAM
#pragma vertex MainVert
#pragma fragment MainFrag
ENDHLSL
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment