SakeTami
thinmatrix
thinmatrix

patreon


Homegrown Update 0.2.2 - Shadows

The graphical update to Homegrown has been progressing very nicely over the last few weeks! I’ve switched all the models over to the new art-style and finished working on the new rendering process for the entities in the game, including improved lighting, shadows, and a wind-effect for foliage.

You can download the current version of Homegrown and try it out here (for “Gamer tier"+ patrons): https://www.patreon.com/posts/game-download-72441007

And the code is available here (for “Programmer Tier"+ patrons): https://www.patreon.com/posts/code-download-72497667


New Models

All the models in the game have now been redone and replaced with shiny new models using the new art style. I’m still getting used to it working with this new, smoother style, so there’s still a bit of “calibration” to do when it comes to things like how thick leaves should be, how rounded edges should be, etc. but I’m sure this is something I’ll improve on as I make more and more models in this new style. I imagine I’ll have to come back and tweak a lot of these early models in the future to ensure that they’re consistent with the art-style later on, but in general I’m very pleased with the new look!

One handy little trick I learnt recently is how to round-off sharp edges (such as the edges of a cube) without adding too much extra geometry. I had always assumed that rounding off these edges would require the addition of a lot of new vertices to create a good curve, but I learnt that it can be done with a single bevel and then editing the normals a bit. By setting all the vertex normals to point in the direction of the cube’s main faces the edges become nice and rounded as the normals get interpolated over them, while the large faces remain completely flat as all four normals are pointing in the same direction.


Lighting

The new models deserved some improved rendering to allow them to look their best in-game, so I reworked the lighting calculations used in the shaders when rendering the entities. The lighting model that I used is heavily based on the tutorials Inigo Quilez, such as this one: https://youtu.be/Cfe5UQ-1L9Q

The lighting is now made up of various components, rather than the single diffuse lighting calculation that I had before. It now includes: general lighting from the sky which lights up the whole model in a blueish light, affecting upwards facing faces more; standard diffuse lighting from the sun which is a reddish colour; Rim lighting which is basically a Fresnel effect – triangles that are at more of an angle to the camera appear more refelective and reflect more of the ambient sky colour. A reflection of the sky-dome (could be done with an actual cube map texture, but I just simulate it with a simple calculation) - specular highlights, or reflection of the sun.

There’s still a lot of room for improvement in this area, but it’s a good start!


Wind Effect

Another new addition to the entity shaders is a simple effect to make plants sway in the wind; the same effect that I used for the plants in Equilinox. Each material can have its own flexibility value, so I can choose which parts of models are affected by the wind (and how much they’re affected). One slight improvement compared to the implementation of this effect in Equilinox is the option to set a custom anchor height. Usually the anchor point is the ground (grass for example, is fixed to the ground) but for some plants in this game it needs to be a bit higher. The turnip leaves for example should be anchored to the top of the turnip, rather than to the ground.

Currently this effect is produced using a couple of sine waves in the vertex shader, but a few people  suggested after the last devlog video that using a perlin noise texture to create the offset could be faster and more realistic, so I’ll be giving that a try soon!


Shadows

My main task over the last couple of weeks was to implement shadows, a topic which has always been something of an Achilles heel for me. I’ve never really implemented shadows properly in any of my projects over the 10 years I’ve been doing game development. For example, in Equilinox the entities could cast shadows on the terrain, but weren’t able to self-shadow or cast shadows over other entities. So I wanted...

The shadows in Homegrown are use standard shadow mapping and this time entities can self shadow. To avoid the common problem of “shadow-acne” I render only the back-faces of objects when rendering to the shadow map. This mostly eliminated the acne problem, but causes some light to bleed into the shadow where the model intersects with the terrain; something I hope to improve on in the future.

To smooth the edges of the shadows I used “Percentage Closer Filtering” (PCF), which samples the shadow map multiple times per fragment and chooses the lighting of the fragment depending on how many of those samples are in the shadow. I then took that a step further compared to my implementation in Equilinox, and introduced some “jitter” to the PCF samples. This adds a random offset to the samples, creating a bit of noise in the shadow edges and making them look slightly more organic.


There’s still a lot more that can be done to improve the edges. This article here give some interesting ideas for improvements and optimizations: https://developer.nvidia.com/gpugems/gpugems2/part-ii-shading-lighting-and-shadows/chapter-17-efficient-soft-edged-shadows-using

It suggests the use of a circular disk pattern for sampling rather than a square grid, and recommends storing the pre-calculated jitter offsets in a 3D texture rather than having to calculate them per-fragment in the shader. It also mentions that you can use comparison mode for sampling the depth texture, which together with linear sampling would basically do one free 2x2 PCF calculation: https://www.khronos.org/opengl/wiki/Sampler_Object

One final little change from the Equilinox code: to ensure optimal shadow resolution in Equilinox I would calculate the area (shadow box) to render to the shadow map every frame to fit the camera’s view frustum as closely as possible. However, this constant calculating of the shadow box meant that the shadow edges would shimmer very obviously when the camera was moving. To improve this, I now calculate a slightly larger shadow box around the camera, and then only recalculate the dimensions of the box when the camera frustum goes out of the area. This means less stuff changing in the shadow map, and much less shadow shimmering.


Up Next

So, that’s what I’ve been working on recently! I’m really happy with the progress of the graphical update so far; the game is looking so much nicer already. Still a long way to go though, and up next I’ll be making improvements to the terrain, adding particle effects, and generally improving the design of the layout and scenery of your garden area. Plus I’ll be creating a brand new town area where you can go to buy and sell items!

If you have any feedback let me know and thanks as always for the great support!

Karl

Homegrown Update 0.2.2 - Shadows Homegrown Update 0.2.2 - Shadows

More Creators