Efficient way to generate normal map (from height) for all 360 degrees of a rotated image

Started by
2 comments, last by Nik02 9 years, 2 months ago

I already have a fragment shader that generates a normal map from a height map. Now I need to apply the effect to all 360 degrees of the rotated heightmap.

Of coarse I could just apply the effect 360 times to each heighmap, but if I have already done it once then is it possible to use what I have to efficiently generate the remaining 359 maps?

Im guessing that each pixel gets offset (in r,g,b) by some constant amount depending on the rotation:

PgxXgCT.png

Advertisement

Given the normal that you unpack from the normalmap, I think all you need to do is transform that normal by a matrix that specifies a rotation about the z-axis by the number of degrees you need.

Awesome. It ended up being much simpler than I made it out to be, thanks!


sampler2D input : register(s0);

float degree : register(C0);

float3 rotatedVec(float3 original)
{
	float rad = radians( degree);
	float3x3 rotationMat = 
	{
		cos( rad), -sin(rad), 0,
		sin( rad), cos(rad), 0,
		0,0,1
	};
	
	return mul( rotationMat , original);
}

float4 main(float2 uv : TEXCOORD) : COLOR 
{ 
	
	float3 norm = normalize(tex2D(input, uv.xy).rgb * 2.0 - 1.0);  
	
	float3 newColor = rotatedVec(norm);

	newColor = newColor * 0.5 + 0.5;

	return float4(newColor,1.0); 
}

You could further optimize the shader by calculating the sine (and the cosine) of the rotation angle in CPU and pass it as a shader constant smile.png

Niko Suni

This topic is closed to new replies.

Advertisement