// instantiate all chunks void Awake() { multiplier = axisMax / size; int y = 0; int counter = 0; for (int z = 0; z < sizeZ; z++) { for (int x = 0; x < sizeX; x++) { Transform clone; clone = Instantiate(chunk, transform.position + new Vector3(x, 0, z) * axisMax, transform.rotation) as Transform; clone.parent = transform; clone.name = "zChunk_" + clone.position.x + "_" + clone.position.y + "_" + clone.position.z; // set offset MarchingCubesC other = clone.gameObject.GetComponent <MarchingCubesC>(); other.offset = new Vector3(x * (size - 1), y * (size - 1), z * (size - 1)); //Debug.Log(other.offset + ", " + new Vector3(x, y, z)); counter++; } } // get list of our neighbours foreach (Transform child in transform) // Downcast notice..uh.. { MarchingCubesC childChunk = child.gameObject.GetComponent <MarchingCubesC>(); Vector3 currentPos = child.position; ///multiplier; // scan forward String searchName = "zChunk_" + currentPos.x + "_" + currentPos.y + "_" + (currentPos.z + axisMax); GameObject neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[0] = neighbour.transform; } // scan forward-right searchName = "zChunk_" + (currentPos.x + axisMax) + "_" + currentPos.y + "_" + (currentPos.z + axisMax); neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[1] = neighbour.transform; } // scan right searchName = "zChunk_" + (currentPos.x + axisMax) + "_" + currentPos.y + "_" + currentPos.z; neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[2] = neighbour.transform; } // scan backward-right searchName = "zChunk_" + (currentPos.x + axisMax) + "_" + currentPos.y + "_" + (currentPos.z - axisMax); neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[3] = neighbour.transform; } // scan backward searchName = "zChunk_" + currentPos.x + "_" + currentPos.y + "_" + (currentPos.z - axisMax); neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[4] = neighbour.transform; } // scan backward-left searchName = "zChunk_" + (currentPos.x - axisMax) + "_" + currentPos.y + "_" + (currentPos.z - axisMax); neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[5] = neighbour.transform; } // scan left searchName = "zChunk_" + (currentPos.x - axisMax) + "_" + currentPos.y + "_" + currentPos.z; neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[6] = neighbour.transform; } // scan forward-left searchName = "zChunk_" + (currentPos.x - axisMax) + "_" + currentPos.y + "_" + (currentPos.z + axisMax); neighbour = GameObject.Find(searchName); if (neighbour != null) { childChunk.neighbours[7] = neighbour.transform; } } }
void Update() { // add block .. with mouseclick if (Input.GetMouseButton(0)) // button is held down { // TODO: layermask for ray, hit only terrain Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 9999)) { lastHit = hit.point; // get object reference Transform chunk = hit.transform; Vector3 localhit = chunk.InverseTransformPoint(lastHit); int hx = (int)(localhit.x / multiplier); int hy = (int)(localhit.y / multiplier); // -0.2 is temporary fix for clicking top plane and getting too big values.. int hz = (int)(localhit.z / multiplier); infoText.text = "Name:" + chunk.name + " Hit:" + hx + "," + hy + "," + hz; // show laser lineRenderer.SetPosition(0, Camera.main.transform.position + new Vector3(0, -1, 0)); lineRenderer.SetPosition(1, lastHit); MarchingCubesC editChunk = chunk.gameObject.GetComponent <MarchingCubesC>(); MarchingCubesC targetChunk; float modifyPower = 0; if (Input.GetKey(KeyCode.LeftShift)) // shift pressed, remove { modifyPower = removeStrength; } else { // add modifyPower = addStrength; } editChunk.ModifyChunkAdd(hx + size * hy + size2 * hz, modifyPower); editChunk.UpdateChunk(); // check diagonals and other neighbours (if we need to affect them) if (hx == border.x && hz == border.z) // modify to forward right { if (editChunk.neighbours[1] != null) { targetChunk = editChunk.neighbours[1].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd(0 + size * hy + size2 * 0, modifyPower); targetChunk.UpdateChunk(); } } if (hx == border.x && hz == 0) // modify to back right { if (editChunk.neighbours[3] != null) { targetChunk = editChunk.neighbours[3].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(0 + size * hy + size2 * border.z), modifyPower); targetChunk.UpdateChunk(); } } if (hx == 0 && hz == 0) // modify to back left { if (editChunk.neighbours[5] != null) { targetChunk = editChunk.neighbours[5].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(border.x + size * hy + size2 * border.z), modifyPower); targetChunk.UpdateChunk(); } } if (hx == 0 && hz == border.z) // modify to forward left { if (editChunk.neighbours[7] != null) { targetChunk = editChunk.neighbours[7].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(border.x + size * hy + size2 * 0), modifyPower); targetChunk.UpdateChunk(); } } if (hx == border.x) // modify to RIGHT { if (editChunk.neighbours[2] != null) { targetChunk = editChunk.neighbours[2].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(0 + size * hy + size2 * hz), modifyPower); targetChunk.UpdateChunk(); } } if (hx == 0) // modify to LEFT { if (editChunk.neighbours[6] != null) { targetChunk = editChunk.neighbours[6].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(border.x + size * hy + size2 * hz), modifyPower); targetChunk.UpdateChunk(); } } if (hz == border.z) // modify to forward { if (editChunk.neighbours[0] != null) { targetChunk = editChunk.neighbours[0].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(hx + size * hy + size2 * 0), modifyPower); targetChunk.UpdateChunk(); } } if (hz == 0) // modify to backward { if (editChunk.neighbours[4] != null) { targetChunk = editChunk.neighbours[4].gameObject.GetComponent <MarchingCubesC>(); targetChunk.ModifyChunkAdd((int)(hx + size * hy + size2 * border.z), modifyPower); targetChunk.UpdateChunk(); } } } } if (Input.GetMouseButtonUp(0)) // button released { lineRenderer.SetPosition(0, Camera.main.transform.position); lineRenderer.SetPosition(1, Camera.main.transform.position); } if (Input.GetKey("r")) // reset scene { Application.LoadLevel(0); } }