Skip to content

Instantly share code, notes, and snippets.

@zephyo
Created May 21, 2024 20:19
Show Gist options
  • Save zephyo/af47dc9c74acc3b0917e9fdbb67bbfac to your computer and use it in GitHub Desktop.
Save zephyo/af47dc9c74acc3b0917e9fdbb67bbfac to your computer and use it in GitHub Desktop.
Shader "Custom/WorldSpaceExample"
{
Properties
{
[Toggle(ENABLE_DEBUG)]
_Lighting ("Enable Debug", Float) = 1
_MainTex("Diffuse", 2D) = "white" {}
_MaskTex("Mask", 2D) = "white" {}
_NormalMap("Normal Map", 2D) = "bump" {}
Size("Size", Range(0, 16)) = 7
Speed("Speed", Range(0, 16)) = 5
Peaks("Peaks", Range(0, 5)) = 0.1
_Intensity("_Intensity", Range(0, 5)) = 1
// Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
[HideInInspector] _Color("Tint", Color) = (1,1,1,1)
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
[HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
[HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
}
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
ENDHLSL
SubShader
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
ZWrite Off
Pass
{
Tags { "LightMode" = "Universal2D" }
HLSLPROGRAM
#pragma vertex CombinedShapeLightVertex
#pragma fragment CombinedShapeLightFragment
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
#pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
#pragma shader_feature ENABLE_DEBUG
struct Attributes
{
float3 positionOS : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
half4 color : COLOR;
float2 uv : TEXCOORD0;
half3 lightingUV : TEXCOORD1;
float3 positionWS : TEXCOORD2;
UNITY_VERTEX_OUTPUT_STEREO
};
#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_MaskTex);
SAMPLER(sampler_MaskTex);
TEXTURE2D(_NormalMap);
SAMPLER(sampler_NormalMap);
half4 _MainTex_ST;
half4 _NormalMap_ST;
float Size;
float Speed;
float Peaks;
float _Intensity;
#if USE_SHAPE_LIGHT_TYPE_0
SHAPE_LIGHT(0)
#endif
#if USE_SHAPE_LIGHT_TYPE_1
SHAPE_LIGHT(1)
#endif
#if USE_SHAPE_LIGHT_TYPE_2
SHAPE_LIGHT(2)
#endif
#if USE_SHAPE_LIGHT_TYPE_3
SHAPE_LIGHT(3)
#endif
Varyings CombinedShapeLightVertex(Attributes v)
{
Varyings o = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.positionCS = TransformObjectToHClip(v.positionOS);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float4 clipVertex = o.positionCS / o.positionCS.w;
o.lightingUV = ComputeScreenPos(clipVertex);
o.positionWS = TransformObjectToWorld(float3(0,0,o.lightingUV.z));
o.color = v.color;
return o;
}
float2 DoodleUV(float2 p, float Size, float Speed, float Distance)
{
float2 co = p;
Speed = ((_Time * 20 * Speed) / Speed) * Speed;
Distance = Distance;
co.x = sin((co.x * Size * Distance + Speed) * 4) * _Intensity;
co.y = cos((co.y * Size * Distance + Speed) * 4) * _Intensity;
p = lerp(p, p + co, 0.0005 * Size);
return p;
}
#include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
half4 CombinedShapeLightFragment(Varyings i) : SV_Target
{
float SCREENSPACES = 5;
float x = abs(i.positionWS.x-i.positionCS.x);
x/= _ScreenParams.x*SCREENSPACES;
// x = saturate(x);
float y = abs(1-(i.positionWS.y-i.positionCS.y));
y/= _ScreenParams.y*SCREENSPACES;
// x = saturate(y);
#ifdef ENABLE_DEBUG
return float4(0,x,y,1);
#endif
float peak = sin(_Time * Peaks);
// i.uv = DoodleUV(i.uv, Size, Speed, distance(i.uv + float2(peak, peak),float2(0.5,0.5)));
float2 xy = float2(x,y);
float2 diff = i.uv - xy;
float2 displace = DoodleUV(xy, Size, Speed, distance(xy + float2(peak, peak),float2(0.5,0.5))) ;
i.uv = diff+displace;
half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
main.r = x;
main.g = y;
return main;
}
ENDHLSL
}
}
Fallback "Sprites/Default"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment