Sunday 12 August 2018

HLSL tutorial : Add reflection

real-time reflection
In real world,  a reflection of and object comes from the environment around it. Offline rendering uses ray trace technique to calculate accurate what's around the target object. In real time rendering shader can use cube map to mimic environment around it and accelerate the calculation of reflection.

I am talking about the implementation of cube map in this chapter.

cube map
A cube map is made from 6 square image. 


When used, you can understand it as : each square image is apply to oneside of the virtual skybox that is surrounding the target object. Althoughthe sky box does not actually exist, but think it in this way help you to understand how the sampling and reflection works.
sampling 
In HLSL, cube map is like any other kinds of texture that can be sampled and used in a shader. Instead of using UV coordinate, cube map use direction to sample.  
 understand it in this way : to sample a cube map, we set a point at the center point of the skybox, then, we start a line at direction R, when the line hit the cube map, the color on the texture on the skybox is the result.

reflection
To correctly sample the cube map to create the reflection effect. We need the reflection vector of the view vector as shown in the following image. The angle between the view vector and normal and the angle between the reflection angle and normal is the same.
the equation to get the reflection :
Reflection = view - 2 x dot (View ,  Normal)  x Normal
or we can simply use reflect ret reflect(in) function to calculate. 

implementation
With the basic knowledge above, the implementation is very straight forward:

reflect objects around

Using cube map can reflect a predefined environment but can not reflect the object surround the reflective object.

A way to achieve this is using reflection prob.

A reflection prob defines a point in the environment that render the surrounding environment into a cubemap that can be later used in the shader. The look of the cubemap heavily depends on the view point of the cube map. In real world the cubemap need to be rendered per fragment, but it is unrealistic at runtime. This cause a problem : the reflection is not accurate due to the wrong cube map is used.

parallax-correction cubemap

This technique require artist define the edges for the cub map sampling point. and use the distance between the sample point and reflection point to approximate the correct cube sampling vector from the sample point.

a good reference


R: the real reflection vector
R' : the approximate sample vector on sample point C

calculate the R' (CP)
W is the looking at point on the ground , WP is the reflection vector
CP = WP  - WC

WC = C - vertex position

to calculate WP :
we know that it is on the same direction on the reflection vector R but has a unknown length.
we can say:
 WP = R * scaler;


as shown in the image,
OR/OP = OB/OA = OR(x) / MaxBox(x)

scaler =   MaxBox(x) /OR(x) 

No comments:

Post a Comment