Spherical environment texture

Source: Internet
Author: User

ComparedCubeAlthough the results of the body and sphere textures are not very accurate, only one texture is required. In this case, spherical environment textures are useful.

I used the search engine to find it on the Internet. Basically I didn't find any content about the spherical environment texture. I wanted to find a simple shader and use it directly, I did not expect that I did not find it for a long time, so I had to write it myself. BelowArticleI wrote some references and my own understanding. If there is any error, I hope you can correct it.

Environment ing is an approximation based on the assumption that an object in an environment is far from a smooth object, that is, place a small smooth object in a large room. For the point on the surface of an object, assuming there is a light from the eyes to the point, the light is reflected out of the direction to determine the color of the point. In a two-dimensional texture map, the color of each direction is encoded, which is equivalent to placing a sphere with a very high finish in the center of the environment, and then shooting the sphere at a very long distance using a camera with a focal length lens. In mathematics, the focal length of the lens is infinite, and the camera is located in an infinite distance. Therefore, it is necessary to encode the inner circular area of the texture map. The texture values outside the circular area are not affected because they are not used during Environment printing. (Excerpted from OpenGL redbooks)

According to the text from the red book, when a sphere texture is generated in an environment, the sphere can be considered as a unit sphere because the environment is infinitely large as the sphere. At the same time, because the camera is in an infinite distance, the vectors of the points from the camera to the sphere can be seen as parallel.

So it is easy to think:

1. Generate the line of sight vector v.

2. Generate the reflection vector R Based on the vertex normal.

3. Search for the intersection of R and sphere.

4. Obtain the UV coordinate based on the intersection.

R can be easily obtained. In order to obtain UV, we need to find the point of E between the R and the sphere on the sphere. In this case, we return to the scenario when generating a spherical texture map. Because the sphere is a unit sphere, we use one of the properties of the unit sphere: the normalized normal of points on the sphere is the position of the point on the sphere.

We only need to know the same points on the sphere when generating a spherical texture map, and the normal value of the point when the reflection vector is R.

Based on the vector addition principle, the normal is the sum of the line of sight vector and the reflection vector. In order to simulate the fact that the viewpoint is in an infinite distance, we can assume that the process of generating a spherical texture map is in view space. In this way, eye VEC is always (0, 0, 1 ). Therefore, you only need to convert the reflection vector to the view space to obtain the normal vector of the sphere.

The last problem is left. The value range of each component of the obtained normal is [-], while that of UV is []. Therefore, we need to convert it. The following are keyCodeFragment:

 

Code
1   // World normal
2
3 Float3 normalworld = Out. worldnormal;
4
5 // World Space eye VEC
6
7 Float3 eyevecworld = Out. worldview;
8
9 // World reflect VEC
10
11 Float3 reflectworld = Reflect (eyevecworld, normalworld );
12
13 // Eye space relect VEC
14
15 Float3 reflectview = Mul (reflectworld, matviewit );
16
17 // Unit sphere normal in view space
18
19 Float3 spherenormal = Float3 ( 0 , 0 , 1 ) + Reflectview;
20
21 Spherenormal = Normalize (spherenormal );
22
23 Float2 newuv = Spherenormal. XY;
24
25 Newuv. x = Newuv. x * 0.5 + 0.5 ;
26
27 Newuv. Y = Newuv. Y * 0.5 + 0.5 ;
28
29 Out. UV = Newuv;
30
31

Postscript: before writing this article, I have referred to the content in the DirectX SDK documentation, which says, you only need to convert the normal of the vertex to camera space and divide it by 2 and add 0.5. I used this method to experiment in fxcomposer and did not get the correct results. I don't know if it was wrong. The SDK documentation does not elaborate on the ins and outs of this method, and I do not know if I have any friends.

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.