Created
August 18, 2016 12:45
-
-
Save ThePhiMa/748c748c723c9e7c7b7da86115e4911d to your computer and use it in GitHub Desktop.
Blend Add Shader (as used in Diablo 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Blend Add Shader (as used in Diablo 3) | |
// Uses the alpha channel to determine if the pixel needs to be blended additively or by transparency. | |
// Is a good way prevent the additive buildup that makes a scene with a lot of particle effects white and unreadable while still having some particle texture features. | |
// Idea by Julian Love - http://www.gdcvault.com/play/1017660/Technical-Artist-Bootcamp-The-VFX | |
Shader "Custom/Blend Add Particle Test" | |
{ | |
Properties | |
{ | |
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {} | |
_BlendThreshold("Blend Treshold (0.0:Additive, 1.0:Trasparency)", Range(0.0, 1.0)) = 0.5 | |
} | |
SubShader | |
{ | |
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" } | |
Lighting Off | |
Fog{ Mode Off } | |
ZWrite Off | |
Cull Off | |
// Alpha Channel: White = Transparent blend; Black = Additive blend | |
Pass | |
{ | |
// Additive blending | |
Blend One One | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#include "UnityCG.cginc" | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _BlendThreshold; | |
// Struct Input || VertOut | |
struct appdata { | |
half4 vertex : POSITION; | |
half2 texcoord : TEXCOORD0; | |
fixed4 color : COLOR; | |
}; | |
//VertIn | |
struct v2f { | |
half4 pos : POSITION; | |
fixed4 color : COLOR; | |
half2 texcoord : TEXCOORD0; | |
}; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.color = v.color; | |
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); | |
return o; | |
} | |
fixed4 frag(v2f i) : COLOR | |
{ | |
fixed4 col; | |
fixed4 tex = tex2D(_MainTex, i.texcoord); | |
if (tex.a >= _BlendThreshold) discard; | |
col.rgb = i.color.rgb * tex.rgb; | |
col.a = i.color.a * tex.a; | |
return col; | |
} | |
ENDCG | |
} | |
Pass | |
{ | |
// Alpha blending | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#include "UnityCG.cginc" | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _BlendThreshold; | |
// Struct Input || VertOut | |
struct appdata { | |
half4 vertex : POSITION; | |
half2 texcoord : TEXCOORD0; | |
fixed4 color : COLOR; | |
}; | |
//VertIn | |
struct v2f { | |
half4 pos : POSITION; | |
fixed4 color : COLOR; | |
half2 texcoord : TEXCOORD0; | |
}; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.color = v.color; | |
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex); | |
return o; | |
} | |
fixed4 frag(v2f i) : COLOR | |
{ | |
fixed4 col; | |
fixed4 tex = tex2D(_MainTex, i.texcoord); | |
if (tex.a < _BlendThreshold) discard; | |
col.rgb = i.color.rgb * tex.rgb; | |
col.a = i.color.a * tex.a; | |
return col; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do this in a single pass (and get rid of the expensive branches and discards) by changing the blend mode to One OneMinusSrcAlpha.
Premultiply the alpha with the RBG either in the texture, or the shader - and then use an alpha channel to modulate the output alpha. An alpha of 1 will be blend, an alpha of 0 will be add.