Skip to content

Instantly share code, notes, and snippets.

@dorodo95
Created October 9, 2020 03:48
Show Gist options
  • Save dorodo95/557b856b20b0463ece39e406736e1c21 to your computer and use it in GitHub Desktop.
Save dorodo95/557b856b20b0463ece39e406736e1c21 to your computer and use it in GitHub Desktop.
Shader "Custom/LED"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_PixelTex ("Pixel Texture", 2D) = "white" {}
_Pixelate ("Pixel Resolution", Range(0.03,1)) = 1
_CameraMaskFallof ("Camera Fallof", Float) = 4
}
SubShader
{
Tags { "RenderType"="Opaque" }
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;
float3 worldPos : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
sampler2D _PixelTex;
float _Pixelate;
float _CameraMaskFallof;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float vertDist = 1 - (distance(i.worldPos, _WorldSpaceCameraPos) * _CameraMaskFallof); //Get distance from camera to vertices
vertDist = saturate(vertDist); //clamp to keep 0-1 range
i.uv += 1 / floor(_Pixelate * _MainTex_TexelSize.zw) * 0.5; //center UV when pixelating
float2 pixelSize = floor(_Pixelate * _MainTex_TexelSize.zw); //get pixel size for tiling
float2 ledUV = i.uv * pixelSize; //UV for LED texture
i.uv = floor(i.uv * pixelSize); //tile orginal UV to pixelate mainTex
i.uv /= floor(pixelSize); //return original uv back to 0-1 range
fixed4 col = tex2D(_MainTex, i.uv); //sample MainTex
float4 pixelTest = tex2D(_PixelTex, ledUV); //sample LED
pixelTest *= col * 2; //Boost LED to simulate emission
pixelTest = lerp(col, pixelTest, vertDist); //Mask based on distance
return pixelTest;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment