// --------------------------------------------------------------------------------------------- private void separateVoxelChunkAlongZAxis(int index, int pointCutZ) { //Debug.Log (" ->separateVoxelChunkAlongZAxis "+pointCutZ.ToString()); VoxelUtils.VoxelChunk vc = _aVoxelChunks [index]; int materialIndex = vc.materialIndex; //Debug.Log ("materialIndex: "+materialIndex); Destroy(vc.go); _aVoxelChunks.RemoveAt(index); int frontZ = vc.corners.bot_left_front.z; int depth1 = Mathf.Abs(pointCutZ - frontZ); // create front part VoxelUtils.VoxelChunk vsFront = createVoxelChunk(vc.pos, vc.size.x, vc.size.y, depth1); vsFront.materialIndex = materialIndex; _aVoxelChunks.Add(vsFront); // create back part vc.pos.z += depth1; int depth2 = vc.size.z - depth1; VoxelUtils.VoxelChunk vsBack = createVoxelChunk(vc.pos, vc.size.x, vc.size.y, depth2); vsBack.materialIndex = materialIndex; _aVoxelChunks.Add(vsBack); }
// --------------------------------------------------------------------------------------------- // // --------------------------------------------------------------------------------------------- private VoxelUtils.VoxelChunk createCutVoxelChunk(VoxelUtils.VoxelVector3Int p, int w, int h, int d) { //if (_isExperimentalChunk) { //Debug.Log ("createCutVoxelChunk " + p.ToString () + ", " + w + ", " + h + ", " + d); //} float width = w * VoxelUtils.CHUNK_SIZE; float height = h * VoxelUtils.CHUNK_SIZE; float depth = d * VoxelUtils.CHUNK_SIZE; Vector3 pos = new Vector3((p.x * VoxelUtils.CHUNK_SIZE) + (width / 2f), (p.y * VoxelUtils.CHUNK_SIZE) + (height / 2f), (p.z * VoxelUtils.CHUNK_SIZE) + (depth / 2f)); Bounds b = new Bounds(); //coll.bounds; b.size = new Vector3(width - VoxelUtils.CHUNK_SIZE, height - VoxelUtils.CHUNK_SIZE, depth - VoxelUtils.CHUNK_SIZE); b.center = pos; VoxelUtils.VoxelChunk vc = new VoxelUtils.VoxelChunk(); vc.pos = p; vc.size = new VoxelUtils.VoxelVector3Int(w, h, d); vc.bounds = b; vc.corners = VoxelUtils.createVoxelCorners(p, w, h, d); vc.materialIndex = 0; vc.meshCreated = false; return(vc); }
// --------------------------------------------------------------------------------------------- private void separateVoxelChunkAlongYAxis(int index, int pointCutY) { //Debug.Log (" ->separateVoxelChunkAlongYAxis "+pointCutY.ToString()); VoxelUtils.VoxelChunk vc = _aVoxelChunks [index]; int materialIndex = vc.materialIndex; //Debug.Log ("materialIndex: "+materialIndex); Destroy(vc.go); _aVoxelChunks.RemoveAt(index); int botY = vc.corners.bot_left_front.y; int height1 = Mathf.Abs(pointCutY - botY); // create bottom part VoxelUtils.VoxelChunk vsBottom = createVoxelChunk(vc.pos, vc.size.x, height1, vc.size.z); vsBottom.materialIndex = materialIndex; _aVoxelChunks.Add(vsBottom); // create top part vc.pos.y += height1; int height2 = vc.size.y - height1; VoxelUtils.VoxelChunk vsTop = createVoxelChunk(vc.pos, vc.size.x, height2, vc.size.z); vsTop.materialIndex = materialIndex; _aVoxelChunks.Add(vsTop); }
// --------------------------------------------------------------------------------------------- // // --------------------------------------------------------------------------------------------- private void separateVoxelChunkAlongXAxis(int index, int pointCutX) { //Debug.Log (" ->separateVoxelChunkAlongXAxis "+pointCutX.ToString()); VoxelUtils.VoxelChunk vc = _aVoxelChunks [index]; int materialIndex = vc.materialIndex; //Debug.Log ("materialIndex: "+materialIndex); Destroy(vc.go); _aVoxelChunks.RemoveAt(index); int leftX = vc.corners.bot_left_front.x; int width1 = Mathf.Abs(pointCutX - leftX); // create left part VoxelUtils.VoxelChunk vsLeft = createVoxelChunk(vc.pos, width1, vc.size.y, vc.size.z); vsLeft.materialIndex = materialIndex; _aVoxelChunks.Add(vsLeft); // create right part vc.pos.x += width1; int width2 = vc.size.x - width1; VoxelUtils.VoxelChunk vsRight = createVoxelChunk(vc.pos, width2, vc.size.y, vc.size.z); vsRight.materialIndex = materialIndex; _aVoxelChunks.Add(vsRight); }
// --------------------------------------------------------------------------------------------- // split them voxels one at a time // --------------------------------------------------------------------------------------------- private bool splitVoxels(VoxelUtils.VoxelChunk vsCut) { bool intersectDetected = false; int i, len = _aVoxelChunks.Count; for (i = 0; i < len; ++i) { // do a bounds intersect check first if (vsCut.bounds.Intersects(_aVoxelChunks[i].bounds)) { //Debug.Log ("bounds intersect detected: "+_aVoxelChunks[i].go.name); // check for identical size and position if (_aVoxelChunks [i].Identical(vsCut)) { //Debug.LogWarning (" ->IDENTICAL: "+i+" - "+_aVoxelChunks [i].go.name); Destroy(_aVoxelChunks [i].go); _aVoxelChunks.RemoveAt(i); intersectDetected = true; } // check for identical size and position else if (_aVoxelChunks [i].Encased(vsCut)) { //Debug.LogWarning (" ->ENCASED!"); Destroy(_aVoxelChunks [i].go); _aVoxelChunks.RemoveAt(i); intersectDetected = true; } else if (checkVoxelChunkIntersectX(i, vsCut)) { intersectDetected = true; } else if (checkVoxelChunkIntersectY(i, vsCut)) { intersectDetected = true; } else if (checkVoxelChunkIntersectZ(i, vsCut)) { intersectDetected = true; } } if (intersectDetected) { break; } } return(intersectDetected); }
// --------------------------------------------------------------------------------------------- // CHECK ALL 8 CORNERS FOR INTERSECTION ALONG Z AXIS // --------------------------------------------------------------------------------------------- private bool checkVoxelChunkIntersectZ(int index, VoxelUtils.VoxelChunk vsCut) { bool intersectDetected = false; VoxelUtils.VoxelChunk vs = _aVoxelChunks [index]; if (_aVoxelChunks [index].IntersectsBackZ(vsCut.corners.bot_left_front)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.bot_left_front.z); intersectDetected = true; } else if (_aVoxelChunks [index].IntersectsBackZ(vsCut.corners.top_left_front)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.top_left_front.z); intersectDetected = true; } else if (_aVoxelChunks [index].IntersectsBackZ(vsCut.corners.top_right_front)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.top_right_front.z); intersectDetected = true; } else if (_aVoxelChunks [index].IntersectsBackZ(vsCut.corners.bot_right_front)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.bot_right_front.z); intersectDetected = true; } // else if (_aVoxelChunks [index].IntersectsFrontZ(vsCut.corners.bot_left_back)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.bot_left_back.z + 1); intersectDetected = true; } else if (_aVoxelChunks [index].IntersectsFrontZ(vsCut.corners.top_left_back)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.top_left_back.z + 1); intersectDetected = true; } else if (_aVoxelChunks [index].IntersectsFrontZ(vsCut.corners.top_right_back)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.top_right_back.z + 1); intersectDetected = true; } else if (_aVoxelChunks [index].IntersectsFrontZ(vsCut.corners.bot_right_back)) { separateVoxelChunkAlongZAxis(index, vsCut.corners.bot_right_back.z + 1); intersectDetected = true; } return(intersectDetected); }
// --------------------------------------------------------------------------------------------- public void paint(RaycastHit hit, Vector3 chunkSize, int materialIndex) { //if (AppController.Instance.appState == AppState.Paint) { //chunkSize.z = 1; //} dig(hit, chunkSize); VoxelUtils.VoxelChunk vc = createVoxelChunk(VoxelUtils.convertVector3ToVoxelVector3Int(_lastChunkPos), (int)chunkSize.x, (int)chunkSize.y, (int)chunkSize.z); vc.materialIndex = materialIndex; _aVoxelChunks.Add(vc); setVoxelChunkMesh(vc); }
// --------------------------------------------------------------------------------------------- public void build(RaycastHit hit, Vector3 chunkSize, int materialIndex) { _lastChunkPos = getHitChunkPos(hit); if (hit.normal.x != 0) { if (hit.normal.x > 0) { _lastChunkPos.x += 1; } else { _lastChunkPos.x -= chunkSize.x; } } else if (hit.normal.y != 0) { if (hit.normal.y > 0) { _lastChunkPos.y += 1; } else { _lastChunkPos.y -= chunkSize.y; } } else if (hit.normal.z != 0) { if (hit.normal.z > 0) { _lastChunkPos.z += 1; } else { _lastChunkPos.z -= chunkSize.z; } } subtractChunk(_lastChunkPos, chunkSize); VoxelUtils.VoxelChunk vc = createVoxelChunk(VoxelUtils.convertVector3ToVoxelVector3Int(_lastChunkPos), (int)chunkSize.x, (int)chunkSize.y, (int)chunkSize.z); vc.materialIndex = materialIndex; _aVoxelChunks.Add(vc); setVoxelChunkMesh(vc); }
// --------------------------------------------------------------------------------------------- // // --------------------------------------------------------------------------------------------- public void setVoxelChunkMesh(VoxelUtils.VoxelChunk vc) { vc.go.transform.localPosition = vc.goPos; Mesh mesh = vc.go.GetComponent <MeshFilter> ().mesh; VoxelChunkMesh.create(mesh, vc.size.x * VoxelUtils.CHUNK_SIZE, vc.size.y * VoxelUtils.CHUNK_SIZE, vc.size.z * VoxelUtils.CHUNK_SIZE, vc.size.x, vc.size.y, vc.size.z, false); BoxCollider coll = vc.go.GetComponent <BoxCollider> (); coll.size = new Vector3(vc.size.x * VoxelUtils.CHUNK_SIZE, vc.size.y * VoxelUtils.CHUNK_SIZE, vc.size.z * VoxelUtils.CHUNK_SIZE); Renderer renderer = vc.go.GetComponent <Renderer> (); if (!_isExperimentalChunk) { renderer.sharedMaterial = LevelEditor.Instance.aMaterials [vc.materialIndex]; } }
// --------------------------------------------------------------------------------------------- // create single voxel // --------------------------------------------------------------------------------------------- public VoxelUtils.VoxelChunk createVoxelChunk(VoxelUtils.VoxelVector3Int p, int w, int h, int d) { //if (_isExperimentalChunk) { // Debug.Log ("createVoxelChunk " + p.ToString () + ", " + w + ", " + h + ", " + d); //} GameObject cube = AssetFactory.Instance.createVoxelChunkClone(); cube.transform.SetParent(_trfmVoxels); _iVoxelCount++; cube.name = "voxchunk_" + _iVoxelCount.ToString(); //if (_isExperimentalChunk) { // Debug.Log ("cube: " + cube.name + ", " + _trfmVoxels.name); //} float width = w * VoxelUtils.CHUNK_SIZE; float height = h * VoxelUtils.CHUNK_SIZE; float depth = d * VoxelUtils.CHUNK_SIZE; Vector3 pos = new Vector3((p.x * VoxelUtils.CHUNK_SIZE) + (width / 2f), (p.y * VoxelUtils.CHUNK_SIZE) + (height / 2f), (p.z * VoxelUtils.CHUNK_SIZE) + (depth / 2f)); Bounds b = new Bounds(); b.size = new Vector3(width, height, depth); b.center = pos; VoxelUtils.VoxelChunk vc = new VoxelUtils.VoxelChunk(); vc.go = cube; vc.goPos = pos; vc.pos = p; vc.size = new VoxelUtils.VoxelVector3Int(w, h, d); vc.bounds = b; vc.corners = VoxelUtils.createVoxelCorners(p, w, h, d); vc.materialIndex = 0; vc.meshCreated = false; return(vc); }
// --------------------------------------------------------------------------------------------- // cut a hole! // --------------------------------------------------------------------------------------------- public bool subtractChunk(Vector3 v3Pos, Vector3 v3Size, bool allowUndo = true) { bool success = true; float timer = Time.realtimeSinceStartup; if (allowUndo && !_isExperimentalChunk) { LevelEditor.Instance.resetUndoActions(); saveCurrentVoxelChunks(); MainMenu.Instance.setUndoButton(true); } VoxelUtils.VoxelVector3Int pos = VoxelUtils.convertVector3ToVoxelVector3Int(v3Pos); VoxelUtils.VoxelChunk vsCut = createCutVoxelChunk(pos, (int)v3Size.x, (int)v3Size.y, (int)v3Size.z); //if (!_isExperimentalChunk) { _levelMap.addCube(pos, new Vector3((int)v3Size.x, (int)v3Size.y, (int)v3Size.z)); //} // does the new voxel intersect with any existing voxels? bool splittage = splitVoxels(vsCut); int loops = 0; while (splittage && loops < 1000) { splittage = splitVoxels(vsCut); loops++; } if (loops >= 1000) { Debug.LogWarning("looks like we got ourselves an endless loop here!"); success = false; } else { VoxelUtils.VoxelChunk vc; int i, len = _aVoxelChunks.Count; //int count = 0; for (i = 0; i < len; ++i) { vc = _aVoxelChunks [i]; if (!vc.meshCreated) { setVoxelChunkMesh(vc); vc.meshCreated = true; //count++; _aVoxelChunks [i] = vc; } } //if (_isExperimentalChunk) { // Debug.Log ("num voxels: " + len + " - loops: " + loops + " - meshes created: " + count); //} } //if (_isExperimentalChunk) { // Debug.Log ("Time to create chunk(s): " + (Time.realtimeSinceStartup - timer).ToString ()); //} if (!_isExperimentalChunk) { MainMenu.Instance.setCubeCountText("Voxel Chunks: " + _aVoxelChunks.Count.ToString()); } return(success); }
// private void createLevel(LevelFile levelFile) { if (levelFile.fileFormatVersion != Globals.levelSaveFormatVersion) { AppController.Instance.showPopup(PopupMode.Notification, "Warning", Globals.warningObsoleteFileFormat); return; } currentLevelId = levelFile.levelId; MainMenu.Instance.setLevelNameText(levelFile.levelName); lastLevelName = levelFile.levelName; LevelEditor levelEditor = LevelEditor.Instance; PropsManager propsManager = PropsManager.Instance; VoxelsLevelChunk levelChunk = levelEditor.curVoxelsLevelChunk; levelEditor.resetAll(); Vector3 savedPos = new Vector3(levelFile.playerPosition.x, levelFile.playerPosition.y, levelFile.playerPosition.z); Vector3 savedRot = new Vector3(levelFile.playerEuler.x, levelFile.playerEuler.y, levelFile.playerEuler.z); levelChunk.setStartPos(savedPos, savedRot); FlyCam.Instance.setNewInitialPosition(savedPos, savedRot); FlyCam.Instance.reset(); //GameObject goQuadrant; //Transform trfmContainer; //GameObject container; Vector3 pos = Vector3.zero; int quadLen = levelEditor.cubesPerQuadrant; float fRockSize = levelEditor.fRockSize; int i, len = levelFile.levelVoxelChunks.Count; Debug.Log("levelFile.levelVoxelChunks.Count: " + levelFile.levelVoxelChunks.Count); for (i = 0; i < len; ++i) { pos.x = (int)levelFile.levelVoxelChunks[i].position.x; pos.y = (int)levelFile.levelVoxelChunks[i].position.y; pos.z = (int)levelFile.levelVoxelChunks[i].position.z; VoxelUtils.VoxelChunk vc = levelEditor.curVoxelsLevelChunk.createVoxelChunk( VoxelUtils.convertVector3ToVoxelVector3Int(pos), (int)levelFile.levelVoxelChunks[i].size.x, (int)levelFile.levelVoxelChunks[i].size.y, (int)levelFile.levelVoxelChunks[i].size.z ); vc.materialIndex = levelFile.levelVoxelChunks [i].materialId; levelEditor.curVoxelsLevelChunk.aVoxelChunks.Add(vc); levelEditor.curVoxelsLevelChunk.setVoxelChunkMesh(vc); } MainMenu.Instance.setCubeCountText("Voxel Chunks: " + levelEditor.curVoxelsLevelChunk.aVoxelChunks.Count.ToString()); if (levelFile.levelProps != null) { LevelProp levelProp; GameObject goProp; Quaternion rotation = Quaternion.identity; propDef prop; string name; len = levelFile.levelProps.Count; for (i = 0; i < len; ++i) { levelProp = levelFile.levelProps [i]; pos.x = levelProp.position.x; pos.y = levelProp.position.y; pos.z = levelProp.position.z; prop = propsManager.getPropDefForId(levelProp.id); if (prop.id != -1) { name = prop.name + "_" + levelChunk.trfmProps.childCount; goProp = propsManager.createProp(prop, pos, name, levelChunk.trfmProps, prop.useCollider, prop.useGravity); rotation.w = levelProp.rotation.w; rotation.x = levelProp.rotation.x; rotation.y = levelProp.rotation.y; rotation.z = levelProp.rotation.z; goProp.transform.rotation = rotation; levelChunk.addWorldProp(prop.id, goProp); } } } }