Beispiel #1
0
    public B45ChunkDataBase(B45Block blk)
    {
        _chunkData = new byte[B45Block.Block45Size];

        _chunkData[0] = blk.blockType;
        _chunkData[1] = blk.materialType;
    }
Beispiel #2
0
    void writeExtendedBlock(int x, int y, int z, int primitiveType, int rotation, int extendDir, int length, byte materialType)
    {
        B45Block b0, b1;

        B45Block.MakeExtendableBlock(primitiveType, rotation, extendDir, length, (int)materialType, out b0, out b1);

        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, y, z)]     = b0.blockType;
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, y, z) + 1] = b0.materialType;

        int idx = B45Block.Block45Size * B45ChunkData.OneIndex(
            x + Block45Kernel._2BitToExDir[extendDir * 3],
            y + Block45Kernel._2BitToExDir[extendDir * 3 + 1],
            z + Block45Kernel._2BitToExDir[extendDir * 3 + 2]);

        chunkData[idx]     = b1.blockType;
        chunkData[1 + idx] = b1.materialType;

        for (int i = 2; i < length; i++)
        {
            idx = B45Block.Block45Size * B45ChunkData.OneIndex(
                x + Block45Kernel._2BitToExDir[extendDir * 3] * i,
                y + Block45Kernel._2BitToExDir[extendDir * 3 + 1] * i,
                z + Block45Kernel._2BitToExDir[extendDir * 3 + 2] * i);

            chunkData[idx]     = (byte)extendDir;
            chunkData[1 + idx] = (byte)(length - 2);
        }
    }
Beispiel #3
0
    bool CheckOnBuildTerrain()
    {
        //float blockLength = BuildBlockManager.MinBrushSize;
        float blockLength = BSBlock45Data.s_Scale;

        for (int x = -1; x <= 0; x++)
        {
            for (int z = -1; z <= 0; z++)
            {
                Vector3 worldPos = transform.TransformPoint(x * blockLength, -blockLength, z * blockLength);

                IntVector3 ipos = new IntVector3(Mathf.FloorToInt(worldPos.x * BSBlock45Data.s_ScaleInverted),
                                                 Mathf.FloorToInt(worldPos.y * BSBlock45Data.s_ScaleInverted),
                                                 Mathf.FloorToInt(worldPos.z * BSBlock45Data.s_ScaleInverted));
                B45Block block = Block45Man.self.DataSource.SafeRead(ipos.x, ipos.y, ipos.z);

                if (block.blockType >> 2 == 0)
                {
                    return(false);
                }
            }
        }

        return(true);
    }
Beispiel #4
0
    public bool WriteVoxelAtIdx4LodUpdate(int x, int y, int z, B45Block voxel)
    {
        if (_chunkData == null)
        {
            return(false);
        }

        try{
            if (IsHollow)
            {
                FillHollow();
            }

            int index   = OneIndexNoPrefix(x, y, z);
            int indexVT = index * B45Block.Block45Size;
            _chunkData[indexVT]     = voxel.blockType;
            _chunkData[indexVT + 1] = voxel.materialType;
            if (_node == null)
            {
                AddToBuildList();
            }
            else if (_node.IsInReq)
            {
                AddToBuildList();
            }
        }
        catch
        {
            Debug.LogWarning("[VFTERRAIN]:Failed to write lod voxel[" + x + "," + y + "," + z + "] of ChunkData" + ChunkPosLod);
        }
        return(true);
    }
    public static void MakeExtendableBS(int primitiveType, int rotation, int extendDir, int length, int materialType, out B45Block b0, out B45Block b1)
    {
        B45Block block0, block1;

        B45Block.MakeExtendableBlock(primitiveType, rotation, extendDir, length, materialType, out block0, out block1);
        b0 = block0;
        b1 = block1;
    }
    public BSVoxel Subtract(BSVoxel voxel, int x, int y, int z, int lod = 0)
    {
        B45Block block = new B45Block();

        Block45Man.self.DataSource.Write(block, x, y, z, lod);

        return(new BSVoxel(block));
    }
    public bool SafeWrite(int x, int y, int z, B45Block voxel, int lod = 0)
    {
        //if(	x<0 || x>=(_chunkNumX<<Block45Constants._shift) ||
        //    y<1 || y>=(_chunkNumY<<Block45Constants._shift) ||
        //    z<0 || z>=(_chunkNumZ<<Block45Constants._shift) )
        //    return false;

        Write(x, y, z, voxel, lod);
        return(true);
    }
    public bool ReadExtendableBlock(IntVector4 pos, out List <IntVector4> posList, out List <B45Block> blocks)
    {
        B45Block ori = Read(pos.x, pos.y, pos.z, pos.w);

        if (!ori.IsExtendable())
        {
            posList = null;
            blocks  = null;
            return(false);
        }

        int e    = ori.materialType & 3;
        int EDx  = Block45Kernel._2BitToExDir[e * 3];
        int EDy  = Block45Kernel._2BitToExDir[e * 3 + 1];
        int EDz  = Block45Kernel._2BitToExDir[e * 3 + 2];
        int posx = pos.x;
        int posy = pos.y;
        int posz = pos.z;
        int posw = pos.w;

        if (!ori.IsExtendableRoot())
        {
            while (true)
            {
                posx -= EDx;
                posy -= EDy;
                posz -= EDz;
                ori   = Read(posx, posy, posz, posw);
                if (!ori.IsExtendable())
                {
                    Debug.LogError("[Block]Get root data error in ReadExtendableBlock:" + pos);
                    posList = null;
                    blocks  = null;
                    return(false);
                }
                else if (ori.IsExtendableRoot())
                {
                    break;
                }
            }
        }

        int len = (ori.materialType >> 2) + 2;

        posList = new List <IntVector4>(len);
        blocks  = new List <B45Block>(len);
        for (int i = 0; i < len; i++)
        {
            IntVector4 curPos = new IntVector4(posx + i * EDx, posy + i * EDy, posz + i * EDz, posw);
            posList.Add(curPos);
            blocks.Add(Read(curPos.x, curPos.y, curPos.z, curPos.w));
        }
        return(true);
    }
    public void AlterBlockInBuild(int vx, int vy, int vz, B45Block blk)
    {
        int nOldCnt = chunkRebuildList.Count;

        _blockDS.SafeWrite(vx, vy, vz, blk, 0);
        int nNewCnt = chunkRebuildList.Count;

        for (int i = nOldCnt; i < nNewCnt; i++)
        {
            AddChunkToSaveList(chunkRebuildList[i]);
        }
    }
Beispiel #10
0
    void test1()
    {
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 0)]     = B45Block.MakeBlockType(1, 0);
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 0) + 1] = 0;

        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 1)]     = B45Block.MakeBlockType(2, 0);
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 1) + 1] = 1;
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 2)]     = B45Block.MakeBlockType(4, 0);
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 2) + 1] = 2;
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 3)]     = B45Block.MakeBlockType(3, 0);
        chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(0, 0, 3) + 1] = 3;
    }
Beispiel #11
0
 public void ProcPostLodUpdate()
 {
     if (_dbgRead)
     {
         B45Block block = _dataSource.Read(_dbgLogicX, _dbgLogicY, _dbgLogicZ);
         _dbg0    = block.blockType;
         _dbg1    = block.materialType;
         _dbgRead = false;
     }
     if (_dbgWrite)
     {
         _dataSource.Write(new B45Block((byte)_dbg0, (byte)_dbg1), _dbgLogicX, _dbgLogicY, _dbgLogicZ);
         _dbgWrite = false;
     }
 }
Beispiel #12
0
        bool CheckOnBuildTerrain()
        {
            if (m_Trans == null)
            {
                return(false);
            }

            //for (int x = -1; x <= 1; x++)
            //{
            //    for (int z = -1; z <= 1; z++)
            //    {
            //        Vector3 worldPos = m_Trans.trans.TransformPoint(x * m_Trans.bound.extents.x, -0.5f, z * m_Trans.bound.extents.z);
            //        IntVector3 ipos = new IntVector3(Mathf.FloorToInt(worldPos.x * BSBlock45Data.s_ScaleInverted),
            //                                         Mathf.FloorToInt(worldPos.y * BSBlock45Data.s_ScaleInverted),
            //                                         Mathf.FloorToInt(worldPos.z * BSBlock45Data.s_ScaleInverted));
            //        B45Block block = Block45Man.self.DataSource.SafeRead(ipos.x, ipos.y, ipos.z);

            //        if (block.blockType >> 2 == 0)
            //            return false;
            //    }
            //}

            //float blockLength = BuildBlockManager.MinBrushSize;
            float blockLength = BSBlock45Data.s_Scale;

            for (int x = -1; x <= 0; x++)
            {
                for (int z = -1; z <= 0; z++)
                {
                    Vector3  worldPos = m_Trans.trans.TransformPoint(x * blockLength, -blockLength, z * blockLength);
                    int      iposx    = Mathf.FloorToInt(worldPos.x * BSBlock45Data.s_ScaleInverted);
                    int      iposy    = Mathf.FloorToInt(worldPos.y * BSBlock45Data.s_ScaleInverted);
                    int      iposz    = Mathf.FloorToInt(worldPos.z * BSBlock45Data.s_ScaleInverted);
                    B45Block block    = Block45Man.self.DataSource.SafeRead(iposx, iposy, iposz);

                    if (block.blockType >> 2 == 0)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Beispiel #13
0
    public bool WriteVoxelAtIdx(int x, int y, int z, B45Block voxel, bool bLodUpdate = false)
    {
        if (_chunkData == S_ChunkDataNull)
        {
            return(false);
        }

        try{
            if (IsHollow)
            {
                FillHollow();
            }

            int index   = OneIndex(x, y, z);
            int indexVT = index * B45Block.Block45Size;
            _chunkData[indexVT]     = voxel.blockType;
            _chunkData[indexVT + 1] = voxel.materialType;
            if (_node == null)
            {
                AddToBuildList();
            }
            else if (_node.IsInReq)
            {
                AddToBuildList();
            }
        }
        catch (Exception ex)
        {
            Debug.LogWarning("[VFTERRAIN]:Failed to write voxel[" + x + "," + y + "," + z + "] of ChunkData" + ChunkPosLod + " : " + ex.ToString());
        }

        if (bLodUpdate && VFVoxelTerrain.self.LodDataUpdate != null)
        {
            VFVoxelTerrain.self.LodDataUpdate.InsertUpdateCoord(x, y, z, _chunkPosLod);
        }
//		VFVoxelTerrain.self.lodDataUpdate.threadProc();

        return(true);
    }
Beispiel #14
0
    void test2()
    {
        UnityEngine.Random.seed = 1223;
        for (int z = 0; z < Block45Constants._numVoxelsPerAxis; z++)
        {
//			for(int y =0; y < Block45Constants._numVoxelsPerAxis;y++)
            {
                for (int x = 0; x < Block45Constants._numVoxelsPerAxis; x++)
                {
                    int rot = (Mathf.RoundToInt(UnityEngine.Random.value * 256)) % 4;
                    chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, 0, z)] =
                        B45Block.MakeBlockType(((x + z * Block45Constants._numVoxelsPerAxis + 0
                                                 * Block45Constants._numVoxelsPerAxis * Block45Constants._numVoxelsPerAxis) % 6) + 1, rot);

                    rot = (Mathf.RoundToInt(UnityEngine.Random.value * 256)) % 4;
                    chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, 1, z)] =
                        B45Block.MakeBlockType(((x + z * Block45Constants._numVoxelsPerAxis + 0
                                                 * Block45Constants._numVoxelsPerAxis * Block45Constants._numVoxelsPerAxis) % 6) + 1, rot);
                }
            }
        }
    }
Beispiel #15
0
    private void OnOutputBlocks(Dictionary <int, BSVoxel> voxels, Vector3 originalPos)
    {
        List <IntVector3> posLst   = new List <IntVector3>();
        List <B45Block>   blockLst = new List <B45Block>();

        foreach (KeyValuePair <int, BSVoxel> kvp in voxels)
        {
            posLst.Add(BSIsoData.KeyToIPos(kvp.Key));
            blockLst.Add(kvp.Value.ToBlock());
        }
        B45Block.RepositionBlocks(posLst, blockLst, m_Rot, originalPos);

        int n = posLst.Count;

        for (int i = 0; i < n; i++)
        {
            IntVector3 inpos = posLst[i];
            m_Indexes.Add(inpos);
            m_OldVoxel.Add(BuildingMan.Blocks.SafeRead(inpos.x, inpos.y, inpos.z));
            m_NewVoxel.Add(new BSVoxel(blockLst[i]));
            m_VoxelMap[inpos] = 0;
        }
    }
Beispiel #16
0
    static void Dig(IntVector3 blockUnitPos, float durDec, ref List <B45Block> removeList)
    {
        B45Block getBlock = Block45Man.self._dataSource.SafeRead(blockUnitPos.x, blockUnitPos.y, blockUnitPos.z);

        if (getBlock.blockType == 0)
        {
            return;
        }

        float digPower = durDec;

        // NaturalResAsset.NaturalRes resData = NaturalResAsset.NaturalRes.GetTerrainResData(getVoxel.Type);
        // if(null != resData)	digPower *= resData.m_duration;
        // else                 Debug.LogWarning("VoxelType[" + getVoxel.Type + "] does't have NaturalRes data.");

        if (digPower >= 127)
        {
            getBlock.blockType = 0;
            Block45Man.self._dataSource.SafeWrite(new B45Block(0), blockUnitPos.x, blockUnitPos.y, blockUnitPos.z);
            removeList.Add(getBlock);
        }
        return;
    }
Beispiel #17
0
    void test3()
    {
        UnityEngine.Random.seed = 1223;
        for (int z = 0; z < Block45Constants._numVoxelsPerAxis; z++)
        {
//			for(int y =0; y < Block45Constants._numVoxelsPerAxis;y++)
            {
                for (int x = 0; x < Block45Constants._numVoxelsPerAxis; x++)
                {
                    int rot = (Mathf.RoundToInt(UnityEngine.Random.value * 256)) % 4;
                    chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, 0, z)] =
                        B45Block.MakeBlockType(((x + z * Block45Constants._numVoxelsPerAxis + 0
                                                 * Block45Constants._numVoxelsPerAxis * Block45Constants._numVoxelsPerAxis) % 6) + 1, rot);

                    chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, 0, z) + 1] = (byte)(x % 3);

                    rot = (Mathf.RoundToInt(UnityEngine.Random.value * 256)) % 4;
                    chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, 1, z)] =
                        B45Block.MakeBlockType(((x + z * Block45Constants._numVoxelsPerAxis + 0
                                                 * Block45Constants._numVoxelsPerAxis * Block45Constants._numVoxelsPerAxis) % 6) + 1, rot);
                    chunkData[B45Block.Block45Size * B45ChunkData.OneIndex(x, 1, z) + 1] = (byte)(z % 3);
                }
            }
        }
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,0)] = B45Block.MakeBlockType(1,0);
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,0)+1] = 0;
//
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,1)] = B45Block.MakeBlockType(1,0);
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,1)+1] = 1;
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,2)] = B45Block.MakeBlockType(1,0);
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,2)+1] = 2;
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,3)] = B45Block.MakeBlockType(1,0);
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,3)+1] = 0;
//
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,3)] = B45Block.MakeBlockType(1,0);
//		chunkData[B45Block.Block45Size*B45ChunkData.OneIndex(0,0,3)+1] = 3;
    }
Beispiel #18
0
 public BSVoxel(B45Block block)
 {
     value0 = block.blockType;
     value1 = block.materialType;
 }
 public bool SafeWrite(B45Block voxel, int x, int y, int z, int lod = 0)
 {
     Write(voxel, x, y, z, lod);
     return(true);
 }
    public int Write(B45Block voxel, int x, int y, int z, int lod = 0)                                  // logic pos
    {
        int vx = (x >> lod) & Block45Constants._mask;
        int vy = (y >> lod) & Block45Constants._mask;
        int vz = (z >> lod) & Block45Constants._mask;
        //Debug.LogWarning("[BlockWrite]:"+x+","+y+","+z+","+voxel.blockType+","+voxel.materialType);

        IntVector4     poslod;
        Block45OctNode curNode;

        if (_octRoot == null)
        {
            poslod = Block45Constants.ToWorldUnitPos(x & ~Block45Constants._mask,
                                                     y & ~Block45Constants._mask,
                                                     z & ~Block45Constants._mask,
                                                     lod);
            IntVector4 rootPosLod = new IntVector4(poslod);
            curNode = _octRoot = Block45OctNode.CreateNode(rootPosLod, _onCreateNode);
            //Extend root node if root.lod < VoxelTerrainConstans.MaxLOD
            _octRoot = _octRoot.RerootToLOD(LODOctreeMan._maxLod);
        }
        else
        {
            poslod  = Block45Constants.ToWorldUnitPos(x, y, z, lod);
            curNode = Block45OctNode.GetNodeRW(poslod, ref _octRoot);
        }
        curNode.Write(vx, vy, vz, voxel.blockType, voxel.materialType);

        // Write neighbour
        int fx = 0, fy = 0, fz = 0;
        int dirtyMask = 0x80;           // 0,1,2 bit for xyz dirty mask;4,5,6 bit for sign(neg->1);7 bit for current pos(now not used)

        // If write one edge's voxel may cause the other edge being modified
        if (vx < Block45OctNode.S_MinNoDirtyIdx)
        {
            fx = -1; dirtyMask |= 0x11;
        }
        else
        if (vx >= Block45OctNode.S_MaxNoDirtyIdx)
        {
            fx = 1; dirtyMask |= 0x01;
        }
        if (vy < Block45OctNode.S_MinNoDirtyIdx)
        {
            fy = -1; dirtyMask |= 0x22;
        }
        else
        if (vy >= Block45OctNode.S_MaxNoDirtyIdx)
        {
            fy = 1; dirtyMask |= 0x02;
        }
        if (vz < Block45OctNode.S_MinNoDirtyIdx)
        {
            fz = -1; dirtyMask |= 0x44;
        }
        else
        if (vz >= Block45OctNode.S_MaxNoDirtyIdx)
        {
            fz = 1; dirtyMask |= 0x04;
        }

        if (dirtyMask != 0x80)
        {
            int _shift = Block45Constants._shift;
            int cxlod  = (x >> (lod + _shift));
            int cylod  = (y >> (lod + _shift));
            int czlod  = (z >> (lod + _shift));
            int cxround;
            int cyround;
            int czround;
            for (int i = 1; i < 8; i++)
            {
                if ((dirtyMask & i) == i)
                {
                    int dx = fx * Block45OctNode.S_NearNodeOfs[i, 0], dy = fy * Block45OctNode.S_NearNodeOfs[i, 1], dz = fz * Block45OctNode.S_NearNodeOfs[i, 2];
                    cxround = (cxlod + dx);
                    cyround = (cylod + dy);
                    czround = (czlod + dz);

                    poslod.x = cxround << (Block45Constants._scaledShift + lod);
                    poslod.y = cyround << (Block45Constants._scaledShift + lod);
                    poslod.z = czround << (Block45Constants._scaledShift + lod);
                    Block45OctNode nearNode = Block45OctNode.GetNodeRW(poslod, ref _octRoot);
                    nearNode.Write(vx - dx * Block45Constants._numVoxelsPerAxis,
                                   vy - dy * Block45Constants._numVoxelsPerAxis,
                                   vz - dz * Block45Constants._numVoxelsPerAxis,
                                   voxel.blockType, voxel.materialType);
                }
            }
        }
        return(dirtyMask);
    }
Beispiel #21
0
 public static byte MakeBlockType(int primitiveType, int rotation)
 {
     return(B45Block.MakeBlockType(primitiveType, rotation));
 }
    public void ComputerPreview(IntVector3 up, IntVector3 dn, bool upVSeal, bool dnVSeal, byte matType)
    {
        int dy  = up.y - dn.y;
        int dx  = up.x - dn.x;
        int dz  = up.z - dn.z;
        int adx = Mathf.Abs(dx);
        int adz = Mathf.Abs(dz);

        int t0 = 2, t1 = 10;

        int        l   = 0;
        int        n   = 0;
        IntVector3 beg = dn;
//		int idxXZ = 0;
        int maxXZ = adx;
        int xstep = dx > 0 ? 1 : -1;
        int zstep = 0;
        int xext0 = 0;
        int zext0 = 0;
        int xext1 = 1;
        int zext1 = 0;
        int xext2 = 2;
        int zext2 = 0;
        int r     = up.x > dn.x ? 2 : 0;
        int e     = B45Block.EBX;

        if (adx < adz)
        {
//			idxXZ = 1;
            maxXZ = adz;
            xstep = 0;
            zstep = dz > 0 ? 1 : -1;
            xext1 = 0;
            zext1 = 1;
            xext2 = 0;
            zext2 = 2;
            r     = up.z > dn.z ? 1 : 3;
            e     = B45Block.EBZ;
        }
        if (dy < 2 * maxXZ && maxXZ < 2 * dy)
        {
            n = Mathf.Min(dy, maxXZ);
            B45Block bOrg = new B45Block((byte)((t0 << 2) | r), matType);
            B45Block bFlp = new B45Block((byte)((t1 << 2) | ((r + 2) & 3)), matType);

            if (dnVSeal)
            {
                m_Computer.AlterBlockInBuild(beg.x, beg.y - 1, beg.z, bFlp);
            }
            for (int i = 0; i < n; i++)
            {
                m_Computer.AlterBlockInBuild(beg.x + i * xstep, beg.y + i, beg.z + i * zstep, bOrg);
                m_Computer.AlterBlockInBuild(beg.x + (i + 1) * xstep, beg.y + i, beg.z + (i + 1) * zstep, bFlp);
            }
            if (upVSeal)
            {
                m_Computer.AlterBlockInBuild(beg.x + n * xstep, beg.y + n, beg.z + n * zstep, bOrg);
            }
        }
        else
        {
            B45Block bOrg0, bOrg1, bOrg2;
            B45Block bFlp0, bFlp1, bFlp2;
            if (dy > maxXZ)
            {
                e = B45Block.EBY;
                l = dy / maxXZ; if (l > 3)
                {
                    l = 3;
                }
                n = maxXZ;
                MakeExtendableBS(t0, r, e, l, matType, out bOrg0, out bOrg1);
                MakeExtendableBS(t1, (r + 2) & 3, e, l, matType, out bFlp0, out bFlp1);
                bOrg2 = bOrg1;                  // dumb block voxel for original block
                bFlp2 = bFlp1;                  // dumb block voxel for flipping block
                if (dnVSeal)
                {
                    m_Computer.AlterBlockInBuild(beg.x, beg.y - l + 0, beg.z, bFlp0);
                    m_Computer.AlterBlockInBuild(beg.x, beg.y - l + 1, beg.z, bFlp1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x, beg.y - l + 2, beg.z, bFlp2);
                    }
                }
                for (int i = 0; i < n; i++)
                {
                    m_Computer.AlterBlockInBuild(beg.x + i * xstep, beg.y + i * l + 0, beg.z + i * zstep, bOrg0);
                    m_Computer.AlterBlockInBuild(beg.x + i * xstep, beg.y + i * l + 1, beg.z + i * zstep, bOrg1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + i * xstep, beg.y + i * l + 2, beg.z + i * zstep, bOrg2);
                    }

                    m_Computer.AlterBlockInBuild(beg.x + (i + 1) * xstep, beg.y + i * l + 0, beg.z + (i + 1) * zstep, bFlp0);
                    m_Computer.AlterBlockInBuild(beg.x + (i + 1) * xstep, beg.y + i * l + 1, beg.z + (i + 1) * zstep, bFlp1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + (i + 1) * xstep, beg.y + i * l + 2, beg.z + (i + 1) * zstep, bFlp2);
                    }
                }
                if (upVSeal)
                {
                    m_Computer.AlterBlockInBuild(beg.x + n * xstep, beg.y + n * l + 0, beg.z + n * zstep, bOrg0);
                    m_Computer.AlterBlockInBuild(beg.x + n * xstep, beg.y + n * l + 1, beg.z + n * zstep, bOrg1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + n * xstep, beg.y + n * l + 2, beg.z + n * zstep, bOrg2);
                    }
                }
            }
            else
            {
                l = maxXZ / dy; if (l > 3)
                {
                    l = 3;
                }
                n = dy;
                MakeExtendableBS(t0, r, e, l, matType, out bOrg0, out bOrg1);
                MakeExtendableBS(t1, (r + 2) & 3, e, l, matType, out bFlp0, out bFlp1);
                bOrg2 = bOrg1;                  // dumb block voxel for original block
                bFlp2 = bFlp1;                  // dumb block voxel for flipping block
                if (dnVSeal)
                {
                    m_Computer.AlterBlockInBuild(beg.x + xext0, beg.y - 1, beg.z + zext0, bFlp0);
                    m_Computer.AlterBlockInBuild(beg.x + xext1, beg.y - 1, beg.z + zext1, bFlp1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + xext2, beg.y - 1, beg.z + zext2, bFlp2);
                    }
                }
                for (int i = 0; i < dy; i++)
                {
                    m_Computer.AlterBlockInBuild(beg.x + i * l * xstep + xext0, beg.y + i, beg.z + i * l * zstep + zext0, bOrg0);
                    m_Computer.AlterBlockInBuild(beg.x + i * l * xstep + xext1, beg.y + i, beg.z + i * l * zstep + zext1, bOrg1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + i * l * xstep + xext2, beg.y + i, beg.z + i * l * zstep + zext2, bOrg2);
                    }

                    m_Computer.AlterBlockInBuild(beg.x + (i + 1) * l * xstep + xext0, beg.y + i, beg.z + (i + 1) * l * zstep + zext0, bFlp0);
                    m_Computer.AlterBlockInBuild(beg.x + (i + 1) * l * xstep + xext1, beg.y + i, beg.z + (i + 1) * l * zstep + zext1, bFlp1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + (i + 1) * l * xstep + xext2, beg.y + i, beg.z + (i + 1) * l * zstep + zext2, bFlp2);
                    }
                }
                if (upVSeal)
                {
                    m_Computer.AlterBlockInBuild(beg.x + n * l * xstep + xext0, beg.y + n, beg.z + n * l * zstep + zext0, bOrg0);
                    m_Computer.AlterBlockInBuild(beg.x + n * l * xstep + xext1, beg.y + n, beg.z + n * l * zstep + zext1, bOrg1);
                    if (l == 3)
                    {
                        m_Computer.AlterBlockInBuild(beg.x + n * l * xstep + xext2, beg.y + n, beg.z + n * l * zstep + zext2, bOrg2);
                    }
                }
            }
        }
    }
Beispiel #23
0
    // Extract Header
    private static int ExtractHeader(Stream stream, out BSIsoHeadData iso_header)
    {
        iso_header = new BSIsoHeadData();
        int len = 0;

        iso_header.Init();
        try
        {
            BinaryReader r = new BinaryReader(stream);

            // Header
            string check_str = r.ReadString();                  // r, string
            if (check_str != "BSISO")
            {
                /************
                 *  Older Building System
                 * *********/

                r.BaseStream.Seek(0, SeekOrigin.Begin);

                int version = r.ReadInt32();
                int count   = r.ReadInt32();

                switch (version)
                {
                case 1:

                    Dictionary <IntVector3, B45Block> old_blocks = new Dictionary <IntVector3, B45Block>();
                    for (int i = 0; i < count; i++)
                    {
                        IntVector3 index = new IntVector3(r.ReadInt32(), r.ReadInt32(), r.ReadInt32());
                        old_blocks[index] = new B45Block(r.ReadByte(), r.ReadByte());
                    }
                    int    texDataSize = r.ReadInt32();
                    byte[] tex_bytes   = r.ReadBytes(texDataSize);

                    iso_header.IconTex = tex_bytes;

                    // Calculate Bound
                    IntVector3 min = new IntVector3(10000, 10000, 10000);
                    IntVector3 max = new IntVector3(-10000, -10000, -10000);

                    foreach (IntVector3 index in old_blocks.Keys)
                    {
                        if (min.x > index.x)
                        {
                            min.x = index.x;
                        }
                        else if (max.x < index.x)
                        {
                            max.x = index.x;
                        }

                        if (min.y > index.y)
                        {
                            min.y = index.y;
                        }
                        else if (max.y < index.y)
                        {
                            max.y = index.y;
                        }

                        if (min.z > index.z)
                        {
                            min.z = index.z;
                        }
                        else if (max.z < index.z)
                        {
                            max.z = index.z;
                        }
                    }

                    IntVector3 size = new IntVector3(max.x - min.x + 1, max.y - min.y + 1, max.z - min.z + 1);

                    iso_header.xSize = size.x;
                    iso_header.ySize = size.y;
                    iso_header.zSize = size.z;

                    foreach (KeyValuePair <IntVector3, B45Block> kvp in old_blocks)
                    {
                        //IntVector3 ipos = new IntVector3(kvp.Key.x + size.x / 2, kvp.Key.y , kvp.Key.z + size.z / 2);
//						int key = IPosToKey(ipos);
//						m_Voxels.Add(key, new BSVoxel( kvp.Value));
                        if (iso_header.costs.ContainsKey(kvp.Value.materialType))
                        {
                            iso_header.costs[kvp.Value.materialType]++;
                        }
                        else
                        {
                            iso_header.costs.Add(kvp.Value.materialType, 1);
                        }
                    }

                    break;

                default:
                    break;
                }
                r.Close();
                len = (int)stream.Length;
                return(len);
            }

//			int l = 0;
            iso_header.Version = r.ReadInt32();                 // r, int
            iso_header.Mode    = (EBSVoxelType)r.ReadInt32();
            iso_header.Author  = r.ReadString();
            iso_header.Name    = r.ReadString();
            iso_header.Desc    = r.ReadString();
            iso_header.Remarks = r.ReadString();
            iso_header.xSize   = r.ReadInt32();
            iso_header.ySize   = r.ReadInt32();
            iso_header.zSize   = r.ReadInt32();

            int length = r.ReadInt32();
            iso_header.IconTex = r.ReadBytes(length);
            length             = r.ReadInt32();
            for (int i = 0; i < length; i++)
            {
                iso_header.costs.Add(r.ReadByte(), r.ReadUInt32());
            }

            len = (int)(stream.Length);
            stream.Close();
        }
        catch (System.Exception)
        {
            return(0);
        }

        iso_header.EnsureIconTexValid();

        return(len);
    }
Beispiel #24
0
    public bool Import(byte[] buffer)
    {
        if (buffer == null)
        {
            return(false);
        }

        Init(EBSVoxelType.Block);

        try
        {
            using (MemoryStream ms_iso = new MemoryStream(buffer))
            {
                BinaryReader r = new BinaryReader(ms_iso);

                // Header
                string check_str = r.ReadString();                      // r, string
                if (check_str != "BSISO")
                {
                    /************
                     *  Older Building System
                     * *********/

                    r.BaseStream.Seek(0, SeekOrigin.Begin);

                    int version = r.ReadInt32();
                    int count   = r.ReadInt32();

                    switch (version)
                    {
                    case 1:

                        Dictionary <IntVector3, B45Block> old_blocks = new Dictionary <IntVector3, B45Block>();
                        for (int i = 0; i < count; i++)
                        {
                            IntVector3 index = new IntVector3(r.ReadInt32(), r.ReadInt32(), r.ReadInt32());
                            old_blocks[index] = new B45Block(r.ReadByte(), r.ReadByte());
                        }
                        int    texDataSize = r.ReadInt32();
                        byte[] tex_bytes   = r.ReadBytes(texDataSize);

                        m_HeadInfo.IconTex = tex_bytes;

                        // Calculate Bound
                        IntVector3 min = new IntVector3(10000, 10000, 10000);
                        IntVector3 max = new IntVector3(-10000, -10000, -10000);

                        foreach (IntVector3 index in old_blocks.Keys)
                        {
                            if (min.x > index.x)
                            {
                                min.x = index.x;
                            }
                            else if (max.x < index.x)
                            {
                                max.x = index.x;
                            }

                            if (min.y > index.y)
                            {
                                min.y = index.y;
                            }
                            else if (max.y < index.y)
                            {
                                max.y = index.y;
                            }

                            if (min.z > index.z)
                            {
                                min.z = index.z;
                            }
                            else if (max.z < index.z)
                            {
                                max.z = index.z;
                            }
                        }

                        IntVector3 size = new IntVector3(max.x - min.x + 1, max.y - min.y + 1, max.z - min.z + 1);

                        m_HeadInfo.xSize = size.x;
                        m_HeadInfo.ySize = size.y;
                        m_HeadInfo.zSize = size.z;

                        foreach (KeyValuePair <IntVector3, B45Block> kvp in old_blocks)
                        {
                            IntVector3 ipos = new IntVector3(kvp.Key.x + Mathf.CeilToInt((float)size.x / 2), kvp.Key.y, kvp.Key.z + Mathf.CeilToInt((float)size.z / 2));
//							Debug.Log(ipos);
                            int key = IPosToKey(ipos);
//							m_Voxels.Add(key, new BSVoxel( kvp.Value));
                            m_Voxels[key] = new BSVoxel(kvp.Value);
                        }

                        CaclCosts();
                        break;

                    default:
                        break;
                    }
                    r.Close();
                    return(true);
                }

                m_HeadInfo.Version = r.ReadInt32();                     // r, int

                switch (m_HeadInfo.Version)
                {
                case 0x01010001:
//					int l = 0;
                    m_HeadInfo.Mode    = (EBSVoxelType)r.ReadInt32();
                    m_HeadInfo.Author  = r.ReadString();
                    m_HeadInfo.Name    = r.ReadString();
                    m_HeadInfo.Desc    = r.ReadString();
                    m_HeadInfo.Remarks = r.ReadString();
                    m_HeadInfo.xSize   = r.ReadInt32();
                    m_HeadInfo.ySize   = r.ReadInt32();
                    m_HeadInfo.zSize   = r.ReadInt32();
                    int length = r.ReadInt32();
                    m_HeadInfo.IconTex = r.ReadBytes(length);
                    length             = r.ReadInt32();
                    for (int i = 0; i < length; i++)
                    {
                        m_HeadInfo.costs.Add(r.ReadByte(), r.ReadUInt32());
                    }

                    length = r.ReadInt32();
                    for (int i = 0; i < length; i++)
                    {
                        BSVoxel voxel = new BSVoxel();
                        int     key   = r.ReadInt32();
                        voxel.value0 = r.ReadByte();
                        voxel.value1 = r.ReadByte();

                        m_Voxels[key] = voxel;
                    }
                    break;

                default:
                    r.Close();
                    return(false);
                }

                r.Close();
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError("Importing ISO Failed :" + e.ToString());
            return(false);
        }

        return(true);
    }
    void randombuild()
    {
//		if(DebugMode == false)
//			return;
        int stX = 1;
        int stY = 1;
        int stZ = 62;

        int size = 16;

        if (Input.GetKeyUp(KeyCode.Alpha5))
        {
            _blockDS.SafeWrite(1, 63, 63, new B45Block(B45Block.MakeBlockType(1, 0), 4), 0);
            _blockDS.SafeWrite(1, 64, 64, new B45Block(B45Block.MakeBlockType(1, 0), 4), 0);

            _blockDS.SafeWrite(1, 63, 64, new B45Block(B45Block.MakeBlockType(1, 0), 4), 0);
            _blockDS.SafeWrite(1, 64, 63, new B45Block(B45Block.MakeBlockType(1, 0), 4), 0);
        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            stX += inc;
            stY += inc;
            stZ += inc;
            for (int z = 0; z < 1; z++)
            {
                for (int y = 0; y < size; y++)
                {
                    for (int x = 0; x < size; x++)
                    {
                        _blockDS.SafeWrite(x + stX, y + stY, z + stZ, new B45Block(B45Block.MakeBlockType(1, 0), 2), 0);
                    }
                }
            }
            inc++;
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            stX += inc;
            stY += inc;
            stZ += inc;
            for (int z = 0; z < size; z++)
            {
                for (int y = 0; y < 1; y++)
                {
                    for (int x = 0; x < size; x++)
                    {
                        _blockDS.SafeWrite(x + stX, y + stY, z + stZ, new B45Block(B45Block.MakeBlockType(1, 0), 3), 0);
                    }
                }
            }
            inc++;
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            stX += inc;
            stY += inc;
            stZ += inc;
            for (int z = 0; z < size; z++)
            {
                for (int y = 0; y < size; y++)
                {
                    for (int x = 0; x < 1; x++)
                    {
                        _blockDS.SafeWrite(x + stX, y + stY, z + stZ, new B45Block(B45Block.MakeBlockType(1, 0), 4), 0);
                    }
                }
            }
            inc++;
        }
    }
    void ComputeOne(byte[] LODChunkData, List <byte[]> srcChunkData, int cid, IntVector3 destChunkOfs)
    {
        byte[] eightTypes = new byte[8];
        byte[] eightMat   = new byte[8];

        byte[] eightOccu = new byte[8];

        for (int z = 0; z < Block45Constants._numVoxelsPerAxis; z += 2)
        {
            for (int y = 0; y < Block45Constants._numVoxelsPerAxis; y += 2)
            {
                for (int x = 0; x < Block45Constants._numVoxelsPerAxis; x += 2)
                {
                    eightTypes[0] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y, z)]);
                    eightTypes[1] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y, z + 1)]);
                    eightTypes[2] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y, z + 1)]);
                    eightTypes[3] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y, z)]);

                    eightTypes[4] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y + 1, z)]);
                    eightTypes[5] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y + 1, z + 1)]);
                    eightTypes[6] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y + 1, z + 1)]);
                    eightTypes[7] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y + 1, z)]);

                    eightMat[0] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y, z) + 1]);
                    eightMat[1] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y, z + 1) + 1]);
                    eightMat[2] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y, z + 1) + 1]);
                    eightMat[3] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y, z) + 1]);

                    eightMat[4] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y + 1, z) + 1]);
                    eightMat[5] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x, y + 1, z + 1) + 1]);
                    eightMat[6] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y + 1, z + 1) + 1]);
                    eightMat[7] = (byte)(srcChunkData[cid][B45Block.Block45Size * B45ChunkData.OneIndex(x + 1, y + 1, z) + 1]);

                    eightOccu[0] = TypeToOccupancy[eightTypes[0] >> 2];
                    eightOccu[1] = TypeToOccupancy[eightTypes[1] >> 2];
                    eightOccu[2] = TypeToOccupancy[eightTypes[2] >> 2];
                    eightOccu[3] = TypeToOccupancy[eightTypes[3] >> 2];

                    eightOccu[4] = TypeToOccupancy[eightTypes[4] >> 2];
                    eightOccu[5] = TypeToOccupancy[eightTypes[5] >> 2];
                    eightOccu[6] = TypeToOccupancy[eightTypes[6] >> 2];
                    eightOccu[7] = TypeToOccupancy[eightTypes[7] >> 2];

                    int sum = eightOccu[0] + eightOccu[1] + eightOccu[2] + eightOccu[3] +
                              eightOccu[4] + eightOccu[5] + eightOccu[6] + eightOccu[7];

                    ///if( sum > 0){
                    ///	int sdl= 0;
                    ///}
                    List <int> indices = GetSimilarIndices(sum, 3);
                    int        smallestError = 255;
                    int        bestMatchI = -1, bestMatchR = -1;
                    for (int i = 0; i < indices.Count; i++)
                    {
                        int errorSum = CalculateError(eightOccu, indices[i], 0);
                        if (smallestError > errorSum)
                        {
                            smallestError = errorSum;
                            bestMatchI    = indices[i];
                            bestMatchR    = 3;
                        }
                        errorSum = CalculateError(eightOccu, indices[i], 1);
                        if (smallestError > errorSum)
                        {
                            smallestError = errorSum;
                            bestMatchI    = indices[i];
                            bestMatchR    = 2;
                        }
                        errorSum = CalculateError(eightOccu, indices[i], 2);
                        if (smallestError > errorSum)
                        {
                            smallestError = errorSum;
                            bestMatchI    = indices[i];
                            bestMatchR    = 1;
                        }
                        errorSum = CalculateError(eightOccu, indices[i], 3);
                        if (smallestError > errorSum)
                        {
                            smallestError = errorSum;
                            bestMatchI    = indices[i];
                            bestMatchR    = 0;
                        }
                    }
                    B45Block blk;
                    blk.blockType    = B45Block.MakeBlockType(bestMatchI, bestMatchR);
                    blk.materialType = 0;

                    // determine the material type for this lod block.
                    byte largestOccu = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        if (eightOccu[i] > largestOccu)
                        {
                            largestOccu      = eightOccu[i];
                            blk.materialType = eightMat[i];
                        }
                    }
                    int tmpIdx = B45Block.Block45Size * B45ChunkData.OneIndex(
                        destChunkOfs.x * Block45Constants._numVoxelsPerAxis / 2 + x / 2,
                        destChunkOfs.y * Block45Constants._numVoxelsPerAxis / 2 + y / 2,
                        destChunkOfs.z * Block45Constants._numVoxelsPerAxis / 2 + z / 2
                        );
                    LODChunkData[tmpIdx]     = blk.blockType;
                    LODChunkData[tmpIdx + 1] = blk.materialType;
                }
            }
        }
    }
Beispiel #27
0
 public void AlterBlockInBuild(int vx, int vy, int vz, B45Block blk)
 {
     DataSource.SafeWrite(blk, vx, vy, vz, 0);
 }
    public int Write(int x, int y, int z, B45Block voxel, int lod = 0)
    {
        int _shift  = Block45Constants._shift;
        int cx      = (x >> _shift);
        int cy      = (y >> _shift);
        int cz      = (z >> _shift);
        int cxround = cx % _chunkNumX;
        int cyround = cy % _chunkNumY;
        int czround = cz % _chunkNumZ;
        int vx      = (x) & Block45Constants._mask;
        int vy      = (y) & Block45Constants._mask;
        int vz      = (z) & Block45Constants._mask;

        IntVector3 index = new IntVector3(cxround, cyround, czround);

        if (!_chunks.ContainsKey(index))
        {
            _chunks[index] = CreateChunk(index);
        }

        if (!_chunks[index].WriteVoxelAtIdx(vx, vy, vz, voxel))
        {
            return(0);
        }

        if ((voxel.blockType >> 2) == 0)
        {
            _SaveData.Remove(new IntVector3(x, y, z));
        }
        else
        {
            _SaveData[new IntVector3(x, y, z)] = voxel;
        }

        int minIdx = Block45Constants._numVoxelsPostfix;
        int maxIdx = Block45Constants._numVoxelsPerAxis - Block45Constants._numVoxelsPrefix;
        int fx = 0, fy = 0, fz = 0;
        int dirtyMask = 0x80;           // 0,1,2 bit for xyz dirty mask;4,5,6 bit for sign(neg->1);7 bit for current pos(now not used)

        // If write one edge's voxel may cause the other edge being modified
        if (vx < minIdx && cx > 0)
        {
            fx = -1; dirtyMask |= 0x11;
        }
        else
        if (vx >= maxIdx && cx < Block45Constants._worldMaxCX - 1)
        {
            fx = 1; dirtyMask |= 0x01;
        }
        if (vy < minIdx && cy > 0)
        {
            fy = -1; dirtyMask |= 0x22;
        }
        else
        if (vy >= maxIdx && cy < Block45Constants._worldMaxCY - 1)
        {
            fy = 1; dirtyMask |= 0x02;
        }
        if (vz < minIdx && cz > 0)
        {
            fz = -1; dirtyMask |= 0x44;
        }
        else
        if (vz >= maxIdx && cz < Block45Constants._worldMaxCZ - 1)
        {
            fz = 1; dirtyMask |= 0x04;
        }

        if (dirtyMask != 0x80)
        {
            for (int i = 1; i < 8; i++)
            {
                if ((dirtyMask & i) == i)
                {
                    int dx = fx * nearChunkOfs[i, 0], dy = fy * nearChunkOfs[i, 1], dz = fz * nearChunkOfs[i, 2];
                    cxround = (cx + dx) % _chunkNumX;
                    cyround = (cy + dy) % _chunkNumY;
                    czround = (cz + dz) % _chunkNumZ;

                    index = new IntVector3(cxround, cyround, czround);
                    if (!_chunks.ContainsKey(index))
                    {
                        _chunks[index] = CreateChunk(index);
                    }

                    try
                    {
                        if (!_chunks[index].WriteVoxelAtIdx(
                                vx - dx * Block45Constants._numVoxelsPerAxis,
                                vy - dy * Block45Constants._numVoxelsPerAxis,
                                vz - dz * Block45Constants._numVoxelsPerAxis, voxel))
                        {
                            dirtyMask |= i << 8;                                // flag for unsuccessful write
                        }
                    }catch (Exception)
                    {
                        Debug.LogError("Unexpected block45 write at(" + cxround + "," + cyround + "," + czround + ")");
                    }
                }
            }
        }
        return(dirtyMask);
    }
Beispiel #29
0
    public override void OnGL()
    {
        if (!enabled || !gameObject.activeInHierarchy)
        {
            return;
        }

        if (BSInput.s_MouseOnUI)
        {
            return;
        }

//		if (_LineMaterial == null)
//		{
//			_LineMaterial = new Material(Shader.Find("Lines/Colored Blended"));
//			_LineMaterial.hideFlags = HideFlags.HideAndDontSave;
//			_LineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
//		}

        if (m_Material == null)
        {
            m_Material                  = new Material(Shader.Find("Unlit/Transparent Colored"));
            m_Material.hideFlags        = HideFlags.HideAndDontSave;
            m_Material.shader.hideFlags = HideFlags.HideAndDontSave;
        }

        GL.PushMatrix();

//		_LineMaterial.SetPass(0);
        m_Material.SetPass(0);

        // Edge
        float expand = m_Expand * Block45Constants._scale;

        List <Bounds> boxes       = new List <Bounds>();
        List <Color>  line_colors = new List <Color>();

        for (float x = -expand; x <= expand; x += Block45Constants._scale)
        {
            for (float y = -expand; y <= expand; y += Block45Constants._scale)
            {
                for (float z = -expand; z <= expand; z += Block45Constants._scale)
                {
                    Vector3 pos = new Vector3(x + m_Center.x, y + m_Center.y, z + m_Center.z);

                    B45Block block = Block45Man.self.DataSource.SafeRead(Mathf.FloorToInt(pos.x * Block45Constants._scaleInverted),
                                                                         Mathf.FloorToInt(pos.y * Block45Constants._scaleInverted),
                                                                         Mathf.FloorToInt(pos.z * Block45Constants._scaleInverted));

//					bool draw = false;
                    bool contain_block = false;
                    if (block.blockType != 0 || block.materialType != 0)
                    {
                        contain_block = true;
                    }

                    VFVoxel voxel = VFVoxelTerrain.self.Voxels.SafeRead(Mathf.FloorToInt(pos.x + 0.5f), Mathf.FloorToInt(pos.y + 0.5f), Mathf.FloorToInt(pos.z + 0.5f));

                    Bounds box = new Bounds(Vector3.zero, Vector3.zero);

                    float sv = (float)(Mathf.Max(Mathf.Abs(x), Mathf.Abs(y), Mathf.Abs(z))) / (float)(expand);
                    Color lc = m_BoxColors.Evaluate(sv);


                    if (voxel.Volume > 1)
                    {
                        box.SetMinMax(new Vector3(pos.x, pos.y, pos.z),
                                      new Vector3(pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f));

                        // Shrink
                        float shrink = 0.02f;
                        if (Camera.main != null)
                        {
                            float dist_min = (Camera.main.transform.position - box.min).magnitude;
                            float dist_max = (Camera.main.transform.position - box.max).magnitude;

                            dist_max = dist_max > dist_min ? dist_max : dist_min;

                            shrink = Mathf.Clamp(dist_max * 0.002f, 0.02f, 0.1f);
                        }


                        box.min -= new Vector3(shrink, shrink, shrink);
                        box.max += new Vector3(shrink, shrink, shrink);

                        Color vc = m_BoxColors.Evaluate((float)voxel.Volume / 255.0f);
                        lc.r = vc.r;
                        lc.g = vc.g;
                        lc.b = vc.b;
//						draw = true;


                        boxes.Add(box);
                        line_colors.Add(lc);
                    }
                    else if (contain_block)
                    {
                        box.SetMinMax(new Vector3(pos.x, pos.y, pos.z),
                                      new Vector3(pos.x + 0.5f, pos.y + 0.5f, pos.z + 0.5f));

                        // Shrink
                        float shrink = 0.02f;
                        if (Camera.main != null)
                        {
                            float dist_min = (Camera.main.transform.position - box.min).magnitude;
                            float dist_max = (Camera.main.transform.position - box.max).magnitude;

                            dist_max = dist_max > dist_min ? dist_max : dist_min;

                            shrink = Mathf.Clamp(dist_max * 0.002f, 0.02f, 0.1f);
                        }


                        box.min -= new Vector3(shrink, shrink, shrink);
                        box.max += new Vector3(shrink, shrink, shrink);

                        boxes.Add(box);
                        line_colors.Add(lc);
//						draw = true;
                    }
                }
            }
        }

        // Shrink

        // Draw line
        GL.Begin(GL.LINES);
        for (int i = 0; i < boxes.Count; i++)
        {
            Color lc = line_colors[i];
            lc.a *= 1.5f;
            if (lc.a > 0.01f)
            {
                Bounds box = boxes[i];
                GL.Color(line_colors[i]);
                GL.Vertex3(box.min.x, box.min.y, box.min.z);
                GL.Vertex3(box.max.x, box.min.y, box.min.z);
                GL.Vertex3(box.min.x, box.min.y, box.max.z);
                GL.Vertex3(box.max.x, box.min.y, box.max.z);
                GL.Vertex3(box.min.x, box.max.y, box.min.z);
                GL.Vertex3(box.max.x, box.max.y, box.min.z);
                GL.Vertex3(box.min.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.max.z);

                GL.Vertex3(box.min.x, box.min.y, box.min.z);
                GL.Vertex3(box.min.x, box.max.y, box.min.z);
                GL.Vertex3(box.min.x, box.min.y, box.max.z);
                GL.Vertex3(box.min.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.min.y, box.min.z);
                GL.Vertex3(box.max.x, box.max.y, box.min.z);
                GL.Vertex3(box.max.x, box.min.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.max.z);

                GL.Vertex3(box.min.x, box.min.y, box.min.z);
                GL.Vertex3(box.min.x, box.min.y, box.max.z);
                GL.Vertex3(box.min.x, box.max.y, box.min.z);
                GL.Vertex3(box.min.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.min.y, box.min.z);
                GL.Vertex3(box.max.x, box.min.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.min.z);
                GL.Vertex3(box.max.x, box.max.y, box.max.z);
            }
        }
        GL.End();

        // Draw quad
        GL.Begin(GL.QUADS);
        for (int i = 0; i < boxes.Count; i++)
        {
            if (line_colors[i].a > 0.01f)
            {
                Bounds box = boxes[i];
                GL.Color(line_colors[i]);
                GL.Vertex3(box.min.x, box.min.y, box.min.z);
                GL.Vertex3(box.min.x, box.min.y, box.max.z);
                GL.Vertex3(box.min.x, box.max.y, box.max.z);
                GL.Vertex3(box.min.x, box.max.y, box.min.z);

                GL.Vertex3(box.max.x, box.min.y, box.min.z);
                GL.Vertex3(box.max.x, box.min.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.min.z);

                GL.Vertex3(box.min.x, box.min.y, box.min.z);
                GL.Vertex3(box.min.x, box.min.y, box.max.z);
                GL.Vertex3(box.max.x, box.min.y, box.max.z);
                GL.Vertex3(box.max.x, box.min.y, box.min.z);

                GL.Vertex3(box.min.x, box.max.y, box.min.z);
                GL.Vertex3(box.min.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.min.z);

                GL.Vertex3(box.min.x, box.min.y, box.min.z);
                GL.Vertex3(box.min.x, box.max.y, box.min.z);
                GL.Vertex3(box.max.x, box.max.y, box.min.z);
                GL.Vertex3(box.max.x, box.min.y, box.min.z);

                GL.Vertex3(box.min.x, box.min.y, box.max.z);
                GL.Vertex3(box.min.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.max.y, box.max.z);
                GL.Vertex3(box.max.x, box.min.y, box.max.z);
            }
        }
        GL.End();

        GL.PopMatrix();
    }
    public void GetBuildingInfo(out Vector3 size, out Dictionary <IntVector3, B45Block> blocks, out List <Vector3> npcPosition
                                , out List <CreatItemInfo> itemList, out Dictionary <int, BuildingNpc> npcIdPosRot)
    {
        Bounds bound = new Bounds(Vector3.zero, Vector3.zero);

        blocks      = new Dictionary <IntVector3, B45Block>();
        npcPosition = new List <Vector3>();
        itemList    = new List <CreatItemInfo>();
        npcIdPosRot = new Dictionary <int, BuildingNpc>();

        TextAsset    textFile = Resources.Load(mPath) as TextAsset;
        MemoryStream ms       = new MemoryStream(textFile.bytes);
        BinaryReader _in      = new BinaryReader(ms);

        int readVersion = _in.ReadInt32();

        switch (readVersion)
        {
        case 2:
            int Size = _in.ReadInt32();
            for (int i = 0; i < Size; i++)
            {
                IntVector3 index = new IntVector3(_in.ReadInt32(), _in.ReadInt32(), _in.ReadInt32());
                B45Block   block = new B45Block(_in.ReadByte(), _in.ReadByte());
                if ((block.blockType >> 2) != 0)
                {
                    blocks[index] = block;
                    index         = new IntVector3(index);
                    index.x      += 1;
                    index.z      += 1;
                    bound.Encapsulate(BSBlock45Data.s_Scale * index.ToVector3());
                }
            }
            break;
        }


        _in.Close();
        ms.Close();

        BoundSize = size = bound.size;
        UnityEngine.Object subInfo = Resources.Load(mPath + "SubInfo");
        if (null != subInfo)
        {
            textFile = subInfo as TextAsset;
            ms       = new MemoryStream(textFile.bytes);
            _in      = new BinaryReader(ms);

            int version = _in.ReadInt32();
            int count   = _in.ReadInt32();
            switch (version)
            {
            case 1:
                for (int i = 0; i < count; i++)
                {
                    npcPosition.Add(new Vector3(_in.ReadSingle(), _in.ReadSingle(), _in.ReadSingle()));
                }
                break;

            case 2:
                for (int i = 0; i < count; i++)
                {
                    npcPosition.Add(new Vector3(_in.ReadSingle(), _in.ReadSingle(), _in.ReadSingle()));
                }
                count = _in.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    CreatItemInfo addItem = new CreatItemInfo();
                    addItem.mPos      = new Vector3(_in.ReadSingle(), _in.ReadSingle(), _in.ReadSingle());
                    addItem.mRotation = Quaternion.Euler(new Vector3(_in.ReadSingle(), _in.ReadSingle(), _in.ReadSingle()));
                    addItem.mItemId   = _in.ReadInt32();
                    itemList.Add(addItem);
                }
                break;
            }
            _in.Close();
            ms.Close();
        }

        //npc stand
        if (!mNpcIdPosRotStand.Equals("") && mNpcIdPosRotStand.Length > 1)
        {
            string[] npcs = mNpcIdPosRotStand.Split('_');
            for (int i = 0; i < npcs.Count(); i++)
            {
                string[]    npcIdPosRotStr = npcs[i].Split('~');
                int         id             = Convert.ToInt32(npcIdPosRotStr[0]);
                string[]    posRot         = npcIdPosRotStr[1].Split(';');
                string[]    posStr         = posRot[0].Split(',');
                Vector3     pos            = new Vector3(float.Parse(posStr[0]), float.Parse(posStr[1]), float.Parse(posStr[2]));
                float       rot            = float.Parse(posRot[1]);
                BuildingNpc bdnpc          = new BuildingNpc(id, pos, rot, true);
                npcIdPosRot.Add(id, bdnpc);
            }
        }

        //npc move
        if (!mNpcIdPosRotMove.Equals("") && mNpcIdPosRotMove.Length > 1)
        {
            string[] npcs = mNpcIdPosRotMove.Split('_');
            for (int i = 0; i < npcs.Count(); i++)
            {
                string[]    npcIdPosRotStr = npcs[i].Split('~');
                int         id             = Convert.ToInt32(npcIdPosRotStr[0]);
                string[]    posRot         = npcIdPosRotStr[1].Split(';');
                string[]    posStr         = posRot[0].Split(',');
                Vector3     pos            = new Vector3(float.Parse(posStr[0]), float.Parse(posStr[1]), float.Parse(posStr[2]));
                float       rot            = float.Parse(posRot[1]);
                BuildingNpc bdnpc          = new BuildingNpc(id, pos, rot, false);
                npcIdPosRot.Add(id, bdnpc);
            }
        }
    }