Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Sampling at the Edges of Heightmaps for Normal Mapping


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 multifractal   Members   -  Reputation: 229

Like
0Likes
Like

Posted 04 September 2014 - 08:51 PM

Hello, 

I am creating heightmaps on the GPU and then using a Sobel filter to create a normal map for that heightmap. The Sobel filter works by sampling the 8 pixels surrounding a central pixel. The obvious problem is that when the central pixel is located on the edge of a heightmap the filter will not be able to sample all the edges. To resolve this problem I decided to expand the size of the heightmap from 256x256 pixels to 258x258 pixels. Then when I generate the heightmap for a particular quad I do the following: 


vec3 p = vec3(uv.x, 0.0, uv.y);

//size is the size of the current quad
p *= (size + 2);
p -= (1.0/(size + 2.0));
//'meshoffset' offsets the scaled meshes points from the origin
p += meshOffset;

when it comes to sample the heightmap I warp the UV coordinates like so: 


//the uv coordinates range from (0.0,0.0),(0.0, 1.0/17.0),(0.0, 2.0/17.0)...-> (1.0, 1.0)
uv *= (15.0/17.0);
uv += (1.0/17.0);

yet this still does not cause the edges to match up. 

 

Does anyone have any experience with this?

Thanks. 



Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 14026

Like
5Likes
Like

Posted 04 September 2014 - 09:32 PM

If the heightmap can be smoothly repeated then all you need to do is set the sampler UV sample modes to wrap and you are done.

 

If not you can use clamped wrap mode.  This will sample the edge texels multiple times, giving them more weight, making the normal point more upwards.  This is a cheap way, but causes artifacts in several places.

 

A more expensive way is to detect when any UV coordinates will be less than 0 or greater than 1, and nudge the source sample down, left, right, or up 1 texel, effectively resampling the second-to-the-edge texels again.  The outer rim would have the same normals as the second-to-outer-rim.

 

To make it look nicer while adding to the complexity of the shader, you could then sample the second-to-outer-most texels and the outer-most-texels and create a normal offset which you can add to the previously calculated normal.  Weight it so as not to offset too much.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS