BLOG

Notes from the KAYAC hyper-casual team

"For Now" Rendering Settings for 3D Smartphone Games

Takashi Hirayama

The sample scene

Click the image to go to the WebGL build.

Hello. I'm Hirayama from the Technology Department.

This article records what I thought about and tried while working out "for now" rendering settings for a 3D smartphone game. The settings mainly concern:

  • Lightmaps
  • Shadows
  • LightProbes

And there's no way to think about these settings without some knowledge of lighting calculation, so I'll touch on that too.

Motivation

Lately I've had chances to prototype 3D games, and even with just a few cubes and spheres placed haphazardly, my phone (Kyocera S2) can't manage a satisfactory frame rate.

There are few draw calls and few material settings, and I don't think I wrote any particularly heavy code, so the CPU can't be what's slow. In fact, when I hook up the profiler, a frame finishes in something like 4 ms.

So the culprit is the GPU. GPU work means vertex processing and pixel processing, and there can't be that many vertices, so it's entirely the pixels. The S2's screen resolution is as high as 1280x720, and given its GPU performance, doing 3D lighting calculation for every one of those pixels must be pretty tough. Granted, it was over 10 years ago, but even home consoles like the PLAYSTATION 3 struggled with that resolution. Back then it was common practice to draw only the 2D UI at 1280x720, draw the 3D part at a lower resolution and composite them, a technique that some called "resolution fraud".

Unity does all sorts of things for you, but it doesn't really give much thought to phones at the bottom end of performance. Build things the ordinary way, and fairly lavish shaders end up running all over the place. I realized I'd better think about this properly.

An outline of lighting calculation

Let me give an extremely rough explanation of lighting calculation in 3DCG.

When it has to run in real time, as in games, lighting calculation roughly works by adding up the results of the following 6 components:

  • Diffuse reflection of direct light
  • Specular reflection of direct light
  • Diffuse reflection of indirect light
  • Specular reflection of indirect light
  • Diffuse reflection of environmental light
  • Specular reflection of environmental light

That's 6 combinations of 3 types, "direct", "indirect" and "environmental", and 2 types, "diffuse" and "specular". Light is light whether direct or indirect, and surfaces vary endlessly, so splitting reflection into two kinds is crude. But think of this classification as what game development, until fairly recently, arrived at in "trying to get a decent picture for a bearable amount of computation".

Classification by light path

"Direct" is when light comes straight from a light source. If there's a sun and its light hits the ground without being blocked by anything, that's "direct".

"Indirect", on the other hand, is when something is lit by light that has been reflected or refracted somewhere once. Sunlight hits a red wall, reflects, then hits the ground and tints it red; that kind of thing.

And "environmental" is light that comes from "far away" in "all directions", like the sky and scenery. It's "far enough" that it can be regarded as not changing as you move around within the scene.

Classification of reflection

Reflection comes as "diffuse" and "specular".

Diffuse reflection

"Diffuse" is the calculation for when something isn't shiny, like clay. The surface is rough, so the light that hits it reflects in all directions, and we treat it as looking the same from anywhere.

Note that when doing it properly, this is calculated only for non-metals. Metals have no diffuse reflection.

Specular reflection

"Specular", on the other hand, is the shiny, glinting kind. Think of the evening sun hitting a manhole cover so that it glares like crazy, yet move your head a little and it's no longer dazzling. How it looks changes a lot depending on where you're looking from.

And it reflects the color of the light regardless of the object's color. Even a blackish manhole cover glows yellow when it reflects the sun. When doing it properly, metals have only this specular reflection.

To repeat, this division is for convenience, and it can come out completely differently depending on implementation constraints, machine performance, the look you want, and so on. Still, it's a reasonably useful classification for understanding Unity's Standard Shader.

Characteristics of the 6 types

Now let's look at the characteristics of each of the 6 types.

1. Diffuse reflection of direct light

Diffuse reflection of direct light

The parts facing the sun, diagonally above, are bright, and the opposite side is dark.

It's direct light, so there's only one path; we don't consider it reflecting or bending anywhere. The processing load is proportional to the number of light sources.

Also, since the color doesn't change depending on "where you look from", if the light sources don't move and the objects don't move either, you can optimize by "saving it to a texture in advance (called 'baking') and just using that at runtime". That's a lightmap.

If the path from the light source is blocked by something, that spot is in shadow. Calculating this is quite heavy. It needs a process called shadow mapping, which means an extra draw call for every single object.

But this too can be baked into the lightmap, shadows and all, if "the light sources don't move" and "the objects don't move". In that case the runtime load is the same whether there are shadows or not.

2. Specular reflection of direct light

Specular reflection of direct light

You can see the sunlight glinting off the surfaces.

It's direct light, so there's only one path, but unlike diffuse reflection it has the property of "changing a lot depending on where you look from". In 3D games the viewpoint is rarely fixed, so basically you can consider it impossible to bake into a lightmap. That said, you could choose to bake it anyway, accepting that "I don't mind if it shifts a little".

Also, the formula for making it glint nicely is fairly complex, so it's fairly heavy. With the Standard Shader, the material has a flag for whether to enable it: "Specular Highlights". For materials that aren't very shiny and take up a large area on screen, it might be fine to turn it off.

And the color from specular reflection has the property of being "unrelated to the object's color". Red object or black object, if the light is white, it's white.

As for shadows, it's the same as diffuse reflection. Usually both are done together in the same shader.

3. Diffuse reflection of indirect light

Diffuse reflection of indirect light

There's a green wall on the left, so the faces facing left are tinted green. Conversely, there's a red wall on the right, so the faces facing right are tinted red.

There are infinitely many paths by which light can arrive from light sources such as the red wall or the green wall. To begin with, a wall has area, so just the question of "which part of the wall did the light come from?" already has infinitely many answers. Even if, in practice, you speed it up with all sorts of approximations, doing this calculation every frame at runtime on a phone is out of the question.

However, it's diffuse reflection, so the color doesn't change with where you look from. That means if "the light doesn't move" and "the objects don't move", it can be baked into a lightmap.

Now, this also has shadows. Strictly speaking, "shading". Since various light comes from all directions rather than from a distinct light source like the sun or a fluorescent lamp, it's not a sharp shadow but more like shading. For example, recessed places are dark, right? The inside of your nostrils looks black. Things like this can be baked into a lightmap too.

And if you want this calculation for moving things too, you can substitute for it by computing and storing lots of "when you're around here, the light coming from the surroundings looks like this" data. Unity calls this a LightProbe.

The LightProbe is what does the indirect diffuse reflection calculation for moving things. If the LightProbe data is brighter or darker depending on the place, a person looks darker when they step into a recess and brighter when they come out.

4. Specular reflection of indirect light

Specular reflection of indirect light

Here and there are red parts, green parts and whitish parts. The red parts are glints reflected from the red wall on the left, and the green ones come from the wall on the right. The white is probably the sky. The surroundings show up as glints like this.

Here too there are countless light sources and countless paths, and on top of that the color changes drastically depending on where you look from, so it can't be baked into a lightmap.

However, as with indirect diffuse reflection, if you calculate data on "when you're around here, the light coming from the surroundings is like this" in advance, you can get something that looks reasonably convincing. Unity calls this a ReflectionProbe.

But each one holds far more data than a LightProbe, so you probably can't place very many. You use it when you want to do something like reflecting the surrounding scenery in a super-shiny metal object.

5. Diffuse reflection of environmental light

Diffuse reflection of environmental light

The whole thing is bluish. It's almost the color of the sky.

This is diffuse reflection from light coming from "far enough away" in "all directions". The light sources are the sky and the ground. The ground is reflecting the sky's light, so it isn't really glowing, but it's easier to pretend the ground is glowing.

For example, blue light comes from the sky, bright yellow from around where the sun is, and dark green from the ground since it's grass. It doesn't depend on where you look from, so "if the objects don't move", this too can be baked into a lightmap.

One drawback is that it can't produce shadows. If you don't do anything about it, you get things like "being under a roof but still receiving light from the sky", but in such cases you can place LightProbes and use them instead.

I suspect environmental diffuse reflection is implemented as "a single LightProbe that's placed in the world by default and can't be turned off", and that this data isn't used when there are LightProbes (I haven't checked rigorously). Conversely, if the whole stage is open and there are no colored glowing objects placed around, I think you can do without LightProbes (sports stadiums are a good example).

Also, any "light source that can be regarded as far enough away" can be stuffed in here, so if you throw in the sun, indoor fluorescent lights, street lamps and so on, you can even fold the direct light calculation into the environmental light calculation. But it can't produce shadows, so I don't recommend including obviously bright light sources like the sun. It's good for complex scenes with many light sources (a city at night, say) where you won't mind the lack of sharp shadows.

In the Environment section of Lighting Settings there's an item called Environment Lighting, and this is where you set it.

6. Specular reflection of environmental light

Specular reflection of environmental light

There are faintly bluish-white spots. These are reflections of the sky. If you prepared a Skybox material that returns something like a panoramic photo as the sky, you'd see the surroundings reflected with a mirror-like shine.

This is specular reflection from light coming from "far enough away" in "all directions", and as with environmental diffuse reflection, the light sources are the sky and the ground. Unlike diffuse reflection, though, the light's color comes out regardless of the object's color, and what gets reflected where changes with the line of sight and position. It can't be baked into a lightmap.

This is a guess, but I think the implementation is "a single ReflectionProbe that's placed by default and can't be turned off". If ReflectionProbe is On on the MeshRenderer side and a ReflectionProbe is actually placed, that one seems to be used instead. The calculation inside is presumably the same.

Summing up the characteristics

Diffuse, direct lightSpecular, direct lightDiffuse, indirect lightSpecular, indirect lightDiffuse, environmental lightSpecular, environmental light
Bakeable?YesNoYesNoYesNo
Heavy?Light × number of lightsFairly heavy × number of lightsImpossibleImpossibleLightLight
Shadows at runtime?YesYesNoNoNoNo
Light can change at runtime?YesYesNoNoYes, but heavyYes, but heavy

It's a rough summary, but I'd say that's about it.

Policy for the settings

Bake whatever doesn't move

I think this is the most important thing.

In an ordinary game, most of the screen should be showing scenery, and moving things like people don't take up that much area.

Fragment shader load is proportional to area, so whether you can bake the scenery, such as ground, buildings, mountains and sky, makes a big difference to the load. The smaller the area moving things take up in a game, the bigger the effect of baking. Conversely, in a game where a huge dragon sits in the middle of the screen and keeps moving, it doesn't help much.

Now, to reduce load by baking lightmaps in Unity, you need to set the gameObject to static.

The Static setting of a GameObject

Once it's static, depending on the Lighting Settings, lightmaps get made and the lighting calculation becomes lighter.

Drawbacks of baking

Baking lets you do complex indirect light calculation that you could never do at runtime, and the resulting picture becomes far more realistic, but there are many drawbacks too.

First, the size goes up. The resulting data is textures, and the larger the stage, the more objects on the stage, and the higher the quality, the more space it eats. On top of that, size increases with each light-source setup. If you "bake 8 versions of the same stage with different sun positions and colors", that's 8 times the size, which is practically impossible. Think "let's cheaply make morning, noon, evening and night stages just by changing the lighting", and the size immediately gets scary.

Next, baking takes time. Making high-quality baked data takes time and lowers development efficiency. In practice you'll probably bake at low quality during development to get a feel for it, and do the real bake before completion.

And the biggest drawback is that you can no longer move objects or light sources at runtime. If the spec says the sun slowly moves and turns red in the evening as shadows lengthen, you can't take this approach. Buildings breaking, moving buildings, placing buildings randomly at runtime: none of that is possible either.

If you really need things like that, you'd better give up on baking. It might also be worth exploring an art style that doesn't need fine shading baked in (an anime look, say).

Alternatively, techniques like Ambient Occlusion can get you at least somewhat closer to baked quality.

All the same, for smartphones, if you want even a little realism in the background, a policy of "bake for now" is probably the safe bet.

Designing shadows

Even if you decide to bake what can be baked, only things that don't move can be baked. On top of that there's the problem of interaction between moving and non-moving things. The biggest of these is shadows.

There are four relationships between moving and non-moving things. For clarity, I'll rephrase "non-moving things" as "buildings" or "the ground", and "moving things" as "people". That should make it easier to picture.

1. People cast shadows on people

The standard way is to compute it every frame with shadow mapping, but on top of doubling the draw calls, the shader load of the shadow test is quite heavy.

If you're targeting machines that can't handle shadow mapping, you'll have to give this one up. In that case, weakening the direct light for an overcast-day feel makes it easier to disguise the lack of shadows.

2. People cast shadows on the ground and buildings

If the ground alone is enough and the ground is flat, you can draw shadows relatively cheaply by projecting the model's vertices onto the ground and painting them black. If even that is too much, a "blob shadow" (a round, blurry plate placed on the ground) is a good choice. Without shadows on the ground you lose the sense that things are touching it, which hurts the game, so doing nothing isn't an option. When using these techniques, you'd presumably set Shadows to Disable in the QualitySettings and handle it yourself.

If you want to cast shadows on things other than the ground, or the ground isn't flat, you'll use standard shadow mapping. Draw calls increase, and the shader load of the shadow test also falls on non-moving things, so the load goes up quite a bit.

There's also a problem of correctness. The lighting of non-moving things is already baked into the lightmap. From the lightmap, you can't tell whether a spot is already in a building's shadow or in the sun. If it's already in shadow, a person's shadow falling on it shouldn't make it any darker, but that distinction can't be made.

But if you don't want to add extra calculation, you just have to live with it, and the parts in the shadow of moving things get "a fixed amount subtracted", whether they were already in shadow or not. In Unity, setting Mixed Lighting in the Lighting Settings to subtractive does that. What color the shadow becomes is set as a fixed value.

A shadow inside a shadow, with the shadow color set to green

An example of a shadow inside a shadow. I made the shadow color green so it's easy to see. Gameplay-wise it makes the character's position easier to see, so it can be acceptable as a game even if it's wrong as a picture. With a somewhat more subdued color it might not bother you.

A shadow inside a shadow, with Unity's default shadow color

Here I set the shadow color to Unity's default. How's that? ...Sorry, it really bugs me...

Of course, if you pay the extra cost, you can keep people's shadows from falling in the shade. In Unity, you just set Mixed Lighting to shadowmask. It probably keeps "whether it was originally in shade" as extra information. But of course it gets heavier.

3. Buildings cast shadows on people

LightProbes can do this to some extent. If that's not enough, the only way is to draw the buildings and everything else into the shadow map, which adds quite a lot of calculation. As a "for now" setting, it's fine to go without. If the product really needs it, you'll figure something out then.

4. Buildings cast shadows on buildings and the ground

These are baked into the lightmap, so no problem. However, trying to bake sharp shadows needs high-resolution baked data, which is tough in both size and computation time, so basically you'll put up with blurry shadows.

So what do we do?

As long as the shadows buildings cast on the ground are baked, the other thing that matters for the game is the shadows people cast on the ground. Without them you can't grasp where things are relative to each other. Ancient techniques like blob shadows are a pain to do in Unity, so I'd like to get by with shadow mapping, which is just a switch to flip.

For a game that sells on its visuals you'd want to aim higher, but such products will probably drop support for cheap phones, so I suppose you could use SSAO, and there's an impressive-sounding checkbox called Realtime Global illumination, so you might as well try turning it On.

Still, "shadows inside shadows" is a bit sad. If you check the load and can afford it, Shadowmask is probably the way to go.

Specular reflection on the ground and buildings is a tough call

Human vision grasps shape from areas of strong contrast. So whether or not there's the high contrast from specular reflection changes the impression considerably. But specular reflection depends on the line of sight, so it can't be baked. And it's fairly heavy.

Setting Lighting Settings - Mixed Lighting - Lighting Mode to Subtractive not only makes shadows fall inside shadows, but also stops direct specular reflection from being calculated at all for static things. It should get quite a bit lighter, but it looks considerably plainer.

If I were making an actual product, I'd give it a lot of thought, but the topic here is only "settings to put in place for now when prototyping something", so I think Subtractive with no specular reflection is fine. Starting light and adding elements is usually better than building something heavy and lightening it later.

Once you get used to a heavy, good-looking picture, you won't want to cut it, and you'll have spent quite a bit of effort on tuning and such to make it look good. Cutting that back involves a lot of waste and is psychologically painful work too.

Placing LightProbes

First, for a game with an open stage, I think it's fine to start without LightProbes. In a game where characters move around a stadium-like place, there probably aren't many obstacles or intricate spots. Placing LightProbes is quite a pain, so I think you can go without.

But once you have building shadows falling on characters, characters being able to go into recessed places, or a stage with several bonfires or street lamps that characters can approach, things look too plain without something that changes brightness depending on the place.

A tool for initial placement

For initial placement, I wrote a script that places LightProbes crudely.

LightProbes placed at regular intervals

It runs from an editor script called LightProbePlacer.cs and just places LightProbes at regular intervals. Really you should take care to place them densely where the light changes sharply and sparsely elsewhere, but if it's going to be adjusted by hand in the end anyway, there's no need to go that far. Even a rough placement gives you a feel for it.

Caveats with LightProbes

Also, as the official documentation says too, if you place LightProbes too densely, the results go wrong. The basic rule is "place them at intervals larger than the size of the moving things". For example, if a person is about the size of a cylinder with a 1-meter radius and 2-meter height, the horizontal spacing has to be more than 1 meter or things go wrong.

A sphere in the middle that is strangely dark

There's a strangely dark sphere in the middle; can you tell what's happening?

This scene has a cylinder in the middle. And one of the LightProbes I placed haphazardly happened to end up inside this cylinder. It's inside the cylinder, so no light comes from the surroundings at all, and it goes pitch black.

The LightProbe closest to the center of this large sphere is that pitch-black one, and it gets applied to the whole sphere. And that's how you get this. Moving it just 1 m to get it out of the cylinder gives this:

Moving the LightProbe out of the cylinder

So LightProbe placement also depends on the size of the moving things, and it isn't simple. You also have to avoid them "getting inside a solid and going pitch black", as in this example.

If you're making something like a game where you fight giant monsters, you'll have to think about it quite a lot (I'd probably just decide to only do it in open spaces that don't need LightProbes).

The actual settings

Now for the actual settings.

Lighting Settings

  • RealTime Lightings
    • RealTime Global Illumination
      • It looks heavy any way you slice it, so it's Off for now. When I measured it on my phone it did get fairly heavy.
  • Mixed Lighting
    • Baked Global Illumination
      • Turning it on bakes lightmaps. Let's have it take care of the non-moving things as much as possible.
    • LighitngMode
      • For now I'm setting it to Subtractive and seeing how it goes. If shadows falling inside shadows become painful, I might switch to shadowmask, but it does get heavier. I'll think about it when we get to polishing the visuals.
  • Lightmapping Settings
    • Direct Samples
      • The fewer, the faster the bake, so you want it low during development. Even 2 gives you a feel for it. Once the objects in the scene are done and the light's angle and color are decided, raise it and do the real bake.
    • Indirect Samples
      • 8 is the minimum. The manual said 10, but you can set it to 8...
    • Environment Samples
      • 8 is the minimum. I can't find a way to set it from code...
    • Lightmap Resolution
      • The larger and more complex the scene, the more you need to lower it, or the texture size grows. It's pixels per Unity unit, so in a game where 1 is 1 m, setting it to 2 means baking at 50 cm precision. During development I think even 2 or 4 gives you a feel for it, and even for the real bake you can't make it very large because of the size problem. You can't feed 100 MB to lightmaps in a smartphone game. You bake once per scene, so I'd like to keep it to about one 1024x1024 per stage.
  • Debug Settings
    • AutoGenerate
      • I keep it off. Since it no longer bakes on its own, when you move a light or move an object, the lighting goes wrong, as it should, so you don't forget that "it's baked". If automatic baking is more convenient for you, on is probably fine too.

Quality Settings

On smartphone platforms Medium is the default, so it seems best to tweak that one.

  • Pixel Light Count
    • I left it at 1. As the number of direct lights increases, the shader gets proportionally heavier, so I think it's better to just accept that only one light will shine on things. You'll want things like getting brighter near a fire or a spot glowing where a hit lands, but faking it with a combination of additive-blend effects and LightProbes is probably the safe bet.
  • Anti Aliasing
    • It makes polygon outlines cleaner, but if you're catering to the lower-end devices, it's off. On smartphones the pixels are small to begin with, so I don't think slightly jagged polygon edges bother you that much. The load from this is entirely device-dependent, so another annoyance is that you can't predict how much heavier it gets on which device. Still, there's also the trade-off of "lower the resolution, but turn anti-aliasing On instead".
  • Realtime Reflection Probes
    • Better left off. Generating and updating indirect specular reflection data while the game runs is quite demanding. Car games are the exception, though. It looks too plain if the moving scenery isn't reflected in your own car, so in that case you'll want it on.
  • Shadows
    • Hard Shadow Only should be fine to start with. Softening shadow edges is heavy. You'll have to think about it if sharp shadows look odd, say on cloudy days or indoors, but the bold move of "dropping shadow maps altogether for blob shadows" might be better. Note that setting it to Disable makes things quite a bit lighter.
  • Shadow Distance
    • If it's too small, shadows disappear in the distance, so increase it as needed. Be careful when you narrow the field of view (FieldOfView) and shoot from far away (closer to telephoto) to reduce distortion at the screen edges, because this tends to run short then. The smaller it is, the less jagged and the cleaner the shadows tend to be, but if the next item is set to Close Fit, it doesn't seem to get that bad.
  • Shadow Projection
    • Close Fit seems good. It makes the shadow map's drawing range as tight as possible to fit the camera's situation, so the image quality goes up. However, the jaggies on the shadow edges keep shimmering, so if that bothers you, you'd go with Stable Fit, but watch out for quality and load. Raising the Shadow Resolution to make up for the lack of resolution will push up memory use and GPU load.

Measurements

It's rough, but I did take some measurements. They're on the Kyocera S2 at full resolution, 1280x720. I've given milliseconds and fps, so read whichever you're used to.

SettingFrame time (ms)Frames per second (fps)
Static baking + shadow maps disabled2147
Static baking + subtractive, LightProbe/ReflectionProbe disabled 2638
Static baking + subtractive2540
Static baking + shadowmask2934
Static baking + shadowmask + RealTimeGI3925
No static3132

At least I could confirm that shadowMask and RealTimeGI are slow. I could also confirm that not making things static and not baking is fairly heavy. Disabling the LightProbe and ReflectionProbe GameObjects didn't make it faster, probably because the environmental light processing is the same processing as LightProbes and ReflectionProbes. I think that even with LightProbes disabled, the environmental light data gets stuffed in and the same calculation is done.

I took screenshots in the process, so here they are.

SubtractiveShadowmaskNo staticSubtractive without shadows

In order: Subtractive, Shadowmask, no static, and Subtractive without shadows.

With Shadowmask, the "shadow inside a shadow" problem doesn't happen, which is really nice. Also the ground is brighter overall, because the specular reflection calculation of direct light makes it that much brighter. It's heavier to match, so it's a tough call.

With all the static flags removed, you can't see the influence of the green and red walls at all. And it isn't lightweight either. But all the shadows are sharp and consistent. Some products would prefer this. Still, the processing is fairly heavy.

And removing shadows makes it quite a bit faster. If you go with blob shadows, the game will run at nearly this speed.

But even so it doesn't reach 60fps, which tells us that as long as you use the Standard Shader, getting 60fps at this resolution looks impossible. If you want it that lightweight, it comes down to "don't make it a 3D game in the first place", "don't do lighting calculation", or even "write all the shaders yourself".

Realistically, you'd just lower the resolution. 1280x720 is too high. Lowering the resolution and putting in more advanced techniques with the savings might give higher overall quality.

Summary

I meant to write something short, and it ended up long again.

With Unity you can enable the latest techniques with a single switch, which is great, but the latest techniques are heavy. Unity's main target is probably PC, so if you care about smartphones, and low-end devices in particular, it seems you need to understand what's inside to some degree and set things accordingly.

It's sad to go around dialing things back to what we were doing in the mid-2000s when there's a heap of late-2010s technology available. There are signs that LWRP will become the standard, but I suspect switching to LWRP would give results far harsher on my phone than this.

Still, it's frustrating not to get 60fps. I feel the urge to replace the whole RenderPipeline. It would never pay off, so I don't intend to, but if I end up doing it, I'll probably write an article about that too.