Reader small image

You're reading from  Unity Cookbook - Fifth Edition

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805123026
Edition5th Edition
Languages
Tools
Right arrow
Authors (3):
Shaun Ferns
Shaun Ferns
author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

Sinéad Murphy
Sinéad Murphy
author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy

View More author details
Right arrow

Cameras, Lighting, and Visual Effects

Whether you’re trying to make a better-looking game or you want to add interesting features, visual effects can add enjoyment and a more professional look and feel to many games:

Figure 13.1: Multiple light sources in Coco VR by Pixar and Magnopus (featured on the Unity website Made with Unity)

At the end of the day, what we can see is the color properties of the rectangular array of pixels (picture elements) that make up the screen (or screens, for VR) that we are looking at. The Unity game engine must use the GameObjects in a scene and their properties to decide what to render (draw) in the Game window. Some GameObjects model 3D objects, with properties relating to physical materials, and renderer components for configuring how they should be displayed. Other GameObjects simulate light sources.

Modern computers with powerful CPUs and GPUs are beginning to support real-time raytracing. Raytracing involves calculating...

Creating the basic scene for this chapter

All of the recipes in this chapter start off with the same basic scene, featuring a modern house layout (made with ProBuilder meshes), some objects, and a keyboard-controllable 3D character. In this recipe, you’ll create a project with this scene that can be duplicated and adapted for each recipe that follows.

Note: For the other recipes in this chapter we provide a completed version of this recipe named BasicScene_completed.unitypackage, which can be imported into a new project to save working through this recipe each time.

Figure 13.7: An example of the basic scene for this chapter

Getting ready

We have prepared a Unity package named BasicSceneSetup.unitypackage that contains all the resources for this recipe. The package can be found in the 13_01 folder.

How to do it…

To create the basic scene for this chapter, perform the following steps:

  1. Create a new 3D (Core) project.
  2. ...

Working with a fixed Main Camera

A quick way to begin creating a scene where the player controls a third-person character is to use a fixed Main Camera that faces the area the character will start moving around in.

We can position and orient the Camera to view most of the area that the player’s character will be moving around in, and we can change the amount of what is in front of a camera, which the camera “sees,” by changing its Field of View. In this recipe, we’ll use the basic 3D scene from the previous recipe and work with the Main Camera so that we can see our character moving around. We’ll delete the Main Camera from the existing scene and learn how to create a new Camera, which we will then turn into the Main Camera for a scene. Then, we’ll work with the Position, Rotation, and Field of View properties so that the player can see much of the house scene from a single, fixed perspective.

In later recipes, we’ll learn...

Changing how much of the screen a Camera renders

Often, we want different parts of the screen to display different things to the player. For example, the top of the screen might be game statistics, such as scores, lives, or pickups. The main area of the screen might show a first- or third-person view of the gameplay, and we might also have a part of the screen showing additional information, such as radars and minimaps.

In this recipe, we’ll create two cameras to add to our player’s view:

  • The Main Camera: This is made a child of the third-person-controller character. This shows the main gameplay to the player.
  • Camera 2: This is the elapsed time display that covers the top 15% of the screen. Note that there is no gameplay going on behind the text.
  • Camera 3: This is a simple minimap located at the bottom left of the screen, created from a top-down orthographic (non-perspective) Camera:

Figure 13.11: Cameras rendering to different...

Adding a top-down orthographic minimap Camera

We saw how we could improve the information shown to the player by using two Cameras displaying the screen. In this recipe, we’ll create a third Camera to add to our player’s view:

  • Camera 3: A simple minimap located at the bottom left of the screen, created from a top-down orthographic (non-perspective) Camera

Figure 13.16: The game running showing the added minimap Camera in the bottom-left of the screen

Getting ready

This project follows on from the previous one, so make a copy of that and work on that copy.

How to do it...

To add a top-down orthographic minimap Camera, perform the following steps:

  1. In the Hierarchy panel, create a third Camera named Camera–3 - minimap. Delete its AudioListener component (since we should only have one in a scene, and there is already one in the Main Camera).
  2. Make Camera–3 - minimap render to the bottom-left corner of...

Creating an in-game mirror using a RenderTexture to send Camera output to a Plane

Cameras do not have to output directly to the screen all the time. Different effects can be achieved by having the cameras send their output to a RenderTexture asset file. In the scene, 3D objects can be linked to a RenderTexture asset file, and so the output of a Camera can be directed to 3D objects such as Planes and Cubes.

In this recipe, first, we’ll duplicate the over-the-shoulder Main Camera child of the PlayerArmature, and send the output of this duplicate Camera (via a RenderTexture asset file) to a Plane on one of the house walls. Then, we’ll add a different Camera facing out from the wall. This is so that our Plane will act just like a mirror, rather than duplicating the over-the-shoulder Main Camera:

Figure 13.20: A copy of Main Camera rendering to RenderTexture, which is being displayed on a Plane in the scene

Getting ready

This recipe builds on the basic...

Saving screenshots and RenderTextures to files

At each point in time, a RenderTexture contains an image – a screenshot from the point of view of the Camera rendering to that RenderTexture. Games can have features such as snapshots at exciting points in time, such as crossing a finish line, landing a difficult jump, or taking the lead in a race. Through scripting we can take the picture data from the RenderTexture and save it as an image file.

Also, if you simply want to be able to take a snapshot of the Game screen (Main Camera view) at any point during the game as a PNG file, this is something Unity has made really easy too. We’ll explore both these features in this recipe.

Figure 13.25: PNG screenshot image of Main Camera view taken while playing the game

Getting ready

This project follows on from the previous one, so make a copy of that one to work on in this recipe. Note – many thanks to krzys-h for posting the RenderTexture-to-image file...

Using Cinemachine ClearShot to switch cameras to keep the player in shot

Cinemachine is a suite of tools that sets up a suite of cameras that improves player capture for dynamic view or improved cutscenes. This recipe implements Cinemachine ClearShot to switch between different virtual cameras to keep the player within shot.

We’ll do this by specifying that the center of the human character’s chest should be kept in the center of the shot as much as possible, and the AI system will decide which Camera to select to best meet these criteria:

Figure 13.29: Two virtual cameras the Cinemachine Brain can switch between for the best shot of the character

Getting ready

This recipe builds on the basic scene created in the first recipe of this chapter, so make a copy of it to work on for this recipe.

How to do it...

To use Cinemachine ClearShot to switch cameras to keep the player in the shot, simply perform the following steps:

  1. Open the...

A Camera to always look at and follow the Third Person Controller

Our project uses the Third Person Character Controller Starter Assets, and this package includes mouse-control functionality for the character’s direction, so in this recipe, we’ll create a Camera to move with and look at the third-person character by creating a Cinemachine Virtual Camera that always follows our robot’s PlayerCameraRoot GameObject.

It’s always good to give players choices and control over their gaming experience. In this recipe, we’ll set up a mouse-controllable Cinemachine FreeLook Camera and let the player switch to it.

Getting ready

This recipe builds on the basic scene created in the first recipe of this chapter. So, make a copy of that one to work on in this recipe.

How to do it...

To create a Camera that will always look at and follow the Third Person Controller, perform the following steps:

  1. Open the BasicScene scene.
  2. This...

Adding film grain and vignette effects with URP postprocessing

The Universal Render Pipeline (URP) is one of the scriptable Render Pipelines that Unity supports. While the Built-In Render Pipeline (BIRP) is fine for most projects, once you start to need very high-quality graphics or special visual effects, then you’ll need to start using one of the Scriptable Render Pipelines.

Note: Our project is already using the URP since it was added as a requirement of the Starter Assets Third Person Controller package. If you are working on a different project, you can choose a URP or HDPR project template rather than the default BIRP when creating a new Unity project.

URP provides workflows that let you quickly and easily create optimized graphics that improve the visual effect of your project. In this recipe, we’ll use the basic scene created in the first recipe to add Film Grain as a postprocessing effect to our scene. The Film Grain effect simulates the...

Creating an HDRP project with an HDRI skybox

HDRI is a method of representing images with a high range of luminosity (human-perceived color brightness). In this recipe, we’ll use a Creative Commons-licensed HDRI image to create a high-quality skybox in an HDRP project.

Figure 13.40: The detailed sun, clouds, and sky scene we see from the Koppenheim HRDI skybox

We’d like to thank Geg Zaal (http://gregzaal.com/) for publishing the Kloppenheim 06 HDRI assets under the CC0 license at HDRI Haven.

Getting ready

For this recipe, we have prepared the image that you need in a folder named HDRI_ Kloppenheim06, inside the 13_10 folder.

How to do it…

To create an HDRP project with an HDRI skybox, perform the following steps:

  1. Create a new 3D (HDRP) project.
  2. Create a new Basic Outdoors (HDRP) scene named HDRI sky scene.
  3. Import the provided HDRI_ Kloppenheim06 folder.
  4. In the Project panel, select the kloppenheim_06_4k...

Baking light from an emissive material onto other scene GameObjects

As well as Lights, other objects can also emit light if their Materials have Emission properties (such as a Texture, and/or tint Color). In this recipe we’ll create a lamp that glows green via its Emission Texture. The lamp and other 3D objects in the scene will be baked to create a pre-computed Lightmap for the scene.

Figure 13.52: Cube with green emissive Material

Getting ready

This recipe follows on from the previous one, so make a copy of that project to work on for this recipe.

How to do it...

To bake light from an emissive Material onto other scene GameObjects, follow these steps:

  1. If there is a Spot Light in the scene, delete it.
  2. Ensure your scene has a Directional Light, and that it has no Cookie Texture. Reduce its Emission Intensity value to 0.5 (so we can better see the glowing green Cube we are about to create!). Also in its General settings, set this light...

Further reading

Projects made using the URP are not compatible with the HDRP or the BIRP. Before you start development, you must decide which Render Pipeline you want to use in your project. Some good sources about the different Render Pipelines can be found as follows:

A complete list of postprocessing volume effects can be found...

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/unitydev

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Cookbook - Fifth Edition
Published in: Nov 2023Publisher: PacktISBN-13: 9781805123026
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Shaun Ferns

Shaun is a lecturer at Technological University Dublin. He is currently teaching on the BA (Hons) in Creative Digital Media where he is lead in the delivery of the Multimedia Stream. He is currently exploring serious games for construction-related training as well as the opportunities transmedia provides in improving user experience and engagement in cultural archive artifacts. His educational research is currently driven by his interest in self-determined learning (heutagogy), rhizomatic learning theory, micro-credentialing /digital badging, and curriculum development.
Read more about Shaun Ferns

author image
Sinéad Murphy

Sinead Murphy is currently Data Analytics Manager for the Irish NGO Trocaire. She has over 25 years of computing experience, including freelance IT training and database consulting, university lecturing in mathematics, IT skills and programming at TU Dublin (Ireland) and Middlesex University (London). She is a published academic, with undergraduate and postgraduate degrees in mathematics, computing and data science. She is passionate about the use of IT for understanding and visualising data, and using that understanding to make meaningful differences in the world. She is currently exploring the use of Python and Unity for data analytics and interactive visualisations.
Read more about Sinéad Murphy