Minggu, 18 Agustus 2013

Unity Voxel Tutorial Part 1: Generating meshes from code

I've been meaning to write this tutorial for a long time. It's going to be a long series so bear with me.


We won't be starting with voxels, instead we'll keep to something a little more basic to understand the systems of generating meshes for render and collision. We will be building a tilebased sidescroller where the tiles can be generated proceduraly and edited in-game. Later on we'll be expanding to 3d. In this part we'll write a script that creates a textured square in 3d space.
Let's start with and empty Unity project. I like to set up a folder structure to get started but that's up to you. Make a new scene "RenderTest" and a new C# script in the level folder (Or anywhere)  and call it "PolygonGenerator".

What we're going to do here is go through the basics of building meshes from scratch using the Mesh Class. So, in our new scene add a Game Object with a Mesh Filter, a Mesh Renderer and a Mesh Collider. Also add our new script PolygonGenerator. Make sure that the Game Object is at (0, 0, 10) so that it can be seen by the default camera.


Because we're using lists we need to add some code below the "using System.Collections", add
using System.Collections.Generic; 
below it.

Now let's move on to the script and start making in do things. We'll start with the following variables:

 // This first list contains every vertex of the mesh that we are going to render
public List<Vector3> newVertices = new List<Vector3>();

// The triangles tell Unity how to build each section of the mesh joining
// the vertices
public List<int> newTriangles = new List<int>();

// The UV list is unimportant right now but it tells Unity how the texture is
// aligned on each polygon
public List<Vector2> newUV = new List<Vector2>();


// A mesh is made up of the vertices, triangles and UVs we are going to define,
// after we make them up we'll save them as this mesh
private Mesh mesh;


With those defined we'll assign the mesh some simple values to display something. In the Start function we'll use the following code:

// Use this for initialization
void Start () {

mesh = GetComponent<MeshFilter> ().mesh;

float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;


newVertices.Add( new Vector3 (x , y , z ));
newVertices.Add( new Vector3 (x + 1 , y , z ));
newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
newVertices.Add( new Vector3 (x , y-1 , z ));

newTriangles.Add(0);
newTriangles.Add(1);
newTriangles.Add(3);
newTriangles.Add(1);
newTriangles.Add(2);
newTriangles.Add(3);

mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
}


What you should see

Now to explain how this works, first of all line 4: We get the component from the Game Object and assign in to the mesh we defined earlier. This means we can now use "mesh" to access the mesh filter. Next I assigned every axis of the object's position a float, this is just because writing x is easier than transform.position.x.

Now the vertices, if you've run this code you'll see it generates a single pink square on the screen. Each corner of that square is a vertex defined here. Now one thing to remember is that positive x is to the right and negative y is down. If we assume the Game Object's origin to be 0,0,0 then we put the first point at origin, the next one to the right at 1,0,0, then 1,-1,0 so the bottom right and lastly 0,-1, 0 the bottom left. 

After that we define the triangles. Because without them all we have is points in space, we have to show how those points are connected and we connect them in triangles. You probably know that in a 3d model a square is made up of two triangles, that's what we need to define here. An important thing to remember is that with the mesh class you create your triangles by adding the three points clockwise, this defines which direction is solid on your mesh. If you find that you can see a mesh from the back but not from the front it's this. The first triangle we define is 0,1,2 referring to the first second and third vertices we defined. The next triangle is 0,2,3 the first, third and fourth vertices. (The reason the 0,1,2 is the 1st, 2nd and 3rd is because the computer refers to the first point made as point 0).

Sometimes it's easiest to explain on paper.
Then last of all we clear anything in the mesh to begin with, we set the mesh's vertices to ours (But we have to convert the list to an array with the .ToArray() command) and set the mesh's triangles to ours. Then unity does some work for us when we call the optimize command (This usually does nothing but it doesn't use any extra time so don't worry) and the recalculate normals command so that the normals are generated automatically.

And that's how you get a square on the screen.

Now let's put a texture on it. For that we'll be using the newUV list we defined. Because we're almost always going to be using a tilesheet for this sort of thing we'll get to that now. We'll use 32x32 size tiles and we'll have 4x4 tiles on the texture, that makes it a 128x128 sized texture.

Like this!
Drop that thing in your art folder. Now click on the texture file in unity because we're going to change some import settings. You need to set Texture type: Advanced, Generate Mipmaps: False, Filter Mode: Point, Format: ARGB 32 bit.

Like so!
Then drag and drop the texture onto your gameobject so that it uses that texture as its material.

Now that you have that we can get back to the code! For convenience we're going to define a float and change it in the start function. This is something I call a tUnit and it's the fraction of space 1 tile takes up out of the width of the texture. In our case it's 1/4 or 0.25. In addition we'll be adding the coordinates for two textures to test with.

 private float tUnit = 0.25f;
private Vector2 tStone = new Vector2 (0, 0);
private Vector2 tGrass = new Vector2 (0, 1);

When we create texture coordinates it's in tiles away from the texture's 0,0 and 0,0 is the bottom left. Assigning the texture to the polygon we just made is not too hard. Just add some code to define the texture coordinates with newUV right after newTriangles and don't forget to apply our UVs to the mesh:

void Start () {

mesh = GetComponent<MeshFilter> ().mesh;

float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;

newVertices.Add( new Vector3 (x , y , z ));
newVertices.Add( new Vector3 (x + 1 , y , z ));
newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
newVertices.Add( new Vector3 (x , y-1 , z ));

newTriangles.Add(0);
newTriangles.Add(1);
newTriangles.Add(3);
newTriangles.Add(1);
newTriangles.Add(2);
newTriangles.Add(3);

newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y + tUnit));
newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y + tUnit));
newUV.Add(new Vector2 (tUnit * tStone.x + tUnit, tUnit * tStone.y));
newUV.Add(new Vector2 (tUnit * tStone.x, tUnit * tStone.y));

mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray(); // add this line to the code here
mesh.Optimize ();
mesh.RecalculateNormals ();
}
You will have to add a light to the scene to be able to see it but your square should now be textured! Hell Yeah! The code to create the UV information is quite simple, it just defines what 4 corners of the texture you would like the four corners of the square to use. So, tUnit*tStone.x is the left of the tStone tile and tUnit*tStone.x+tUnit is the left plus one tile's width making it the tile's right. To match the order we made the vertices in we define the top left texture coordinate first, then the top right, then the bottom right and last the bottom left.

You should see this.
So that was a lot of work to make a textured square but I promise, this really is the backbone of the whole thing. Next time we'll do collision meshes and generating based on an array.

Please if you're interested in this tutorial leave a comment here or on g+ letting me know what you think, what needs more explanation etc. If you have a better suggestion for a part of my implementation let me know and if you have any problems I'll try and answer as soon as possible.

Bonus: Take this chunk out of the start function and put it in the update loop:

  mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();

Now you can change the values of newVertices while the game runs and it will move the cube's corners.

Edit: Feel free to follow me on twitter (@STV_Alex) or G+ to get updated when I post part two. Or check back here every day, that's good too.

Edit 2: Thanks to Taryndactyl for pointing out some errors. Those are now fixed, link.

Part 2
templates-office.com Tutorial, Unity, Voxel Tut, Voxels
Sabtu, 17 Agustus 2013

Grapple: an android game where you swing on a rope



Decided to upload an early version of Grapple for those that would like to try it. So far it has been testing on android JellyBean and IceCream Sandwich on two different phones. I haven't done much testing yet so I apologize if there are bugs for your device/version. Please do leave comments here, on reddit or on g+ if you try. Let me know what levels got you stuck or what levels weren't fun.

Known bugs:
-Grapple can misfire when pressing the drag button before fully releasing the tap to aim the grapple.
-Slow Mo when holding in before releasing a grapple will reduce velocity over time.
- Sometimes a level will start with the grapple already attached
- swinging hard into rock will often cause the ball to stick there (releasing the grapple or pulling yourself will unstick you).

This version is far from finished and not completely playable, the tutorial overlay is a 10 minute thing to be able to release this version and there are only 12 levels so far. Have fun.

http://www.mediafire.com/?8u4kgrarz4mhl2o 

templates-office.com Grapple
Jumat, 16 Agustus 2013

Something different

So I've been quiet all summer and now I'm back. Through the summer I haven't been working on the voxel game but on a summer project for mobile. I think releasing games is an important experience for a game dev and I think that a smaller game is what I need to complete and release a finished stand alone game.

The game I've been working on is a remake of a prototype I developed months ago in flash and I decided to try again in Unity. It's a very satisfying gameplay prototype where the player controls a ball by swinging from ropes. It's not that unique an idea but it's an implementation I have yet to see, at least on mobile.

It involves a lot of lines
My implementation makes ropes bend and wrap around obstacles. I've been working for a while to make this element as smooth as possible and the result is a very natural feeling effect. Also I've written physics code to handle the ball hanging on a rope which makes the core gameplay possible.

Once the rope can bend you can swing in ways never possible before.

As well as making a prototype I moved further, started adding art assets and making playable levels and the project is in alpha stage.

The ball wraps around small obstacles quickly.
Although I enjoy the core programming above all it is very satisfying to see the game become more polished with the addition of functional menus and the things a real game needs.


So this is my first attempt at mobile games but it's an interesting platform. It's hard to find a gameplay element that is any fun but I feel like this is pretty fun. I guess I'll get a clearer picture once I move on to playtesting, but I feel I should have more levels ready before then.

templates-office.com Grapple
Rabu, 07 Agustus 2013

Shooting and blasting - making a revolver

After the hacking and slaying it's time to move forward in time and get to the shooting and blasting - guns and rifles and those things in between. 

Note:
This is part of the 1 million views give away. The files attached to this blog post are free and contain the guns and rifles in inkscape's .svg format and as an exploded version to use as gun construction kit.



This is what we are after - simplified handguns and rifles to use in your game. 

Note:
In order to be easily readable I created these with outlines. You can take those off but will have to add additional shading to make up for the loss of detail. 

I took the revolver as a sample to show you how these assets were created. 



Even though I am not big on guns (and please forgive me if I named bits wrongly in the tutorial - as to me they are just 'bits of a gun'), I had a lot of fun creating these. I hope you enjoy the assets as well.



Free!



templates-office.com

Thank you! - the blog just reached 1 Million views



Thanks to all those who read my blog post [that have been spread way too far and there are still too few - or too many topics still to cover. I am working on it... but time seems to fly by too quickly.]

Anyway... I want to thank all of you and am working on a nice [well I hope it is] give away to celebrate reaching this mile stone. 

Sunny greetings,
Chris 
templates-office.com
Kamis, 20 Juni 2013

Running Sheep: Tiny Worlds

Domba bosan duduk-duduk di peternakan, sehingga mereka memutuskan untuk melakukan perjalanan! Ini petualangan yang menarik, tetapi berisiko. Hindari serigala dan lubang saat Anda membimbing domba untuk keselamatan! Download gratis permainan versi lengkap dan nikmati bermain tanpa batas!



Game Description:

Domba bosan duduk-duduk di peternakan, sehingga mereka memutuskan untuk melakukan perjalanan! Petualangan sepanjang perjalanan akan menarik, tetapi berisiko. Domba akan memerlukan asisten untuk menjadi pemandu!

Permainan ini merupakan brainteaser rumit yang terdiri dari labirin kecil yang diisi dengan berbagai rintangan. Yang paling berbahaya adalah serigala dan lubang yang akan menyebabkan pemain kehilangan domba. Tujuannya pemain adalah memandu domba dengan aman melalui labirin. Seperti penggembala memandu domba sepanjang jalur yang aman menuju tempat mereka, di mana mereka akan diselamatkan! Jumlah panah terbatas, jadi untuk membuat mereka bertahan dalam perjalanan, Anda harus menghapus panah yang digunakan dan digunakan di area yang lain.

Game ini merupakan game brainteaser yang mengasyikkan dengan kontrol sederhana, grafis hidup, dengan 250 level. Ini game yang menyenangkan untuk semua usia!

System Requirements:

- Windows 95/98/XP/ME/Vista/7;
- Processor 800 Mhz or better;
- RAM: minimum 1024Mb;
- DirectX 9.0 or higher;
- DirectX compatible sound board;
- Easy game removal through the Windows Control Panel.


File Size : 15 Mb
templates-office.com Arcade, PC Game

Super Bikes

Ambil bagian dalam kompetisi MotoRacing terbesar di permainan balap 3D Superbike ini. Download gratis permainan versi lengkap dan nikmati bermain tanpa batas!



Game Description:

3D Superbike racing game. Mainkan dalam mode balapan tunggal atau berkompetisi untuk hasil terbaik dalam mode Time Attack. Lakukan stunts untuk mendapatkan poin dan untuk mendapatkan waktu untuk menyelesaikan.

Download gratis permainan versi lengkap hari ini dan mengambil bagian dalam kompetisi MotoRacing.

Fitur Game:

- Real game balap Superbike;
- Grafis 3D;
- Extraordinarily adiktif;
- Soundtrack dan efek suara yang powerful;
- Simpan / Load game option;
- Statistik Pertandingan;
- Gratis permainan versi lengkap tanpa ada pembatasan.

System Requirements:

- Windows 95/98/XP/ME/Vista/7;
- Processor 800 Mhz or better;
- RAM: minimum 1024Mb;
- DirectX 9.0 or higher;
- DirectX compatible sound board;
- Easy game removal through the Windows Control Panel.


File Size : 42 Mb
templates-office.com PC Game, Racing

Star Gunner

Hanya satu Space Tank yang dapat menghancurkan serangan Space Shooter dan menyelamatkan galaksi. Download gratis permainan versi lengkap dan menikmati bermain tanpa batas!



Game Description:

Bersiaplah untuk misi galaksi!

Masa-masa sulit jatuh pada galaksi dan Anda adalah satu-satunya yang dapat meyelamatkannya. Masalahnya adalah bahwa Anda hanya memiliki satu Space Tank untuk menghancurkan serangan Space Shooter.

Planet-planet menunggu Anda untuk menyelamatkan mereka! Download gratis permainan versi lengkap dan memulai misi Anda!

Fitur Game:

- Unit musuh benar-benar asli;
- Tujuh misi besar dengan Boss besar di setiap akhir;
- Lebih dari 100 level;
- Gameplay unik dan benar-benar adiktif;
- Unik power-up;
- Simpan / Load pilihan permainan;
- Statistik Permainan;
- Gratis permainan versi lengkap tanpa ada pembatasan.

System Requirements:

- Windows 95/98/XP/ME/Vista/7;
- Processor 800 Mhz or better;
- RAM: minimum 1024Mb;
- DirectX 9.0 or higher;
- DirectX compatible sound board;
- Easy game removal through the Windows Control Panel.



File Size : 20 Mb
templates-office.com Action, PC Game, Shooter

Atlantis

Bantu Anna menelusuri peta kuno dan menaklukkan hambatan dalam cara untuk menemukan harta karun yang hilang di Atlantis kuno. Download gratis permainan versi lengkap dan menikmati bermain tanpa batas!



Game Description:

Peradaban kuno dan reruntuhan mereka memegang banyak rahasia dan misteri. Seorang arkeolog muda yang ambisius tiba-tiba menemukan sebuah fragmen dari peta yang hilang menuju harta di tanah yang paling misterius - Atlantis.

Tanpa ragu sedikit pun, Anna mulai perjalanannya, mengambil segala sesuatu yang dia butuhkan. Sebagai bagian dari pencarian, dia akan mengunjungi kota-kota kuno dan goa-goa yang sepi serta dasar laut dan gurun es.

Download game gratis ini full version pc dan bantulah Anna!

Fitur Game:

- 8 jenis objek permainan tersembunyi;
- Mode pencarian gratis yang memungkinkan Anda memilih setiap tingkat selesai untuk bermain;
- Berbagai penghargaan dan prestasi;
- Grafis yang indah;
- Temukan harta karun kuno.

System Requirements:

- Windows 95/98/XP/ME/Vista/7;
- Processor 800 Mhz or better;
- RAM: minimum 1024Mb;
- DirectX 9.0 or higher;
- DirectX compatible sound board;
- Easy game removal through the Windows Control Panel.



File Size : 70 Mb
templates-office.com Hidden Objects, PC Game, Puzzle

Exotic Farm

Panen buah-buahan eksotis, memelihara hewan, dan mengoperasikan mesin buatan tangan di Exotic Farm! Download gratis permainan versi lengkap dan menikmati bermain tanpa batas!



Game Description:

Mimpi Jill akhirnya menjadi kenyataan! Dia memiliki peternakannya sendiri di pulaunya sendiri. Apa yang bisa lebih baik daripada hidup di sebuah pulau terpencil dan indah berada di tengah laut! Bergabunglah sebagai Jill, yang baru memperoleh pulau pertaniannya. Penuh dengan buah-buahan dan sayuran lokal ini adalah salah satu pulau yang paling menakjubkan di laut!

Sementara di pulau Anda akan membantu Jill tumbuh dan menjaga buah-buahan eksotis dan membantu memelihara hewan yang bebas berkeliaran yang mengisi pulau. Dia juga akan membutuhkan bantuan untuk menjalankan semua yang mesin pertanian di Exotic Farm. Gunakan keterampilan manajemen waktu yang hebat Anda untuk memberikan target sehingga Anda dapat membeli mesin-mesin baru. Ini adalah salah satu peternakan pekerja keras. Download game gratis hari ini dan bekerja peternakan Anda di bawah matahari yang indah dan membantu Jill dengan Manajemen Waktu Anda.

Fitur Game:

- Pilih mesin baru untuk pertanian;
- Lokasi pulau yang indah;
- Menjadi petani yang sukses;
- Mengelola tanaman Anda;
- Mengelola buah-buahan dan hewan.

System Requirements:

- Windows 95/98/XP/ME/Vista/7;
- Processor 800 Mhz or better;
- RAM: minimum 1024Mb;
- DirectX 9.0 or higher;
- DirectX compatible sound board;
- Easy game removal through the Windows Control Panel.



File Size : 68 Mb
templates-office.com Arcade, PC Game, Simulation