A beginner's guide to writing a looping distortion effect
Benjamin Swee - Custom Unity Shaders
0:00 / 0:00
A beginner's guide to writing a looping distortion effect
2 112 просмотров · 3 года назад
Benjamin Swee - Custom Unity Shaders
3,79 тыс. подписчиков
2 112 просмотров · 3 года назад
If you want to support my Patreon: https://patreon.com/user?u=92850367
Ben here, and I've been writing shaders and full systems of shaders in Unity from scratch for 6-7 years now. Shaders, especially HLSL or CG shaders seem to be less known in Unity, so I just wanted to do my part, share my experiences and really show others how to write shader code.
The goal is to teach others how to write optimal shader code that is production-ready. I also want to try to recreate the cool-looking effects we see in other games. It would be awesome to see more and more people write shaders from scratch, understand it at a deeper level and ultimately create their own shaders.
Shader "YouTube/AnimatedDissolve"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_DistortTexture ("Distort Texture", 2D) = "white" {}
_Color ("Color", color) = (1,1,1,1)
_Intensity ("Intensity", float) = 1
_Ramp ("Ramp", float) = 1
_GlowThickness ("Glow Thickness", float) = 1
_GlowIntensity ("Glow Intensity", float) = 1
_DistortThickness ("Distort Thickness", float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex, _DistortTexture;
float4 _MainTex_ST;
float4 _Color;
float _Intensity, _Ramp, _DistortThickness, _GlowThickness, _GlowIntensity;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 mainTex = tex2D(_MainTex, i.uv);
float luminance = Luminance(mainTex);
float3 mainColor = luminance * _Color;
mainColor = pow(mainColor, _Ramp) * _Intensity;
float3 distortTex = tex2D(_DistortTexture, i.uv);
float distortMask = abs(sin(distortTex.r * 10 + _Time.y));
float distortStep = step(distortMask, _DistortThickness);
float glow = 1 - smoothstep(distortMask - _GlowThickness, distortMask + _GlowThickness, _DistortThickness);
glow *= _GlowIntensity;
return fixed4( mainColor + glow , distortStep);
}
ENDCG
}
}
}
#unity3d #unitydeveloper #gamedev #shaders
For more in-depth access I do have a Udemy course that I will continue to update as I create more mini-lessons.
https://www.udemy.com/course/unity-sh...