Sign in to follow this  

Parallax mapping shader

This topic is 4089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have parallax shader which is working perfectly with static geometry. Hovever, when I premultiply world and mvp matrix with rotation matrix, lighting goes really weird.
float4x4 mvp;
float4x4 world;
float3 lightPos;
float3 camPos;
float invRad;

struct VS_OUTPUT {
	float4 Pos:      POSITION;
	float2 texCoord: TEXCOORD0;
	float3 lightVec: TEXCOORD1;
	float3 viewVec:  TEXCOORD2;
};

VS_OUTPUT main(float4 pos: POSITION, float2 texCoord: TEXCOORD0, 
               float3 tangent: TEXCOORD1, float3 binormal: TEXCOORD2,
               float3 normal: TEXCOORD3)
{
	VS_OUTPUT Out;

	Out.Pos = mul(mvp, pos);
	Out.texCoord = texCoord;

	float4x4 world_pos = mul(pos, world);
	float3 lightVec = invRad * (lightPos - world_pos);
	float3 viewVec = camPos - world_pos;

	Out.lightVec.x = dot(lightVec, tangent);
	Out.lightVec.y = dot(lightVec, binormal);
	Out.lightVec.z = dot(lightVec, normal);

	Out.viewVec.x = dot(viewVec, tangent);
	Out.viewVec.y = dot(viewVec, binormal);
	Out.viewVec.z = dot(viewVec, normal);
	return Out;
}



sampler Bump;
sampler Base;

float4 lightColor;
float2 pSize;

float4 main(float2 texCoord: TEXCOORD0, float3 lightVec: TEXCOORD1, 
           float3 viewVec: TEXCOORD2) : COLOR 
{
	float atten = saturate(1 - dot(lightVec, lightVec));
	lightVec = normalize(lightVec);
	viewVec = normalize(viewVec);
	
	float height = tex2D(Bump, texCoord).a;
	float2 newTexCoord = texCoord + viewVec.xy * 
                             (height * pSize.x + pSize.y);

	float4 base = tex2D(Base, newTexCoord);
	float3 bump = tex2D(Bump, newTexCoord) * 2 - 1;

	bump = normalize(bump);

	float diffuse = saturate(dot(lightVec, bump));
	float spec = pow(saturate(dot(reflect(-viewVec, bump), lightVec)), 16);

	return lightColor * (diffuse * base + 0.7 * spec) * atten;
}
EDIT: Please remember to use 'source' tags around blocks of source code. [Edited by - jollyjeffers on October 2, 2006 9:56:13 AM]

Share this post


Link to post
Share on other sites
You have to transform the normals, binormals and tangents into world space as well. If you don't do this, your object will rotate and the lighting will rotate with it.

Share this post


Link to post
Share on other sites

This topic is 4089 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this