In addition to transparent effects, we can also use Alpha blend to achieve the effect of FOG (FOG. By gradually clarifying the fog effect, you can increase the sense of realism of the scene.
The implementation of the fog effect is very simple. First, we need a color to indicate the fog, usually gray.
In fact, the effect of fog is closely related to the viewpoint. The closer the viewpoint, the lighter the fog, the farther the distance, and the thicker the fog.
The final object color is the mixture of the fog color and the calculated pixel color. The formula used is as follows:
Final color = fogfactor * Computed pixel color + (1.0-fogfactor) * fogcolor
We can see that the final color is the fog color and the calculated pixel color is based on the weighted average of the fog factor.
Let's take a look at how to calculate the fog factor:
First, define a fog range (fogstart, fogend). In this range, the fog gradually fades from light to thick. After the fogend is exceeded, it is completely the fog color, assuming that the distance from the vertex to the viewpoint is viewdistance, the calculation formula of the fog factor is as follows:
1. linear factor
Linear fog = (fogend-viewpointdistance)/(fogend-fogstart)
2. exponential factor
Exponential fog = exp2 (-ABS (viewpointdistance * fogdensity ))
3. Secondary index factor
Exponential fog 2 = exp2 (-(viewpointdistance * fogdensity) * (viewpointdistance * fogdensity ))
Next we will implement the fog Effect Based on mytutoriald3d11_41:
The first thing to modify is lighttex. vs and lighttex. PS. In vs, we define a constant buffer that represents the fog parameter. Then, we calculate the fog factor based on these parameters and pass the fog factor to the PS stage.
Lighttex. vs code:
Cbuffer fogbuffer
{
Float fogstart;
Float fogend;
Float fogdensity;
Float padding;
};
...
// Calculate the camera position.
Cameraposition = MUL (input. Position, worldmatrix );
Cameraposition = MUL (cameraposition, viewmatrix );
// Calculate linear fog.
Output. fogfactor = saturate (fogend-cameraposition. Z)/(fogend-fogstart ));
Lighttex. PS code:
// Mixed fog color.
Finalcolor = input. fogfactor * finalcolor1 + (1.0-input. fogfactor) * fogcolor;
In lighttexshaderclass, we also need to make some minor changes, add the code for setting the fogbuffer, and add three parameters in the render function and setshaderparameters to set the fog.
Finally, in graphicsclass, define four parameters and pass them into the shader.
Float fogcolor, fogstart, fogend, fogdensity;
// Fog color.
Fogcolor = 0.5f;
// Fog distance.
Fogstart = 20366f;
Fogend = 80366f;
Fogdensity = 0.04f;
First, use fogcolor to set the background. In this way, the fog color is very far away ,...
After the program is executed, the interface is as follows:
Next we will try to modify the calculation method of the fog factor in vs to see the effects of the exponential factor and the secondary index factor.
Exponential factor:
Fogdensity = 0.04f;
// Calculate the exponential factor.
Output. fogfactor = saturate (exp2 (-ABS (cameraposition. z * fogdensity )));
Secondary index factor:
Fogdensity = 0.02f;
// Calculate the exponential factor.
Output. fogfactor = saturate (exp2 (-(cameraposition. z * fogdensity) * (cameraposition. z * fogdensity )));
Complete code can be found:
Project File mytutoriald3d11_42
Download Code:
Http://files.cnblogs.com/mikewolf2002/d3d1139-49.zip
Http://files.cnblogs.com/mikewolf2002/pictures.zip