[Unity Game Development Tutorial 15] - The obvious path to cubic voxels and the problem with this approach...

in #voxel8 years ago (edited)

Minecraft and other games make the cubic style voxels popular. Once you use Unity at some point the idea of just using the Primitive for the Cube and simply placing it may come to your mind.

This can work, but has performance issues. Some games you may consider using this method. The bigger the map and the more blocks you plan to display the worse it will be. I decided I'd throw one of these projects together so you could see for yourself. It is super simple, but it does give a chance to consider some problems.

I thought it was a good idea to show you what happens when you use the method that seems the most obvious when you've messed with Unity a bit.

The Demo


How nice the cubes look on screen really depends upon how much work you put into making interesting textures and perhaps softening the edges some. In this case all I used was the standard Unity Cube Primitive. I have quite a few textures that I made 1 material for each texture and built a corresponding prefab for each material.

I am not going to spend a lot of time explaining how this was done. It is not the best approach. I did want to show the obvious method though.

Here is the Setup


This quick how NOT to do voxels in Unity assumes you know the basics of setting things up in Unity. If you do not I do have some older tutorials that might be useful to you.

[1] -The basics - An Intro to Unity - Layout, Non-Coder, Coder
[2] - Coding Education Speed Boost - Coding Lesson 1 - also my naming conventions

Here are the unity screens to show how this project is setup.
First the two objects other than the Main Camera, and Directional Light are simply Empty Game Objects at 0,0,0 that I have given the labels VoxelLevelGenerator, and Map. Map is simply an object to parent the instantiated cubes to so they do not clutter up the hierarchy. VoxelLevelGenerator is the object we attach our script to that does everything. This also has the standard Character asset imported that Unity provides for quick access to the First Person Controller.

The next screen shows how the script is populated. You can see a lot of these are prefabs that have been dragged into slots. Other than the FPSController which is a standard Unity Asset the prefabs are all the different cubes with different materials for textures.

This is what the folder layout for the assets looks like for this project.

Here are the cube prefabs. They are all standard cube primitives at 0,0,0 with one per material.

Here are the materials used by the prefabs.

Here are the textures used by the prefabs, and larger versions will be available that you can download and USE if you want near the end of this tutorial.

Here is the code... ALL OF IT. It does not use perlin noise. I used more of a cellular and controlled approach. My purpose with this demo was to get a certain amount of cubes on the screen ins some semi-interesting appearance so performance can be tested.




Here is a generated scene using the above code, and prefabs. This is only a 64x64x16 area. So it is really small in terms of Minecraft style scenes. I am running it on a machine with 16GB RAM, Nvidia GTX 960, and an i5-4670K CPU @ 3.4 Ghz, Windows 10, Unity 5.4.1f1 Personal Edition. Given this information the performance is bad. I don't have frames set to vsync so my frames per second is uncapped an a normal empty screen runs somewhere around 2200 FPS!!! So as you can see the FPS are quite low with this scene.


Here is a similar experiment but 128x128x16 sized map area. The FPS are less than half what they were on the 64x64x16 display. This is not very far and pulling off minecraft distance and complexity would not be very doable with this.


I personally know that colliders can cause a pretty big performance hit, so I'm going to do one last experiment also at 128x128x16 where I have removed the colliders from all of the prefabs. I did not bother with screenshots on this one as they look similar to the last and the FPS was at 26, so in this case colliders made no difference.

Textures


Here are the textures I used for this demo. These textures are intentionally simple dirt, grass, and rock. I used variations on the same textures as if I was concerned about art fatigue, but in this example it was only so I'd have a lot of different textures to more closely simulate how a minecraft style level would likely perform when you have a lot of textures. So even though I used similar textures imagine if each one of these was a unique texture and no variations. You would get a similar performance in such a case.







Conclusion


This is obviously not how you want to approach something like this using Unity. It does however, naturally pop into the mind as something to try. It popped into mine years ago. So I hopefully saved you some time heading down this wrong path by showing you the results.

The correct path is to build chunks of multiple cubes on the fly as one big mesh, and not render invisible sides. If you are interested in my pursuing such a project and in detail showing you how it is done, please consider up voting this post. It takes a lot of time to put together these tutorial posts. If you are not interested then don't vote and I will concentrate on other types of blog posts.

Steem on!

Sort:  

Great tutorial @dwinblood. Thank you for sharing! I work at Unity, the game engine and I'd definitely like to get your insight on how you'd use cryptocurrency across a mobile platform with 50% of all mobile games? I’m wondering if you or your audience is familiar with Unity Game Engine and Development Platform. If so, from a Gaming or social Perspective, how would you use Crypto Currency in the Unity Game Engine?

I've been posting about Unity on here since I joined in July 2016. I've done a number of tutorials. I've also talked about different ways to monetize it. I would like to merge it with the steem blockchain as well.

The problem for me right now is I am focused on the game dev side while 99% of the people seem to be focused on web apps. The biggest problem I've run into is the idea that if a person plays a game built in the engine there is a trust issue. There are a lot of techniques that people have been working on that can resolve this so, it is less of an issue. I really liked the idea of being able to embed WebGL games in my posts and that is not likely to happen due to the security issues/holes it could open.

Yet, we could still make games that tap into the steem blockchain. I was actually thinking of making games that gave different appearances and avatars based upon player stats.

This is something I'd like to work out myself, and I am interested in pursuing it if we can resolve these issues. I am the only one on my team doing any coding, so I am spread thin. I didn't really have time to devote to coding web apps so I could learn what I needed to know in order to link steem in.

Also with the SMTs (Smart Media Tokens) coming from the steem blockchain soon this could be HUGE for Unity. I am very interested in this.

Here are some posts I've made on similar subjects since I've been here:

There are others, including one I linked to a WebGL build of a game in. That one is down at the moment as I need to renew my Godaddy hosting that the WebGL build is on.

Anyway, let me know if you need help, or if I can be involved to make this happen.

Interesting... thanks for sharing! It's not personally something I have needed... yet, but I am sure some people out there do. I have seen several posts mentioning developing video games in the last several weeks...

When I did a post with some options a few weeks ago for direction to go. The two people voted on were the Turing Test, and cubic voxels. So I thought I'd test the waters before devoting much more time to making a tutorial on how to actually make it by manually building meshes from vertices, mapping UVs, building texture atlases on the fly, etc.

Anything that you are knowledgeable about, and is not steemit related is the way to go in my opinion! Also, don't do art... we have enough of that on here ;)

I enjoyed reading your coding style and I learned from your experiences. Great examples, good post. Thanks!

Wow that was super cool man, I do love your posts. I'm a gamer but you have a unique perspective coming from the developer side that's truly nice to see.
Love your posts man.

Thanks it is always nice to get feedback on posts.