Share a function between two passes inside CG Shader for unity3d -


i'm writing shader in cg language unity3d.
if make shader transparent object need create 2 similar passes in subshader. first 1 render faces (with cull front) , second 1 render front faces (with cull back). code vertex , fragment function same 2 passes.

is possible not double code , declare functions, shared between passes?
want have in code example:

shader "cool shader" { properties {     ... } subshader {      cgprogram     // need declare vertexoutput somewhow here     float4 sharedfragfoo(vertexoutput i) : color  // how make smth this?     {         ....         return float4(...);     }     endcg      pass {         cgprogram         #pragma vertex vert         #pragma fragment frag         vertexoutput vert(vertexinput v) {             vertexoutput o;                  ...             return o;         }          float4 frag(vertexoutput i) : color         {             return sharedfragfoo(i); // call shared between passes function         }          endcg     }      pass {         cgprogram         #pragma vertex vert         #pragma fragment frag         vertexoutput vert(vertexinput v) {             vertexoutput o;             ...             return o;         }          float4 frag(vertexoutput i) : color         {             return sharedfragfoo(i); // call shared between passes function         }          endcg     } } } 

upd: found out how using includes.
possible inside one file?

you can in 1 file using cginclude. if @ shader mobileblur ("hidden/fastblur") unity has shared code @ top , passes below.

here key parts - note cginclude/endcg outside of subshader/pass

shader "yourshader" {     ...      cginclude      #include "unitycg.cginc"      struct shared_v2f     {         float4 pos : sv_position;     }      shared_v2f myvert( appdate_img v )     {         shared_v2f o;          o.pos = mul (unity_matrix_mvp, v.vertex);          return o;     }      fixed4 myfrag( shared_v2f ) : sv_target     {         return fixed4( 1.0, 0.5, 0.0, 1.0 );     }      endcg      subshader     {         ...          pass         {             cgprogram               #pragma vertex myvert             #pragma fragment myfrag              endcg         }     } } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -