Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class World_Chunks : MonoBehaviour
- {
- public List<MP_Chunk> thisChunk = new List<MP_Chunk>();
- IEnumerator Start()
- {
- GameObject.DestroyImmediate(GetComponent<MeshCollider>());
- GameObject.DestroyImmediate(GetComponent<MeshFilter>());
- gameObject.AddComponent<MeshCollider>();
- gameObject.AddComponent<MeshFilter>();
- for (int x = 0; x < World_ChunkLoader.instance.chunk_X_Width; x++)
- {
- for (int z = 0; z < World_ChunkLoader.instance.chunk_Z_Width; z++)
- {
- MP_Chunk tempchunk = new MP_Chunk();
- tempchunk.x = x;
- tempchunk.y = 0;
- tempchunk.z = z;
- tempchunk.isFloor = true;
- tempchunk.Color_Top = new Color(1, 1, 1);
- tempchunk.Color_Bottom = new Color(1, 1, 1);
- tempchunk.Color_Left = new Color(1, 1, 1);
- tempchunk.Color_Right = new Color(1, 1, 1);
- tempchunk.Color_Back = new Color(1, 1, 1);
- tempchunk.Color_Front = new Color(1, 1, 1);
- thisChunk.Add (tempchunk);
- }
- }
- yield return StartCoroutine (FloorkRender ());
- }
- IEnumerator FloorkRender()
- {
- Mesh mesh = GetComponent<MeshFilter>().mesh;
- List<Vector3> vertices = new List<Vector3>();
- List<Vector2> uvs = new List<Vector2>();
- List<int> triangles = new List<int>();
- List<Color> colors = new List<Color>();
- int vertexIndex;
- vertexIndex = 0;
- mesh.Clear();
- foreach(MP_Chunk tempchunk in thisChunk)
- {
- int Render_Top;
- int Render_Bottom;
- int Render_Left;
- int Render_Right;
- int Render_Front;
- int Render_Back;
- if(tempchunk.isFloor)
- {
- Render_Top = 1;
- Render_Bottom = 0;
- Render_Left = 0;
- Render_Right = 0;
- Render_Front = 0;
- Render_Back = 0;
- }
- else
- {
- if (BlockExist (tempchunk.x, tempchunk.y + 1, tempchunk.z)) {Render_Top = 0;} else{Render_Top = 1;}
- if (BlockExist (tempchunk.x, tempchunk.y - 1, tempchunk.z)) {Render_Bottom = 0;} else{Render_Bottom = 1;}
- if (BlockExist (tempchunk.x - 1, tempchunk.y, tempchunk.z)) {Render_Left = 0;} else{Render_Left = 1;}
- if (BlockExist (tempchunk.x + 1, tempchunk.y, tempchunk.z)) {Render_Right = 0;} else{Render_Right = 1;}
- if (BlockExist (tempchunk.x, tempchunk.y, tempchunk.z + 1)) {Render_Front = 0;} else{Render_Front = 1;}
- if (BlockExist (tempchunk.x, tempchunk.y, tempchunk.z - 1)) {Render_Back = 0;} else{Render_Back = 1;}
- }
- if(Render_Top == 1)
- {
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y + 1, tempchunk.z ));
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y + 1, tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z ));
- colors.Add (tempchunk.Color_Top);
- colors.Add (tempchunk.Color_Top);
- colors.Add (tempchunk.Color_Top);
- colors.Add (tempchunk.Color_Top);
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex+1);
- triangles.Add(vertexIndex+2);
- // second triangle for the block top
- triangles.Add(vertexIndex+2);
- triangles.Add(vertexIndex+3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2(0, 0));
- uvs.Add(new Vector2(0, 1));
- uvs.Add(new Vector2(1, 1));
- uvs.Add(new Vector2(1, 0));
- vertexIndex += 4;
- }
- if(Render_Bottom == 1)
- {
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y, tempchunk.z ));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y, tempchunk.z ));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y, tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y, tempchunk.z + 1));
- colors.Add (tempchunk.Color_Bottom);
- colors.Add (tempchunk.Color_Bottom);
- colors.Add (tempchunk.Color_Bottom);
- colors.Add (tempchunk.Color_Bottom);
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex+1);
- triangles.Add(vertexIndex+2);
- // second triangle for the block top
- triangles.Add(vertexIndex+2);
- triangles.Add(vertexIndex+3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2(0, 0));
- uvs.Add(new Vector2(0, 1));
- uvs.Add(new Vector2(1, 1));
- uvs.Add(new Vector2(1, 0));
- vertexIndex += 4;
- }
- if(Render_Left == 1)
- {
- vertices.Add(new Vector3(tempchunk.x, tempchunk.y , tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x, tempchunk.y + 1, tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x, tempchunk.y + 1, tempchunk.z ));
- vertices.Add(new Vector3(tempchunk.x, tempchunk.y , tempchunk.z ));
- colors.Add (tempchunk.Color_Left);
- colors.Add (tempchunk.Color_Left);
- colors.Add (tempchunk.Color_Left);
- colors.Add (tempchunk.Color_Left);
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex+1);
- triangles.Add(vertexIndex+2);
- // second triangle for the block top
- triangles.Add(vertexIndex+2);
- triangles.Add(vertexIndex+3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2(0, 0));
- uvs.Add(new Vector2(0, 1));
- uvs.Add(new Vector2(1, 1));
- uvs.Add(new Vector2(1, 0));
- vertexIndex += 4;
- }
- if(Render_Right == 1)
- {
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y , tempchunk.z ));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z ));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y , tempchunk.z + 1));
- colors.Add (tempchunk.Color_Right);
- colors.Add (tempchunk.Color_Right);
- colors.Add (tempchunk.Color_Right);
- colors.Add (tempchunk.Color_Right);
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex+1);
- triangles.Add(vertexIndex+2);
- // second triangle for the block top
- triangles.Add(vertexIndex+2);
- triangles.Add(vertexIndex+3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2(0, 0));
- uvs.Add(new Vector2(0, 1));
- uvs.Add(new Vector2(1, 1));
- uvs.Add(new Vector2(1, 0));
- vertexIndex += 4;
- }
- if(Render_Front == 1)
- {
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y , tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y , tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z + 1));
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y + 1, tempchunk.z + 1));
- colors.Add (tempchunk.Color_Front);
- colors.Add (tempchunk.Color_Front);
- colors.Add (tempchunk.Color_Front);
- colors.Add (tempchunk.Color_Front);
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex+1);
- triangles.Add(vertexIndex+2);
- // second triangle for the block top
- triangles.Add(vertexIndex+2);
- triangles.Add(vertexIndex+3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2(0, 0));
- uvs.Add(new Vector2(0, 1));
- uvs.Add(new Vector2(1, 1));
- uvs.Add(new Vector2(1, 0));
- vertexIndex += 4;
- }
- if(Render_Back == 1)
- {
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y , tempchunk.z));
- vertices.Add(new Vector3(tempchunk.x , tempchunk.y + 1, tempchunk.z));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y + 1, tempchunk.z));
- vertices.Add(new Vector3(tempchunk.x + 1, tempchunk.y , tempchunk.z));
- colors.Add (tempchunk.Color_Back);
- colors.Add (tempchunk.Color_Back);
- colors.Add (tempchunk.Color_Back);
- colors.Add (tempchunk.Color_Back);
- // first triangle for the block top
- triangles.Add(vertexIndex);
- triangles.Add(vertexIndex+1);
- triangles.Add(vertexIndex+2);
- // second triangle for the block top
- triangles.Add(vertexIndex+2);
- triangles.Add(vertexIndex+3);
- triangles.Add(vertexIndex);
- // add UV
- uvs.Add(new Vector2(0, 0));
- uvs.Add(new Vector2(0, 1));
- uvs.Add(new Vector2(1, 1));
- uvs.Add(new Vector2(1, 0));
- vertexIndex += 4;
- }
- }
- // Build the Mesh:
- mesh.vertices = vertices.ToArray();
- mesh.triangles = triangles.ToArray();
- mesh.colors = colors.ToArray ();
- mesh.uv = uvs.ToArray();
- //mesh.Optimize();
- mesh.RecalculateNormals();
- // update mesh collider
- GetComponent<MeshCollider>().sharedMesh = null;
- GetComponent<MeshCollider>().sharedMesh = mesh;
- yield return null;
- }
- bool BlockExist(int x, int y, int z)
- {
- foreach(MP_Chunk tempchunk in thisChunk)
- {
- if(tempchunk.x == x && tempchunk.y == y && tempchunk.z == z)
- {
- return true;
- }
- }
- return false;
- }
- public void ChangeColor(int iTriangle, Color iColor)
- {
- if(iTriangle % 2 == 1)
- iTriangle--;
- Mesh mesh = GetComponent<MeshFilter>().mesh;
- Color[] colors = mesh.colors;
- int iStart = mesh.triangles[iTriangle*3];
- for (int i = iStart; i < iStart+4; i++)
- colors[i] = iColor;
- mesh.colors = colors;
- }
- void Update()
- {
- int hx;
- int hy;
- int hz;
- if(Input.GetMouseButtonDown (0))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100))
- {
- if(this.name == hit.collider.gameObject.name)
- {
- //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 ());
- hx = (int)Mathf.Floor((hit.point.x + (hit.normal.x/10) - hit.collider.transform.position.x));
- hy = (int)Mathf.Floor((hit.point.y + (hit.normal.y/10) - hit.collider.transform.position.y)); // rounds down for accuracy
- hz = (int)Mathf.Floor((hit.point.z + (hit.normal.z/10) - hit.collider.transform.position.z));
- MP_Chunk tempchunk = new MP_Chunk();
- tempchunk.x = hx;
- tempchunk.y = hy;
- tempchunk.z = hz;
- tempchunk.isFloor = false;
- tempchunk.Color_Top = new Color(1, 1, 1);
- tempchunk.Color_Bottom = new Color(1, 1, 1);
- tempchunk.Color_Left = new Color(1, 1, 1);
- tempchunk.Color_Right = new Color(1, 1, 1);
- tempchunk.Color_Back = new Color(1, 1, 1);
- tempchunk.Color_Front = new Color(1, 1, 1);
- thisChunk.Add (tempchunk);
- StartCoroutine(FloorkRender ());
- }
- }
- }
- if(Input.GetMouseButtonDown (1))
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100))
- {
- if(this.name == hit.collider.gameObject.name)
- {
- //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 ());
- hx = (int)Mathf.Floor((hit.point.x + (hit.normal.x/10) - hit.collider.transform.position.x));
- hy = (int)Mathf.Floor((hit.point.y + (hit.normal.y/10) - hit.collider.transform.position.y)); // rounds down for accuracy
- hz = (int)Mathf.Floor((hit.point.z + (hit.normal.z/10) - hit.collider.transform.position.z));
- //Debug.Log (hit.triangleIndex.ToString ());
- foreach(MP_Chunk tempchunk in thisChunk)
- {
- if(tempchunk.x == hx && tempchunk.y == hy && tempchunk.z == hz)
- Debug.Log ("Treffer");
- }
- }
- }
- }
- if(World_ChunkLoader.instance.playerLoaded)
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- RaycastHit hit;
- if (Physics.Raycast(ray, out hit, 100))
- {
- if(this.name == hit.collider.gameObject.name)
- {
- ChangeColor(hit.triangleIndex, new Color(0, 0, 0));
- }
- }
- }
- }
- }
- public class MP_Chunk
- {
- public int x;
- public int y;
- public int z;
- public bool isFloor;
- public Color Color_Top;
- public Color Color_Bottom;
- public Color Color_Left;
- public Color Color_Right;
- public Color Color_Front;
- public Color Color_Back;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement