Add light in the scene-Add a HLSL mirror highlight

Source: Internet
Author: User
Tags eye vector
Problem

You want to use a custom HLSL effect to add a mirror highlight to the scene. A mirror highlight is a high brightness area in the reflective position, as shown in Figure 6-11.

Solution

The following discussion will help you determine which pixel has a highlight weight.

On the left of Figure 6-11, a line of light l is displayed, pointing from the light source to a pixel in the triangle. The eye vector is displayed on the left, pointing from the camera to the pixel. If the reflection vector of L is the same as that of E, the pixel has a highlight component.

Figure 6-11 use the light direction near the eye vector to detect pixels

You can obtain the reflection vector of L by finding an image about the pixel normal. If the image direction and eye vector have a small angle, the two directions are almost the same. You can use the dot multiplication vector to check the angle between the two (see tutorial 6-8 ).

If the angle is 0, the two directions are the same. You need to add a highlight component. Then, the result of the dot multiplication is 1. If the two directions are different, the vertex multiplication result is smaller than 1.

Note:The dot multiplication result of the two vectors A and B is equal to (the length of a) * (the length of B) * (the cosine of the angle between the two ). If both A and B have been normalized, the result of the dot multiplication changes to the cosine of the angle between the two ). If the angle between A and B is 0, the cosine is 1. If the two are vertical, the angle is 90 degrees, and the cosine is 0, 6-11, as shown in the right figure. If the two vectors are in the opposite direction, the angle is 180 degrees, and the cosine value is-1. When the angle between the reflected direction and the eye vector is less than 90 degrees, the point multiplication result is positive.

You cannot use this value to determine the highlight immediately, because in this case, the highlight will be added to all pixels with the angle between the reflection vector and the eye vector less than 90 degrees, you want to add a highlight when the angle is less than 10 degrees.

This can be achieved through a higher power of the Point multiplication result. For example, if you perform a 12-power operation on the vertex multiplication result, the value of this value is greater than 0, 6-11, and the angle is smaller than 10 degrees.

The calculation result of each pixel is a single value, indicating the highlight intensity.

Working Principle

As before, you must first set the world, view, and projection matrices to convert 3d positions to 2D screen positions. Because this tutorial uses a point light source, you also need to specify its position. To calculate the eye vector, you need to know the camera position. You also need to be able to set the illumination intensity to control the highlight size. Because the illumination intensity may be greater than 1, you need to reduce the illumination intensity to avoid saturation ).

Note:In most cases, you need to reduce the intensity of the light source. In the case of multiple light sources, the illumination of a large majority of pixels is saturated, which wastes the illumination effect.

 
Using xworld; using xview; using xprojection; float3 xlightposition; float3 lag; float xambient; float xspecularpower; float limit; struct limit {float4 position: position; float3 normal: direction; float3 lightdirection: texcoord1; float3 eyedirection: texcoord2;}; struct slpixeltoframe {float4 color: color0 ;};

Vertex shader also calculates eyedirection and performs interpolation. Pixel shader still only outputs the color of each pixel.

Vertex shader

Vertex shader is not much different from the previous tutorial. The only new thing is that the eye vector is computed in vertex shader. A vector pointing from one point to another can be achieved by subtracting the end point from the start point.

Partition partition (float4 inpos: position0, float3 innormal: normal0) {partition output = (partition) 0; float4x4 previewprojection = MUL (xview, xprojection); float4x4 preworldviewprojection = MUL (xworld, previewprojection); output. position = MUL (inpos, preworldviewprojection); float3 final3dpos = MUL (inpos, xworld); output. lightdirection = final3dpos-xlightposition; output. eyedirection = final3dpos-xcamerapos; float3x3 rotmatrix = (float3x3) xworld; float3 rotnormal = MUL (innormal, rotmatrix); output. normal = rotnormal; return output ;}
Pixel shader

Pixel shader is more interesting. The basic color is blue, so you don't need to pay too much attention. Normalize each direction in pixel shader because its length may not be 1 (see tutorial 6-3 ).

As in the past, you calculated the light and multiplied it by xlightstrength to reduce it by a little (xlightstrength is less than 1 ).

Slpixeltoframe slpixelshader (slvertextopixel pSIN): color0 {slpixeltoframe output = (slpixeltoframe) 0; float4 basecolor = float4 (,); float3 normal = normalize (pSIN. normal); float3 lightdirection = normalize (pSIN. lightdirection); float shading = dot (normal,-lightdirection); shading * = xlightstrength; float3 reflection =-reflect (lightdirection, normal); float3 eyedirection = normalize (pSIN. eyedirection); float specular = dot (reflection, eyedirection); specular = POW (specular, xspecularpower); specular * = xlightstrength; output. color = basecolor * (shading + xambient) + specular; return output ;}

Then, use the reflect function to calculate the image in the light direction. Because the light direction points to the pixel, its reflection direction will point to the eye, the reflection direction is opposite to the eye vector, so a negative value is required.

The specular value can be obtained through the point multiplication eye vector and the reflection direction. The value is calculated by the higher power, so that the pixel highlight values with the angle between the two vectors smaller than 10 degrees will be greater than 0. This value needs to be smaller by multiplying xlightstrength.

Finally, ambient, shading, and specular components are combined to obtain the final color of the pixel.

Note:The specular component is white in the final color. If the light has different colors, you need to multiply the specular value by the color of the light.

Define Technique

The following is the definition of technique:

 
Technique specularlighting {pass pass0 {vertexshader = compile vs_2_0 slvertexshader (); pixelshader = compile ps_2_0 slpixelshader ();}}
Code

Because all HLSL code has been written before, the following is only the xNa code:

Effect. currenttechnique = effect. techniques ["specularlighting"]; effect. parameters ["xworld"]. setvalue (matrix. identity); effect. parameters ["xview"]. setvalue (fpscam. viewmatrix); effect. parameters ["xprojection"]. setvalue (fpscam. projectionmatrix); effect. parameters ["xambient"]. setvalue (0.0f); effect. parameters ["xlightstrength"]. setvalue (0.5f); effect. parameters ["xlightposition"]. setvalue (New vector3 (5.0f, 2.0f,-15.0f); effect. parameters ["xcamerapos"]. setvalue (fpscam. position); effect. parameters ["xspecularpower"]. setvalue (128.0f); effect. begin (); foreach (effectpass pass in effect. currenttechnique. passes) {pass. begin (); device. vertexdeclaration = myvertexdeclaration; device. drawuserprimitives <vertexpositionnormaltexture> (primitivetype. trianglestrip, vertices, 0, 6); pass. end ();} effect. end ();

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.