Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Flynn · Nov 27, 2012 at 06:42 AM · shaderpass

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
         }
     }
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simon-O · Mar 11, 2015 at 02:38 AM 1
Share

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/

avatar image ilya_ca · Mar 11, 2015 at 02:43 AM 0
Share

@Simon O Yeah, right, I didn't know it was possible. Thanks for the link!

avatar image okaybenji · Feb 05, 2020 at 10:07 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges