- Home /
What is a simple way to make a voxel face?
Hi im trying to learn voxel and I keep looking at all these tuts and they are good but its to much going on at once to understand. So I was wondering if there was a very straightforward way to just make one simple face and put a material on it. The material would be a public variable that you set in unity. Thanks ahead of time :) o and one more thing c# lol
Answer by jmgek · Dec 27, 2014 at 11:33 PM
So I would be able to pass you the code to make a simple polly and attach a material but your problem would not be solved. I have been working with procedural gen for about 2 months now and IT is HARD! the first thing you need to do is start learning vertex math, you need to know wheere a 3D point in space is in order to make your code both modular and easy to work with. I would start with a simple cube you can find almost on any tutorial. start with perimatives and then you can start making pollys. But again it is tough to do!
i well figure the rest out I just want to know what part of the code is actually doing something the tuts a confusing because they add all kinds of extra stuff and don't explain what the code is doing so thats why I am asking just for one face with a material attached to it
Yeah I know but it's not as simple as you think it is. You can make a simple polly easily with 10 lines of code with 3 verts but you need to understand how to make many. So you need to understand vertex math in order to loop and create pollys attached to pollys.You will need to understand the system inorder to make something out of it.
void GenSquare(int x, int y, Vector2 texture)
{
float side = Random.Range (0.1f, 1f);
newVertices.Add( new Vector3 (x , y , 0 ));
newVertices.Add( new Vector3 (x + side , y , 0 ));
newVertices.Add( new Vector3 (x + 1 , y-1 , 0 ));
newVertices.Add( new Vector3 (x , y-1 , 0 ));
newTriangles.Add(squareCount*4);
newTriangles.Add((squareCount*4)+1);
newTriangles.Add((squareCount*4)+3);
newTriangles.Add((squareCount*4)+1);
newTriangles.Add((squareCount*4)+2);
newTriangles.Add((squareCount*4)+3);
newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
newUV.Add(new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
newUV.Add(new Vector2 (tUnit * texture.x, tUnit * texture.y));
squareCount++;
}
void Update$$anonymous$$esh () {
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
newVertices.Clear();
newTriangles.Clear();
newUV.Clear();
squareCount=0;
$$anonymous$$esh new$$anonymous$$esh = new $$anonymous$$esh();
new$$anonymous$$esh.vertices = colVertices.ToArray();
new$$anonymous$$esh.triangles = colTriangles.ToArray();
col.shared$$anonymous$$esh= new$$anonymous$$esh;
colVertices.Clear();
colTriangles.Clear();
colCount=0;
}
Thats fine can you add the variables to this script I well read it a few times to understand it.
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
public List<Vector3> colVertices = new List<Vector3>();
public List<int> colTriangles = new List<int>();
private int colCount;
private $$anonymous$$esh mesh;
private $$anonymous$$eshCollider col;
private float tUnit = 0.25f;
private Vector2 tStone = new Vector2 (1, 0);
private Vector2 tGrass = new Vector2 (0, 1);
public byte[,] blocks;
private int squareCount;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Grid based water system 0 Answers
Create a circle using cubes? 3 Answers