Skip to content

Instantly share code, notes, and snippets.

@ellioman
Created March 18, 2020 15:15
Show Gist options
  • Save ellioman/196e0814a4faa2b463c8114264526101 to your computer and use it in GitHub Desktop.
Save ellioman/196e0814a4faa2b463c8114264526101 to your computer and use it in GitHub Desktop.
Shader "Custom/Dissolve" {
Properties {
//_Color and _MainTex are the generic properties needed to add a texture and a color overlay on our object.
[MainTexture] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
[MainColor] _BaseMap("Base Map (RGB) Smoothness / Alpha (A)", 2D) = "white" {}
//The _SliceGuide and _SliceAmount are responsible for the dissolving of our model.
//The first is a texture that will be used to determine the overall shape of the dissolving, while the latter is how much will the object be dissolved,
//where 0 means that the object is unaffected from the dissolving and 1 means that the object is completely dissolved.
_SliceGuide("Slice Guide (RGB)", 2D) = "white" {}
_SliceAmount("Slice Amount", Range(0.0, 1.0)) = 0
// _BurnSize, _BurnRamp and _EmissionColor properties are used to make the burning effect around the dissolved areas.
//The first one dictates how large the burn areas around the holes will be.
//_BurnRamp is a texture that the burn colors can follow and _EmissionColor is a color that is multiplied with the _BurnRamp
_BurnSize("Burn Size", Range(0.0, 1.0)) = 0.15
_EmissionColor("Emission Color", Color) = (0,0,0)
[NoScaleOffset]_EmissionMap("Emission Map", 2D) = "white" {}
_EmissionAmount("Emission amount", float) = 2.0
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"}
LOD 300
Pass
{
Name "ForwardLit"
Tags { "LightMode" = "UniversalForward" }
// Use same blending / depth states as Standard shader
Blend One Zero
ZWrite On
Cull Off
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma target 2.0
// -------------------------------------
// Material Keywords
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _ALPHAPREMULTIPLY_ON
#pragma shader_feature _ _SPECGLOSSMAP _SPECULAR_COLOR
#pragma shader_feature _GLOSSINESS_FROM_BASE_ALPHA
#pragma shader_feature _NORMALMAP
#pragma shader_feature _EMISSION
#pragma shader_feature _RECEIVE_SHADOWS_OFF
// -------------------------------------
// Universal Pipeline keywords
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile _ _SHADOWS_SOFT
#pragma multi_compile _ _MIXED_LIGHTING_SUBTRACTIVE
// -------------------------------------
// Unity defined keywords
#pragma multi_compile _ DIRLIGHTMAP_COMBINED
#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile_fog
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
#pragma vertex LitPassVertexSimple
#pragma fragment LitPassFragmentSimple2
#define BUMP_SCALE_NOT_SUPPORTED 1
#include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitForwardPass.hlsl"
sampler2D _SliceGuide;
float _BurnSize;
float _SliceAmount;
float _EmissionAmount;
// Used for StandardSimpleLighting shader
half4 LitPassFragmentSimple2(Varyings input) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.uv;
half4 diffuseAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));
half3 diffuse = diffuseAlpha.rgb * _BaseColor.rgb;
half test = tex2D(_SliceGuide, uv).rgb - _SliceAmount;
clip(test);
half alpha = diffuseAlpha.a * _BaseColor.a;
AlphaDiscard(alpha, _Cutoff);
#ifdef _ALPHAPREMULTIPLY_ON
diffuse *= alpha;
#endif
half3 normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap));
half4 specular = SampleSpecularSmoothness(uv, alpha, _SpecColor, TEXTURE2D_ARGS(_SpecGlossMap, sampler_SpecGlossMap));
half smoothness = specular.a;
InputData inputData;
InitializeInputData(input, normalTS, inputData);
half3 emission = 0;
if (test < _BurnSize && _SliceAmount > 0)
{
//apply the emission, map the color ramp to some custom UV coordinates in order to achieve the following effect:
//the leftmost color of the ramp is returned on the edges of the hole, and for the rest of the area that the burning covers the emission fades towards the rightmost color.
emission = SampleEmission(float2(test * (1 / _BurnSize), 0), _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap)) * _EmissionAmount;
}
half4 color = UniversalFragmentBlinnPhong(inputData, diffuse, specular, smoothness, emission, alpha);
color.rgb = MixFog(color.rgb, inputData.fogCoord);
return color;
};
ENDHLSL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment