Wolfos

Voxel Terrain

Feb 21st, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class VoxTerrain{
  6.    
  7.     public static int width = 10;
  8.     public static int height = 10;
  9.     public static int depth = 10;
  10.     public float cubesize = 1;
  11.     public float[,,] terrainarray = new float[width,height,depth];
  12.     public GameObject terrain = new GameObject("Terrain");
  13.    
  14.     public VoxTerrain(){
  15.        
  16.     }  
  17.     // Use this for initialization
  18.     void Start () {
  19.     }
  20.    
  21.     // Update is called once per frame
  22.     void Update () {
  23.     }
  24.    
  25.    
  26.     public void GenerateEmpty(){
  27.         terrain.transform.position=new Vector3(0,0,0);
  28.         for(int x = 0; x < width; x++){
  29.             for(int y = 0; y < height; y++){
  30.                 for(int z = 0; z < height; z++){
  31.                     terrainarray[x,y,z]=0F;
  32.                 }
  33.             }
  34.         }
  35.         for(int x = 0; x < width; x++){
  36.             for(int y = 0; y < height-5; y++){
  37.                 for(int z = 0; z < height; z++){
  38.                     terrainarray[x,y,z]=1F;
  39.                 }
  40.             }
  41.         }
  42.         RenderCubes();
  43.     }
  44.    
  45.    
  46.    
  47.     void MakeTriangles(){
  48.         for(int x = 0; x < width; x++){
  49.             for(int y = 0; y < height; y++){
  50.                 for(int z = 0; z < height; z++){
  51.                    
  52.                 }
  53.             }
  54.         }
  55.     }
  56.    
  57.     void addnewcube(){
  58.         //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  59.     }
  60.    
  61.     void RenderCubes(){
  62.         for(int x = 0; x < width; x++){
  63.             //CombineInstance[] combine = new CombineInstance[width*height*depth];
  64.             for(int y = 0; y < height; y++){
  65.                 for(int z = 0; z < height; z++){
  66.                     if(terrainarray[x,y,z]==1){
  67.                         /*if(terrainarray[x+1,y,z]==0 || terrainarray[x,y+1,z]==0 || terrainarray[x,y,z+1]==0
  68.                     ||terrainarray[x+1,y+1,z]==0 || terrainarray[x,y+1,z+1]==0 || terrainarray[x+1,y,z+1]==0 || terrainarray[x+1,y+1,z+1]==0||terrainarray[x-1,y,z]==0 || terrainarray[x,y-1,z]==0 || terrainarray[x,y,z-1]==0||terrainarray[x-1,y-1,z]==0 || terrainarray[x,y-1,z+1]==0 || terrainarray[x-1,y,z+1]==0 || terrainarray[x-1,y-1,z-1]==0){*/
  69.                         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
  70.                         cube.transform.position = new Vector3(x*cubesize,y*cubesize,z*cubesize);
  71.                         cube.transform.parent = terrain.transform;
  72.                         //MeshFilter meshfilter = cube.GetComponent<MeshFilter>();
  73.                         //meshfilter.sharedMesh.CombineMeshes(combine);
  74.                         //}
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.    
  81.    
  82.     void RenderTriangle(Vector3 x, Vector3 y, Vector3 z){
  83.         GL.PushMatrix();
  84.         GL.Begin(GL.TRIANGLES);
  85.         GL.Vertex3(x.x,x.y,x.z);
  86.         GL.Vertex3(y.x,y.y,y.z);
  87.         GL.Vertex3(z.x,z.y,z.z);
  88.         GL.End();
  89.         GL.PopMatrix();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment