All the time, the camera's aspect and game window aspect are smattering, one day from a book to see the camera API, but always feel that the aspect to explain the problem. So seriously thinking about this problem, but also found that after the installation of Cmera.aspect, scene window of the apparent vertebral body unexpectedly different steps, also do not know the reason. Distressed for a long time. After some research and discussions with colleagues to gain, write this article. On the one hand, in order to strengthen their understanding, on the other hand to share to more people.
In the end, we all know that the objects we put in the scene are ultimately rendered to the screen without our cameras. For the perspective camera (Perspective Projection), in unity you will see a white border cone in the scene scenes, which is often said to be the visual vertebral body, which is very important throughout the rendering process, It participates in the calculation and cropping of the perspective projection matrix.
Such as:
The vertebral body has 4 very important parameters, one of which is field of View, which represents half the angle between the top and bottom sides of the camera in the Y direction, and the aspect of the camera, which represents the aspect ratio of the camera's viewport. In the final projection matrix, there are two near-and far-plane. These 4 parameters are very important.
The cameras in unity have a camera component that directly adjusts the fieldofview, in degrees. But you can't find the Aspect property on top of it. Maybe someone will drag the scene in the camera on the far plane of the four white spots to change the shape of the visual vertebral body, you can observe the camera component changes, you find that the change of the revelation is Fieldofview, whether you go to adjust the width or high, Unity will follow the aspect to adjust the height or width accordingly. To ensure that your adjustments do not affect the aspect of the camera.
So how is the camera's aspect set? You can, of course, use Camera.aspect = x in the code to change his value. But why do we not feel any inconvenience every time we do not write code for the aspect assignment? That's because unity automatically sets it according to the settings of the game window. such as
You can see that the game window also has a Aspect, but this Aspect does not refer to the Aspect of the camera, but the aspect ratio of the final game screen. You can see in general these optional settings can be divided into three categories, one is free Aspect, one is to give the aspect ratio, The other type is the specified resolution. Explain it separately.
1. Select the free aspect time (default option):
The aspect ratio of the screen is actually the aspect ratio of your game window, and you can manually drag it to adjust it, and unity will change the aspect value of the camera in the scene in real time, keeping it consistent with the aspect of the game window. You can change the size of the game window over and over and look at the changes in the camera's view of the vertebral body in the scene window.
2. When X:y is selected:
The aspect ratio of the screen is fixed to the size of x:y, and the camera's aspect will be set to x:y, so no matter how you change the size of the game window, the camera's aspect will not change. The body of the scene is not changed.
3. Select the specified resolution (XXXX:YYYY):
Another way of behaving like this, the camera's aspect will also be set to xxxx:yyyy, and no matter how you change the size of the game window, the camera's aspect will not change. In the scene, the vertebral body does not change.
A lot of people may wonder why I'm changing the aspect option in the game window, and I see on the screen that the rendered picture doesn't change much (like being stretched or shrunk). This is because the aspect of the game has always been caused by the aspect of the camera. This involves projection transformations, perspective division, and eventual projection into screen space. It's a mess, and I'm afraid I'm wrong about it. If you find the wrong place welcome correction, first in order to cut the convenience, the final need to change the coordinates of the object from the camera space to a box clipping space, this step is done by the projection transformation, the general engine will be in this step after the space clipping (and some will choose after perspective division to cut). Then through the perspective Division, the coordinates are normalized to a so-called normalized device space, the size of the coordinates is limited to a [ -1~1] space, can be seen as a box is compressed into a cube (or may not be a cube, DX and OpenGL is different). Want to learn more about the students may wish to see Twinsen predecessor Blog related articles.
Then we can imagine that joining me in the camera's visual vertebral body placed a aspect ratio of 2:1 of the plane (such as). In the perspective transformation of the clipping space we should still be a (2:1) plane, but after the normalization of perspective division, we imagine that the visual vertebral body from a 2:1 ( The aspect of the camera) is compressed into a (1:1) cube process. In the normalized device space our rectangular plane should be pressed into a 1:1 square plane (actually not the shape is changing, but the coordinates of the vertices have changed). It is obviously not the result we want if we project this result directly into the plane. This is when you need the game window aspect.
After the perspective Division has normalized the coordinates, it is also important to do a viewport transform on the normalized device space to actually map the object to the screen space, and this step is essentially to solve the distortion caused by the previous projection transform and the perspective Division (just like our long provide NdFeB is squashed), the concrete way is to put the normalized x, The y-coordinate is adjusted according to the aspect of the screen, so if the aspect of the screen and the aspect of the camera are also 2:1, the flattened rectangle is pulled back to the original 2:1. Finally, after the rasterization, we get the result we see in the screen finally.
So the reason why the screen aspect and camera aspect always consistent, is to ensure that the perspective projection is correct. Then if we force the two to be inconsistent, then the image distortion caused by perspective transformation and Perspective division cannot be corrected, and the image of the screen that we render finally is wrong. For example, you hang a script on camera and write Camera.aspect = 2 (that is, 2:1) in its Start method. And the aspect that sets the game viewport is 1:1 then you'll see a square on the screen. If the aspect of the game viewport is set to 1:2, You will find that the width of the rectangle on the original screen has changed from 2:1 to 1:2. This is proving what we have just said.
When I do this, I also find a problem, that is, after you modify the camera's aspect in the code, you will find that the camera in the scene window does not sync to the value you set. And if you change the aspect of the game window at this time, The visual vertebral body in scene actually changes, but you update the output camera.aspect in the camera's script just now, or the value that we just set. And it is true that the values we set are also observed from the rendering effect. So I think once you set the Camera.aspect in the code, the camera in the scene window doesn't sync to the new value, and it loses its reference value. I don't know if this is a bug in unity. Or maybe the white vertebral body is not the apparent vertebral body but has other meanings? Please tell me if you have any students who know.
In the end there are two other places to mention:
1. When we render the contents of the camera to the rendertexture rather than the screen, Then the camera's aspect is set to the same resolution as the rendertexture by default. But eventually, if you put rendertexture as a sticker on the model, it will be stretched and shrunk by UV.
2. For the viewport Rect property of the camera component, the so-called viewport, he affects the screen window size that is actually rendered, and the final rendering window's aspect is actually the aspect and viewport of the game window. The result of the aspect multiplied by the rect. Pay attention to this point.
These are some of my views on camera.aspect. because I do not have a high level of mathematics, a lot of theoretical things can not speak very thorough, and may even understand the error. So I hope we only do reference. If you find that I have a problem with the writing, be sure to point it out.
respect for the wisdom of others, welcome reprint, please specify the author Esfog, the original address http://www.cnblogs.com/Esfog/p/4172896.html
About the aspect of camera in unity