- Home /
Question by
Theacesofspades · Jan 11, 2015 at 07:08 PM ·
c#colliderruntimevoxelmesh collider
Mesh collider not working correctly on voxel terrain
So i have been trying to make a voxel world and I have it generating perfectly but the only problem i am having right now is generating the collider. For some reason it is all wacky and only generates some of the collider or will generate the collider where the mesh isnt. Can anyone take a look at this? I know its sort of a lot but im not sure what else to post so thank you.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ChunkScript : MonoBehaviour
{
public byte[ , , ] data;
public List<Vector2> allTextures = new List<Vector2> ();
public int chunkSizeX = 50;
public int chunkSizeY = 50;
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
private Mesh mesh;
public float texturesAcross = 4;
private float tUnit;
private int faceCount;
public List<Vector3> colVertices = new List<Vector3>();
public List<int> colTriangles = new List<int>();
private int colCount;
private MeshCollider col;
public float scale1 = 10;
public float height1 = 3;
public float power1 = 1;
public int startHeight2 = 300;
public float scale2 = 20;
public float height2 = 4;
public float power2 = 0;
public int addition2 = 10;
public bool update=false;
void Start ()
{
tUnit = 1 / texturesAcross;
mesh = GetComponent<MeshFilter> ().mesh;
col = GetComponent<MeshCollider> ();
GenerateMesh ();
}
void Update ()
{
if(update)
{
UpdateBlockSides();
update=false;
}
}
void OnGUI ()
{
GUILayout.Label ("Scale: ");
scale1 = GUILayout.HorizontalSlider (scale1, 0, 100);
GUILayout.Label ("Height: ");
height1 = GUILayout.HorizontalSlider (height1, 0, chunkSizeY-1);
GUILayout.Label ("Power: ");
power1 = GUILayout.HorizontalSlider (power1, 0, 2);
if (GUILayout.Button("GenerateMesh"))
{
GenerateMesh ();
}
}
void GenerateMesh ()
{
data = new byte[chunkSizeX, chunkSizeY, chunkSizeX];
for (int x=0; x<data.GetLength(0); x++)
{
for (int z=0; z<data.GetLength(2); z++)
{
int grass = PerlinNoise(x,0,z,scale1,height1,power1);
grass += PerlinNoise(x,startHeight2,z,scale2,height2,power2)+addition2;
grass += 3;
int dirt = PerlinNoise(x,0,z,scale1,height1,power1);
dirt += PerlinNoise(x,startHeight2,z,scale2,height2,power2)+addition2;
dirt += 2;
int stone = PerlinNoise(x,0,z,scale1,height1,power1);
stone += PerlinNoise(x,startHeight2,z,scale2,height2,power2)+addition2;
stone += 0;
for (int y=0; y<data.GetLength(1); y++)
{
if (y < grass)
{
data[x, y, z] = 1;
}
if (y < dirt)
{
data[x, y, z] = 2;
}
if (y < stone)
{
data[x, y, z] = 3;
}
}
}
}
UpdateBlockSides ();
}
int PerlinNoise(int x,int y, int z, float scale, float height, float power)
{
float rValue;
rValue=Noise.GetNoise (((double)x) / scale, ((double)y)/ scale, ((double)z) / scale);
rValue*=height;
if(power!=0)
{
rValue=Mathf.Pow( rValue, power);
}
return (int) rValue;
}
void UpdateBlockSides ()
{
for (int x=0; x<data.GetLength(0); x++)
{
for (int y=0; y<data.GetLength(1); y++)
{
for (int z=0; z<data.GetLength(2); z++)
{
if (data[x, y, z] != 0)
{
GenerateCollider (x, y, z);
if (Block (x+1, y, z) == 0)
{
CubeEast(x, y, z, data[x, y, z]);
}
if (Block (x-1, y, z) == 0)
{
CubeWest(x, y, z, data[x, y, z]);
}
if (Block (x, y+1, z) == 0)
{
CubeTop(x, y, z, data[x, y, z]);
}
if (Block (x, y-1, z) == 0)
{
CubeBottom(x, y, z, data[x, y, z]);
}
if (Block (x, y, z+1) == 0)
{
CubeNorth(x, y, z, data[x, y, z]);
}
if (Block (x, y, z-1) == 0)
{
CubeSouth(x, y, z, data[x, y, z]);
}
}
}
}
}
UpdateMesh ();
}
void CubeNorth (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x + 1, y-1, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x, y-1, z + 1));
Vector2 texturePos;
texturePos = allTextures[block];
CreateTexture (texturePos);
}
void CubeSouth (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y - 1, 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));
Vector2 texturePos;
texturePos = allTextures[block];
CreateTexture (texturePos);
}
void CubeEast (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x + 1, y - 1, z));
newVertices.Add(new Vector3 (x + 1, y, z));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y - 1, z + 1));
Vector2 texturePos;
texturePos = allTextures[block];
CreateTexture (texturePos);
}
void CubeWest (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y- 1, z + 1));
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x, y, z));
newVertices.Add(new Vector3 (x, y - 1, z));
Vector2 texturePos;
texturePos = allTextures[block];
CreateTexture (texturePos);
}
void CubeTop (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z + 1));
newVertices.Add(new Vector3 (x + 1, y, z ));
newVertices.Add(new Vector3 (x, y, z ));
Vector2 texturePos;
texturePos = allTextures[block];
CreateTexture (texturePos);
}
void CubeBottom (int x, int y, int z, byte block)
{
newVertices.Add(new Vector3 (x, y-1, z ));
newVertices.Add(new Vector3 (x + 1, y-1, z ));
newVertices.Add(new Vector3 (x + 1, y-1, z + 1));
newVertices.Add(new Vector3 (x, y-1, z + 1));
Vector2 texturePos;
texturePos = allTextures[block];
CreateTexture (texturePos);
}
void CreateTexture (Vector2 texturePos)
{
newTriangles.Add(faceCount * 4 ); //1
newTriangles.Add(faceCount * 4 + 1 ); //2
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 ); //1
newTriangles.Add(faceCount * 4 + 2 ); //3
newTriangles.Add(faceCount * 4 + 3 ); //4
newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y));
newUV.Add(new Vector2 (tUnit * texturePos.x + tUnit, tUnit * texturePos.y + tUnit));
newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y + tUnit));
newUV.Add(new Vector2 (tUnit * texturePos.x, tUnit * texturePos.y));
faceCount ++;
}
void UpdateMesh ()
{
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.uv = newUV.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
faceCount=0;
newVertices.Clear();
newTriangles.Clear();
newUV.Clear();
Mesh newMesh = new Mesh();
newMesh.vertices = colVertices.ToArray();
newMesh.triangles = colTriangles.ToArray();
col.sharedMesh= newMesh;
colVertices.Clear();
colTriangles.Clear();
colCount=0;
}
void ColliderTriangles()
{
colTriangles.Add(colCount*4);
colTriangles.Add((colCount*4)+1);
colTriangles.Add((colCount*4)+3);
colTriangles.Add((colCount*4)+1);
colTriangles.Add((colCount*4)+2);
colTriangles.Add((colCount*4)+3);
}
void GenerateCollider(int x, int y, int z)
{
//Top
if(Block(x,y+1,z)==0){
colVertices.Add( new Vector3 (x , y , 1));
colVertices.Add( new Vector3 (x + 1 , y , 1));
colVertices.Add( new Vector3 (x + 1 , y , 0 ));
colVertices.Add( new Vector3 (x , y , 0 ));
ColliderTriangles();
colCount++;
}
//bot
if(Block(x,y-1,z)==0){
colVertices.Add( new Vector3 (x , y -1 , 0));
colVertices.Add( new Vector3 (x + 1 , y -1 , 0));
colVertices.Add( new Vector3 (x + 1 , y -1 , 1 ));
colVertices.Add( new Vector3 (x , y -1 , 1 ));
ColliderTriangles();
colCount++;
}
//left
if(Block(x-1,y,z)==0){
colVertices.Add( new Vector3 (x , y -1 , 1));
colVertices.Add( new Vector3 (x , y , 1));
colVertices.Add( new Vector3 (x , y , 0 ));
colVertices.Add( new Vector3 (x , y -1 , 0 ));
ColliderTriangles();
colCount++;
}
//right
if(Block(x+1,y,z)==0){
colVertices.Add( new Vector3 (x +1 , y , 1));
colVertices.Add( new Vector3 (x +1 , y -1 , 1));
colVertices.Add( new Vector3 (x +1 , y -1 , 0 ));
colVertices.Add( new Vector3 (x +1 , y , 0 ));
ColliderTriangles();
colCount++;
}
//Front
if(Block(x,y,z+1)==0){
colVertices.Add(new Vector3 (x + 1, y-1, z + 1));
colVertices.Add(new Vector3 (x + 1, y, z + 1));
colVertices.Add(new Vector3 (x, y, z + 1));
colVertices.Add(new Vector3 (x, y-1, z + 1));
ColliderTriangles();
colCount++;
}
//Back
if(Block(x,y,z-1)==0){
colVertices.Add(new Vector3 (x, y - 1, z));
colVertices.Add(new Vector3 (x, y, z));
colVertices.Add(new Vector3 (x + 1, y, z));
colVertices.Add(new Vector3 (x + 1, y - 1, z));
ColliderTriangles();
colCount++;
}
}
public byte Block(int x, int y, int z)
{
if( x>=data.GetLength(0) || x<0 || y>=data.GetLength(0) || y<0 || z>=data.GetLength(0) || z<0)
{
return (byte) 1;
}
return data[x,y,z];
}
}
Comment
Best Answer
Answer by Theacesofspades · Jan 11, 2015 at 08:37 PM
I had to add z to all of the vertecies.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to create a custom cuboids at runtime? 1 Answer
Object hover over collider during runtime? 2 Answers
Runtime mesh decimation 1 Answer