First Steps in Unity

I’ve installed Unity (2017.1) for the first time… and want to get to grips with some of the basics, so I’m going to build a small scene with a few moving parts covering some of the things I expect to be important to me.

When you open Unity you start with a default setup that provides a few things out of the box. Namely a camera, directional light, and some default render settings. This is what you are presented with when you first open Unity.

Out of the box, what you don’t get, is access to a deferred rendering pipeline. I want one of these as I think deferred lighting, bloom, decent reflections, etc, are all essential components, so the first thing I need to do is install the Post Processing Stack, described here. Once you’ve got that installed you create a post processing profile, which describes the precise pipeline you want to use and add it to the camera via a Post Processing Behavior component.

Next, Unity adds a lot of ambient light and reflections by default. I don’t want any of these as I want tight control over the lighting. Basically, if I don’t add any lights, that means I want everything to be black, not some default ambient color that’s it’s chosen to use for me. I understand why they do this… because an out of the box experience where everything is black until you add some lights might be confusing for some people… but I want to get rid of this light source. To do that you need to go to the lighting settings and set the Environment Lighting and Environment Reflections to both be sourced from a color… and then make that color black.

Having done all that we still need to activate the deferred pipeline. To do that you go to the project graphics settings, under Edit -> Project Settings -> Graphics and switch all tiers to deferred.

Finally, I’ve turned off the sky too, which you can do by setting the clear color of the camera to black. I’ll add a sky again as and when I think I need one. For now it looks odd that I have so little light in the scene and then this bright sky sitting on top of everything, so I’m better off without it.

Unless I’ve missed a step there, you should now be able to drop in a shadow casting point light and a few cubes and get something similar to this…

Next up, I want to add a few textures. I have some normal and albedo maps lying around from previous experiments so I grab a few of those and drop them in as assets. For the normal map you need to open it up and set it’s type to Normal Map. Then you can create a material and connect them as Albedo and Normal Map under Main Maps. Finally select the floor and map it’s material to the new material that we just created. In my case I’ve also had to set the tiling on the material to 15 to make the maps fit the large floor I’m using.

Now I want to animate a few things, which seems like an excuse to try adding a C# script to my scene. I start by making a new script in the asset library, then I open it, which should open VS2017 Community Edition. From there you are presented with methods called Start and Update to which you can add code. I type this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    public Vector3 initialPosition;
    public float time;

    void Start () {
        initialPosition = transform.position;
        time = 0.0f;
    }
	
	void Update () {
        time += Time.deltaTime;
        transform.position = initialPosition;
        transform.Translate(Vector3.up * Mathf.Cos(time * 5.0f));
        transform.Translate(Vector3.left * Mathf.Sin(time) * 2.0f);
    }

}

Once saved I drag the script into the space below the existing components on each light which automatically creates a new script component for each, and when you click play now the lights slide back and forth in the X axis and bob up and down in Y.

There are a few things here I’m not happy with still. The first is that the shadows cast from the column exhibit whats known as Peter-Panning, where there is a disconnect between the caster and the shadow itself making it look as though the object is not quite sat on the floor. I’ve checked and the object actually intersects the floor so there is no gap at all. Unity exposes a per light shadow bias setting that seems to control this, but it appears that a bias of 0 doesn’t really mean 0 and so we still end up with a gap. I believe we should be able to get away with a much smaller bias and shouldn’t need to put up with this artifact, but I can’t see an obvious way to achieve that…

Other things I’ve struggled with a little are the points lights, where I seem to have to make them very intense to get enough light in the scene, but doing so seems to create very intense lighting some distance from the object still, which doesn’t feel realistic. I need to do some experiments as I’m not sure if this is down to the bloom settings, the tone-mapper or eye adaption, or more likely the light falloff, or some other thing… but when I’ve previously coded everything myself it seemed much easier to balance the lighting and get a more natural look to things.

I’ve a feeling that my next set of experiments might revolve around writing custom shaders for Unity.

Leave a Reply

Your email address will not be published. Required fields are marked *