- Home /
Is it Possible to Have an Arbitrary Number of Passes in GLSL shader
I am trying to write a fur shader using GLSL. I'm a total newbie to shaders but it's going pretty well so far.
I achieve through a multi pass shader where each pass shifts the vertex shader out along the surface normal (also droops with gravity)
Unfortunately the way I have it at the moment is my shader has 20 nearly identical shader passes written (with the only difference being some "extrustion" value increasing for each pass) in one file. This makes my shader pretty frustrating to change as any change to my shader require changing lines in every single pass.
Is there a way to have an arbitrary number of passes.
What I would love is something like
for(int extrustion=0.0f;extrusion<1.0;extrusion+=0.1){
Pass{
shader code...
}
}
Is there some kind of equivelant to this or am is this really not how shaders should be used.
Incase it's relevant here is one pass of my shader code
Pass {
Cull back
ZWrite off
Blend SrcAlpha OneMinusSrcAlpha
GLSLPROGRAM
uniform sampler2D _MainTex;
uniform vec4 _MainTex_ST;
uniform float _minAlpha;
uniform float _maxAlpha;
uniform float _spikiness;
uniform float _extrusionDist;
uniform vec4 _Time;
varying vec4 textureCoordinates;
float extrusion=0.05 ;
#ifdef VERTEX
void main()
{
textureCoordinates = gl_MultiTexCoord0;
vec4 displacedVert = gl_Vertex;
//adjust for gravity (squared based on exstrusion so hair droops)
displacedVert.y -=extrusion*extrusion*0.04*_spikiness;
//sway with "wind"
float breeziness = 0.5;
float num = sin(_Time.z*2.0+displacedVert.x*2.0)+0.7;
if(num>1.0){
num = 1.0;
}
displacedVert.x-=num*extrusion*0.02*breeziness;
//move vertex along surface normal
vec4 norm4 = vec4(gl_Normal,0.0);
float totalExtrusion = _extrusionDist*extrusion;
displacedVert+=norm4*totalExtrusion;
gl_Position = gl_ModelViewProjectionMatrix * displacedVert;
}
#endif
#ifdef FRAGMENT
void main()
{
float cutoff = 0.1;
//get color from teture
vec2 newTexCo = vec2(textureCoordinates);
vec4 newCol = texture2D(_MainTex, newTexCo);
newCol=texture2D(_MainTex,
_MainTex_ST.xy * textureCoordinates.xy
+ _MainTex_ST.zw);
//darken if closer to base
float factor=0.5;
newCol.r = factor*newCol.r+factor*extrusion*newCol.r;
newCol.g = factor*newCol.g+factor*extrusion*newCol.g;
newCol.b = factor*newCol.b+factor*extrusion*newCol.b;
//make more transparent along hair
float alphaness = extrusion*(_maxAlpha-_minAlpha);
if(newCol.a>cutoff){
newCol.a =newCol.a+alphaness;
}
gl_FragColor = newCol;
}
#endif
ENDGLSL
}
Thanks in advance for any advice
Your answer
![](https://koobas.hobune.stream/wayback/20220613162257im_/https://answers.unity.com/themes/thub/images/avi.jpg)