Skip to content

Instantly share code, notes, and snippets.

@blewert
Last active February 17, 2020 09:31
Show Gist options
  • Save blewert/183036cadf47808768a2ff27ae32a3ab to your computer and use it in GitHub Desktop.
Save blewert/183036cadf47808768a2ff27ae32a3ab to your computer and use it in GitHub Desktop.
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
/**
* @author Benjamin Williams <bwilliams@lincoln.ac.uk>
* @file threshold-shader.shader
*/
Shader "blewert/Dist-based Threshold"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_BandTex("Band Texture", 2D) = "white" {}
[Space(15)]
[Header(Effect parameters)][Space] _ShotPosition("Reference position (W = distance)", Vector) = (0,0,0,0)
_Percent("Effect completion", Range(0.0, 1.0)) = 0.0
[Enum(Local, 0, World, 1)] _DistanceCalculation("Space", Float) = 0
_OutsideTint("Outside tint", Color) = (1,1,1,1)
[Space(15)]
[Header(Frontier options)][Space]
[PowerSlider(3.0)] _Frontier("Frontier size", Range(0.0, 1.0)) = 0.0
[KeywordEnum(Texture, Bands)] _FrontierMode("Frontier mode", Float) = 0
_FrontierBandThickness("Band thickness", Range(0.0, 1.0)) = 0.0
_FrontierBandFrequency("Band frequency", Range(0.0, 150.0)) = 0.0
_FrontierThresholdTex("Threshold (tex)", Range(0.0, 1.0)) = 0.0
_FrontierTint("Frontier tint", Color) = (1,1,1,1)
_PercentPow("Frontier falloff", Float) = 1
_FrontierJaggedness("Frontier Jaggedness", Float) = 0
}
SubShader
{
Tags { "RenderType" = "Opaque" }
Cull Off
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input
{
float2 uv_MainTex;
float2 uv_BandTex;
float3 worldPos;
float3 objPos;
};
static const float SPACE_LOCAL = 0;
static const float SPACE_WORLD = 1;
static const float FRONTIER_MODE_TEXTURE = 0;
static const float FRONTIER_MODE_BANDS = 0;
sampler2D _MainTex;
sampler2D _BandTex;
uniform half4 _ShotPosition;
uniform half _Percent;
uniform half _Frontier;
uniform half _DistanceCalculation;
uniform half _FrontierMode;
uniform half _FrontierThresholdTex;
uniform half _FrontierBandThickness;
uniform half _PercentPow;
uniform half _FrontierBandFrequency;
uniform half _FrontierJaggedness;
uniform half4 _OutsideTint;
uniform half4 _FrontierTint;
half4 renderOutsideEffect(Input IN)
{
half4 col = tex2D(_MainTex, IN.uv_MainTex);
return col * _OutsideTint;
}
half4 renderInsideEffect(Input IN)
{
clip(-1);
return half4(1, 1, 1, 1);
}
half ilerp(half x, half a, half b)
{
return (x - a) / (b - a);
}
half4 renderFrontierEffect(Input IN, float threshold, float dist)
{
float3 col = tex2D(_BandTex, IN.uv_BandTex).rgb;
float startDist = threshold;
float endDist = _ShotPosition.w;
float percent = ilerp(dist, startDist, endDist);
float lum = (col.r + col.g + col.b) / 3.0;
if (_FrontierMode == FRONTIER_MODE_TEXTURE)
clip(lum * pow(percent, _PercentPow) - _FrontierThresholdTex);
else
clip(frac(percent * _FrontierBandFrequency) - _FrontierBandThickness);
half4 renderCol = tex2D(_MainTex, IN.uv_MainTex) * _FrontierTint;
return lerp(renderCol, _FrontierTint, _FrontierTint.a);
}
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
o.objPos = v.vertex;
}
void surf(Input IN, inout SurfaceOutput o)
{
//Calculate distance from surf to shot position
half dist = distance(IN.objPos.xyz, _ShotPosition.xyz);
//World space?
if (_DistanceCalculation == SPACE_WORLD)
dist = distance(IN.worldPos.xyz, _ShotPosition.xyz);
//The distance threshold is just the max distance (w) mul percent
half threshold = _ShotPosition.w * _Percent;
half4 finalColour = half4(0, 0, 0, 0);
float3 col = tex2D(_BandTex, IN.uv_BandTex).rgb;
half lum = (col.r + col.g + col.b) / 3.0;
lum = lerp(lum, 1, _FrontierJaggedness);
if (dist < threshold)
finalColour = renderInsideEffect(IN);
else if (dist < (threshold + _Frontier * lum))
{
finalColour = renderFrontierEffect(IN, threshold, dist);
o.Emission = lerp(finalColour, float4(0, 0, 0, 0), 1 - _FrontierTint.a);
}
else
finalColour = renderOutsideEffect(IN);
//clip(frac((IN.worldPos.y + IN.worldPos.z * 0.1) * 5) - 0.5);
//tex2D(_MainTex, IN.uv_MainTex).rgb;
o.Albedo = finalColour;
//o.Normal = UnpackNormal(tex2D(_BandTex, IN.uv_BandTex));
}
ENDCG
}
Fallback "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment