How to Program Voxel Worlds Like Minecraft with C# in Unity
4.5 (811 ratings)
4,126 students enrolled

# How to Program Voxel Worlds Like Minecraft with C# in Unity

A comprehensive guide to blocky mesh building for procedurally-generated worlds like those found in Minecraft.
4.5 (811 ratings)
4,126 students enrolled
Last updated 3/2019
English
English [Auto-generated]
Current price: \$15.00 Original price: \$149.99 Discount: 90% off
30-Day Money-Back Guarantee
This course includes
• 6.5 hours on-demand video
• 6 articles
• Access on mobile and TV
• Certificate of Completion
Training 5 or more people?

What you'll learn
• Build a cube-based geometric world like that generated in Minecraft.

• ### Build and skin meshes from scratch using C#.

• Save and Retrieve Serialised Data.
• Build and manage a voxel data structure.
Course content
Expand all 47 lectures 06:23:13
+ Introduction
6 lectures 23:10

This lecture gives a general overview of the course and how it will be taught.

Preview 02:45

This lecture discusses the text editor used throughout this course.

FAQs
00:01

Voxels are data points in three-dimensional arrays. Contrary to popular belief a voxel game world need not be blocky. This lecture explains what a voxel really is.

Preview 03:19

In this lecture we will examine the nature of three dimensional data and look at an array that stores it and a nested loop structure that creates it.

3D Arrays and Data Management
02:40

It might seem like a good idea to build a world like Minecraft with cubes. But an examination of the performance of rendering such a world proves otherwise. This lecture takes a look at the issue.

Preview 14:18

Modify the cube world developed in the previous lecture to build a world that has a cube width and depth of 10 with a height of 2.

Challenge Exercise
00:06
+ Anatomy of a Cube
11 lectures 01:34:26
Preview 05:53

Now that you have the values required to build a cube we will start in this lecture by writing code to build one face. By following along you will construct a fully texture quad build by specifying vertices, normals, uvs and triangles.

08:43

Single quads are as inefficient as building an entire world with cubes so in this lecture we will start combining meshes together to create bigger ones. By continuing to follow along with the instructions you will complete writing the code to build a single cube from scratch.

Building a Cube
11:46

In this lecture we will examine UVs in detail exploring how coordinate values mapped between a polygon's vertices and the 2d coordinates inside a texture can optimise rendering performance as well as allowing for easy customisation of meshes.

UVs and Texture Atlases
03:31

Being able to change the texture that's on a quad really makes the environment start to look like a Minecraft world even if it is only one block. In this lecture we will extend the code to add in block type selection and pick the appropriate texture to display on the quad from a texture atlas using UV mapping.

10:37

Chunks are necessary to reduce the number of polygons in the world and optimise rendering. In this first part on creating chunks we will restructure the existing code into two new classes for chunks and blocks.

Chunks Part 1
09:34

In the second part of this lecture on building chunks we will examine new code to cull any internal quads that are being drawn where they don't need to be. This will involve all blocks checking with their neighbours as to whether they are against something solid or not.

Chunks Part 2
09:27

In the third part of this lecture we will add air blocks into the data to ensure the algorithm for building quads will work with them. In addition, the limitations of a single chunk in Unity will be explored.

Chunks Part 3
07:11

In this lecture a new World class will be added to the project to allow for the building of chunk columns. This will inturn require small changes to the Block and Chunk classes but add the extra functionality of building more height into a terrain.

Chunk Columns
16:49

In this lecture we will examine in detail the coordinates of chunks and blocks and determine how to locate and reference neighbouring blocks in adjacent chunks.

Locating Inter-Chunk Neighbours
08:19

As the final lecture in this section we will extend the World class to build more than one column to populate an entire world with breadth and depth. Through the addition of a couple of simple loops the column building code is changed to build something much larger.

World Building
02:36
+ Noise
7 lectures 56:54

Generating a realistic terrain requires a degree of predictable and smooth randomness. This can't be accomplished with a random number generator. Instead we must look for another solution in the theory of noise. In this lecture we will examine Perlin Noise and how it can be utilised in procedural content generation.

Preview 05:58

In this workshop you will follow along and create code to generate and graph height values using Unity's build-in PerlinNoise function.

Graphing Perlin
13:03

In this workshop we will extend the Perlin Noise generating code to fractal Brownian Motion, an algorithm that calculates and combines a series of Perlin octaves.

Brownian Motion
07:24

Returning to the project in which we last built chunks, a new utility class will be added to generate terrain heights based on fractal Brownian motion. In this lecture you will follow along as a method for generating heights is created and applied.

Chunking Noise
10:37

In this lecture we will create more methods to calculate Perlin Noise at differing heights to add in other block type layers including stone. In addition we will modify the neighbour checking code to ensure the top layer of dirt is grass.

Noisy Block Types
07:06

Now that we've got the hang of building chunks with a set terrain height and layers of different blocks, in this lecture we will start building caves and adding other probablistic minerals using three dimensional Perlin Noise.

Preview 12:34
10 questions
Challenge Exercise
00:12
+ To Infinity and Beyond
7 lectures 01:25:19

In this lecture we will add a player controller to the terrain to enable us to walk around and check out the world. Once this is added we will be able to control the creation of the world so the chunks appear around the player's location.

12:09

14:55

In moving the player around the world we need to consider what chunks should and should not appear. In this lecture we will look at a means of producing chunks around the player and determining which new chunks should be built while discussing how chunks can be marked for removal.

11:40

In this lecture you will follow along using the RecursiveBuildStarter project given as a resource. We will look at the workings of a recursive flood fill algorithm and start implementing it in our code to build chunks around the player.

16:59

As the radius of the world increases so too will the possible number of threads run by the method generating the chunks. In this lecture we will add a thread queuing system to reduce the possibility of program crashes and allow for the safe use of larger radii.

11:21

In this short lecture we will add in a new method for removing chunks that are outside the player's drawing radius.

05:29

In this lecture we will examine serialization and build methods for saving and loading the chunk data.

12:46
+ Interactivity
7 lectures 55:10

Now all the functionality for building and drawing chunks is in place, destroying a block with a click is elementary. In this lecture we will build a new class that allows you to destroy blocks with the click of a mouse.

Removing Blocks
18:22

In this lecture we will increase the amount of clicks a block requires in order to have them be destroyed. At the same time we will examine the use of a secondary texture and UV values to place cracks onto the surface of clicked blocks.

Removing Stubborn Blocks
16:03

In this short lecture we will create a new MonoBehaviour script to attach via code to the chunks in the world to control timed events such as the healing of cracked blocks.

Healing Blocks
06:12

In this lecture we will extend the code for destroying a block to create a new block in the environment with a right mouse click.

Building
04:32

In adding the functionality to build we now need to consider building across chunks. This requires a small but powerful tweek to the code that will allow you to get the chunk and block at any clicked on world position.

Working Across Chunks
05:51

Each time you add or remove a block from the world you'll want a permanent record of the change. Utilising the existing saves that have been integrated into the remove chunk functionality we will add a timed progress saver into the project.

Saving Progress
03:54
Challenge Exercise
00:16
+ Dynamics
6 lectures 53:02

Adding water creates new issues in the way the world is built as water is transparent and does not have a collider. In this lecture we begin our examination of water by building a blanket water layer.

Water
14:01

Water of course moves and when you add a block of water to the terrain you would like to see a simulation of the effects of gravity on it. In Minecraft water comes from a water spawn block. In this lecture you will learn how to add such a block that produces a quantity of water and spreads it over the environment.

Moving Water
12:11

Some types of blocks react differently to static and flowing blocks, for example, sand. In this case the block might just be one that falls if nothing is beneath it. This is a similar dynamic to flowing, however more blocks of the same type are not produced. It's just a single block moving through the y axis of a chunk.

Sand
06:12

In this lecture we will examine how to update dynamic blocks when something changes in a chunk. The example will be given where a sand block is removed from the terrain and the sand blocks above it fall down.

Updating Dynamics
05:36

Adding trees introduces a new complexity for the code as trees are placed in one chunk but can grow into another. This means trees should be one of the last things generated for the terrain. In this lecture we will place seeds for trees in one loop and draw them in another.

Trees
09:41

All you've already learned for building the world lends itself well to creating different biomes. By applying different surface blocks and trees to differing fBM probabilities and world height values you'll be able to create a vast array of different landscapes from deserts to tundra. This lecture gives you some pointers on how to achieve this.

Biomes
05:21
+ Extras
2 lectures 04:29
Moving Texture On Water
00:46

This lecture demonstrates how to extend the block texturing code to create unique blocks with multiple textures beyond that of grass.

Customising Complex Block Textures
03:43
Requirements
• You should be familiar with C# and the Unity Game Development Engine.
• It would be useful if you have played Minecraft but its not necessary.
Description

Did you know there is not one single cube used in Minecraft? Have you ever looked at Minecraft and wondered, "how did they build it?".  And a great thing to ponder it is.  If you haven't and think that programming a whole bunch of cubes is child's play, then think again. On the surface Minecraft looks as though it would be a simple world to recreate in Unity. Start placing cubes around a scene and see how far you get before the frame-rate grinds to a mind-numbing halt.  So wonder and fret no longer as this course reveals the secrets of programming and rendering procedurally generated voxel worlds, like Minecraft, with C# in Unity.

In this course, Penny teaches all the invaluable skills you will require to build a blocky world from scratch using her internationally acclaimed teaching style and knowledge from over 25 years working with games and graphics.  But hold on tight as you'll be taken on a journey across the computer graphics realm as it is taught to post-graduate university students. Through detailed descriptions and hands-on workshops examining all you need to know about 3d data structures, building meshes from scratch, using noise algorithms to generate terrain features and caves, and texturing blocks you will be programming your own world in no time.

Learn how to program and work with:

• voxels;
• custom built polygons and meshes;
• vertices and normals;
• UV mapping;
• texture atlases;
• Perlin noise;
• infinite terrain generation; and,

Contents and Overview

After getting a little experience in why it's such a bad idea to build a Minecraft type world using cubes, you'll start following along with Penny, exercise after exercise, toward the end of building your very own and unique blocky terrain.

You'll begin by dissecting a cube and examining its fundamental parts from vertices to normals to texture mapping.  Armed with this knowledge you will then begin writing code that redefines the cube and makes it far more efficient to draw as a terrain component.  You'll then bring these pseudo-cubes together to form chunks to optimise rendering performance. Once you've worked on creating a solid world of chunks you will then learn about smooth noise and a little fractal geometry will be introduced to assist you in carving out realistic landscape features including caves.

With terra-firma to stand on, you'll next add a player character and start exploring the new world.  But so you don't fall off you will also start working on the logic to build the world around you in real time out to infinite boundaries.

And when you are done with exploring it will be time to start digging and building.  It's in this part that you will learn how to add and remove blocks at the click of a button to further mould the terrain to your liking.

To round it off, you'll cover the dynamics of adding flowing water, falling sand and start working on the logic for creating your very own biomes.