Esempio n. 1
0
        // ---------------------------------------------------------------------------------------------
        //
        // ---------------------------------------------------------------------------------------------
        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);
        }
Esempio n. 2
0
        // ---------------------------------------------------------------------------------------------
        private void createDefaultLevel()
        {
            VoxelUtils.VoxelVector3Int pos = VoxelUtils.convertVector3ToVoxelVector3Int(Vector3.zero);
            VoxelUtils.VoxelChunk      vc;

            if (_isExperimentalChunk)
            {
                // create the full chunk voxel
                vc = createVoxelChunk(pos, VoxelUtils.MAX_CHUNK_UNITS, VoxelUtils.MAX_CHUNK_UNITS, VoxelUtils.MAX_CHUNK_UNITS);
                _aVoxelChunks.Add(vc);
                setVoxelChunkMesh(vc);
            }
            else
            {
                int half = VoxelUtils.MAX_CHUNK_UNITS / 2;

                vc = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.x += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z = 0;
                vc    = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.x  = 0;
                pos.y += half;

                vc = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.x += half;
                vc     = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                pos.z = 0;
                vc    = createVoxelChunk(pos, half, half, half);
                _aVoxelChunks.Add(vc);

                // create room in center of level
                //subtractChunk (new Vector3 (32, 34, 32), new Vector3 (8, 4, 8));
                subtractChunk(new Vector3(64, 68, 64), new Vector3(16, 8, 16));
            }
        }
        // ---------------------------------------------------------------------------------------------
        public void addCube(VoxelUtils.VoxelVector3Int pos, Vector3 size)
        {
            GameObject go = Instantiate(prefabCube);

            go.name = "x" + pos.x.ToString() + "-" + "y" + pos.y.ToString() + "-" + "z" + pos.z.ToString();
            go.transform.SetParent(container);

            float width  = size.x * VoxelUtils.CHUNK_SIZE;
            float height = size.y * VoxelUtils.CHUNK_SIZE;
            float depth  = size.z * VoxelUtils.CHUNK_SIZE;

            go.transform.localScale = new Vector3(width, height, depth);

            Vector3 posNew = new Vector3((pos.x * VoxelUtils.CHUNK_SIZE) + (width / 2f), (pos.y * VoxelUtils.CHUNK_SIZE) + (height / 2f), (pos.z * VoxelUtils.CHUNK_SIZE) + (depth / 2f));

            go.transform.localPosition = posNew;

            _aCubes.Add(go);
        }
Esempio n. 4
0
        // ---------------------------------------------------------------------------------------------
        // 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);
        }
Esempio n. 5
0
        // ---------------------------------------------------------------------------------------------
        // 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);
        }