示例#1
0
        ushort GetVoxelForNeighbor(Direction dir, int x, int y, int z)
        {
            Chunk n = neighborChunks[(int)dir];

            if (n == null)
            {
                return(ushort.MaxValue);
            }
            return(n.GetVoxel(x, y, z));
        }
示例#2
0
        // a raycast which returns the index of the hit voxel and the gameobject of the hit chunk
        public static VoxelInfo VoxelRaycast(Vector3 origin, Vector3 direction, float range, bool ignoreTransparent)
        {
            RaycastHit hit = new RaycastHit();

            if (!Physics.Raycast(origin, direction, out hit, range))
            {
                return(null);
            }

            GameObject g = hit.collider.gameObject;

            if (g.GetComponent <Chunk>() == null &&
                g.GetComponent <ChunkExtension>() == null)
            {
                return(null);
            }

            if (g.GetComponent <ChunkExtension>() != null)
            {
                // if we hit a mesh container instead of a chunk
                g = g.transform.parent.gameObject; // swap the mesh container for the actual chunk object
            }

            // check if we're actually hitting a chunk
            Chunk ch  = g.GetComponent <Chunk>();
            Index idx = ch.PositionToVoxelIndex(hit.point, hit.normal, false);

            if (ignoreTransparent)
            {
                // punch through transparent voxels by raycasting again when a transparent voxel is hit
                ushort hitVoxel = ch.GetVoxel(idx.x, idx.y, idx.z);

                if (GetVoxelType(hitVoxel).VTransparency != Transparency.solid)
                {
                    Vector3 newOrigin = hit.point;
                    newOrigin.y -= 0.5f; // push the new raycast down a bit

                    return(VoxelRaycast(newOrigin, Vector3.down, range - hit.distance, true));
                }
            }

            return(new VoxelInfo(ch.PositionToVoxelIndex(hit.point, hit.normal, false), // get hit voxel index
                                 ch.PositionToVoxelIndex(hit.point, hit.normal, true),  // get adjacent voxel index
                                 ch));                                                  // get chunk
        }
 public ushort GetVoxel()
 {
     return(chunk.GetVoxel(index));
 }
示例#4
0
        // ==== Voxel updates =====================================================================================



        public void RebuildMesh()
        {
            if (!initialized)
            {
                Initialize();
            }

            // destroy additional mesh containers
            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }

            int x = 0, y = 0, z = 0;

            // Refresh neighbor chunks
            chunk.GetNeighbors();

            // for each voxel in Voxels, check if any of the voxel's faces are exposed, and if so, add their faces to the main mesh arrays (named Vertices and Faces)
            while (x < SideLength)
            {
                while (y < SideLength)
                {
                    while (z < SideLength)
                    {
                        ushort voxel = chunk.GetVoxel(x, y, z); // the current voxel data
                        if (voxel != 0)
                        {                                       // don't render empty blocks.
                            Voxel voxelType = Engine.GetVoxelType(voxel);
                            if (voxelType.VCustomMesh == false && voxelType.VCustomPrefab == false)
                            { // if cube
                                //Transparency transparency = Engine.GetVoxelType (chunk.GetVoxel(x,y,z)).VTransparency;
                                Transparency transparency = voxelType.VTransparency;
                                ColliderType colliderType = voxelType.VColliderType;

                                if (CheckAdjacent(x, y, z, Direction.forward, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.forward, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.back, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.back, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.up, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.up, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.down, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.down, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.right, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.right, colliderType, x, y, z);
                                }

                                if (CheckAdjacent(x, y, z, Direction.left, transparency) == true)
                                {
                                    CreateFace(voxel, Facing.left, colliderType, x, y, z);
                                }

                                // if no collider, create a trigger cube collider
                                if (colliderType == ColliderType.none && Engine.GenerateColliders)
                                {
                                    AddCubeMesh(x, y, z, false);
                                }
                            }
                            else
                            { // if not cube
                                if (voxelType.VCustomMesh == true)
                                {
                                    if (CheckAllAdjacent(x, y, z) == false)
                                    { // if any adjacent voxel isn't opaque, we render the mesh
                                        CreateCustomMesh(voxel, x, y, z, voxelType.VMesh);
                                    }
                                }
                                else if (voxelType.VCustomPrefab == true)
                                {
                                    CreateCustomPrefab(voxel, x, y, z, voxelType.VPrefab);
                                }
                            }
                        }
                        z += 1;
                    }
                    z  = 0;
                    y += 1;
                }
                y  = 0;
                x += 1;
            }

            // update mesh using the values from the arrays
            UpdateMesh(GetComponent <MeshFilter>().mesh);
        }
示例#5
0
        // ==== Voxel updates =====================================================================================

        public void RebuildMesh()
        {
            if (!_initialized)
            {
                Initialize();
            }

            // destroy additional mesh containers
            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }

            int x = 0, y = 0, z = 0;

            // Refresh neighbor chunks
            _chunk.AssignNeighbors();

            // for each voxel in Voxels, check if any of the voxel's faces are exposed, and if so, add their faces to the main mesh arrays (named Vertices and Faces)
            while (x < _sizeX)
            {
                while (y < _sizeY)
                {
                    while (z < _sizeZ)
                    {
                        ushort voxel = _chunk.GetVoxel(x, y, z); // the current voxel data
                        if (voxel != 0)                          // don't render empty blocks.
                        {
                            Voxel voxelType = Engine.GetVoxelType(voxel);
                            if (voxelType.VCustomMesh)      // if not cube
                            {
                                if (!CheckAllAdjacent(x, y, z))
                                {
                                    // if any adjacent voxel isn't opaque, we render the mesh
                                    CreateCustomMesh(voxel, x, y, z, voxelType.VMesh);
                                }
                            }
                            else    // if cube
                            {
                                Transparency tr = voxelType.VTransparency;
                                ColliderType ct = voxelType.VColliderType;

                                foreach (Direction d in Enum.GetValues(typeof(Direction)))
                                {
                                    if (CheckAdjacent(x, y, z, d, tr))
                                    {
                                        CreateFace(voxel, (Facing)d, ct, x, y, z);
                                    }
                                }

                                // if no collider, create a trigger cube collider
                                if (ct == ColliderType.none && Engine.GenerateColliders)
                                {
                                    AddCubeMesh(x, y, z, false);
                                }
                            }
                        }
                        z++;
                    }
                    z = 0;
                    y++;
                }
                y = 0;
                x++;
            }

            // update mesh using the values from the arrays
            UpdateMesh(GetComponent <MeshFilter>().mesh);
        }
        // ==== Voxel updates =====================================================================================
        public void RebuildMesh()
        {
            chunk = GetComponent<Chunk>();

            // destroy additional mesh containers
            foreach (Transform child in transform) {
            Destroy(child.gameObject);
            }

            int x=0,y=0,z=0;

            // Refresh neighbor chunks
            chunk.GetNeighbors();

            // for each voxel in Voxels, check if any of the voxel's faces are exposed, and if so, add their faces to the main mesh arrays (named Vertices and Faces)
            while (x < SideLength) {
            while (y < SideLength) {
                while (z < SideLength) {

                    ushort voxel = chunk.GetVoxel(x,y,z); // the current voxel data
                    if ( voxel != 0 ) { // don't render empty blocks.

                        Voxel voxelType = Engine.GetVoxelType(voxel);
                        if ( voxelType.VCustomMesh == false ) { // if cube

                            //Transparency transparency = Engine.GetVoxelType (chunk.GetVoxel(x,y,z)).VTransparency;
                            Transparency transparency = voxelType.VTransparency;
                            ColliderType colliderType = voxelType.VColliderType;

                            if (CheckAdjacent(x,y,z, Direction.forward, transparency) == true)
                             	CreateFace(voxel, Facing.forward, colliderType, x,y,z);

                            if (CheckAdjacent(x,y,z, Direction.back, transparency) == true)
                                CreateFace(voxel, Facing.back, colliderType, x,y,z);

                            if (CheckAdjacent(x,y,z, Direction.up, transparency) == true)
                                CreateFace(voxel, Facing.up, colliderType, x,y,z);

                            if (CheckAdjacent(x,y,z, Direction.down, transparency) == true)
                                CreateFace(voxel, Facing.down, colliderType, x,y,z);

                            if (CheckAdjacent(x,y,z, Direction.right, transparency) == true)
                                CreateFace(voxel, Facing.right, colliderType, x,y,z);

                            if (CheckAdjacent(x,y,z, Direction.left, transparency) == true)
                                CreateFace(voxel, Facing.left, colliderType, x,y,z);

                            // if no collider, create a trigger cube collider
                            if (colliderType == ColliderType.none && Engine.GenerateColliders) {
                                AddCubeMesh (x,y,z, false);
                            }
                        }
                        else { // if not cube
                            if (CheckAllAdjacent (x,y,z) == false) { // if any adjacent voxel isn't opaque, we render the mesh
                                CreateCustomMesh(voxel, x,y,z, voxelType.VMesh);
                            }
                        }
                    }
                    z += 1;
                }
                z = 0;
                y += 1;

            }
            y = 0;
            x += 1;
            }

            // update mesh using the values from the arrays
            UpdateMesh ( GetComponent<MeshFilter>().mesh );
        }