Problem
Even if pixel-by-pixel brightness is enabled, some metal or flashing surfaces are still a little dimmed. In real life, when you look at metal, glass, or plastic, you will find that some areas are very reflective. Such a dotted circle in Area 6-6 is shown. These highlighted areas are called specular highlights ).
Figure 6-6 highlights
Solution
Like pixel-by-pixel illumination, you simply need to tell basiceffect to create a mirror highlight to enable it.
Note:The mirror highlight should only be added to the luminous material. Do not add them to soft surfaces such as clothing or grass, or at least make the reflection very weak.
Working Principle
It is very easy to enable the mirror highlight with basiceffect. Specify the highlight color for each light source. Then, set the highlight intensity. This intensity can specify the width of the highlight. The higher the intensity, the narrower the mirror height. See the tutorial 6-8 to learn the relevant details.
Basiceffect. lightingenabled = true; basiceffect. directionallight0.direction = lightdirection; basiceffect. directionallight0.diffusecolor = color. white. tovector3 (); basiceffect. directionallight0.enabled = true; basiceffect. preferperpixellighting = true; basiceffect. directionallight0.specularcolor = color. white. tovector3 (); basiceffect. specularpower = 32;
Note:When using a mirror highlight, always use pixel-by-pixel illumination. This is because the mirror highlight is usually a small, non-linear point, so they should not be interpolated, but should be calculated pixel by pixel.
Code
The following code defines the vertex and draws a rectangle:
Private void initvertices () {vertices = new vertexpositionnormaltexture [4]; vertices [0] = new vertexpositionnormaltexture (New vector3 (0, 0, 0), new vector3 (0, 1, 0), new vector2 (0, 1); vertices [1] = new vertexpositionnormaltexture (New vector3 (0, 0,-10), new vector3 (0, 1, 0), new vector2 (0, 0); vertices [2] = new vertexpositionnormaltexture (New vector3 (10, 0, 0), new vector3 (0, 1, 0 ), new vector2 (1, 1); vertices [3] = new vertexpositionnormaltexture (New vector3 (10, 0,-10), new vector3 (0, 1, 0 ), new vector2 (1, 0); myvertexdeclaration = new vertexdeclaration (device, vertexpositionnormaltexture. vertexelements );}
In the draw method, add the following code to create basiceffect and add a mirror highlight to the rectangle:
Basiceffect. world = matrix. identity; basiceffect. view = fpscam. viewmatrix; basiceffect. projection = fpscam. projectionmatrix; basiceffect. texture = bluetexture; basiceffect. textureenabled = true; vector3 lightdirection = new vector3 (0,-3,-10); lightdirection. normalize (); basiceffect. lightingenabled = true; basiceffect. directionallight0.direction = lightdirection; basiceffect. directionallight0.diffusecolor = color. white. tovector3 (); basiceffect. directionallight0.enabled = true; basiceffect. preferperpixellighting = true; basiceffect. directionallight0.specularcolor = color. white. tovector3 (); basiceffect. specularpower = 32; basiceffect. begin (); foreach (effectpass pass in basiceffect. currenttechnique. passes) {pass. begin (); device. vertexdeclaration = myvertexdeclaration; device. drawuserprimitives <vertexpositionnormaltexture> (primitivetype. trianglestrip, vertices, 0, 2); pass. end ();} basiceffect. end ();