0% found this document useful (0 votes)
20 views19 pages

Lecture Cameras

Uploaded by

Iqra Zafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views19 pages

Lecture Cameras

Uploaded by

Iqra Zafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

CS304: Game Programming

Lecture: Cameras
Cameras
Cameras are essentially the players view into the world you have created for
them. They provide (the players) with perspective and control how things appear
to them.
As you have already noticed, a Camera (name Main Camera) is created by
default every time you create a new scene. By customizing and manipulating
cameras, you can make the presentation of your game truly unique.
Cameras can be customized, scripted, or parented to achieve just about any kind
of effect imaginable.

> For a puzzle game, you might keep the Camera static for a full view of the
puzzle.
> For a first-person shooter, you would parent the Camera to the player
character, and place it at the character’s eye level.
> For a racing game, you’d probably have the Camera follow your player’s
vehicle
and so forth.
Cameras
You can create multiple cameras in the scene.
Every scene in Unity needs to have a camera, or nothing will be shown.

Screen Space is a 2D area with the


origin in the lower left corner, and
with a size equal to your resolution.

Viewport Space is a normalized area,


from (0, 0) to (1, 1).
Camera Projection
The Viewing Frustum is the shape of the region that can be seen and rendered
by a camera. The projection of the camera allows it to simulate perspective
in the rendered scene.

In a Perspective Projection, the


viewing frustum of the camera takes
the shape of a pyramid.

In an Orthographic Projection the


size of the near plane and the far
plane is the same (therefore, the
viewing frustum is shaped as a cube).
Thus all objects of equal size are
rendered the same size independent of
distance to the camera (i.e. the
perspective is lost).
Camera object – Components (Default)

Transform: transform component


of the camera.
Camera:
camera
component
(can be
attached
Flare Layer: to to enable/disable
any game
Lens Flares (lights refracting
object!).
inside
GUILayer:
camera lens) to in the image.
enable/disable
Audio Listener: calculates
ren- dering of
the out- put audio. There
2D GUIs.
can only be one audio
listener in the scene!
Camera - Properties
Settings of the Camera Component:
Clear Flags: This property determines what the camera displays in the areas where
there are no game objects. By default Skybox is shown. If there is no skybox available,
the camera will show a solid color.
Note: “Depth Only” selection should be used only when there are multiple cameras and “Don’t
Clear” causes streaking and should be used only if writing a custom shader.
You can set the clear flags setting to specify what to draw in these empty areas:
Skybox: Any empty portions of the screen will display the current Cam- eras
skybox (chosen in the Lighting window).
Solid Color: Any empty portions of the screen will display the current
Camera’s Background Color.
Depth Only: Nothing is drawn in the empty portions of the screen. This is
typically used with more than one layered cameras (bad setting for only one
camera).
Don’t Clear: Scene rendered in the previous screen does not get deleted.
Camera - Properties
Background Color: Sets the color displayed if there is no skybox present.
Culling Mask: Determines what layers are picked up by the camera. By
default, the camera sees everything, so if you uncheck certain layers they won’t
be visible to the camera.
Example : All user interface elements are set to a UI Layer, that is rendered by a
separate camera. A “scene” camera draws the rest of the scene. In order for the
UI to display on top of the “scene” Camera, you need to set the Clear Flags to
Depth only and make sure that the UI Cameras Depth is higher than the “scene”
Camera
Projection: Perspective or Orthographic. Orthographic projection lacks per-
spective, and it is mostly useful for making isometric or 2D games, and
they are great for making 3D user interfaces.
Field Of View: Width of the Cameras view angle, measured in degrees along
the local Y axis.
Clipping planes: Far and Near planes, specified in Unity units. Near is the
closest point relative to the camera that drawing will occur, while Far is the
furthest point relative to the camera that drawing will occur.
Camera - Properties
Viewport Rect: Establishes what part of the actual screen the camera is projected on,
and is short for View Port Rectangle. By default, the x and y are both set to 0, which
causes the camera to start in the lower left of the screen. The width and height are both
set to 1, which causes the camera to cover 100% of the screen vertically and horizontally.
Depth: : Sets the priority of the camera when multiple cameras exist. Lower numbers
come first, which means that higher numbers may be drawn on top and effectively hide
them, like a stack.
Rendering Path: The Rendering Path property determines how the camera renders. It
should be left as Use Player Settings For more details see:
http://docs.unity3d.com/Manual/ RenderingPaths.html
Target Texture: Allows us to specify a texture for the camera to draw to instead of the
screen.
Occlusion Culling: Disables rendering of objects when they are not currently seen
by the camera because they are obscured by other objects.
HDR: (Hyper-Dynamic Range) determines whether Unity’s internal light calculations
are limited to the basic color range. The property allows for advanced visual effects. You
don’t need to modify this property during your first steps.
http://docs.unity3d.com/Manual/ HDR.html
An Example of a Follow Camera
1 p u b l i cc l a s s C a m e r a F o l l o w : M o n o B e h a v i o u r
2 {
3 //Thep o s i t i o nt h a tthec a m e r aw i l lbef o l l o w i n g.
4 p u b l i cT r a n s f o r m t a r g e t ;
5 //Thes p e e dw i t hw h i c hthec a m e r aw i l lbef o l l o w i n g.
6 p u b l i cf l o a t s m o o t h i n g = 5 f ;
7 //Thei n i t i a lo f f s e tf r o mthet a r g e t.
8 Vector3 offset ;
9
10 voidStart ()
11 {
12 //C a l c u l a t ethei n i t i a lo f f s e t.
13 o f f s e t =new V e c t o r 3 (0 ,10 , -10 ) ;
14 }
15
16
17 voidLateUpdate ()
18 {
19 //C r e a t eap o s t i o nthec a m e r aisa i m i n gfor
20 //b a s e dontheo f f s e tf r o mthet a r g e t.
21 Vector3 targetCamPos = target . position + offset ;
22
23 //Smoothlyinterpolatebetweenthecamera’s
24 //c u r r e n tp o s i t i o nandit’st a r g e tp o s i t i o n.
25 transform . position = Vector3 . Lerp ( transform . position ,
targetCamPos , smoothing * Time . deltaTime );
26 }
27 }

Also: Consider using Transform.LookAt to set the camera’s transform forward


vector.
Using Multiple Cameras in a Scene

When having multiple cameras, the Depth parameter controls the order
in which the cameras draw the scene. If the values are different, the camera
with the lower Depth value will be drawn first. If the values are equal, the
first camera added to the scene will be drawn first.

Note that all cameras actually draw the scene, but the last one overrides
all the others if its Clear Flags is not set to Depth Only.
Using Multiple Cameras in a Scene

It is possible to avoid this overriding by setting the property Clear


Flags to Depth Only in one of the cameras.

Clear Flags = Clear Flags =


Skybox. Depth Only.

The camera with Clear Flags = Depth Only only draws those objects hit by
the camera. The rest is not drawn.
Using Multiple Cameras in a Scene
Split Screen: 2 cameras drawing in different parts of the viewport. They can
both be assigned to the same Depth, as they are not overriding each other. In
order to indicate which parts of the viewport each camera draws, modify the
Viewport Rect attribute of the cameras.

Culling Masks: By assigning objects


to layers, and setting the culling mask
property of the cameras, it is possible
to make cameras draw objects from
specific layers:
Exercise: Multiple Cameras in a Scene
Unity allows for the use of multiple cameras in the same scene. This way we have the
ability to to create a serious of beautiful effects for our games in order to provide the best
possible UX to the player. Note: All cameras come by-default with an Audio Listener
component. However only a single Audio Listener can exist in a scene. You access the
project-wide audio settings using the Audio Manager, found in the Edit > Project Settings
> Audio menu.
• Create a new project or scene and add two cubes. Place the cubes at (–2, 1,
–5) and (2, 1, –5).
• Move the Main Camera to (–3, 1, –8) and change its rotation to (0, 45, 0).
• Add a new camera to the scene (GameObject > Camera), position it at (3, 1,
–8).
• Change its rotation to (0, 315, 0).
• Always remember to remove the audio listener component from all of the
cameras except from one (in 99% of the cases this is the Main Camera). Run
the scene.
• Notice how the second camera is the only one displayed (fig.1).
• This is because the second camera has a higher depth than the Main
Camera.
• The Main Camera is drawn to the screen first, and then the second camera is
drawn overtop of it.
• Change the Main Camera Depth to 1 and then run the scene again. Notice
Exercise: Multiple Cameras in a Scene

fig.1 fig.2
Exercise: Multiple Cameras- Split Screen
In order to achieve a Split Screen effect we have to fiddle with the Normalized View Port
Rect property. The normalized view port basically treats the screen as a simple rectangle.
The lower-left corner of the rectangle is (0, 0) and the upper-right corner is (1, 1). Think
of the coordinates as percentages of the actual size.
So, a coordinate of 1 means 100%, and a coordinate of .5 means 50%.
By default, cameras project from (0, 0) with a width and height of 1 (or 100%), in sort,
they see the whole screen by-default.
• Continue working in the previous scene.
• Change the depth of the Main Camera back to –1.
• X and Y properties of the camera’s View Port Rect property should both be 0.
• Set the W to 1 and H to 0.5.
• The second camera must also have a depth of –1.
• Set the X and Y properties of the view port to (0, .5).
• This will cause the camera to begin drawing halfway down the screen.
• Set the W to 1 and H to 0.5.
• Run the scene and notice how both cameras are now projecting on the
screen at the same time. Using the same steps try splitting the screen one
more time (cut it in 3 sections).
Exercise: Multiple Cameras- Split Screen
Exercise: Camera – Picture-in-Picture

Picture in picture is the most common way of creating effects like


minimaps. With this effect, one camera (or multiple) is drawing
over another in a specific area.
Again we are modifying the View Port Rect property to achieve
the desired results.

• Continue working in the previous scene.


• Check that the Main Camera has a depth of –1.
• X and Y properties of the camera’s Normalized View Port Rect property
should both be 0.
• W and H properties should both be 1.
• The depth of the second camera must be 0.
• Set the X and Y property of the view port to (0.75, 0.75) and set both W and
H to 0.2.
• Run the scene.
• See how the second camera appears in the upper-right corner of the screen.
• Experiment with the different view port settings to get the camera to appear
in the different corners.
• By following the previous steps try adding another camera which will appear
in the top left corner of the screen along with the one we have already
Exercise: Camera – Picture-in-Picture
Reading List
• https://docs.unity3d.com/Manual/class-Camera.html
• Camera - Unity Tutorial: https://unity3d.com/learn/tutorials/topics/graphics/cameras

• Hour 6, Sams Teach Yourself Unity Game Development in 24 Hours by Ben Tristem
and Mike Geig

You might also like