No sun on normalmap

Posted By: Impaler

No sun on normalmap - 06/14/08 07:41

I just downloaded the shader listed as "Normalmapping Shader" on the wiki shader list, and it works perfectly with lights, but not the sun. With only a sun, it appears with no shading whatsoever, it actually looks worse than the other models in the level. I know nothing about actual shader programming, so what can be done to fix this?
Posted By: RedPhoenix

Re: No sun on normalmap - 06/14/08 08:30

Yes this shader is only suited for lights, propably thought as an indoor scene shader.
It could be rewritten easily, however why don't you just use another normalmappingshader, eg. one of this ones:
http://files.filefront.com/Massive+3DGS+Shadercollection/;8119277;/fileinfo.html

Posted By: Impaler

Re: No sun on normalmap - 06/14/08 11:19

Can someone point me to a shader that uses the sun light? I tried the specular normalmapping shader in the projects folder, but it seems to have no light attenuation, and also is very washed out.
Posted By: RedPhoenix

Re: No sun on normalmap - 06/14/08 12:19

Well isn't that exactly what I have done??
Posted By: Slin

Re: No sun on normalmap - 06/14/08 12:23

http://www.coniserver.net/ubb7/ubbthreads.php?ubb=showflat&Number=207861#Post207861
Posted By: MMike

Re: No sun on normalmap - 06/14/08 12:45

i can do anyshader object shader you want, now that i have mental mil eheh
Posted By: Slin

Re: No sun on normalmap - 06/14/08 12:48

Originally Posted By: MMike
i can do anyshader object shader you want, now that i have mental mil eheh


Probably, but they are not really optimised and very dirty layouted... I would not really recomment using it if you arenīt able to clean it up.
Posted By: MMike

Re: No sun on normalmap - 06/14/08 18:47

hum, well thats true, but, i can clea it up, i usauly do it whe i convert it for GS, and remove layouts and stuff, They run fast on my laptop anyway.
i do't see other way of building shaders, since the visual part would be hard.
Posted By: Slin

Re: No sun on normalmap - 06/14/08 19:08

Quote:

i do't see other way of building shaders, since the visual part would be hard.

Wtf? I just write them down and am very happy with it. I havenīt done it yet, but you can even write them in "realtime" getting the visual feedback directly from within A7. It is much more flexible then any shadereditor and a lot cleaner. In many cases it is even faster then converting other shaders.
Posted By: MMike

Re: No sun on normalmap - 06/14/08 21:27

ok but i mean,m you know all the formulas to achive falloff, gradient, noise, etc etc, that reflectio refraction, so easly, than a visual editor, like for example mental mil.

But of course it will be clenear and possible to write them down..
Posted By: Impaler

Re: No sun on normalmap - 06/15/08 07:14

Thanks, Redphoenix, but in my region I am paying internet usage by the megabyte mad - that massive compilation costs a lot!! smile
That set of shaders in your download library, Slin, look awesome with dynamic lights, but again, they don't react properly to the sun. They are completely black where they are not lit by the sun or dynamic light, and the ambient lighting does nothing for that.
I'm sorry about my shader programming ignorance, but this one sunlight bumpmapping shader would really add some spice to my game. If I could have a post, with imbedded code, that would be great wink

Thanks, all you shader pros!

Posted By: Slin

Re: No sun on normalmap - 06/15/08 08:04

In my collection are two outdoor normalmapping shaders supporting the sun as light. One is with clipping and the other without. I used both with the sun and it works without a problem.
Posted By: Joey

Re: No sun on normalmap - 06/15/08 09:26

Originally Posted By: MMike
ok but i mean,m you know all the formulas to achive falloff, gradient, noise, etc etc, that reflectio refraction, so easly, than a visual editor, like for example mental mil.


probably you should know this math for writing shaders. these editors are ok if you want to draft something new. i've also used fx composer just to look at the assembly output of the shaders, but these tools can never replace handwritten shader code.

@impaler: when you post your code we can have a look at it and correct it.
Posted By: Impaler

Re: No sun on normalmap - 06/15/08 11:53

Right!!!
What am I thinking??
Here it is:

Code:
 
//--------------------------------------------------------------
// Diffuse and specular shader
// -------------------------------------------------------------

float4x4 matWorldViewProj;	
float4x4 matWorld;
float4 vecLightPos[8]; //light position
float4 vecLightColor[8]; //light position
float4 vecViewPos;
float4 vecFog;

texture entSkin1; //this is the color map
texture entSkin2; //this is the normal map

sampler ColorMapSampler = sampler_state
{
   Texture = <entSkin1>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = wrap;
   AddressV  = wrap;
};


sampler BumpMapSampler = sampler_state
{
   Texture = <entSkin2>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = wrap;
   AddressV  = wrap;
};


// -------------------------------------------------------------
// 2.0
// -------------------------------------------------------------


struct VS_OUTPUT0
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light1 : TEXCOORD2;
    float Att1 : TEXCOORD3;
    
    float3 Light2 : TEXCOORD4;
    float Att2 : TEXCOORD5;

    float Fog : FOG;
};


VS_OUTPUT0 VS_PASS0(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT0 Out = (VS_OUTPUT0)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, - Viewer);		// V

    //light 1	 
    float3 Light1 = PosWorld - vecLightPos[0]  ; 
    Out.Light1.xyz = mul(worldToTangentSpace, -Light1);	// L
       
    Out.Att1 = distance(PosWorld,vecLightPos[0])/vecLightPos[0].w;				// Point light
    
    //light 2	 
    float3 Light2 = PosWorld - vecLightPos[1]  ; 
    Out.Light2.xyz = mul(worldToTangentSpace, -Light2);	// L
       
    Out.Att2 = distance(PosWorld,vecLightPos[1])/vecLightPos[1].w;				// Point light

    float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    Out.Fog = ofog;	

   return Out;
}


struct PS_INPUT0
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light1 : TEXCOORD2;
    float Att1 : TEXCOORD3;
    
    float3 Light2 : TEXCOORD4;
    float Att2 : TEXCOORD5;
};


 float4 PS_PASS0( PS_INPUT0 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
    
    float3 ViewDir = normalize(psInStruct.View);  

    //light1
    float3 LightDir1 = normalize(psInStruct.Light1);   
    float4 diff1 = saturate(dot(bumpNormal, LightDir1));    // diffuse component 
    float shadow1 = saturate(4 * diff1);
    float3 Reflect1 = normalize(2 * diff1 * bumpNormal - LightDir1);  // R
    float4 spec1 = pow(saturate(dot(Reflect1, ViewDir)), 15); 
    float4 Attenuation1 = saturate(dot(psInStruct.Att1, psInStruct.Att1));
    
    //light2
    float3 LightDir2 = normalize(psInStruct.Light2);     
    float4 diff2 = saturate(dot(bumpNormal, LightDir2));    // diffuse component 
    float shadow2 = saturate(4 * diff2);
    float3 Reflect2 = normalize(2 * diff2 * bumpNormal - LightDir2);  // R
    float4 spec2 = pow(saturate(dot(Reflect2, ViewDir)), 15); 
    float4 Attenuation2 = saturate(dot(psInStruct.Att2, psInStruct.Att2));

    return
    (  
       (0.3 * color) + //ambient
	    ((shadow1 * (color * diff1 + (spec1*gloss.w)) * (1 -Attenuation1))*vecLightColor[0])+ 
	    ((shadow2 * (color * diff2 + (spec2*gloss.w)) * (1 -Attenuation2))*vecLightColor[1])
    );	    
}


struct VS_OUTPUT1
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light3 : TEXCOORD2;
    float Att3 : TEXCOORD3;
    
    float3 Light4 : TEXCOORD4;
    float Att4 : TEXCOORD5;

    float Fog : FOG;
};


VS_OUTPUT1 VS_PASS1(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT1 Out = (VS_OUTPUT1)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, -Viewer);		// V
 
    //light 3	 
    float3 Light3 = PosWorld - vecLightPos[2]  ; 
    Out.Light3.xyz = mul(worldToTangentSpace, -Light3);	// L
       
    Out.Att3 = distance(PosWorld,vecLightPos[2])/vecLightPos[2].w;				// Point light
  
    
    //light 4	 
    float3 Light4 = PosWorld - vecLightPos[3]  ; 
    Out.Light4.xyz = mul(worldToTangentSpace, -Light4);	// L
       
    Out.Att4 = distance(PosWorld,vecLightPos[3])/vecLightPos[3].w;				// Point light

    float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    Out.Fog = ofog;	

    return Out;
}


struct PS_INPUT1
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light3 : TEXCOORD2;
    float Att3 : TEXCOORD3;
    
    float3 Light4 : TEXCOORD4;
    float Att4 : TEXCOORD5;
};


float4 PS_PASS1( PS_INPUT1 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
     
    float3 ViewDir = normalize(psInStruct.View);

    //light3
    float3 LightDir3 = normalize(psInStruct.Light3);  
    float4 diff3 = saturate(dot(bumpNormal, LightDir3));    // diffuse component 
    float shadow3 = saturate(4 * diff3);
    float3 Reflect3 = normalize(2 * diff3 * bumpNormal - LightDir3);  // R
    float4 spec3 = pow(saturate(dot(Reflect3, ViewDir)), 15); 
    float4 Attenuation3 = saturate(dot(psInStruct.Att3, psInStruct.Att3));
   
    //light4
    float3 LightDir4 = normalize(psInStruct.Light4);   
    float4 diff4 = saturate(dot(bumpNormal, LightDir4));    // diffuse component 
    float shadow4 = saturate(4 * diff4);
    float3 Reflect4 = normalize(2 * diff4 * bumpNormal - LightDir4);  // R
    float4 spec4 = pow(saturate(dot(Reflect4, ViewDir)), 15); 
    float4 Attenuation4 = saturate(dot(psInStruct.Att4, psInStruct.Att4));

    return
    (  
	    ((shadow3 * (color * diff3 + (spec3*gloss.w)) * (1 -Attenuation3))*vecLightColor[2])+
	    ((shadow4 * (color * diff4 + (spec4*gloss.w)) * (1 -Attenuation4))*vecLightColor[3])

    );	    
}



struct VS_OUTPUT2
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light5 : TEXCOORD2;
    float Att5 : TEXCOORD3;
   
    float3 Light6 : TEXCOORD4;
    float Att6 : TEXCOORD5;

    float Fog : FOG;
};


VS_OUTPUT2 VS_PASS2(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT2 Out = (VS_OUTPUT2)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, -Viewer);		// V

	//light 5	 
    float3 Light5 = PosWorld - vecLightPos[4]  ; 
    Out.Light5.xyz = mul(worldToTangentSpace, -Light5);	// L
       
    Out.Att5 = distance(PosWorld,vecLightPos[4])/vecLightPos[4].w;				// Point light
    
    //light 6	 
    float3 Light6 = PosWorld - vecLightPos[5]  ; 
    Out.Light6.xyz = mul(worldToTangentSpace, -Light6);	// L
       
    Out.Att6 = distance(PosWorld,vecLightPos[5])/vecLightPos[5].w;				// Point light

	float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    	Out.Fog = ofog;

   return Out;
}



struct PS_INPUT2
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light5 : TEXCOORD2;
    float Att5 : TEXCOORD3;
    
    float3 Light6 : TEXCOORD4;
    float Att6 : TEXCOORD5;
};


float4 PS_PASS2( PS_INPUT2 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
    
    float3 ViewDir = normalize(psInStruct.View);

    //light5
    float3 LightDir5 = normalize(psInStruct.Light5); 
    float4 diff5 = saturate(dot(bumpNormal, LightDir5));    // diffuse component 
    float shadow5 = saturate(4 * diff5);
    float3 Reflect5 = normalize(2 * diff5 * bumpNormal - LightDir5);  // R
    float4 spec5 = pow(saturate(dot(Reflect5, ViewDir)), 15); 
    float4 Attenuation5 = saturate(dot(psInStruct.Att5, psInStruct.Att5));
    
    //light2
    float3 LightDir6 = normalize(psInStruct.Light6);
    float4 diff6 = saturate(dot(bumpNormal, LightDir6));    // diffuse component 
    float shadow6 = saturate(4 * diff6);
    float3 Reflect6 = normalize(2 * diff6 * bumpNormal - LightDir6);  // R
    float4 spec6 = pow(saturate(dot(Reflect6, ViewDir)), 15); 
    float4 Attenuation6 = saturate(dot(psInStruct.Att6, psInStruct.Att6));

    return
    (  
	    ((shadow5 * (color * diff5 + (spec5*gloss.w)) * (1 -Attenuation5))*vecLightColor[4])+ 
	    ((shadow6 * (color * diff6 + (spec6*gloss.w)) * (1 -Attenuation6))*vecLightColor[5])
	   
    );	    
}



// -------------------------------------------------------------
// 1.1
// -------------------------------------------------------------

float3x3 matTangent;
float4x4 matWorldView;
float4 vecSunDir;

float4 DoTransform(float4 Pos)
{
	return mul(Pos,matWorldViewProj);
}

float DoFog(float4 Pos)
{
	float3 P = mul(Pos,matWorldView); // convert vector to view space to get it's depth (.z)
   	return saturate((vecFog.y-P.z) * vecFog.z); // apply the linear fog formula
}

float4 DoPos(float4 Pos)
{
	return (mul(Pos,matWorld));
}
float3 DoPos(float3 Pos)
{
	return (mul(Pos,matWorld));
}

void CreateTangents(float3 inNormal,float3 inTangent)
{
	matTangent[0] = DoPos(inTangent);
	matTangent[1] = DoPos(cross(inTangent,inNormal));	// binormal
	matTangent[2] = DoPos(inNormal);
}

float3 DoTangent(float3 inVector)
{
	return normalize(mul(matTangent,inVector));
}

struct out_bump
{
    	float4 Pos:     POSITION;
    	float  Fog:	FOG;
    	float4 Color:	COLOR;
    	float2 Tex:   	TEXCOORD0;
    	float2 Bump:   	TEXCOORD1;
    	float3 Normal:	TEXCOORD2;
    	float3 Light:	TEXCOORD3;
};

out_bump vs_bump( in float4 inPos: POSITION, in float3 inNormal: NORMAL, in float2 inTex: TEXCOORD0, in float3 inTangent: TEXCOORD2)
{
   	out_bump Out;

	Out.Pos	= DoTransform(inPos);
	Out.Tex	= inTex;
	Out.Bump = inTex; // different coordinates required for ps_1_1
        Out.Color = float4(1.0,1.0,1.0,1.0);
	Out.Fog	= DoFog(inPos);
		
	CreateTangents(inNormal,inTangent);
	float3 N = matTangent[2];
		
	// transform the output values into the 0..1 range
	Out.Light = DoTangent(-vecSunDir) * 0.5 + 0.5;
	Out.Normal = DoTangent(N) * 0.5 + 0.5;

	return Out;		
}


float4 ps_bump(out_bump In): COLOR
{
	float4 base = tex2D(ColorMapSampler,In.Tex);
	float3 bumpNormal = tex2D(BumpMapSampler,In.Bump);
	float diffuse = saturate(dot(In.Light*2 - 1,bumpNormal*2 - 1));	
	diffuse *= saturate(4 * dot(In.Light*2 - 1,In.Normal*2 - 1));

	return base * diffuse;
}


// -------------------------------------------------------------
// techniques//
// -------------------------------------------------------------

// 2.0
technique SpecularNormalMapping_20
{
    pass P0
    {
    	 alphablendenable=false;
   	 srcblend=zero;
        // compile shaders
        VertexShader = compile vs_2_0 VS_PASS0();
        PixelShader  = compile ps_2_0 PS_PASS0();
    }
   
    pass P1
    {
    	 //blend second pass additively with first 
    	 alphablendenable=true;
   	 srcblend=one;
    	 destblend=one;
    	 
       // compile shaders
       VertexShader = compile vs_2_0 VS_PASS1();
       PixelShader  = compile ps_2_0 PS_PASS1();
    } 
    
    pass P2
    {
    	 //blend second pass additively with first and second 
    	 alphablendenable=true;
   	 srcblend=one;
    	 destblend=one;
    	 
       // compile shaders
       VertexShader = compile vs_2_0 VS_PASS2();
       PixelShader  = compile ps_2_0 PS_PASS2();
    } 
}

// 1.1
technique SpecularNormalMapping_11
{
	pass P0
	{		
		VertexShader = compile vs_1_1 vs_bump();
		PixelShader  = compile ps_1_1 ps_bump();
	}
}

// Fallback; If nothing works
technique fallback { pass one { } }


Posted By: Joey

Re: No sun on normalmap - 06/16/08 07:17

Code:
//--------------------------------------------------------------
// Diffuse and specular shader
// -------------------------------------------------------------

float4x4 matWorldViewProj;	
float4x4 matWorld;
float4 vecLightPos[8]; //light position
float4 vecLightColor[8]; //light position
float4 vecSunPos;
float4 vecSunColor;
float4 vecViewPos;
float4 vecFog;

texture entSkin1; //this is the color map
texture entSkin2; //this is the normal map

sampler ColorMapSampler = sampler_state
{
   Texture = <entSkin1>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = wrap;
   AddressV  = wrap;
};


sampler BumpMapSampler = sampler_state
{
   Texture = <entSkin2>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = wrap;
   AddressV  = wrap;
};


// -------------------------------------------------------------
// 2.0
// -------------------------------------------------------------


struct VS_OUTPUT0
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light1 : TEXCOORD2;
    float Att1 : TEXCOORD3;
    
    float3 Light2 : TEXCOORD4;
    float Att2 : TEXCOORD5;

    float Fog : FOG;
};


VS_OUTPUT0 VS_PASS0(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT0 Out = (VS_OUTPUT0)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, - Viewer);		// V

    //light 1	 
    float3 Light1 = PosWorld - vecLightPos[0]  ; 
    Out.Light1.xyz = mul(worldToTangentSpace, -Light1);	// L
       
    Out.Att1 = distance(PosWorld,vecLightPos[0])/vecLightPos[0].w;				// Point light
    
    //light 2	 
    float3 Light2 = PosWorld - vecLightPos[1]  ; 
    Out.Light2.xyz = mul(worldToTangentSpace, -Light2);	// L
       
    Out.Att2 = distance(PosWorld,vecLightPos[1])/vecLightPos[1].w;				// Point light

    float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    Out.Fog = ofog;	

   return Out;
}


struct PS_INPUT0
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light1 : TEXCOORD2;
    float Att1 : TEXCOORD3;
    
    float3 Light2 : TEXCOORD4;
    float Att2 : TEXCOORD5;
};


 float4 PS_PASS0( PS_INPUT0 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
    
    float3 ViewDir = normalize(psInStruct.View);  

    //light1
    float3 LightDir1 = normalize(psInStruct.Light1);   
    float4 diff1 = saturate(dot(bumpNormal, LightDir1));    // diffuse component 
    float shadow1 = saturate(4 * diff1);
    float3 Reflect1 = normalize(2 * diff1 * bumpNormal - LightDir1);  // R
    float4 spec1 = pow(saturate(dot(Reflect1, ViewDir)), 15); 
    float4 Attenuation1 = saturate(dot(psInStruct.Att1, psInStruct.Att1));
    
    //light2
    float3 LightDir2 = normalize(psInStruct.Light2);     
    float4 diff2 = saturate(dot(bumpNormal, LightDir2));    // diffuse component 
    float shadow2 = saturate(4 * diff2);
    float3 Reflect2 = normalize(2 * diff2 * bumpNormal - LightDir2);  // R
    float4 spec2 = pow(saturate(dot(Reflect2, ViewDir)), 15); 
    float4 Attenuation2 = saturate(dot(psInStruct.Att2, psInStruct.Att2));

    return
    (  
       (0.3 * color) + //ambient
	    ((shadow1 * (color * diff1 + (spec1*gloss.w)) * (1 -Attenuation1))*vecLightColor[0])+ 
	    ((shadow2 * (color * diff2 + (spec2*gloss.w)) * (1 -Attenuation2))*vecLightColor[1])
    );	    
}


struct VS_OUTPUT1
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light3 : TEXCOORD2;
    float Att3 : TEXCOORD3;
    
    float3 Light4 : TEXCOORD4;
    float Att4 : TEXCOORD5;

    float Fog : FOG;
};


VS_OUTPUT1 VS_PASS1(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT1 Out = (VS_OUTPUT1)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, -Viewer);		// V
 
    //light 3	 
    float3 Light3 = PosWorld - vecLightPos[2]  ; 
    Out.Light3.xyz = mul(worldToTangentSpace, -Light3);	// L
       
    Out.Att3 = distance(PosWorld,vecLightPos[2])/vecLightPos[2].w;				// Point light
  
    
    //light 4	 
    float3 Light4 = PosWorld - vecLightPos[3]  ; 
    Out.Light4.xyz = mul(worldToTangentSpace, -Light4);	// L
       
    Out.Att4 = distance(PosWorld,vecLightPos[3])/vecLightPos[3].w;				// Point light

    float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    Out.Fog = ofog;	

    return Out;
}


struct PS_INPUT1
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light3 : TEXCOORD2;
    float Att3 : TEXCOORD3;
    
    float3 Light4 : TEXCOORD4;
    float Att4 : TEXCOORD5;
};


float4 PS_PASS1( PS_INPUT1 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
     
    float3 ViewDir = normalize(psInStruct.View);

    //light3
    float3 LightDir3 = normalize(psInStruct.Light3);  
    float4 diff3 = saturate(dot(bumpNormal, LightDir3));    // diffuse component 
    float shadow3 = saturate(4 * diff3);
    float3 Reflect3 = normalize(2 * diff3 * bumpNormal - LightDir3);  // R
    float4 spec3 = pow(saturate(dot(Reflect3, ViewDir)), 15); 
    float4 Attenuation3 = saturate(dot(psInStruct.Att3, psInStruct.Att3));
   
    //light4
    float3 LightDir4 = normalize(psInStruct.Light4);   
    float4 diff4 = saturate(dot(bumpNormal, LightDir4));    // diffuse component 
    float shadow4 = saturate(4 * diff4);
    float3 Reflect4 = normalize(2 * diff4 * bumpNormal - LightDir4);  // R
    float4 spec4 = pow(saturate(dot(Reflect4, ViewDir)), 15); 
    float4 Attenuation4 = saturate(dot(psInStruct.Att4, psInStruct.Att4));

    return
    (  
	    ((shadow3 * (color * diff3 + (spec3*gloss.w)) * (1 -Attenuation3))*vecLightColor[2])+
	    ((shadow4 * (color * diff4 + (spec4*gloss.w)) * (1 -Attenuation4))*vecLightColor[3])

    );	    
}



struct VS_OUTPUT2
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light5 : TEXCOORD2;
    float Att5 : TEXCOORD3;
   
    float3 Light6 : TEXCOORD4;
    float Att6 : TEXCOORD5;

    float Fog : FOG;
};


VS_OUTPUT2 VS_PASS2(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT2 Out = (VS_OUTPUT2)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, -Viewer);		// V

	//light 5	 
    float3 Light5 = PosWorld - vecLightPos[4]  ; 
    Out.Light5.xyz = mul(worldToTangentSpace, -Light5);	// L
       
    Out.Att5 = distance(PosWorld,vecLightPos[4])/vecLightPos[4].w;				// Point light
    
    //light 6	 
    float3 Light6 = PosWorld - vecLightPos[5]  ; 
    Out.Light6.xyz = mul(worldToTangentSpace, -Light6);	// L
       
    Out.Att6 = distance(PosWorld,vecLightPos[5])/vecLightPos[5].w;				// Point light

	float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    	Out.Fog = ofog;

   return Out;
}



struct PS_INPUT2
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light5 : TEXCOORD2;
    float Att5 : TEXCOORD3;
    
    float3 Light6 : TEXCOORD4;
    float Att6 : TEXCOORD5;
};


float4 PS_PASS2( PS_INPUT2 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
    
    float3 ViewDir = normalize(psInStruct.View);

    //light5
    float3 LightDir5 = normalize(psInStruct.Light5); 
    float4 diff5 = saturate(dot(bumpNormal, LightDir5));    // diffuse component 
    float shadow5 = saturate(4 * diff5);
    float3 Reflect5 = normalize(2 * diff5 * bumpNormal - LightDir5);  // R
    float4 spec5 = pow(saturate(dot(Reflect5, ViewDir)), 15); 
    float4 Attenuation5 = saturate(dot(psInStruct.Att5, psInStruct.Att5));
    
    //light2
    float3 LightDir6 = normalize(psInStruct.Light6);
    float4 diff6 = saturate(dot(bumpNormal, LightDir6));    // diffuse component 
    float shadow6 = saturate(4 * diff6);
    float3 Reflect6 = normalize(2 * diff6 * bumpNormal - LightDir6);  // R
    float4 spec6 = pow(saturate(dot(Reflect6, ViewDir)), 15); 
    float4 Attenuation6 = saturate(dot(psInStruct.Att6, psInStruct.Att6));

    return
    (  
	    ((shadow5 * (color * diff5 + (spec5*gloss.w)) * (1 -Attenuation5))*vecLightColor[4])+ 
	    ((shadow6 * (color * diff6 + (spec6*gloss.w)) * (1 -Attenuation6))*vecLightColor[5])
	   
    );	    
}


struct VS_OUTPUT3
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light5 : TEXCOORD2;
    float Att5 : TEXCOORD3;
   
    float3 Light6 : TEXCOORD4;

    float Fog : FOG;
};


VS_OUTPUT3 VS_PASS3(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0  )
{
    VS_OUTPUT3 Out = (VS_OUTPUT3)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = texcoord0.xy;

    float3 PosWorld = mul(Pos, matWorld);

    float3 Viewer = PosWorld - vecViewPos;						
    Out.View = mul(worldToTangentSpace, -Viewer);		// V

	//light 7	 
    float3 Light5 = PosWorld - vecLightPos[7]  ; 
    Out.Light5.xyz = mul(worldToTangentSpace, -Light5);	// L
       
    Out.Att5 = distance(PosWorld,vecLightPos[7])/vecLightPos[7].w;				// Point light
    
    // sun	 
    float3 Light6 = PosWorld - vecSunPos  ; 
    Out.Light6.xyz = mul(worldToTangentSpace, -Light6);	// L
 
	float ofog = 1 - (distance(PosWorld, vecViewPos) - vecFog.x) * (vecFog.z);
    	Out.Fog = ofog;

   return Out;
}



struct PS_INPUT3
{
    float2 Tex : TEXCOORD0;
    float3 View : TEXCOORD1;
   
    float3 Light5 : TEXCOORD2;
    float Att5 : TEXCOORD3;
    
    float3 Light6 : TEXCOORD4;
};


float4 PS_PASS3( PS_INPUT3 psInStruct ):COLOR
{
   
    float4 color = tex2D(ColorMapSampler, psInStruct.Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map
    float4 gloss = tex2D( BumpMapSampler, psInStruct.Tex );
    
    float3 ViewDir = normalize(psInStruct.View);

    //light5
    float3 LightDir5 = normalize(psInStruct.Light5); 
    float4 diff5 = saturate(dot(bumpNormal, LightDir5));    // diffuse component 
    float shadow5 = saturate(4 * diff5);
    float3 Reflect5 = normalize(2 * diff5 * bumpNormal - LightDir5);  // R
    float4 spec5 = pow(saturate(dot(Reflect5, ViewDir)), 15); 
    float4 Attenuation5 = saturate(dot(psInStruct.Att5, psInStruct.Att5));
    
    // sun
    float3 LightDir6 = normalize(psInStruct.Light6);
    float4 diff6 = saturate(dot(bumpNormal, LightDir6));    // diffuse component 
    float shadow6 = saturate(4 * diff6);
    float3 Reflect6 = normalize(2 * diff6 * bumpNormal - LightDir6);  // R
    float4 spec6 = pow(saturate(dot(Reflect6, ViewDir)), 15); 
 
    return
    (  
	    ((shadow5 * (color * diff5 + (spec5*gloss.w)) * (1 -Attenuation5))*vecLightColor[4])+ 
	    ((shadow6 * (color * diff6 + (spec6*gloss.w)) * vecSunColor)
	   
    );	    
}



// -------------------------------------------------------------
// 1.1
// -------------------------------------------------------------

float3x3 matTangent;
float4x4 matWorldView;
float4 vecSunDir;

float4 DoTransform(float4 Pos)
{
	return mul(Pos,matWorldViewProj);
}

float DoFog(float4 Pos)
{
	float3 P = mul(Pos,matWorldView); // convert vector to view space to get it's depth (.z)
   	return saturate((vecFog.y-P.z) * vecFog.z); // apply the linear fog formula
}

float4 DoPos(float4 Pos)
{
	return (mul(Pos,matWorld));
}
float3 DoPos(float3 Pos)
{
	return (mul(Pos,matWorld));
}

void CreateTangents(float3 inNormal,float3 inTangent)
{
	matTangent[0] = DoPos(inTangent);
	matTangent[1] = DoPos(cross(inTangent,inNormal));	// binormal
	matTangent[2] = DoPos(inNormal);
}

float3 DoTangent(float3 inVector)
{
	return normalize(mul(matTangent,inVector));
}

struct out_bump
{
    	float4 Pos:     POSITION;
    	float  Fog:	FOG;
    	float4 Color:	COLOR;
    	float2 Tex:   	TEXCOORD0;
    	float2 Bump:   	TEXCOORD1;
    	float3 Normal:	TEXCOORD2;
    	float3 Light:	TEXCOORD3;
};

out_bump vs_bump( in float4 inPos: POSITION, in float3 inNormal: NORMAL, in float2 inTex: TEXCOORD0, in float3 inTangent: TEXCOORD2)
{
   	out_bump Out;

	Out.Pos	= DoTransform(inPos);
	Out.Tex	= inTex;
	Out.Bump = inTex; // different coordinates required for ps_1_1
        Out.Color = float4(1.0,1.0,1.0,1.0);
	Out.Fog	= DoFog(inPos);
		
	CreateTangents(inNormal,inTangent);
	float3 N = matTangent[2];
		
	// transform the output values into the 0..1 range
	Out.Light = DoTangent(-vecSunDir) * 0.5 + 0.5;
	Out.Normal = DoTangent(N) * 0.5 + 0.5;

	return Out;		
}


float4 ps_bump(out_bump In): COLOR
{
	float4 base = tex2D(ColorMapSampler,In.Tex);
	float3 bumpNormal = tex2D(BumpMapSampler,In.Bump);
	float diffuse = saturate(dot(In.Light*2 - 1,bumpNormal*2 - 1));	
	diffuse *= saturate(4 * dot(In.Light*2 - 1,In.Normal*2 - 1));

	return base * diffuse;
}


// -------------------------------------------------------------
// techniques//
// -------------------------------------------------------------

// 2.0
technique SpecularNormalMapping_20
{
    pass P0
    {
    	 alphablendenable=false;
   	 srcblend=zero;
        // compile shaders
        VertexShader = compile vs_2_0 VS_PASS0();
        PixelShader  = compile ps_2_0 PS_PASS0();
    }
   
    pass P1
    {
    	 //blend second pass additively with first 
    	 alphablendenable=true;
   	 srcblend=one;
    	 destblend=one;
    	 
       // compile shaders
       VertexShader = compile vs_2_0 VS_PASS1();
       PixelShader  = compile ps_2_0 PS_PASS1();
    } 
    
    pass P2
    {
    	 //blend second pass additively with first and second 
    	 alphablendenable=true;
   	 srcblend=one;
    	 destblend=one;
    	 
       // compile shaders
       VertexShader = compile vs_2_0 VS_PASS2();
       PixelShader  = compile ps_2_0 PS_PASS2();
    } 
    
    pass P3
    {
    	 //blend third pass additively with first and second 
    	 alphablendenable=true;
   	 srcblend=one;
    	 destblend=one;
    	 
       // compile shaders
       VertexShader = compile vs_2_0 VS_PASS3();
       PixelShader  = compile ps_2_0 PS_PASS3();
    } 
}

// 1.1
technique SpecularNormalMapping_11
{
	pass P0
	{		
		VertexShader = compile vs_1_1 vs_bump();
		PixelShader  = compile ps_1_1 ps_bump();
	}
}

// Fallback; If nothing works
technique fallback { pass one { } }


the shader now uses another pass and thus supports 7 lights + sun light. it is not very optimized from what i can see... you could reuse much of the code above, but making it faster and shorter is another thing...

i haven't tested it but i think it should work. if not, just post the error message you get and i'll have a look at it.

joey.
© 2024 lite-C Forums