English Stranded III Dev. Blog

103 replies
Goto Page
To the start Previous 1 2 3 4 5 6 Next To the start
11.07.21 09:23:11 pm
Up
DC
Admin
Offline Off
Particle Effects
I've worked on particle effects. Not only the particles themselves but also systems around them. You can now define effects which consist of particles and sounds (both optional - it can also be just sounds or just particles) in Stranded III. These effects can either be specified in other places e.g. the effect that is played if someone steps on or hits a specific material or they can be spawned at will via Lua scripts. The system uses pooling so effects don't have to be re-instantiated all the time (= better performance).

I've also drawn my own particle sprite sheet. The advantage of having multiple sprites in one single texture is that you only need one material for all your particle effects. This reduces rendering overhead if multiple particle effects are visible at the same time. Okay, well.. in reality I need glowing and non-glowing sprites etc. so I have to use different shaders and therefore it's not just one material. Using the sheet improves performance nevertheless.

That's what the sheet looks like right now. There's still enough room for additional effects!
IMG:https://stuff.unrealsoftware.de/pics/s3dev/particlesheet_pre.jpg

> Click for bigger version


I was quite scepticale when I started using Unity's particle system. Before that I was used to entirely programming particle effects on my own. That's how I did it in all my previous games. The advantage of programming particle effects is that you can do whatever you want. There are absolutely no limitations.

Coming from this approach with unlimited possibilities I was sure that the Unity particle system, which uses a (big) set of settings, would limit me and my creativity a lot. Luckily I did not spot any major limitations yet. Kudos Unity! That particle stuff is actually quite nice and allows to create complex effects. The best part is that you can instantly see your changes which massively reduces iteration times. When I programmed particle effects I had to recompile my projects all the time to see what my adjustments look like.

Here are some of the effects I made. Note that these are just first attempts and that I may (or may not) improve them later.
IMG:https://stuff.unrealsoftware.de/pics/s3dev/fire_pre.gif

> Click for bigger version

If you look carefully you can see that the're even bouncing fire sparks with collision (just one visible in this gif)!

IMG:https://stuff.unrealsoftware.de/pics/s3dev/bubbles_pre.gif

> Click for bigger version


Resarch & Recommendation: Subnautica
I started (and finished) playing Subnautica. Of course primarily for research purposes
It has two major things in common with Stranded III:
1. it's a 3D first person survival game
2. it's made with Unity

If you didn't play Subnautica yet I can highly recommend to give it a try. I got it for free on Epic store some time ago but it's also worth the money. It's super fun to explore the world and it doesn't get boring because events and new crafting abilities keep you entertained.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
19.09.21 08:26:26 pm
Up
DC
Admin
Offline Off
Responsive Website
Stranded3.com is now responsive and works better on mobile phones. This is something I planned to do for all my pages this year. It's not 100% finished though and I may change a few things in future.

Equipment Icons
I've drawn new equipment icons which better match the building category icons. That means: doodle style stuff.
IMG:https://stuff.unrealsoftware.de/pics/s3dev/ui/equipment_pre.jpg

> Click for bigger version


Automatically Generated Icons
Items in the game can either use images as icons for the inventory or the game can automatically render icons from the 3D models. Automatic rendering can be adjusted with some definition settings to make the items look better. I now made a simple hacky dialog which helps to find the right definition values:
IMG:https://stuff.unrealsoftware.de/pics/s3dev/ui/iconmenu_pre.jpg

> Click for bigger version


Technical Stuff: IDs & Lua
Stranded III is using string identifiers to make things more mod- and script-friendly. Internally however it's working with integer identifiers and references/pointers because they consume less memory and are processed much faster.
When writing Lua scripts you can now benefit from the best of both of these worlds. You can use string identifiers (which will automatically be converted to the internal identifiers) or you can use the internal identifiers.

This script will get the internal identifier for the "log" entity definition:
Code:
1
log = entityDefinition.get("log")

Retrieving the identifier once and using it multiple times in subsequent script parts will be slightly faster than using the string identifier all the time. That's because each time you're using the string identifier, Stranded III will have to do a hash map lookup (it's actually a C# dictionary). If you get the identifier once and reuse it, there will be only one lookup.

Simple, unoptimized approach (100 hash map lookups):
Code:
1
2
3
for i=1,100 do
     entity.spawnOnGround("log", math.random(-50,50), math.random(-50,50))
end


Optimized approach with cached identifier (1 hash map lookup):
Code:
1
2
3
4
logId = entityDefinition.get("log")
for i=1,100 do
     entity.spawnOnGround(logId, math.random(-50,50), math.random(-50,50))
end


This is a micro optimization though and in allmost all cases it's probably easier to just stick with strings. It's just nice to have the possibility to use less of these slow and nasty strings.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
28.11.21 10:14:17 pm
Up
DC
Admin
Offline Off
Advent
Advent is coming! First candle is on! Hooray!
IMG:https://stuff.unrealsoftware.de/pics/s3dev/advent1.png

Wow! I even painted 2D candles on that 3D lifebuoy! That looks... uh. Whatever! Christmas! Santa! Hey!
Okay.. now prepare for some technical stuff! Fun Fun Fun!

Technical Stuff: Dithering
Dithering is a technique which "fakes" color detail by smartly painting pixels in only few different colors.
One common use case is image compression which works with color palettes. The advantage of palettes is that you don't have to save red, green and blue (and possibly alpha) color information per pixel. Instead you just create a palette with all colors used in the image and then you just save the palette index pointing to that color for each pixel.
In best case this requires much less memory per pixel.
To make that approach efficient the palette must be as small as possible though.
Firstly because the palette itself needs to be saved as well and secondly because the data required for each pixel grows with the size of colors in the palette.
Dithering can be used to reduce the amount of colors in the image. You can for instance create a fake orange by making every even pixel yellow and every odd pixel red. With more sophisticated patterns you can even fake color gradients using just 2 colors.

Fake Transparency With Dithering
A different use case for dithering is faking transparency. It basically works the same way. If you want an image to look transparent you just skip some pixels using a pattern. The more transparent the image is the more pixels are skipped when rendering it.

But Why?
Why would you want to do that though? I mean dithering is visible and looks worse than using more colors / mixing pixels to calculate proper transparency.
The answer is simple: Speed!
When rendering 3D scenes real transparency is quite expensive and costs a lot of performance. The reason for this is they way a 3D scene is rendered. The GPU creates a Z-Buffer which calculates the depth of 3D objects in the scene and is used to determine what to render and in which order. That way the GPU can quickly skip rendering of pixels of objects which are occluded by other objects in front of them. Transparent objects however don't work with that approach because their color has to be mixed with objects behind them. They kind of break the Z-Buffer rendering approach. In Unity transparency can also have a negative impact on batching which can also decrease performance. Fake transparency with dithering dodges these problem because there are no semitransparent pixels anymore which need to be mixed.

Can I Dither In Unity?
Because of the stuff explained above I want to use dithering for transparency! Especially for showing/hiding objects which are far away. I want these to fade in/out smoothly instead of suddenly popping in and out.
Unity supports dithering but there is no proper documentation for that feature and I had to search a lot to make it work. Unity can use dithering for LOD crossfading but all used shaders need to be properly adjusted to support it. At the bottom of the "LOD Group" documentation page we see the variable which controls the transparency: unity_LODFade. So we don't have to use the "LOD Group"-component to make it work (and I don't want to use it for some reasons). We just have to set the unity_LODFade value in all used shaders/renderers to control their fading/transparency value.
Luckily I found this forum post which explains how to do it. Hooray!
Code:
1
2
3
4
5
6
7
8
9
10
//Call inside Start()
rend.sharedMaterial.EnableKeyword("LOD_FADE_CROSSFADE");
 
// Coroutine/Lerp this
Vector4 fadeOutVec = new Vector4(tFadeOut, tFadeOut);
block.SetVector("unity_LODFade", fadeOutVec);
rend.SetPropertyBlock(block); // Blocks can be pooled
 
// Call this one frame after tFadeOut == 0, this will cause static batching to work again
rend.SetPropertyBlock(null);

(code taken from the posted linked above. Posted by sewy. Thanks for that!)

That post also mentions how shaders need to be adjusted for that dither crossfade fun but the explanation isn't very detailed. A better explanation for shader adjustment can be found in this post. Furthermore in that post there's a link to a simple shader which supports dithered transparency / crossfading. It's great for reference and to get started and helped me to adjust other existing shaders to also support dithered transparency.

Optimizations
The code snippet above says in a comment that the ProperyBlocks can be pooled.
In fact you don't even need real pooling there. You can just create one PropertyBlock for each transparency level and use those as often as you want to. This is also what the documentation suggests:
Quote:
The block passed to Graphics.DrawMesh or Renderer.SetPropertyBlock is copied, so the most efficient way of using it is to create one block and reuse it for all DrawMesh calls.

Actually they even suggest to only use one PropertyBlock for everything. I want to skip the overhead of changing the fade value however so I guess my approach is fine too.
I somewhere read that Unity's dither pattern has 16 levels so I prepared my PropertyBlocks this way:
Code:
1
2
3
4
5
6
7
8
9
readonly int LodFadeNameID = Shader.PropertyToID("unity_LODFade");
var propBlocks = new MaterialPropertyBlock[16];
for (int i = 0; i < 16; i++)
{
     propBlocks[i] = new MaterialPropertyBlock();
     float fadeValue = (i + 1) / 16f;
     Vector4 fade = new Vector4(fadeValue, fadeValue);
     propBlocks[i].SetVector(LodFadeNameID, fade);
}

Also note that I used Shader.PropertyToID because using strings is always bad. (okay... it doesn't really matter in a loop with only 16 iterations which is just called once but whatever)

Now to set the transparency of an object the following needs to be done:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Enable crossfade keyword, only needs to be done once
// (Maybe this doesn't need to be done at runtime at all? Further research required!)
var renderers = GetComponentsInChildren<Renderer>(true);
foreach (var renderer in renderers)
{
     renderer.sharedMaterial.EnableKeyword("LOD_FADE_CROSSFADE");
}

// Set transparency (assuming that transparency is a float from 0.0 to 1.0)
int index = Mathf.Clamp(Mathf.RoundToInt(transparency * 15f), 0, 15);
foreach (var renderer in renderers)
{
     renderer.SetPropertyBlock(propBlocks[index]);
}

All this code is just an example. It's not 1:1 the code I'm using in Stranded III. The renderers for instance should be cached because GetComponentsInChildren isn't very fast.

Also when the transparency is back to 1 (fully opaque) the PropertyBlock should be unset. That's because using a different material block breaks Unity's batching and therefore can have a negative impact on rendering performance.
Code:
1
2
3
4
foreach (var renderer in renderers)
{
     renderer.SetPropertyBlock(null);
}


Whew! That was a long one!

Not sure if this is helpful to anyone but I think it's a very important and useful thing which is why I wanted to write it down.

Here's what it looks like (in the big version you can even see the dither pattern):
IMG:https://stuff.unrealsoftware.de/pics/s3dev/ditherfade_pre.gif

> Click for bigger version (2.6 mb)


btw: The compression of that gif is probably one of the best examples for color palettes and dithering!

btw²: Dithering can be seen in a lot of games. So now you know why. It's all about performance (oh and sometimes it also prevents some ugly rendering/sorting issues which can occur with real transparency)
edited 1×, last 28.11.21 10:30:49 pm
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
05.12.21 09:39:57 pm
Up
DC
Admin
Offline Off
Advent II
Advent is here! Second candle is on fire!
IMG:https://stuff.unrealsoftware.de/pics/s3dev/advent2.png


Monstera
Let's welcome a new plant to the Stranded III family: The Monstera! Monsteras are pretty easy to recognize because of their funny leaves which can have holes and slits. That unique look also makes it a sought-after house plant.
IMG:https://stuff.unrealsoftware.de/pics/s3dev/models/monstera_pre.jpg

> Click for bigger version


I plan to make a climbing plant version later which can not only grow on the ground but also on trees, rocks etc.
Monsteras can actually yield edible fruits but I'm not sure yet if this will make it into the game.

... and that's it for today. Sorry. I had to make a post for the second candle and that's all I have to show right now
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
To the start Previous 1 2 3 4 5 6 Next To the start