For VR hand-tour development of Small white Tutorial: (iv) Supplemental, Detailed introduction of unity in the projection matrix camera

Source: Internet
Author: User
Tags in degrees sin

This article, as a supplement to the previous article, focuses on the problem of the projection matrix inside Unity:

On the link to the VR Hand tour development of Small white Tutorial: (iii) UNITYVR plug-in cardboardsdkforunity analysis (ii)

About the camera in unity, the Holy Scriptures have a brief introduction to each of the attributes, and the little partner portal is below

Http://www.ceeger.com/Components/class-Camera.html

first, cutting surface

Starting with this professional vocabulary, here is a description of the cutting surface of the Holy Scriptures:

The Near and far Clip Plane properties determine where the Camera ' s view begins and ends. The planes is laid out perpendicular to the Camera ' s direction and is measured from the IT position. The near plane are the closest location that'll be rendered, and the far plane is the furthest.

The near clipping surface and the far clipping surface properties determine the beginning and end of the camera's field of view. The plane is positioned vertically in the direction of the camera and measured from its position. The near clipping surface is the nearest render position, and the far plane is the farthest rendering position.


This sentence feeling said and did not say the same, because we have not seen the cutting surface, so understand the first step of the cutting surface, we need to look at unity in the intuitive to see it, when we close to the camera, we will find a white line of pyramid (four pyramid), which is very good understanding, He expressed the camera's field of vision, oddly enough, that the pyramid was missing a corner, so that the pyramid had not only the bottom, but also a top face.

I believe I can guess, the top of the pyramid is near the cutting surface (nearly clip planes), and the bottom side is the far cutting surface (close clip planes)

So, what's the use of cutting surface for half a day?

We continue to change the value of the clipping planes in Unity's camera, to see the changes, first to clipping planes's near and far respectively 2 and 6

such as the preview interface, there is no object between the near clipping surface and the far clipping surface, there is no object in the rendered image.


Add far to the value, now a small piece of the cube is included, but we look at the preview interface, and do not see it is a cube, can only see the plane effect


OK, continue to increase, now the whole cube is included in, look at the preview, finally can clearly see is the cube



This example shows that the camera seems to only render near the cutting surface and the far cutting surface between the object, this principle is like the rendering of the plan, we need to crop the picture, the program will know the scope to be rendered, whether this picture is all need or only a part, the first step, are cropped. Now, 3D of the rendering needs to be cropped, so the near clipping surface and the far cutting surface was born, but the clipping range is not planar, but three-dimensional (cut off the top of the pyramid), this three-dimensional shape, we call the visual clipping body.

second, depending on the cutting body

Depending on the cutting body, the professional term is the cone of Sight (fusum), which consists of 6 sides, up and down, around, before and after, look at the introduction of the Holy Scriptures:

Http://www.ceeger.com/Manual/UnderstandingFrustum.html

The outer edges of the image is defined by the diverging lines this correspond to the corners of the image. If those lines were traced backwards towards the camera, they would all eventually converge at a single point. In Unity, this is located exactly at the camera's transform position and is known as the centre of perspective. The angle subtended by the lines converging from the top and bottom centres of the screens at the centre of perspective are Called the field of view (often abbreviated to FOV).

The edge of the image is called the deviation line at the corner of the image. If the lines depicted are to the rear of the camera, they will eventually converge at one point. In unity, this point happens to be located at the transformation location called the center of the view. In the view, the angle between the center of the top of the screen and the center of the bottom is called the field of view (usually abbreviated to FOV).

As stated above, anything that falls outside the diverging lines at the edges of the image is not being visible to the came RA, but there is also, and restrictions on what it would render. The near and far clipping planes is parallel to the camera's XY plane and each set at a certain distance along its centre Line. Anything closer to the camera than the near clipping plane and anything farther away than the far clipping plane would not Be rendered.

As mentioned above, anything that goes beyond the edge of the image is invisible. There are two other limitations to rendering. The near and far clipping faces are parallel to the XY plane of the camera, and each clipping surface has a certain distance from the centerline. Any object that is within the near clipping plane and beyond the far clipping plane will not be rendered.


We have learned on the top of the cutting surface, that is, before and after, then up and down about four faces and how to define it? The above introduction already involves, is the concept of the FOV.

Go back to unity, look at the specific effects of the FOV, modify the value of field of view, 30, the cone shrinks, the cube is no longer inside


The FOV is modified to 60, the apparent cone expands, and the preview can see the cube.


From the above, the value of the FOV seems to determine the upper and lower left and right four sides of the angle, and its size is expressed in degrees, here the 60 means 60 degrees

Well, now that all the parameters of a view cone are clear, the coordinates of the camera are known, as long as you know the value of the clipping surface near and far, the value of the FOV defines a unique cone of view.

Said for a long, how to use the cone? OK, then start the subject, projection transformation.

three, projection transformationThere are two types of projection transforms for camera in unity: Perspective projection and orthographic projection.

A brief description of the difference between the two, the orthogonal projection of the observer body is a box, is a rule, it uses a set of parallel projection to the projection plane of the three-dimensional objects, I believe that the unity of understanding of the students are aware of the orthogonal projection function, distance from the camera will not affect the scale of the object, For example, 10m and 1000m of the actual size of the same object, rendered in the screen size is the same, this is obviously not what we want, 3D game simulation is real life, and in real life, away from our distant objects, it seems of course relatively small, and even a mobile phone, placed in front of the eyes, It looks like it's going to gigantic. Therefore, the use of orthogonal projection in 3D games is very limited.


The next is the perspective projection, a technique widely used in 3D games that uses a set of radial projection lines produced by a projection center to project three-dimensional objects onto the projection plane. The observation body of the perspective projection is the view cone that has been said above. It has the ability to zoom in and out of objects, and now it needs to project the objects contained in the cones into a picture, the process that needs to be transformed, the projection transformation

So why change it?

The cones are not actually a rule body, which is very detrimental to our tailoring, from the 3D object to the picture process, which usually takes the following three steps:

1. Using the Perspective transformation matrix to transform vertices from the viewing cone to the Rule Observer body (CVV) of the clipping space
2. Cropping with CVV
3. Screen Map: Maps the coordinates obtained through the preceding procedure to the screen coordinate system.


This process can be represented by a graph (the image is excerpted from it)

From the View cone transformation to the cube process, the near clipping surface is enlarged into the front surface of the cube, the far cutting surface is reduced to the back surface of the cube, which explains why the perspective projection can be very intuitive to express the distance of the object is very simple, because it magnifies the near object, narrow the distant objects.


So how to do this transformation, we can understand that a certain point in the cone (x,y,z,1) and a matrix multiplied by the new Point (x1,y1,z1,1) is the corresponding point in the CVV, so that all the points in the view cone and the matrix is multiplied, the acquisition is a CVV. And this matrix is the perspective projection matrix.

Directly on the value of this matrix, want to see the detailed deduction of the classmate, give a link:

http://blog.csdn.net/popy007/article/details/1797121




There are a lot of parameters that are meant to represent

For a view cone, we take its section generally has the following two methods, but generally take the yz surface as the section to calculate the parameters, here we want to take fov,near,far,botton,top,right,left value, where botton,top,right, Left is the upper and lower boundary values of the projection plane, and the projection plane is the near clipping surface.



Iv. Modifying the projection matrix to establish a non-standard projectionAs we continue to return to unity, Unity's documentation of the camera projection matrix is fairly small, and the only available is the sporadic introduction to the Camera.projectionmatrix API, Links:

Http://www.ceeger.com/Script/Camera/Camera.projectionMatrix.html

But at least we can output the projection matrix and look at it.

Print (Camera.main.projectionMatrix); The projection matrix of the output main camera


See the value of the projection matrix, fov=60,near=0.3,far=1000 the case, the calculation, found that except the first value has a problem with the other is correct.

What is the first value of 1.08878?

After study, I found Unity has a feature, regardless of how to modify the scale of the window, the value of M "Max" is always constant, fixed to 1.73205 however, as long as the FOV is changed, it will change, so unity must be defined as the FOV of the projection plane between the upper edge and the lower edge of the angle, namely top= Near*tan (FOV/2), and right can not be calculated by Right=near*tan (FOV/2), but to use right=top*aspect this formula, when we adjust the screen size, actually changed the M "0,0" Results without changing the other values.


Let's write a script to change the value of the projection matrix to see the effect

Using Unityengine;
Using System.Collections;


public class Example:monobehaviour {
Public matrix4x4 originalprojection;
void Update () {
Change some values of the original matrix
matrix4x4 p = originalprojection;
Camera.main.projectionMatrix = p;
}
public void Awake () {
Originalprojection = Camera.main.projectionMatrix;
Print (Camera.main.projectionMatrix);
}
}

Here take the e01=0.5, found that far and near the cutting surface into a parallel four deformation, the corresponding picture is inclined



I'm not going to show you the rest, change the other values to get the effect.

The reason is simple, or to post the formula that was deduced before


The m01 of M-matrices we changed him from 0 to 0.5, which is the result of the x-coordinate transformation, the x-coordinate is not related to Y, and now as y increases, X also increases

For example, the equivalent of each pixel in the square is not related to Y, now each pixel in Y is not 0 will be shifted to the right 0.5y distance, so that, it makes it look like parallelogram


Others are not deduced, anyway, VR in the projection of the time will involve this piece, so when it comes to the projection matrix when everyone will not be so confused

Finally, a string of code, is found in the Holy Scriptures, the realization of the screen like a wave of water-like effects, but also by modifying the projection matrix of the way to achieve

After copying the paste, added to the main camera can be achieved, so powerful special effects actually a few lines of code can be done, it is incredible.

Using Unityengine;
Using System.Collections;


Let the camera shake in a popular way
public class Example:monobehaviour {
Public matrix4x4 originalprojection;
void Update () {
Change some values of the original matrix
matrix4x4 p = originalprojection;
P.M01 + = Mathf.sin (Time.time * 1.2F) * 0.1F;
P.M10 + = Mathf.sin (Time.time * 1.5F) * 0.1F;
Camera.main.projectionMatrix = p;
}
public void Awake () {
Originalprojection = Camera.main.projectionMatrix;
}
}

The following will continue to return to the theme of VR and continue to explain the contents of the script in detail.

For VR hand-tour development of Small white Tutorial: (iv) Supplemental, Detailed introduction of unity in the projection matrix camera

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.