Week 6: Learning about 3D Viewing and Camera Manipulation.
· Please download and unzip the Week6.zip file in the Files/WeeklyExamples folder.
== End of matrix mumble jumble … start with 3D scene and camera
1. Simple 3D Scene:
a. Small View Camera and its viewport
i. Viewport: 50% in width and height, with proper offset
ii. Remember to remove one of the Audio Listeners (either from Main Camera) or from Small View
iii. Camera position (0, 10, -25)
iv. Look at origin: è Rotation (21.8, 0, 0)
1. Rotation in X: inv-tan(10/25)
b. Planes:
i. WorldPlane: for reference and sanity check (at y=-0.1f)
ii. LargeSquare: T(0, 0, 0)R(90, 0, 0) S(8, 8, 8)
iii. SmallSquare: T(-6.5, 0, 6.5) R(90, 0, 0) S(4, 4, 4)
c. On Small view cam
i. Try Orthographic Projection
ii. What are the differences between Orthographic and Perspective?
iii. How to “pretend” working in 2D space?
d. We learn by observing Small view cam:
i. Left handed coordinate system (!) L
2. Near and Far Plane distant, and underlying math accuracy …
a. Almost the same scene as above, only difference is SCALE, must MUCH larger
b. Select Small View Camera:
i. Near/Far: 0.01 and 999999 (Very far apart)
c. Now, select WorldPlane and change the y-value to -0.00001
i. Notice error!!
d. We learn,
i. 32-bit (or may be 20 bits) to resolve the distance between Near and Far
ii. Good practice to set N/F as close as possible.
ð Recall: GPU programming model: Vertex and Fragment processors
o Vertex Professor output: in NDC
§ X/Y maps to screen X/Y
§ Z maps to depth (distant from the eye)
§ this is the reason for the View Space:
· eye at the origin
· viewing direction is the z-axis
o Scan conversion occurs between Vertex and Fragment processors
§ Slope of edges means constant changes (by slope of the edge) in between pixels
§ Maps well to linear interpolation
o Fragment processors offer excellent support for linear interpolation
3. Our own View Matrix:
a. The Scene: same as Simple3D except
i. Large/Small squares: 451Material + XformLoader.cs
ii. XformLoader.cs: We know this, computes Object To World transform and loads to 451Shader, also sets MyColor
b. 451Material è 451ShaderWithTexture
i. Seen this shader before nothing new, except
ii.
MyVert:
1.
Work with CameraViewMatrix!
2.
UNITY_MATRIX_P (and not UNITY_MATRIX_VP)
iii. MyFrag: notice how we blend MyColor with the texture
1. This is referred to texture tinting
c. CameraMatrices.cs: Computes the camera view matrix!
i. Remember, Camera/View/Eye space
1. Camera position goes to the origin
2. Transform.right è X-axis
3. Transform.up è Y-axis
4. Transform.forward è -ve Z-axis
ii. Camera GameObject’s Transform.localRotation, rotates X/Y/Z to transform right/up/forward!
iii. Needs operator: R * T
1. T: Translation from camera position to the origin
2. R: inverse of the transform.localRotation with z-axis flipped
a. This is the setColumn + reverse OR
b. This can be done by setting the matrix Rows!
iv.
OnPreRender()
Called before a camera is used to render
d.
Note:
i.
MainCamera cannot render Large/Small Square
1.
Unless, we put the CameraMatrices script on
it!
ii.
The Editor’s view is what is seen from the
Small View camera J
e.
We learned:
i.
Camera view is defined entirely in the
transform [the 10 float numbers]
ii.
It is a translation followed by a rotation
4. Our own camera projection matrix
a. Exact same code as above, only difference …
b. 451Shader: now has a CameraProjMatrix
i. Notice, all MyVert() transformation operators are controlled by us!
ii. MyXformMat: is the Object to World transform
iii. CameraViewMatrix: is the World to Eye/View/Camera space
iv. CameraProjMatrix: takes view into NDC space
c. CameraMatrices: at the end of the OnPreRender() function
i. ProjectionMode, compute either Perspective or Ortho
ii. Loads to CameraProjMatrix
d. For larger/small square, since not respecting UNITY_MATRIX_VP, all cameras see exactly what small view camera sees
e. We learned:
i. Projection matrix is responsible to squeeze the view volume into a Normalized Cube
ii. Scan conversion can easily follow (to scale X/Y into device size)
iii. A Computer graphics camera: defined entirely by the View and the Projection matrix
5. SetLookAt: make small view camera follow a look at position
a. Scene: SmallViewCamera, LookAtPosition, LineOfSight, Large/SmallSquare
i. LingOfSight: is the viewing ray of the small view camera
ii. LookAtPosition: small view camera always look at this point
b. Run Game:
i. Move LookAtPosition aroud to see SmallViewCamera follows it
ii. Main Camera: can see the line of sight
c. CameraManipulation.cs:
i. Has LookAtPosition and LineOfSight (LineSegment from MP3)
ii. Update(): computes camera view in two ways
1. First: Computes a rotation using Quaternion
2. Second: Transform.LookAt function call
6. Tumble:
a. CameraManipulation.cs ComputeHorizontalOrbit(), important!!
i. Rotate with respect to the transform.right axis
ii. Rotation pivoted at LookAtPosition!
b. Other Manipulations: (MP4) … let’s derive the math on how to do all of these!
i. Pan: swivel/rotate around a fix position (what Unity Editor let us do)
ii. Tumble: the rotation to examine an object
iii. Track: movements along vertical and right axis
iv. Dolly: zoom