Week 9: Mouse click position and Shaders: Please download and unzip the Week9.zip file in the Files/WeeklyExamples folder.

1.       Drawing the camera frustum: in order to see the mouse click position well, we need to see the frustum of the camera!

a.       Scene: View + World

                                                               i.      View: two cameras, each with manipulation

b.      Rectangle class

                                                               i.      Simple (?) four sides of a rectangle in 3D space

c.       CameraManipulation::MouseInCameraViewport()

                                                               i.      make sure mouse is in viewport before allow manipulation of camera

                                                             ii.      Here, we use ScreenToViewportPoint()

d.      CameraManipulation_DrawFrustum::UpdateFrustumPosition()

                                                               i.      What are we computing here?

e.      Run the project and adjust

                                                               i.      FOV or NearPlane to see that drawn frustum can potentially block the small view camera

                                                             ii.      Oh yes, we can switch on the Main Camera draw frustum as well, cool?

 

2.       Showing the mouse click positions …

a.       NOTE: on the Camera, there are many functions for points in different coordinate system!

                                                               i.      forward transform: from world to screen

1.       WorldToViewportPoint (viewport is between 0 to 1 for each camera)

2.       WorldToScreenPoint()

3.       ViewportToScreenPoint()

                                                             ii.      Inversed transform: from screen back to world

1.       ScreenToViewportPoint()

2.       ScreenToWorldPoint()

3.       ViewportToWorldPoint()

b.      CameraManipulation_Xform::MyScreenToViewport()

                                                               i.      This is a simple percentage computation!

                                                             ii.      Notice we call our own function in MouseInCameraViewport() and print out the computed values, we are right on!

c.       CameraManipulation_Xform::MyScreenToWorldPoints()

                                                               i.      Note, we use the nearPlane’s corner and size to compute the click position on the nearPlane

d.      To run

                                                               i.      Left-Control click, notice, we click on a position, but in 3D, this is all positions along the “view ray”

                                                             ii.      Note, since by default MainCamera’s Frustum is not computed, Left-Control click on the large view does not do anything!!

 

·         èHow to ray cast to select a scene node primitive? … Answer: perform all operations in World Coordinate (WC).

o   èWe know how to transform SceneNode primitive position into WC (MP4, small view camera position!)

§  è Compute PrimitivePosWC (scene node primitive position in WC)

o   èAbove says, we know how to cast rays and intersect in WC!

§  Cast rays and intersect with a sphere around PrimitivePosWC

               

 

è Back to continuation from last week’s example 6 … let’s try out transparency …

3.       Alpha, Mask, and Transparency

a.       MyMesh::Start()

                                                               i.      Must cache Transform setting and set Transform to identity before creating the vertices

                                                             ii.      Otherwise, Unity will apply the transforms on vertices! L

b.      CSS451NoCullShader

                                                               i.      Blend SrcAlpha OneMinusSrcAlpha ß switch on blending

1.       Ref: https://docs.unity3d.com/Manual/SL-Blend.html

                                                             ii.      AlphaToMask On ß determines if a pixel is completely covered based on alpha value

                                                            iii.      Comment out “AlphaToMask On”

1.       Put camera behind Right, look at Left,

a.       Through transparent parts: cannot see part of Left!!

2.       Right is drawn first, values in the Z-buffer, Left cannot be drawn

                                                           iv.      Uncomment out this line

1.       Put camera behind Right, look at Left, sees everything

2.       Right is drawn first, the alpha value is converted into a Mask

3.       When Left is drawn, the Mask is consulted, and scaled accordingly

 

4.       Diffuse illumination:

a.       Shader references: https://docs.unity3d.com/Manual/SL-Reference.html

                                                               i.      Semantics: the SV_POSITION, expressing “intent” this is a rather limited set

1.       https://docs.unity3d.com/Manual/SL-ShaderSemantics.html

                                                             ii.      Build-in shader variables: https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

                                                            iii.      Build-in shader helper functions: https://docs.unity3d.com/Manual/SL-BuiltinFunctions.html  

                                                           iv.      Other build-in functions: not clear to me, but I think Unity support many of the GLSL functions: https://www.khronos.org/registry/OpenGL-Refpages/gl4/index.php

1.       Use accordion-style index and look under GLSL

b.      The scene:

                                                               i.      A Simple mesh + a sphere (named: ALightPosition)

                                                             ii.      LoadLight.cs: script on the Camera

1.       Pre-Render: loads the position of the ALightPosition to the shader’s LightPosition

                                                            iii.      Shader: defines a LightPosition

1.       Vert: transforms everything

a.       Vertex position

b.      Notice the: vertexWC  ß vertex in WC space!!

c.       Notice the: Normal

                                                                                                                                       i.      we don’t have inversed-transposed Model transform

                                                                                                                                     ii.      So, we transform two points and compute the difference

2.       Frag:

a.       ComputeDiffuse() function!

c.       Cool? What else can we do?

5.       PointLight: modeling a light bulb …

a.       Run, and look at the Scene (NOT game) window:

                                                               i.       Select/Move the APointLight to see you can manipulate the light

                                                             ii.      With APointLight selected, look at Inspector, under “Point Light (Script)”: you can manipulate

1.       Near/Far: the near/far distant to see the point light effect

2.       Color: change the light color

3.       Toggle the “Show Light Ranges” check box to see the near/far light radii

b.      Analyze the Scene: PointLight empty GameObject that contatins:

                                                               i.      Near/Far Sphere: represent the volume where the point light can illuminate

1.       Near: anything inside NearSphere illuminate to the max

2.       Far: anything outside of FarSphere will not be illuminated

3.       In between: a nice/smooth transition between max and zero

                                                             ii.      APointLight: the transform.localPosition is used to present the point light position

c.       Analyze the Scripts:

                                                               i.      PointLight: knows how to load appropriate information into the shader (451NoCullShader in this case).

1.       This is hung on APointLight GameObject.

                                                             ii.      LoadLight: defines OnPreRender() that calls the PointLight’s loadlight,

1.       this is hung off the MainCamera.

d.      Look at 451NoCullShader:

                                                               i.      Note the vertexWC in v2f

                                                             ii.      Note: v2f(): lines 51 to 56: on how we compute

1.       o.vertexWC and

2.       o.normal

                                                            iii.      Note: ComputeDiffuse() function

1.       Line 61: how we compute the l-vector (the light vector) using i.vertexWC and LightPosition

2.       Line 66: how we compute ndotl

3.       Lines 67-76: how we use the LightFar and LightNear to compute a “soft transition region” between the near/far spheres

e.      Do we understand how everything works now?