Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (AndrewAMD, TipmyPip), 12,420 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Normal Mapping Shader + UV Shift #211892
06/18/08 21:44
06/18/08 21:44
Joined: Dec 2005
Posts: 490
Germany/Berlin-Velten
kasimir Offline OP
Senior Member
kasimir  Offline OP
Senior Member

Joined: Dec 2005
Posts: 490
Germany/Berlin-Velten
Hello,
I need a normal mapping shader with UV-Shifting!

I found following Shader, perhaps it is simple to modify them?!?!
Code:

//--------------------------------------------------------------
//                    Normal Mapping Shader
//
//      Matt_Aufderheide, Bloodline, William, xXxGuitar511
// -------------------------------------------------------------

float4x4 matWorldViewProj;	
float4x4 matWorld;
float4 vecViewPos;
float4 vecFog;
float3 vecSunDir;
float4 vecLightPos[8];
float4 vecLightColor[8];
float4 mtlSkill1;
float4 vecAmbient;
float4 vecDiffuse;
float4 vecSpecular;
float4 vecEmissive;
float4 vecLight;
float fAmbient;
float fAlpha;
float fAlbedo;

texture entSkin1;
texture entSkin2;

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;
};


float doFog(float3 WPos)
{
	return(1 - (distance(WPos, vecViewPos) - vecFog.x) * (vecFog.z));
}


// -------------------------------------------------------------
// Pass 1
// -------------------------------------------------------------

struct VS_INPUT1
{
	float4 Pos		: POSITION;
	float3 Normal	: NORMAL;
	float2 Tex		: TEXCOORD0;
	float3 Tangent	: TEXCOORD2;
};

struct VS_OUTPUT1
{
	float4 Pos		: POSITION;
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float3 Sun		: TEXCOORD2;
	float4 Light1	: TEXCOORD3;
	float  Fog		: FOG;
};

struct PS_INPUT1
{
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float3 Sun		: TEXCOORD2;
	float4 Light1	: TEXCOORD3;
};


VS_OUTPUT1 VS_PASS1(VS_INPUT1 In)
{
	VS_OUTPUT1 Out = (VS_OUTPUT1)0; 
	Out.Pos = mul(In.Pos, matWorldViewProj);
	float3 PosWorld = mul(In.Pos, matWorld);
	Out.Fog = doFog(PosWorld);
	Out.Tex = In.Tex;
	
	float3x3 worldToTangentSpace;
	worldToTangentSpace[0] = mul(In.Tangent, matWorld);
	worldToTangentSpace[1] = mul(cross(In.Tangent, In.Normal), matWorld);
	worldToTangentSpace[2] = mul(In.Normal, matWorld);
	
	float3 Viewer = PosWorld - vecViewPos; 
	Out.View = mul(worldToTangentSpace, -Viewer);
	
	// Sun
	Out.Sun = mul(worldToTangentSpace, -vecSunDir);
	
	// Light 1
	float3 Light1 = PosWorld - vecLightPos[0] ;
	Out.Light1.xyz = mul(worldToTangentSpace, -Light1);
	Out.Light1.w = length(Light1)/vecLightPos[0].w;
	
	return Out;
}

float4 PS_PASS1(PS_INPUT1 In) : COLOR
{
	
	float4 inColor = tex2D(ColorMapSampler, In.Tex) * vecDiffuse;
	float4 outColor = tex2D(BumpMapSampler, In.Tex);
	float3 bumpNormal = 2*outColor.xyz - 1;
	float  gloss = outColor.w * fAlbedo;
	float3 ViewDir = normalize(In.View);
	outColor = 0;
	
	float3 sunDir = normalize(In.Sun);   
	float4 sunDiff = saturate(dot(bumpNormal, sunDir));
	float4 sunShadow = saturate(4 * sunDiff);
	float3 sunReflect = normalize(2 * sunDiff * bumpNormal - sunDir);
	float4 sunSpec = pow(saturate(dot(sunReflect, ViewDir)), 15);
	float4 sunOut = (inColor*sunDiff + sunSpec*gloss) * sunShadow * vecSpecular;
	
	float4 ambOut = 0;
	float SLF = round(mtlSkill1.a*10);
	if (SLF == 2 || SLF == 3)
	{ambOut += (inColor*sunDiff*vecLight*vecEmissive*3 + sunSpec*gloss*length(vecLight)) * sunShadow;}
	if (SLF == 1 || SLF == 3)
	{ambOut += vecLight*vecEmissive*inColor;}

	outColor = ambOut;
	
	float4 Light1 = In.Light1;
	float3 LightDir1 = normalize(Light1.xyz);     
	float4 diff1 = saturate(dot(bumpNormal, LightDir1));
	float4 shadow1 = saturate(4 * diff1);
	float3 Reflect1 = normalize(2 * diff1 * bumpNormal - LightDir1);
	float4 spec1 = pow(saturate(dot(Reflect1, ViewDir)), 15); 
	float4 Att1 = saturate(dot(Light1.w, Light1.w));
	
	outColor = (vecAmbient + fAmbient)*inColor + sunOut + ambOut +
	(shadow1 * (inColor * diff1 + (spec1*gloss)) * (1 - Att1))*vecLightColor[0];
	outColor.a = inColor.a * fAlpha;
	
	return outColor;
}


// -------------------------------------------------------------
// Pass 2
// -------------------------------------------------------------

struct VS_INPUT2
{
	float4 Pos		: POSITION;
	float3 Normal	: NORMAL;
	float2 Tex		: TEXCOORD0;
	float3 Tangent	: TEXCOORD2;
};

struct VS_OUTPUT2
{
	float4 Pos		: POSITION;
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float4 Light2	: TEXCOORD2;
	float4 Light3	: TEXCOORD3;
};

struct PS_INPUT2
{
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float4 Light2	: TEXCOORD2;
	float4 Light3	: TEXCOORD3;
};


VS_OUTPUT2 VS_PASS2(VS_INPUT2 In)
{
	VS_OUTPUT2 Out = (VS_OUTPUT2)0; 
	Out.Pos = mul(In.Pos, matWorldViewProj);
	float3 PosWorld = mul(In.Pos, matWorld);
	Out.Tex = In.Tex;
	
	float3x3 worldToTangentSpace;
	worldToTangentSpace[0] = mul(In.Tangent, matWorld);
	worldToTangentSpace[1] = mul(cross(In.Tangent, In.Normal), matWorld);
	worldToTangentSpace[2] = mul(In.Normal, matWorld);
	
	float3 Viewer = PosWorld - vecViewPos; 
	Out.View = mul(worldToTangentSpace, - Viewer);
	
	// light 2
	float3 Light2 = PosWorld - vecLightPos[1];
	Out.Light2.xyz = mul(worldToTangentSpace, -Light2);
	Out.Light2.w = length(Light2)/vecLightPos[1].w;
	
	// light 3
	float3 Light3 = PosWorld - vecLightPos[2] ;
	Out.Light3.xyz = mul(worldToTangentSpace, -Light3);
	Out.Light3.w = length(Light3)/vecLightPos[2].w;
	
	return Out;
}

float4 PS_PASS2(PS_INPUT2 In) : COLOR
{
	
	float4 inColor = tex2D(ColorMapSampler, In.Tex);
	float4 outColor = tex2D(BumpMapSampler, In.Tex);
	float3 bumpNormal = 2*outColor.xyz - 1;
	float  gloss = outColor.w * fAlbedo;
	float3 ViewDir = normalize(In.View);
	outColor = 0;
	
	//light2
	float4 Light2 = In.Light2;
	float3 LightDir2 = normalize(Light2.xyz);     
	float4 diff2 = saturate(dot(bumpNormal, LightDir2));
	float4 shadow2 = saturate(4 * diff2);
	float3 Reflect2 = normalize(2 * diff2 * bumpNormal - LightDir2);
	float4 spec2 = pow(saturate(dot(Reflect2, ViewDir)), 15); 
	float4 Att2 = saturate(dot(Light2.w, Light2.w));
	
	//light3
	float4 Light3 = In.Light3;
	float3 LightDir3 = normalize(Light3.xyz);   
	float4 diff3 = saturate(dot(bumpNormal, LightDir3));
	float4 shadow3 = saturate(4 * diff3);
	float3 Reflect3 = normalize(2 * diff3 * bumpNormal - LightDir3);
	float4 spec3 = pow(saturate(dot(Reflect3, ViewDir)), 15); 
	float4 Att3 = saturate(dot(Light3.w, Light3.w));
	
	outColor += ((shadow2 * (inColor * diff2 + (spec2*gloss)) * (1 - Att2))*vecLightColor[1]);
	outColor += ((shadow3 * (inColor * diff3 + (spec3*gloss)) * (1 - Att3))*vecLightColor[2]);
	outColor.a = inColor.a;
	return outColor;
}


// -------------------------------------------------------------
// Render
// -------------------------------------------------------------

technique SpecularNormalMapping_20
{
	pass P1
	{
		alphaBlendEnable = false;
		srcBlend = zero;
		//
		VertexShader = compile vs_2_0 VS_PASS1();
		PixelShader  = compile ps_2_0 PS_PASS1();
	}
	//
	pass P2
	{
		alphaBlendEnable = true;
		alphaTestEnable = true;
		srcBlend = One;
		destBlend = One;
		fogEnable = false;
		VertexShader = compile vs_2_0 VS_PASS2();
		PixelShader  = compile ps_2_0 PS_PASS2();
	}
}


THX kasimir

Re: Normal Mapping Shader + UV Shift [Re: kasimir] #212851
06/24/08 17:03
06/24/08 17:03
Joined: Sep 2005
Posts: 508
Texas
not_me Offline
User
not_me  Offline
User

Joined: Sep 2005
Posts: 508
Texas
what is uv shift???


-Initiate Games
-Level designer

http://www.sckratchmagazine.com
Re: Normal Mapping Shader + UV Shift [Re: not_me] #212855
06/24/08 17:29
06/24/08 17:29
Joined: Oct 2006
Posts: 873
S
Shadow969 Offline
User
Shadow969  Offline
User
S

Joined: Oct 2006
Posts: 873
it's shifting the skin on the model... i guess you need it for your bulldozer model? smile

Re: Normal Mapping Shader + UV Shift [Re: Shadow969] #212965
06/25/08 06:09
06/25/08 06:09
Joined: Dec 2005
Posts: 490
Germany/Berlin-Velten
kasimir Offline OP
Senior Member
kasimir  Offline OP
Senior Member

Joined: Dec 2005
Posts: 490
Germany/Berlin-Velten
its quite easy to change the script:

changes are marked with "TEST"^^
Code:

//--------------------------------------------------------------
//                    Normal Mapping Shader
//
//      Matt_Aufderheide, Bloodline, William, xXxGuitar511
// -------------------------------------------------------------

float4x4 matWorldViewProj;	
float4x4 matWorld;
float4 vecViewPos;
float4 vecFog;
float3 vecSunDir;
float4 vecLightPos[8];
float4 vecLightColor[8];
float4 mtlSkill1;

float4 vecSkill1;				//TEST

float4 vecAmbient;
float4 vecDiffuse;
float4 vecSpecular;
float4 vecEmissive;
float4 vecLight;
float fAmbient;
float fAlpha;
float fAlbedo;

texture entSkin1;
texture entSkin2;

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;
};


float doFog(float3 WPos)
{
	return(1 - (distance(WPos, vecViewPos) - vecFog.x) * (vecFog.z));
}


// -------------------------------------------------------------
// Pass 1
// -------------------------------------------------------------

struct VS_INPUT1
{
	float4 Pos		: POSITION;
	float3 Normal	: NORMAL;
	float2 Tex		: TEXCOORD0;
	float3 Tangent	: TEXCOORD2;
};

struct VS_OUTPUT1
{
	float4 Pos		: POSITION;
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float3 Sun		: TEXCOORD2;
	float4 Light1	: TEXCOORD3;
	float  Fog		: FOG;
};

struct PS_INPUT1
{
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float3 Sun		: TEXCOORD2;
	float4 Light1	: TEXCOORD3;
};


VS_OUTPUT1 VS_PASS1(VS_INPUT1 In)
{
	VS_OUTPUT1 Out = (VS_OUTPUT1)0; 
	Out.Pos = mul(In.Pos, matWorldViewProj);
	float3 PosWorld = mul(In.Pos, matWorld);
	Out.Fog = doFog(PosWorld);
	Out.Tex = In.Tex + vecSkill1.zw;					//TEST
	
	float3x3 worldToTangentSpace;
	worldToTangentSpace[0] = mul(In.Tangent, matWorld);
	worldToTangentSpace[1] = mul(cross(In.Tangent, In.Normal), matWorld);
	worldToTangentSpace[2] = mul(In.Normal, matWorld);
	
	float3 Viewer = PosWorld - vecViewPos; 
	Out.View = mul(worldToTangentSpace, -Viewer);
	
	// Sun
	Out.Sun = mul(worldToTangentSpace, -vecSunDir);
	
	// Light 1
	float3 Light1 = PosWorld - vecLightPos[0] ;
	Out.Light1.xyz = mul(worldToTangentSpace, -Light1);
	Out.Light1.w = length(Light1)/vecLightPos[0].w;
	
	return Out;
}

float4 PS_PASS1(PS_INPUT1 In) : COLOR
{
	
	float4 inColor = tex2D(ColorMapSampler, In.Tex) * vecDiffuse;
	float4 outColor = tex2D(BumpMapSampler, In.Tex);
	float3 bumpNormal = 2*outColor.xyz - 1;
	float  gloss = outColor.w * fAlbedo;
	float3 ViewDir = normalize(In.View);
	outColor = 0;
	
	float3 sunDir = normalize(In.Sun);   
	float4 sunDiff = saturate(dot(bumpNormal, sunDir));
	float4 sunShadow = saturate(4 * sunDiff);
	float3 sunReflect = normalize(2 * sunDiff * bumpNormal - sunDir);
	float4 sunSpec = pow(saturate(dot(sunReflect, ViewDir)), 15);
	float4 sunOut = (inColor*sunDiff + sunSpec*gloss) * sunShadow * vecSpecular;
	
	float4 ambOut = 0;
	float SLF = round(mtlSkill1.a*10);
	if (SLF == 2 || SLF == 3)
	{ambOut += (inColor*sunDiff*vecLight*vecEmissive*3 + sunSpec*gloss*length(vecLight)) * sunShadow;}
	if (SLF == 1 || SLF == 3)
	{ambOut += vecLight*vecEmissive*inColor;}

	outColor = ambOut;
	
	float4 Light1 = In.Light1;
	float3 LightDir1 = normalize(Light1.xyz);     
	float4 diff1 = saturate(dot(bumpNormal, LightDir1));
	float4 shadow1 = saturate(4 * diff1);
	float3 Reflect1 = normalize(2 * diff1 * bumpNormal - LightDir1);
	float4 spec1 = pow(saturate(dot(Reflect1, ViewDir)), 15); 
	float4 Att1 = saturate(dot(Light1.w, Light1.w));
	
	outColor = (vecAmbient + fAmbient)*inColor + sunOut + ambOut +
	(shadow1 * (inColor * diff1 + (spec1*gloss)) * (1 - Att1))*vecLightColor[0];
	outColor.a = inColor.a * fAlpha;
	
	return outColor;
}


// -------------------------------------------------------------
// Pass 2
// -------------------------------------------------------------

struct VS_INPUT2
{
	float4 Pos		: POSITION;
	float3 Normal	: NORMAL;
	float2 Tex		: TEXCOORD0;
	float3 Tangent	: TEXCOORD2;
};

struct VS_OUTPUT2
{
	float4 Pos		: POSITION;
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float4 Light2	: TEXCOORD2;
	float4 Light3	: TEXCOORD3;
};

struct PS_INPUT2
{
	float2 Tex		: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float4 Light2	: TEXCOORD2;
	float4 Light3	: TEXCOORD3;
};


VS_OUTPUT2 VS_PASS2(VS_INPUT2 In)
{
	VS_OUTPUT2 Out = (VS_OUTPUT2)0; 
	Out.Pos = mul(In.Pos, matWorldViewProj);
	float3 PosWorld = mul(In.Pos, matWorld);
	Out.Tex = In.Tex + vecSkill1.zw;					//TEST
	
	float3x3 worldToTangentSpace;
	worldToTangentSpace[0] = mul(In.Tangent, matWorld);
	worldToTangentSpace[1] = mul(cross(In.Tangent, In.Normal), matWorld);
	worldToTangentSpace[2] = mul(In.Normal, matWorld);
	
	float3 Viewer = PosWorld - vecViewPos; 
	Out.View = mul(worldToTangentSpace, - Viewer);
	
	// light 2
	float3 Light2 = PosWorld - vecLightPos[1];
	Out.Light2.xyz = mul(worldToTangentSpace, -Light2);
	Out.Light2.w = length(Light2)/vecLightPos[1].w;
	
	// light 3
	float3 Light3 = PosWorld - vecLightPos[2] ;
	Out.Light3.xyz = mul(worldToTangentSpace, -Light3);
	Out.Light3.w = length(Light3)/vecLightPos[2].w;
	
	return Out;
}

float4 PS_PASS2(PS_INPUT2 In) : COLOR
{
	
	float4 inColor = tex2D(ColorMapSampler, In.Tex);
	float4 outColor = tex2D(BumpMapSampler, In.Tex);
	float3 bumpNormal = 2*outColor.xyz - 1;
	float  gloss = outColor.w * fAlbedo;
	float3 ViewDir = normalize(In.View);
	outColor = 0;
	
	//light2
	float4 Light2 = In.Light2;
	float3 LightDir2 = normalize(Light2.xyz);     
	float4 diff2 = saturate(dot(bumpNormal, LightDir2));
	float4 shadow2 = saturate(4 * diff2);
	float3 Reflect2 = normalize(2 * diff2 * bumpNormal - LightDir2);
	float4 spec2 = pow(saturate(dot(Reflect2, ViewDir)), 15); 
	float4 Att2 = saturate(dot(Light2.w, Light2.w));
	
	//light3
	float4 Light3 = In.Light3;
	float3 LightDir3 = normalize(Light3.xyz);   
	float4 diff3 = saturate(dot(bumpNormal, LightDir3));
	float4 shadow3 = saturate(4 * diff3);
	float3 Reflect3 = normalize(2 * diff3 * bumpNormal - LightDir3);
	float4 spec3 = pow(saturate(dot(Reflect3, ViewDir)), 15); 
	float4 Att3 = saturate(dot(Light3.w, Light3.w));
	
	outColor += ((shadow2 * (inColor * diff2 + (spec2*gloss)) * (1 - Att2))*vecLightColor[1]);
	outColor += ((shadow3 * (inColor * diff3 + (spec3*gloss)) * (1 - Att3))*vecLightColor[2]);
	outColor.a = inColor.a;
	return outColor;
}


// -------------------------------------------------------------
// Render
// -------------------------------------------------------------

technique SpecularNormalMapping_20
{
	pass P1
	{
		alphaBlendEnable = false;
		srcBlend = zero;
		//
		VertexShader = compile vs_2_0 VS_PASS1();
		PixelShader  = compile ps_2_0 PS_PASS1();
	}
	//
	pass P2
	{
		alphaBlendEnable = true;
		alphaTestEnable = true;
		srcBlend = One;
		destBlend = One;
		fogEnable = false;
		VertexShader = compile vs_2_0 VS_PASS2();
		PixelShader  = compile ps_2_0 PS_PASS2();
	}
}


Last edited by kasimir; 06/25/08 06:10.

Moderated by  Blink, Hummel, Superku 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1