Sign in to follow this  

Parallax Mapping Help

This topic is 3737 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

My Parallax Mapping is not performing as what it should, I am taking the images from the SDK Demo, which is the wood and the shapes heightmap.
struct VS_INPUT
{
	float3 Position	: POSITION0;
	float3 Normal	: NORMAL0;
	float3 Tangent	: TANGENT0;
	float3 Binormal	: BINORMAL0;
	float2 Texture	: TEXCOORD0;
};

struct VS_OUTPUT
{
	float4 Position	: POSITION0;
	float2 Texture	: TEXCOORD0;
	float3 LightTS	: TEXCOORD1;
	float3 ViewTS	: TEXCOORD2;
	float3 NormalTS	: TEXCOORD3;
};

struct PS_OUTPUT
{
	float4 Color0	: COLOR0;
};

VS_OUTPUT vs_floor(in VS_INPUT In)
{
	VS_OUTPUT Out;
	
	float3 NormalWS = mul(In.Normal, (float3x3)World);
	float3 BinormalWS = mul(In.Binormal, (float3x3)World);
	float3 TangentWS = mul(In.Tangent, (float3x3)World);
	
	float3x3 TBNMatrix = float3x3(normalize(TangentWS), normalize(BinormalWS), normalize(NormalWS));
	float4 PositionWS = mul(float4(In.Position, 1.0f), World);
	
	Out.Position = mul(PositionWS, mul(View, Projection));
	Out.Texture = In.Texture;
	Out.LightTS = mul(Light.Position - PositionWS, TBNMatrix);
	Out.ViewTS = mul(EyePosition - PositionWS, TBNMatrix);
	Out.NormalTS = mul(NormalWS, TBNMatrix);
	
	return Out;
}

float4 ComputeIllumination(float4 BaseColor, float3 LightTS, float3 ViewTS, float3 NormalTS)
{
	float4 FinalColor;
	
	float4 Diffuse = saturate(dot(NormalTS, LightTS)) * float4(1.0f, 1.0f, 1.0f, 1.0f);
	
	float3 HalfVec = normalize(ViewTS + LightTS);
	
	float fRdotL = saturate(dot(NormalTS, HalfVec));
	
	float4 Specular = saturate(pow(fRdotL, 130));
	
	FinalColor = (Diffuse * BaseColor) + Specular;
	
	return FinalColor;
}

PS_OUTPUT ps_floor_parallax_occlusion(in VS_OUTPUT In)
{
	PS_OUTPUT Out;
	
	float MaxParallaxLength = length(In.ViewTS.xy) / In.ViewTS.z;
	MaxParallaxLength *= 0.5f;
	float2 ParallaxOffsetVector = MaxParallaxLength * normalize(-In.ViewTS.xy);
	
	float3 nLightTS = normalize(In.LightTS);
	float3 nViewTS = normalize(In.ViewTS);
	float3 nNormalTS = normalize(In.NormalTS);
	
	int NumSamples = (int)lerp(MaxSamples, MinSamples, dot(nViewTS, nNormalTS));
	float StepSize = 1.0f / (float)NumSamples;
	float2 ParallaxOffsetStep = ParallaxOffsetVector * StepSize;
	float2 dx, dy;
	dx = ddx(In.Texture);
	dy = ddy(In.Texture);
	float2 TexSampler = In.Texture;
	float2 TexOffset = In.Texture;
	int iSample = 0;
	float CurrHeight = 0.0f;
	float PrevHeight = 1.0f;
	float RayHeight = 1.0f;
	float Ratio = 0.0f;
	
	while (iSample < NumSamples)
	{
		CurrHeight = tex2Dgrad(NormalHeightSampler, TexOffset, dx, dy).a;
		
		if (CurrHeight > RayHeight)
		{
			Ratio = (CurrHeight - RayHeight) / (StepSize - PrevHeight + CurrHeight);
			TexOffset = TexOffset - (Ratio * ParallaxOffsetStep);
			iSample = NumSamples + 1;
		}
		else
		{
			TexOffset = TexOffset + ParallaxOffsetStep;
			RayHeight = RayHeight - StepSize;
			PrevHeight = CurrHeight;
			iSample++;
		}
	}
	
	TexSampler = TexOffset;
	float3 NormalTS = normalize(2.0f * tex2D(NormalHeightSampler, TexSampler).rgb - 1.0f);
	float4 BaseColor = tex2D(TexSceneSampler, TexSampler);
		
	Out.Color0 = ComputeIllumination(BaseColor, nLightTS, nViewTS, NormalTS);
	Out.Color0.a = 1.0f;
	
	return Out;
}
Here is the image of what it turn out to be: The image seems warped. Is there anything wrong with my HLSL code? Sorry for the long post

Share this post


Link to post
Share on other sites
It's difficult to say from just static code analysis what your problem is, but I have seen similar results when I implemented POM last year. It tends to be a simple case of flipping the sign/direction of a ray or vector that you're using - you'll probably find you're tracing in the wrong direction or offsetting in the wrong direction...

It can take a while to work it through from the theory, but there are lots of POM/RM samples online you can use as references. See how they're doing it and see if there are any obvious differences. Start with the SDK implementation.

hth
Jack

Share this post


Link to post
Share on other sites

This topic is 3737 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