Advertisement
Guest User

Unity Chunk

a guest
Feb 2nd, 2013
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.45 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class World_Chunks : MonoBehaviour
  6. {
  7.     public List<MP_Chunk> thisChunk = new List<MP_Chunk>();
  8.    
  9.     IEnumerator Start()
  10.     {
  11.         GameObject.DestroyImmediate(GetComponent<MeshCollider>());
  12.         GameObject.DestroyImmediate(GetComponent<MeshFilter>());
  13.        
  14.         gameObject.AddComponent<MeshCollider>();
  15.         gameObject.AddComponent<MeshFilter>();
  16.        
  17.         for (int x = 0; x < World_ChunkLoader.instance.chunk_X_Width; x++)
  18.         {
  19.             for (int z = 0; z < World_ChunkLoader.instance.chunk_Z_Width; z++)
  20.             {
  21.                 MP_Chunk tempchunk = new MP_Chunk();
  22.                 tempchunk.x        = x;
  23.                 tempchunk.y        = 0;
  24.                 tempchunk.z        = z;
  25.                 tempchunk.isFloor  = true;
  26.                
  27.                 tempchunk.Color_Top     = new Color(1, 1, 1);
  28.                 tempchunk.Color_Bottom  = new Color(1, 1, 1);
  29.                 tempchunk.Color_Left    = new Color(1, 1, 1);
  30.                 tempchunk.Color_Right   = new Color(1, 1, 1);
  31.                 tempchunk.Color_Back    = new Color(1, 1, 1);
  32.                 tempchunk.Color_Front   = new Color(1, 1, 1);
  33.                
  34.                 thisChunk.Add (tempchunk);
  35.             }
  36.         }
  37.        
  38.         yield return StartCoroutine (FloorkRender ());
  39.     }
  40.    
  41.     IEnumerator FloorkRender()
  42.     {
  43.         Mesh          mesh      = GetComponent<MeshFilter>().mesh;
  44.         List<Vector3> vertices  = new List<Vector3>();
  45.         List<Vector2> uvs       = new List<Vector2>();
  46.         List<int>     triangles = new List<int>();
  47.         List<Color>   colors    = new List<Color>();
  48.         int           vertexIndex;
  49.         vertexIndex = 0;
  50.         mesh.Clear();
  51.        
  52.         foreach(MP_Chunk tempchunk in thisChunk)
  53.         {
  54.             int Render_Top;
  55.             int Render_Bottom;
  56.             int Render_Left;
  57.             int Render_Right;
  58.             int Render_Front;
  59.             int Render_Back;
  60.            
  61.             if(tempchunk.isFloor)
  62.             {
  63.                 Render_Top = 1;
  64.                 Render_Bottom = 0;
  65.                 Render_Left = 0;
  66.                 Render_Right = 0;
  67.                 Render_Front = 0;
  68.                 Render_Back = 0;
  69.             }
  70.             else
  71.             {
  72.                 if (BlockExist (tempchunk.x, tempchunk.y + 1, tempchunk.z)) {Render_Top = 0;}    else{Render_Top = 1;}
  73.                 if (BlockExist (tempchunk.x, tempchunk.y - 1, tempchunk.z)) {Render_Bottom = 0;} else{Render_Bottom = 1;}
  74.                 if (BlockExist (tempchunk.x - 1, tempchunk.y, tempchunk.z)) {Render_Left = 0;}   else{Render_Left = 1;}
  75.                 if (BlockExist (tempchunk.x + 1, tempchunk.y, tempchunk.z)) {Render_Right = 0;}  else{Render_Right = 1;}
  76.                 if (BlockExist (tempchunk.x, tempchunk.y, tempchunk.z + 1)) {Render_Front = 0;}  else{Render_Front = 1;}
  77.                 if (BlockExist (tempchunk.x, tempchunk.y, tempchunk.z - 1)) {Render_Back = 0;}   else{Render_Back = 1;}
  78.             }
  79.            
  80.             if(Render_Top == 1)
  81.             {
  82.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y + 1, tempchunk.z    ));
  83.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y + 1, tempchunk.z + 1));
  84.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z + 1));
  85.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z    ));
  86.                
  87.                 colors.Add (tempchunk.Color_Top);
  88.                 colors.Add (tempchunk.Color_Top);
  89.                 colors.Add (tempchunk.Color_Top);
  90.                 colors.Add (tempchunk.Color_Top);
  91.                
  92.                 // first triangle for the block top
  93.                 triangles.Add(vertexIndex);
  94.                 triangles.Add(vertexIndex+1);
  95.                 triangles.Add(vertexIndex+2);
  96.                    
  97.                 // second triangle for the block top
  98.                 triangles.Add(vertexIndex+2);
  99.                 triangles.Add(vertexIndex+3);
  100.                 triangles.Add(vertexIndex);
  101.                        
  102.                 // add UV
  103.                 uvs.Add(new Vector2(0, 0));
  104.                 uvs.Add(new Vector2(0, 1));
  105.                 uvs.Add(new Vector2(1, 1));
  106.                 uvs.Add(new Vector2(1, 0));
  107.                 vertexIndex += 4;
  108.             }
  109.            
  110.             if(Render_Bottom == 1)
  111.             {
  112.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y, tempchunk.z    ));
  113.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y, tempchunk.z    ));
  114.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y, tempchunk.z + 1));
  115.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y, tempchunk.z + 1));
  116.                
  117.                 colors.Add (tempchunk.Color_Bottom);
  118.                 colors.Add (tempchunk.Color_Bottom);
  119.                 colors.Add (tempchunk.Color_Bottom);
  120.                 colors.Add (tempchunk.Color_Bottom);
  121.                
  122.                 // first triangle for the block top
  123.                 triangles.Add(vertexIndex);
  124.                 triangles.Add(vertexIndex+1);
  125.                 triangles.Add(vertexIndex+2);
  126.                    
  127.                 // second triangle for the block top
  128.                 triangles.Add(vertexIndex+2);
  129.                 triangles.Add(vertexIndex+3);
  130.                 triangles.Add(vertexIndex);
  131.                        
  132.                 // add UV
  133.                 uvs.Add(new Vector2(0, 0));
  134.                 uvs.Add(new Vector2(0, 1));
  135.                 uvs.Add(new Vector2(1, 1));
  136.                 uvs.Add(new Vector2(1, 0));
  137.                 vertexIndex += 4;
  138.             }
  139.            
  140.             if(Render_Left == 1)
  141.             {
  142.                 vertices.Add(new Vector3(tempchunk.x, tempchunk.y    , tempchunk.z + 1));
  143.                 vertices.Add(new Vector3(tempchunk.x, tempchunk.y + 1, tempchunk.z + 1));
  144.                 vertices.Add(new Vector3(tempchunk.x, tempchunk.y + 1, tempchunk.z    ));
  145.                 vertices.Add(new Vector3(tempchunk.x, tempchunk.y    , tempchunk.z    ));
  146.                
  147.                 colors.Add (tempchunk.Color_Left);
  148.                 colors.Add (tempchunk.Color_Left);
  149.                 colors.Add (tempchunk.Color_Left);
  150.                 colors.Add (tempchunk.Color_Left);
  151.                
  152.                 // first triangle for the block top
  153.                 triangles.Add(vertexIndex);
  154.                 triangles.Add(vertexIndex+1);
  155.                 triangles.Add(vertexIndex+2);
  156.                    
  157.                 // second triangle for the block top
  158.                 triangles.Add(vertexIndex+2);
  159.                 triangles.Add(vertexIndex+3);
  160.                 triangles.Add(vertexIndex);
  161.                        
  162.                 // add UV
  163.                 uvs.Add(new Vector2(0, 0));
  164.                 uvs.Add(new Vector2(0, 1));
  165.                 uvs.Add(new Vector2(1, 1));
  166.                 uvs.Add(new Vector2(1, 0));
  167.                 vertexIndex += 4;
  168.             }
  169.            
  170.             if(Render_Right == 1)
  171.             {
  172.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y    , tempchunk.z    ));
  173.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z    ));
  174.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z + 1));
  175.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y    , tempchunk.z + 1));
  176.                
  177.                 colors.Add (tempchunk.Color_Right);
  178.                 colors.Add (tempchunk.Color_Right);
  179.                 colors.Add (tempchunk.Color_Right);
  180.                 colors.Add (tempchunk.Color_Right);
  181.                
  182.                 // first triangle for the block top
  183.                 triangles.Add(vertexIndex);
  184.                 triangles.Add(vertexIndex+1);
  185.                 triangles.Add(vertexIndex+2);
  186.                    
  187.                 // second triangle for the block top
  188.                 triangles.Add(vertexIndex+2);
  189.                 triangles.Add(vertexIndex+3);
  190.                 triangles.Add(vertexIndex);
  191.                        
  192.                 // add UV
  193.                 uvs.Add(new Vector2(0, 0));
  194.                 uvs.Add(new Vector2(0, 1));
  195.                 uvs.Add(new Vector2(1, 1));
  196.                 uvs.Add(new Vector2(1, 0));
  197.                 vertexIndex += 4;
  198.             }
  199.            
  200.             if(Render_Front == 1)
  201.             {
  202.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y    , tempchunk.z + 1));
  203.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y    , tempchunk.z + 1));
  204.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z + 1));
  205.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y + 1, tempchunk.z + 1));
  206.                
  207.                 colors.Add (tempchunk.Color_Front);
  208.                 colors.Add (tempchunk.Color_Front);
  209.                 colors.Add (tempchunk.Color_Front);
  210.                 colors.Add (tempchunk.Color_Front);
  211.                
  212.                 // first triangle for the block top
  213.                 triangles.Add(vertexIndex);
  214.                 triangles.Add(vertexIndex+1);
  215.                 triangles.Add(vertexIndex+2);
  216.                    
  217.                 // second triangle for the block top
  218.                 triangles.Add(vertexIndex+2);
  219.                 triangles.Add(vertexIndex+3);
  220.                 triangles.Add(vertexIndex);
  221.                        
  222.                 // add UV
  223.                 uvs.Add(new Vector2(0, 0));
  224.                 uvs.Add(new Vector2(0, 1));
  225.                 uvs.Add(new Vector2(1, 1));
  226.                 uvs.Add(new Vector2(1, 0));
  227.                 vertexIndex += 4;
  228.             }
  229.            
  230.             if(Render_Back == 1)
  231.             {
  232.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y    , tempchunk.z));
  233.                 vertices.Add(new Vector3(tempchunk.x    , tempchunk.y + 1, tempchunk.z));
  234.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z));
  235.                 vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y    , tempchunk.z));
  236.                
  237.                 colors.Add (tempchunk.Color_Back);
  238.                 colors.Add (tempchunk.Color_Back);
  239.                 colors.Add (tempchunk.Color_Back);
  240.                 colors.Add (tempchunk.Color_Back);
  241.                
  242.                 // first triangle for the block top
  243.                 triangles.Add(vertexIndex);
  244.                 triangles.Add(vertexIndex+1);
  245.                 triangles.Add(vertexIndex+2);
  246.                    
  247.                 // second triangle for the block top
  248.                 triangles.Add(vertexIndex+2);
  249.                 triangles.Add(vertexIndex+3);
  250.                 triangles.Add(vertexIndex);
  251.                        
  252.                 // add UV
  253.                 uvs.Add(new Vector2(0, 0));
  254.                 uvs.Add(new Vector2(0, 1));
  255.                 uvs.Add(new Vector2(1, 1));
  256.                 uvs.Add(new Vector2(1, 0));
  257.                 vertexIndex += 4;
  258.             }
  259.         }
  260.        
  261.         // Build the Mesh:
  262.         mesh.vertices = vertices.ToArray();
  263.         mesh.triangles = triangles.ToArray();
  264.         mesh.colors = colors.ToArray ();
  265.         mesh.uv = uvs.ToArray();
  266.         //mesh.Optimize();
  267.         mesh.RecalculateNormals();
  268.        
  269.         // update mesh collider
  270.         GetComponent<MeshCollider>().sharedMesh = null;
  271.         GetComponent<MeshCollider>().sharedMesh = mesh;
  272.         yield return null;
  273.     }
  274.    
  275.     bool BlockExist(int x, int y, int z)
  276.     {
  277.         foreach(MP_Chunk tempchunk in thisChunk)
  278.         {
  279.             if(tempchunk.x == x && tempchunk.y == y && tempchunk.z == z)
  280.             {
  281.                 return true;
  282.             }
  283.         }
  284.         return false;
  285.     }
  286.    
  287.     public void ChangeColor(int iTriangle, Color iColor)
  288.     {
  289.         if(iTriangle % 2 == 1)
  290.             iTriangle--;
  291.         Mesh          mesh      = GetComponent<MeshFilter>().mesh;
  292.         Color[] colors = mesh.colors;
  293.         int iStart = mesh.triangles[iTriangle*3];
  294.         for (int i = iStart; i < iStart+4; i++)
  295.             colors[i] = iColor;
  296.         mesh.colors = colors;
  297.     }
  298.    
  299.     void Update()
  300.     {
  301.         int hx;
  302.         int hy;
  303.         int hz;
  304.            
  305.         if(Input.GetMouseButtonDown (0))
  306.         {
  307.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  308.             RaycastHit hit;
  309.             if (Physics.Raycast(ray, out hit, 100))
  310.             {
  311.                 if(this.name == hit.collider.gameObject.name)
  312.                 {
  313.                     //Debug.Log ((Mathf.Floor(hit.point.x + (hit.normal.x/10)) - hit.collider.transform.position.x).ToString () + " - " + Mathf.Floor(hit.point.y + (hit.normal.y+2/10)).ToString () + " - " + Mathf.Floor(hit.point.z + (hit.normal.z/10)).ToString ());
  314.                     hx = (int)Mathf.Floor((hit.point.x + (hit.normal.x/10) - hit.collider.transform.position.x));
  315.                     hy = (int)Mathf.Floor((hit.point.y + (hit.normal.y/10) - hit.collider.transform.position.y)); // rounds down for accuracy
  316.                     hz = (int)Mathf.Floor((hit.point.z + (hit.normal.z/10) - hit.collider.transform.position.z));
  317.                    
  318.                     MP_Chunk tempchunk = new MP_Chunk();
  319.                     tempchunk.x = hx;
  320.                     tempchunk.y = hy;
  321.                     tempchunk.z = hz;
  322.                     tempchunk.isFloor = false;
  323.                     tempchunk.Color_Top     = new Color(1, 1, 1);
  324.                     tempchunk.Color_Bottom  = new Color(1, 1, 1);
  325.                     tempchunk.Color_Left    = new Color(1, 1, 1);
  326.                     tempchunk.Color_Right   = new Color(1, 1, 1);
  327.                     tempchunk.Color_Back    = new Color(1, 1, 1);
  328.                     tempchunk.Color_Front   = new Color(1, 1, 1);
  329.                     thisChunk.Add (tempchunk);
  330.                     StartCoroutine(FloorkRender ());
  331.                 }
  332.             }
  333.         }
  334.        
  335.         if(Input.GetMouseButtonDown (1))
  336.         {
  337.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  338.             RaycastHit hit;
  339.             if (Physics.Raycast(ray, out hit, 100))
  340.             {
  341.                 if(this.name == hit.collider.gameObject.name)
  342.                 {
  343.                     //Debug.Log ((Mathf.Floor(hit.point.x + (hit.normal.x/10)) - hit.collider.transform.position.x).ToString () + " - " + Mathf.Floor(hit.point.y + (hit.normal.y+2/10)).ToString () + " - " + Mathf.Floor(hit.point.z + (hit.normal.z/10)).ToString ());
  344.                     hx = (int)Mathf.Floor((hit.point.x + (hit.normal.x/10) - hit.collider.transform.position.x));
  345.                     hy = (int)Mathf.Floor((hit.point.y + (hit.normal.y/10) - hit.collider.transform.position.y)); // rounds down for accuracy
  346.                     hz = (int)Mathf.Floor((hit.point.z + (hit.normal.z/10) - hit.collider.transform.position.z));
  347.                    
  348.                     //Debug.Log (hit.triangleIndex.ToString ());
  349.                     foreach(MP_Chunk tempchunk in thisChunk)
  350.                     {
  351.                         if(tempchunk.x == hx && tempchunk.y == hy && tempchunk.z == hz)
  352.                             Debug.Log ("Treffer");
  353.                     }
  354.                 }
  355.             }
  356.         }
  357.        
  358.         if(World_ChunkLoader.instance.playerLoaded)
  359.         {
  360.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  361.             RaycastHit hit;
  362.             if (Physics.Raycast(ray, out hit, 100))
  363.             {
  364.                 if(this.name == hit.collider.gameObject.name)
  365.                 {
  366.                     ChangeColor(hit.triangleIndex, new Color(0, 0, 0));
  367.                 }
  368.             }
  369.         }
  370.     }
  371. }
  372.  
  373. public class MP_Chunk
  374. {
  375.     public int       x;
  376.     public int       y;
  377.     public int       z;
  378.     public bool      isFloor;
  379.    
  380.     public Color      Color_Top;
  381.     public Color      Color_Bottom;
  382.     public Color      Color_Left;
  383.     public Color      Color_Right;
  384.     public Color      Color_Front;
  385.     public Color      Color_Back;
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement