Parallax Mapping (Parallax Mapping) and steep parallax mapping (steep palallax Mapping)

Source: Internet
Author: User
Tags mul

Parallax Mapping (Parallax Mapping)
Demo Download

parallax mapping has been widely used, only need to increase the depth of a model surface texture information, you can approximate the simulation model of the bump, in the case of no need for light, no need for environmental reflection, can be more realistic simulation of the real world.
Let 's take a look at the difference between parallax mapping and other maps.

It can be seen that Parallax Mapped and steep Parallax Mapped achieve much better results.
The following picture better shows the benefits of parallax mapping

principle:By shifting the texture coordinates according to the user's viewing angle, the position of the upper surface of the model is lowered to cover the lower position. When the user observes the angle change, let some pixel's texture coordinate offset to block some pixels, This allows the viewer to feel the bump. The offset of the texture needs to be determined at the same time as the concave and convex textures of the model and the observation position.


This is a picture of the principle, where the observer's eye is at V, the actual observed object's H (T1) point, which means that the object's real position is at H (T1), but the gaze of the eye will fall to where it is located.to get the real scene, we need to shift the light in to the H (T1), which looks more realistic.
how to calculate the offset of the pixel to H (T1), which requires the observer's position and the position of the current pixel to be calculated. Let's take a look at how the calculations are implemented.is OFFSETUV and viewdir.x and viewdir.y we already know.
A simplified formula is usually used
OFFSETUV = Offsetuv + float2 (-viewdir.x,viewdir,y) *v2scale;

where OFFSETUV represents the resulting color texture offset, viewdir represents the texture observation vector (normalized), V2scale represents the scaling factor associated with the height and the model, which is related to the size of the surface undulation and the overall size of the scale.

in shader there are two parts written:part is the change of the observation direction on each vertex with model space, the whole part is calculated and realized in vertex; the other part is the completion of the calculation of pixel offsets, which is done in PixelShader.
PixelShader
PixelShaderfloat4 rendersceneps (Vs_output in): COLOR0  {     //Calculate parallax Map offset    float3 Viewdir = Normalize ( In.vlookat);    Float2 offsetuv = IN.TEXTUREUV;    Float3 h = tex2d (Meshheightsampler, IN.TEXTUREUV). Y;    Offsetuv + = Float2 (-viewdir.x, VIEWDIR.Y) * (h * 0.04-0.02);    FLOAT4 Texcolor = tex2d (Meshtexturesampler, OFFSETUV);     return texcolor  ;      }


Offsetuv + = FLOAT2 (-viewdir.x, VIEWDIR.Y) * (h * 0.04-0.02);
where this line of code is the expression of parallax mapping formula;
in VertexShader to implement the coordinates of Tangent Space (which I have mentioned in another blog post),
Vs_output Renderscenevs (vs_input in)  {        vs_output out = (vs_output) 0;   The vertex of the normal process change model    float4x4 Matworldview = Mul (G_matworld, g_matview);    float4x4 matworldviewproject = Mul (Matworldview, g_matproject);    Out.position = Mul (in.position, matworldviewproject);    OUT.TEXTUREUV = IN.TEXTUREUV;    -----------------here to compute a transformation matrix---------------------    //transform the world coordinate into a tangent space system    float3x3 Matworldtomodel = float3x3 (      mul (In.tanget,                   g_matworld). XYZ,      Mul (Cross (in.tanget,in.normal), G_matworld). XYZ,      Mul (In.normal,                   g_matworld). xyz);     float4 Position = Mul (in.position, g_matworld);    The direction of the observer on each vertex    Out.vlookat = Mul (Matworldtomodel, normalize (position-g_veyeposition));    return out;  }


steep parallax map steep Parallax MappedLet's take a look at the steep Parallax Mapping, which looks more realistic, shows better results, and, of course, doesn't calculate the same way.
here is the formula for steep Parallax mapping:
Ti = s + (Ex, Ey) I/(n Ez) 0≤i < n
Steep parallax mapping, unlike a simple parallax mapping approximation, does not simply skew the texture coordinates without checking for rationality and associativity, and checks whether the result is close to the correct value. The core idea of this method is to cut the depth of the surface into several layers equidistant. The height map is then sampled from the topmost layer, and each time the texture coordinates are shifted along the direction of V. If the point is already below the surface (the depth of the current layer is greater than the sampled depth), stop checking and use the texture coordinates of the last sample as the result.

principle:The way the steep parallax mapping works is illustrated in the following image. The depth is divided into 8 layers, and the height of each layer is 0.125. The texture coordinate offset for each layer is v.xy/v.z * scale/numlayers. Starting with the position of the top yellow square, here is the manual calculation step:
1. The depth of the layer is 0, and the height graph depth H (T0) is approximately 0.75. The depth of the sample is greater than the depth of the layer, so the next iteration begins.
2. Offset the texture coordinates along the V-direction to select the next layer. The depth of the layer is 0.125, and the height graph depth H (T1) is approximately 0.625. The depth of the sample is greater than the depth of the layer, so the next iteration begins.
3. Offset the texture coordinates along the V-direction to select the next layer. The depth of the layer is 0.25, and the height graph depth H (T2) is approximately 0.4. The depth of the sample is greater than the depth of the layer, so the next iteration begins.
4. Offset the texture coordinates along the V-direction to select the next layer. The depth of the layer is 0.375, and the height graph depth H (T3) is approximately 0.2. The depth sampled is less than the depth of the layer, so the current point on the vector v is below the surface. We found the texture coordinate tp=t3 is the approximate point of the actual intersection.
The following is an algorithm for steep Parallax mapped.
VEC2 steeppallaxmapping (in Vec3 V, in vec2 t) {//Determine number of layers from angle between V and Nconst float Minlayer  s = 5;const float maxlayers = 15;float numlayers = Mix (maxlayers, minlayers, ABS (dot (vec3 (0, 0, 1), v)));//height of each Layerfloat layerheight = 1.0/numlayers;//depth of current layerfloat Currentlayerheight = 0;//shift of texture Coordi Nates for each ITERATIONVEC2 Dtex = Gheightscale * v.xy/v.z/numlayers; Current texture COORDINATESVEC2 currenttexturecoords = t;//get first depth from heightmap float heightfromtexture = te Xture (Normtexsampler, currenttexturecoords). a;//while point is above Surfacewhile (Heightfromtexture > Currentlayerheight) {//to the next Layercurrentlayerheight + = layerheight;//shift texture coordinates along vector vcurr Enttexturecoords-= dtex;//get new depth from heightmapheightfromtexture = Texture (Normtexsampler, currenttexturecoords   ). A; } return currenttexturecoords;}

Demo Download




Parallax Mapping (Parallax Mapping) and steep parallax mapping (steep palallax Mapping)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.