- Home /
Multiple pass Surface Shader
I am working on a multiple pass surface shader based on the Unity builtin VertexLit shader...
Basically, what I am trying to do is let the shader do its thing in its own passes, and then add a second pass to layer some extra color on top of what color was originally there.
I've gotten that far, however the problem is, my surface shader pass is ignoring transparency -- returning an alpha of zero should cause colors from the the original VertexLit shader to bleed through, right? Apparently not!
I am very new to any form of advanced shaders, and clearly there is something here I don't understand. Does anyone care to give a bit of clarity to a noob? Thankyou!
Here is the code for the aforementioned shader: -- I've noted the part of the code that stores my "secondary pass"
Shader "VertexLit Toon" {
Properties {
_RimPower ("Rim Power", Float) = 2.5
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Spec Color", Color) = (1,1,1,1)
_Emission ("Emissive Color", Color) = (0,0,0,0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.7
_MainTex ("Base (RGB)", 2D) = "white" {}
}
// 2/3 texture stage GPUs
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
// Non-lightmapped
Pass {
Tags { "LightMode" = "Vertex" }
Material {
Diffuse [_Color]
Ambient [_Color]
Shininess [_Shininess]
Specular [_SpecColor]
Emission [_Emission]
}
Lighting On
SeparateSpecular On
SetTexture [_MainTex] {
Combine texture * primary DOUBLE, texture * primary
}
}
// Lightmapped, encoded as dLDR
Pass {
Tags { "LightMode" = "VertexLM" }
BindChannels {
Bind "Vertex", vertex
Bind "normal", normal
Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
Bind "texcoord", texcoord1 // main uses 1st uv
}
SetTexture [unity_Lightmap] {
matrix [unity_LightmapMatrix]
constantColor [_Color]
combine texture * constant
}
SetTexture [_MainTex] {
combine texture * previous DOUBLE, texture * primary
}
}
// Lightmapped, encoded as RGBM
Pass {
Tags { "LightMode" = "VertexLMRGBM" }
BindChannels {
Bind "Vertex", vertex
Bind "normal", normal
Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
Bind "texcoord1", texcoord1 // unused
Bind "texcoord", texcoord2 // main uses 1st uv
}
SetTexture [unity_Lightmap] {
matrix [unity_LightmapMatrix]
combine texture * texture alpha DOUBLE
}
SetTexture [unity_Lightmap] {
constantColor [_Color]
combine previous * constant
}
SetTexture [_MainTex] {
combine texture * previous QUAD, texture * primary
}
}
// Pass to render object as a shadow caster
Pass {
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
Fog {Mode Off}
ZWrite On ZTest Less Cull Off
Offset 1, 1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_SHADOW_CASTER;
};
v2f vert( appdata_base v )
{
v2f o;
TRANSFER_SHADOW_CASTER(o)
return o;
}
float4 frag( v2f i ) : COLOR
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
// Pass to render object as a shadow collector
Pass
{
Name "ShadowCollector"
Tags { "LightMode" = "ShadowCollector" }
Fog {Mode Off}
ZWrite On ZTest Less
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcollector
#define SHADOW_COLLECTOR_PASS
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
V2F_SHADOW_COLLECTOR;
};
v2f vert (appdata v)
{
v2f o;
TRANSFER_SHADOW_COLLECTOR(o)
return o;
}
half4 frag (v2f i) : COLOR
{
SHADOW_COLLECTOR_FRAGMENT(i)
}
ENDCG
}
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//########## BEGIN SECONDARY SURFACE SHADER PASS ############
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//###########################################################
//###########################################################
CGPROGRAM
#pragma surface surf Lambert
struct Input
{
float4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = 0;
o.Alpha = 0;
}
ENDCG
}
// 1 texture stage GPUs
SubShader {
Tags { "RenderType"="Opaque" }
LOD 100
// Non-lightmapped
Pass {
Tags { "LightMode" = "Vertex" }
Material {
Diffuse [_Color]
Ambient [_Color]
Shininess [_Shininess]
Specular [_SpecColor]
Emission [_Emission]
}
Lighting On
SeparateSpecular On
SetTexture [_MainTex] {
Combine texture * primary DOUBLE, texture * primary
}
}
// Lightmapped, encoded as dLDR
Pass {
Tags { "LightMode" = "VertexLM" }
BindChannels {
Bind "Vertex", vertex
Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
}
SetTexture [unity_Lightmap] {
matrix [unity_LightmapMatrix]
constantColor [_Color]
combine texture * constant
}
}
Pass {
Tags { "LightMode" = "VertexLM" }
ZWrite Off
Blend DstColor Zero
SetTexture [_MainTex] {
combine texture
}
}
}
}
Answer by tanoshimi · Nov 09, 2013 at 06:22 PM
I have to admit I got slightly lost trying to follow your code, but your description of "let the shader do its thing in its own passes, and then add a second pass to layer some extra color on top of what color was originally there." makes it sound like you're looking for a final colour modifier?
See http://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderExamples.html - near the bottom.
Answer by ilya_ca · Nov 09, 2013 at 05:55 PM
It's impossible to have a multiple pass surface shader. You have to write a CG vertex/fragment shader for that purpose.
I'm far from an expert but this thread seems to disagree: http://forum.unity3d.com/threads/achieving-a-multi-pass-effect-with-a-surface-shader.96393/
@Simon O Yeah, right, I didn't know it was possible. Thanks for the link!
Yeah, just for a straightforward answer to this question: Simply put two or more CGPROGRA$$anonymous$$ sections in your shader. Each will get a pass, in the order of the programs in your shader. Here's an example of this, plus some other pass options:
http://albertshih.blogspot.com/2014/11/rules-for-multi-pass-shaders-in-unity.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613084549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shader - Using Multiple Pass for different shader target 1 Answer
Shader - Share values between different pass 0 Answers
Get color of pixel before pass: prefer cg, shaderlab is last resort 0 Answers
is it possible to have two different passes in Shader 0 Answers
Use the stencil buffer in multiple passes in a single shader 0 Answers