Пример #1
0
        void DestroyTerrian()
        {
            BSVoxel voxel = new BSVoxel();

            voxel.blockType    = (byte)0;
            voxel.materialType = (byte)0;

            for (int i = 9; i <= 14; i++)
            {
                Vector3 pos1 = new Vector3(10f, 2f, i);
                pos1 = new Vector3(pos1.x * BuildingMan.Blocks.ScaleInverted,
                                   pos1.y * BuildingMan.Blocks.ScaleInverted,
                                   pos1.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos1.x, (int)pos1.y, (int)pos1.z);

                Vector3 pos2 = new Vector3(10f, 2f, i + 0.5f);
                pos2 = new Vector3(pos2.x * BuildingMan.Blocks.ScaleInverted,
                                   pos2.y * BuildingMan.Blocks.ScaleInverted,
                                   pos2.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos2.x, (int)pos2.y, (int)pos2.z);

                Vector3 pos3 = new Vector3(10f, 1.5f, i);
                pos3 = new Vector3(pos3.x * BuildingMan.Blocks.ScaleInverted,
                                   pos3.y * BuildingMan.Blocks.ScaleInverted,
                                   pos3.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos3.x, (int)pos3.y, (int)pos3.z);

                Vector3 pos4 = new Vector3(10f, 1.5f, i + 0.5f);
                pos4 = new Vector3(pos4.x * BuildingMan.Blocks.ScaleInverted,
                                   pos4.y * BuildingMan.Blocks.ScaleInverted,
                                   pos4.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos4.x, (int)pos4.y, (int)pos4.z);
            }
        }
Пример #2
0
    public void DeleteVoxels()
    {
        List <IntVector3> selections = GetSelectionPos();

        if (selections.Count == 0)
        {
            return;
        }


        List <BSVoxel> new_voxels = new List <BSVoxel>();
        List <BSVoxel> old_voxels = new List <BSVoxel>();

        for (int i = 0; i < selections.Count; i++)
        {
            BSVoxel voxel = dataSource.Read(selections[i].x, selections[i].y, selections[i].z);

            new_voxels.Add(new BSVoxel());
            old_voxels.Add(voxel);
        }

        BSAction      action = new BSAction();
        BSVoxelModify vm     = new BSVoxelModify(selections.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, EBSBrushMode.Subtract);

        action.AddModify(vm);
        vm.Redo();
        BSHistory.AddAction(action);

        ResetDrawing();
    }
Пример #3
0
    internal static void ReadBSVoxel(BinaryReader _reader, out BSVoxel _value)
    {
        byte value0 = _reader.ReadByte();
        byte value1 = _reader.ReadByte();

        _value = new BSVoxel(value0, value1);
    }
Пример #4
0
    protected static void FindExtraExtendableVoxels(IBSDataSource ds, List <BSVoxel> new_voxels, List <BSVoxel> old_voxels, List <IntVector3> indexes, Dictionary <IntVector3, int> refMap)
    {
        List <BSVoxel>    ext_new_voxels = new List <BSVoxel>();
        List <IntVector3> ext_indexes    = new List <IntVector3>();
        List <BSVoxel>    ext_old_voxels = new List <BSVoxel>();

        for (int i = 0; i < indexes.Count; i++)
        {
            List <IntVector4> ext_posList = null;
            List <BSVoxel>    ext_voxels  = null;

            if (ds.ReadExtendableBlock(new IntVector4(indexes[i], 0), out ext_posList, out ext_voxels))
            {
                for (int j = 0; j < ext_voxels.Count; j++)
                {
                    IntVector3 _ipos = new IntVector3(ext_posList[j].x, ext_posList[j].y, ext_posList[j].z);


                    if (!refMap.ContainsKey(_ipos))
                    {
                        BSVoxel v = ds.Read(_ipos.x, _ipos.y, _ipos.z);
                        ext_old_voxels.Add(v);
                        ext_indexes.Add(_ipos);
                        ext_new_voxels.Add(new BSVoxel());
                    }
                }
            }
        }

        indexes.AddRange(ext_indexes);
        new_voxels.AddRange(ext_new_voxels);
        old_voxels.AddRange(ext_old_voxels);
    }
Пример #5
0
 // Set voxel
 public void SetVoxel(int pos, BSVoxel voxel)
 {
     if (m_HeadInfo.Mode == EBSVoxelType.Block)
     {
         if (voxel.type == 0)
         {
             m_Voxels.Remove(pos);
         }
         else
         {
             m_Voxels[pos] = voxel;
         }
     }
     else if (m_HeadInfo.Mode == EBSVoxelType.Voxel)
     {
         if (voxel.volmue == 0)
         {
             m_Voxels.Remove(pos);
         }
         else
         {
             m_Voxels[pos] = voxel;
         }
     }
     else
     {
         Debug.LogError("Unknow voxel type");
     }
 }
Пример #6
0
    public BSPattern(SqliteDataReader reader)
    {
        ID        = Convert.ToInt32(reader.GetString(reader.GetOrdinal("ID")));
        size      = Convert.ToInt32(reader.GetString(reader.GetOrdinal("Size")));
        IconName  = reader.GetString(reader.GetOrdinal("IconName"));
        MeshPath  = reader.GetString(reader.GetOrdinal("MeshPath"));
        voxelList = new BSVoxel[size, size, size];
        type      = EBSVoxelType.Block;

        string blockString = reader.GetString(reader.GetOrdinal("Blocks"));

        string[] _blockList = blockString.Split(';');
        int      count      = 0;

        for (int y = 0; y < size; y++)
        {
            for (int z = 0; z < size; z++)
            {
                for (int x = 0; x < size; x++)
                {
                    string[] valueList = _blockList[count].Split(',');
                    if (type == EBSVoxelType.Block)
                    {
                        voxelList[x, y, z] = new BSVoxel(BSVoxel.MakeBlockType(Convert.ToInt32(valueList[0]), Convert.ToInt32(valueList[1])), 0);
                    }
                    else if (type == EBSVoxelType.Voxel)
                    {
                        voxelList[x, y, z] = new BSVoxel(255, 2);
                    }

                    count++;
                }
            }
        }
    }
Пример #7
0
    public static void MakeExtendableBS(int primitiveType, int rotation, int extendDir, int length, int materialType, out BSVoxel b0, out BSVoxel b1)
    {
        B45Block block0, block1;

        B45Block.MakeExtendableBlock(primitiveType, rotation, extendDir, length, materialType, out block0, out block1);
        b0 = new BSVoxel(block0);
        b1 = new BSVoxel(block1);
    }
Пример #8
0
    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));
    }
Пример #9
0
    bool OnCanClickSaveBtn()
    {
        if (PEBuildingMan.Self == null)
        {
            return(true);
        }


        if (!PEBuildingMan.Self.selectVoxel)
        {
            BSSelectBrush select_brush = m_CurBrush as BSSelectBrush;

            if (select_brush != null)
            {
                if (!select_brush.IsEmpty())
                {
                    PEBuildingMan.Self.IsoCaputure.Computer.ClearDataDS();
                    foreach (var kvp in select_brush.Selections)
                    {
                        BSVoxel voxel = BuildingMan.Blocks.Read(kvp.Key.x, kvp.Key.y, kvp.Key.z);
                        PEBuildingMan.Self.IsoCaputure.Computer.AlterBlockInBuild(kvp.Key.x, kvp.Key.y, kvp.Key.z, voxel.ToBlock());
                    }

                    PEBuildingMan.Self.IsoCaputure.Computer.RebuildMesh();
                    PEBuildingMan.Self.IsoCaputure.EnableCapture();

                    mSaveWnd.SetIsoItemContent(PEBuildingMan.Self.IsoCaputure.photoRT);
                    select_brush.canDo = false;
                    return(true);
                }
            }

            BSIsoSelectBrush iso_select = m_CurBrush as BSIsoSelectBrush;
            //BSIsoSelectBrush iso_select = PEBuildingMan.Self.Manipulator.activeBrush as BSIsoSelectBrush;
            if (iso_select != null)
            {
                List <IntVector3> voxels = iso_select.GetSelectionPos();
                if (voxels.Count != 0)
                {
                    PEBuildingMan.Self.IsoCaputure.Computer.ClearDataDS();
                    for (int i = 0; i < voxels.Count; i++)
                    {
                        BSVoxel voxel = BuildingMan.Blocks.Read(voxels[i].x, voxels[i].y, voxels[i].z);
                        PEBuildingMan.Self.IsoCaputure.Computer.AlterBlockInBuild(voxels[i].x, voxels[i].y, voxels[i].z, voxel.ToBlock());
                    }

                    PEBuildingMan.Self.IsoCaputure.Computer.RebuildMesh();
                    PEBuildingMan.Self.IsoCaputure.EnableCapture();

                    mSaveWnd.SetIsoItemContent(PEBuildingMan.Self.IsoCaputure.photoRT);
                    return(true);
                }
            }
        }

        return(false);
    }
Пример #10
0
    public bool SafeWrite(BSVoxel voxel, int x, int y, int z, int lod = 0)
    {
        if (voxelWrite != null)
        {
            voxelWrite.Invoke(new int[] { x, y, z });
        }

        return(Block45Man.self.DataSource.SafeWrite(voxel.ToBlock(), x, y, z, lod));
    }
Пример #11
0
    protected void ExtrudeSelection(int x, int y, int z)
    {
        if (x == 0 && y == 0 && z == 0)
        {
            return;
        }

        if (m_PrevDS != dataSource)
        {
            return;
        }

        _extruding = true;

        Dictionary <IntVector3, byte> new_selection = new Dictionary <IntVector3, byte>();

        //BSVoxelModify modify = new BSVoxelModify(
        List <IntVector3>            indexes     = new List <IntVector3>();
        List <BSVoxel>               old_voxels  = new List <BSVoxel>();
        List <BSVoxel>               new_voxels  = new List <BSVoxel>();
        Dictionary <IntVector3, int> refVoxelMap = new Dictionary <IntVector3, int>();

        foreach (KeyValuePair <IntVector3, byte> kvp in m_Selections)
        {
            IntVector3 ipos = new IntVector3(kvp.Key);
            ipos.x += x;
            ipos.y += y;
            ipos.z += z;


            BSVoxel new_voxel = dataSource.SafeRead(kvp.Key.x, kvp.Key.y, kvp.Key.z);
            BSVoxel old_voxel = dataSource.SafeRead(ipos.x, ipos.y, ipos.z);

            indexes.Add(ipos);
            old_voxels.Add(old_voxel);
            new_voxels.Add(new_voxel);

            new_selection.Add(ipos, kvp.Value);
            refVoxelMap.Add(ipos, 0);
        }

        // Extra Extendable
        FindExtraExtendableVoxels(dataSource, new_voxels, old_voxels, indexes, refVoxelMap);



        BSVoxelModify modify = new BSVoxelModify(indexes.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, EBSBrushMode.Add);

        m_Action.AddModify(modify);
        modify.Redo();

        Dictionary <IntVector3, byte> old_selection = new Dictionary <IntVector3, byte>(m_Selections);
        BSSelectedBoxModify           sm            = new BSSelectedBoxModify(old_selection, new_selection, this);

        m_Action.AddModify(sm);
        sm.Redo();
    }
Пример #12
0
    public BSVoxel Add(BSVoxel voxel, int x, int y, int z, int lod = 0)
    {
        VFVoxel old_voxel = VFVoxelTerrain.self.Voxels.Read(x, y, z, lod);

        voxel.volmue = (byte)Mathf.Clamp(old_voxel.Volume + voxel.volmue, 0, 255);

        VFVoxelTerrain.self.Voxels.Write(x, y, z, voxel.ToVoxel(), lod);

        return(voxel);
    }
Пример #13
0
    public BSVoxel Add(BSVoxel voxel, int x, int y, int z, int lod = 0)
    {
        if ((voxel.value0 >> 2) == 0)
        {
            return(new BSVoxel(Block45Man.self.DataSource.Read(x, y, z)));
        }
        Block45Man.self.DataSource.Write(voxel.ToBlock(), x, y, z, lod);

        return(voxel);
    }
Пример #14
0
    public BSVoxel Subtract(BSVoxel voxel, int x, int y, int z, int lod = 0)
    {
        VFVoxel old_voxel = VFVoxelTerrain.self.Voxels.Read(x, y, z, lod);
        VFVoxel new_voxel = new VFVoxel(old_voxel.Volume, old_voxel.Type);

        new_voxel.Volume = (byte)Mathf.Clamp(old_voxel.Volume - voxel.volmue, 0, 255);

        VFVoxelTerrain.self.AlterVoxelInBuild(x, y, z, new_voxel);

        return(voxel);
    }
Пример #15
0
    void WriteBSVoxel(IBSDataSource ds, List <BSVoxel> new_voxels, List <BSVoxel> old_voxels, List <IntVector3> indexes, Dictionary <IntVector3, int> refMap, BSVoxel voxel, int x, int y, int z)
    {
        //				ds.SafeWrite(voxel, x, y, z);
        // Old voxel
        IntVector3 idx = new IntVector3(x, y, z);
        BSVoxel    old = ds.SafeRead(idx.x, idx.y, idx.z);

        old_voxels.Add(old);
        new_voxels.Add(voxel);
        indexes.Add(idx);
        refMap[idx] = 0;
    }
Пример #16
0
    public void DeleteVoxel()
    {
        if (m_SelectionBoxes.Count == 0)
        {
            return;
        }

        List <BSVoxel>    new_voxels = new List <BSVoxel>();
        List <IntVector3> indexes    = new List <IntVector3>();
        List <BSVoxel>    old_voxels = new List <BSVoxel>();

        foreach (BSTools.SelBox box in m_SelectionBoxes)
        {
            for (int x = box.m_Box.xMin; x <= box.m_Box.xMax; x++)
            {
                for (int y = box.m_Box.yMin; y <= box.m_Box.yMax; y++)
                {
                    for (int z = box.m_Box.zMin; z <= box.m_Box.zMax; z++)
                    {
                        BSVoxel voxel = dataSource.Read(x, y, z);

                        new_voxels.Add(new BSVoxel());
                        indexes.Add(new IntVector3(x, y, z));
                        old_voxels.Add(voxel);
                    }
                }
            }
        }


        // Select modify
        ClearSelection(m_Action);

        // Modity modify
        if (indexes.Count != 0)
        {
            BSVoxelModify vm = new BSVoxelModify(indexes.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, EBSBrushMode.Subtract);

            m_Action.AddModify(vm);
            vm.Redo();
        }
    }
Пример #17
0
        void CreatTerrian()
        {
            BSVoxel voxel = new BSVoxel();

            voxel.blockType = BSVoxel.MakeBlockType(1, 0);
            if (BSBlockMatMap.s_ItemToMat.ContainsKey(281))
            {
                voxel.materialType = (byte)BSBlockMatMap.s_ItemToMat[281];
            }
            else
            {
                voxel.materialType = 0;
            }

            for (int i = 9; i <= 14; i++)
            {
                Vector3 pos1 = new Vector3(10f, 2f, i);
                pos1 = new Vector3(pos1.x * BuildingMan.Blocks.ScaleInverted,
                                   pos1.y * BuildingMan.Blocks.ScaleInverted,
                                   pos1.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos1.x, (int)pos1.y, (int)pos1.z);

                Vector3 pos2 = new Vector3(10f, 2f, i + 0.5f);
                pos2 = new Vector3(pos2.x * BuildingMan.Blocks.ScaleInverted,
                                   pos2.y * BuildingMan.Blocks.ScaleInverted,
                                   pos2.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos2.x, (int)pos2.y, (int)pos2.z);

                Vector3 pos3 = new Vector3(10f, 1.5f, i);
                pos3 = new Vector3(pos3.x * BuildingMan.Blocks.ScaleInverted,
                                   pos3.y * BuildingMan.Blocks.ScaleInverted,
                                   pos3.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos3.x, (int)pos3.y, (int)pos3.z);

                Vector3 pos4 = new Vector3(10f, 1.5f, i + 0.5f);
                pos4 = new Vector3(pos4.x * BuildingMan.Blocks.ScaleInverted,
                                   pos4.y * BuildingMan.Blocks.ScaleInverted,
                                   pos4.z * BuildingMan.Blocks.ScaleInverted);
                BuildingMan.Blocks.SafeWrite(voxel, (int)pos4.x, (int)pos4.y, (int)pos4.z);
            }
        }
Пример #18
0
        public void CloseMission_buildCreateISO()
        {
            GameObject.Destroy(isoCube1);
            GameObject.Destroy(isoCube2);

            BSBlock45Data.voxelWrite -= AddRecordVoxel;
            BSVoxel vol = new BSVoxel(0, 0);

            for (int i = 0; i < recordVoxelRemove.Count; i++)
            {
                int[] tmp = recordVoxelRemove[i];
                BuildingMan.Blocks.SafeWrite(vol, tmp[0], tmp[1], tmp[2]);
            }
            Pathea.MotionMgrCmpt mmc = Pathea.PeCreature.Instance.mainPlayer.GetCmpt <Pathea.MotionMgrCmpt>();
            if (mmc != null)
            {
                mmc.EndAction(Pathea.PEActionType.Build);
            }

            receiverGroup.SetActive(false);
            DestroyTerrian();
        }
Пример #19
0
    public List <IntVector3> GetSelectionPos()
    {
        IntVector3 min = Min * dataSource.ScaleInverted;
        IntVector3 max = Max * dataSource.ScaleInverted;

        List <IntVector3> pos = new List <IntVector3>();

        for (int x = min.x; x < max.x; x++)
        {
            for (int y = min.y; y < max.y; y++)
            {
                for (int z = min.z; z < max.z; z++)
                {
                    BSVoxel voxel = dataSource.Read(x, y, z);
                    if (!dataSource.VoxelIsZero(voxel, 0))
                    {
                        pos.Add(new IntVector3(x, y, z));
                    }
                }
            }
        }

        return(pos);
    }
Пример #20
0
    public bool SaveToIso(string IsoName, byte[] icon_tex, out BSIsoData outData)
    {
        outData = null;

        List <IntVector3> selections = GetSelectionPos();

        if (selections.Count == 0)
        {
            return(false);
        }

        // Only the block can save to be ISO
        if (pattern.type != EBSVoxelType.Block)
        {
            Debug.LogWarning("The iso is not support the Voxel");
            return(false);
        }

        BSIsoData iso = new BSIsoData();

        iso.Init(pattern.type);
        iso.m_HeadInfo.Name = IsoName;

        IntVector3 min = new IntVector3(selections[0]);
        IntVector3 max = new IntVector3(selections[0]);

        for (int i = 1; i < selections.Count; i++)
        {
            min.x = (min.x > selections[i].x ? selections[i].x:min.x);
            min.y = (min.y > selections[i].y ? selections[i].y:min.y);
            min.z = (min.z > selections[i].z ? selections[i].z:min.z);
            max.x = (max.x < selections[i].x ? selections[i].x:max.x);
            max.y = (max.y < selections[i].y ? selections[i].y:max.y);
            max.z = (max.z < selections[i].z ? selections[i].z:max.z);
        }

        iso.m_HeadInfo.xSize   = max.x - min.x + 1;
        iso.m_HeadInfo.ySize   = max.y - min.y + 1;
        iso.m_HeadInfo.zSize   = max.z - min.z + 1;
        iso.m_HeadInfo.IconTex = icon_tex;

        for (int i = 0; i < selections.Count; i++)
        {
            BSVoxel voxel = dataSource.SafeRead(selections[i].x, selections[i].y, selections[i].z);
            int     key   = BSIsoData.IPosToKey(selections[i].x - min.x, selections[i].y - min.y, selections[i].z - min.z);
            iso.m_Voxels.Add(key, voxel);
        }

        iso.CaclCosts();

        string FilePath = GameConfig.GetUserDataPath() + BuildingMan.s_IsoPath;

        /*bool r = */ SaveFile(FilePath, iso);

        if (SaveFile(FilePath, iso))
        {
            outData = iso;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Пример #21
0
    public bool SaveToIso(string IsoName, byte[] icon_tex, out BSIsoData outData)
    {
        outData = null;
        if (m_SelectionBoxes.Count == 0)
        {
            return(false);
        }

        // Only the block can save to be ISO
        if (pattern.type != EBSVoxelType.Block)
        {
            Debug.LogWarning("The iso is not support the Voxel");
            return(false);
        }

        BSIsoData iso = new BSIsoData();

        iso.Init(pattern.type);
        iso.m_HeadInfo.Name = IsoName;

        BSTools.IntBox bound = BSTools.SelBox.CalculateBound(m_SelectionBoxes);

        iso.m_HeadInfo.xSize   = bound.xMax - bound.xMin + 1;
        iso.m_HeadInfo.ySize   = bound.yMax - bound.yMin + 1;
        iso.m_HeadInfo.zSize   = bound.zMax - bound.zMin + 1;
        iso.m_HeadInfo.IconTex = icon_tex;

        foreach (BSTools.SelBox box in m_SelectionBoxes)
        {
            for (int x = box.m_Box.xMin; x <= box.m_Box.xMax; x++)
            {
                for (int y = box.m_Box.yMin; y <= box.m_Box.yMax; y++)
                {
                    for (int z = box.m_Box.zMin; z <= box.m_Box.zMax; z++)
                    {
                        BSVoxel voxel = dataSource.SafeRead(x, y, z);
                        int     key   = BSIsoData.IPosToKey(x - bound.xMin, y - bound.yMin, z - bound.zMin);
                        if (!dataSource.VoxelIsZero(voxel, 1))
                        {
                            iso.m_Voxels.Add(key, voxel);
                        }
                    }
                }
            }
        }

        if (iso.m_Voxels.Count == 0)
        {
            return(false);
        }

        iso.CaclCosts();

        string FilePath = GameConfig.GetUserDataPath() + BuildingMan.s_IsoPath;

        /*bool r = */ SaveFile(FilePath, iso);

        if (SaveFile(FilePath, iso))
        {
            ClearSelection(m_Action);
            outData = iso;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Пример #22
0
    // should be 2 multiply cur coord
    public void ApplyBevel2_10(IntVector3 up, IntVector3 dn, bool upVSeal, bool dnVSeal, IBSDataSource ds, byte matType,
                               List <BSVoxel> new_voxels, List <BSVoxel> old_voxels, List <IntVector3> indexes, Dictionary <IntVector3, int> refMap)
    {
        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);
            BSVoxel bOrg = new BSVoxel((byte)((t0 << 2) | r), matType);
            BSVoxel bFlp = new BSVoxel((byte)((t1 << 2) | ((r + 2) & 3)), matType);
            if (dnVSeal)
            {
                WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp, beg.x, beg.y - 1, beg.z);
            }
            for (int i = 0; i < n; i++)
            {
                WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg, beg.x + i * xstep, beg.y + i, beg.z + i * zstep);
                WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp, beg.x + (i + 1) * xstep, beg.y + i, beg.z + (i + 1) * zstep);
            }
            if (upVSeal)
            {
                WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg, beg.x + n * xstep, beg.y + n, beg.z + n * zstep);
            }
        }
        else
        {
            BSVoxel bOrg0, bOrg1, bOrg2;
            BSVoxel 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)
                {
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp0, beg.x, beg.y - l + 0, beg.z);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp1, beg.x, beg.y - l + 1, beg.z);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp2, beg.x, beg.y - l + 2, beg.z);
                    }
                }
                for (int i = 0; i < n; i++)
                {
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg0, beg.x + i * xstep, beg.y + i * l + 0, beg.z + i * zstep);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg1, beg.x + i * xstep, beg.y + i * l + 1, beg.z + i * zstep);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg2, beg.x + i * xstep, beg.y + i * l + 2, beg.z + i * zstep);
                    }

                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp0, beg.x + (i + 1) * xstep, beg.y + i * l + 0, beg.z + (i + 1) * zstep);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp1, beg.x + (i + 1) * xstep, beg.y + i * l + 1, beg.z + (i + 1) * zstep);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp2, beg.x + (i + 1) * xstep, beg.y + i * l + 2, beg.z + (i + 1) * zstep);
                    }
                }
                if (upVSeal)
                {
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg0, beg.x + n * xstep, beg.y + n * l + 0, beg.z + n * zstep);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg1, beg.x + n * xstep, beg.y + n * l + 1, beg.z + n * zstep);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg2, beg.x + n * xstep, beg.y + n * l + 2, beg.z + n * zstep);
                    }
                }
            }
            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)
                {
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp0, beg.x + xext0, beg.y - 1, beg.z + zext0);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp1, beg.x + xext1, beg.y - 1, beg.z + zext1);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp2, beg.x + xext2, beg.y - 1, beg.z + zext2);
                    }
                }
                for (int i = 0; i < dy; i++)
                {
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg0, beg.x + i * l * xstep + xext0, beg.y + i, beg.z + i * l * zstep + zext0);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg1, beg.x + i * l * xstep + xext1, beg.y + i, beg.z + i * l * zstep + zext1);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg2, beg.x + i * l * xstep + xext2, beg.y + i, beg.z + i * l * zstep + zext2);
                    }

                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp0, beg.x + (i + 1) * l * xstep + xext0, beg.y + i, beg.z + (i + 1) * l * zstep + zext0);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp1, beg.x + (i + 1) * l * xstep + xext1, beg.y + i, beg.z + (i + 1) * l * zstep + zext1);
                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bFlp2, beg.x + (i + 1) * l * xstep + xext2, beg.y + i, beg.z + (i + 1) * l * zstep + zext2);
                    }
                }
                if (upVSeal)
                {
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg0, beg.x + n * l * xstep + xext0, beg.y + n, beg.z + n * l * zstep + zext0);
                    WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg1, beg.x + n * l * xstep + xext1, beg.y + n, beg.z + n * l * zstep + zext1);

                    if (l == 3)
                    {
                        WriteBSVoxel(ds, new_voxels, old_voxels, indexes, refMap, bOrg2, beg.x + n * l * xstep + xext2, beg.y + n, beg.z + n * l * zstep + zext2);
                    }
                }
            }
        }
    }
Пример #23
0
 public bool SafeWrite(BSVoxel voxel, int x, int y, int z, int lod = 0)
 {
     VFVoxelTerrain.self.AlterVoxelInBuild(x, y, z, voxel.ToVoxel());
     return(true);
 }
Пример #24
0
 public bool VoxelIsZero(BSVoxel voxel, float volume)
 {
     return(voxel.value0 < volume);
 }
Пример #25
0
    protected override void Do()
    {
        int     cnt    = pattern.size;
        Vector3 center = new Vector3((cnt - 1) / 2.0f, 0, (cnt - 1) / 2.0f);

        if (mode == EBSBrushMode.Add)
        {
            List <BSVoxel>    new_voxels = new List <BSVoxel>();
            List <IntVector3> indexes    = new List <IntVector3>();
            List <BSVoxel>    old_voxels = new List <BSVoxel>();


            for (int x = 0; x < cnt; x++)
            {
                for (int y = 0; y < cnt; y++)
                {
                    for (int z = 0; z < cnt; z++)
                    {
                        // rote if need
                        Vector3 offset = Quaternion.Euler(0, 90 * m_Rot, 0) * new Vector3(x - center.x, y - center.y, z - center.z) + center;

                        IntVector3 inpos = new IntVector3(Mathf.FloorToInt(m_Cursor.x * dataSource.ScaleInverted) + offset.x,
                                                          Mathf.FloorToInt(m_Cursor.y * dataSource.ScaleInverted) + offset.y,
                                                          Mathf.FloorToInt(m_Cursor.z * dataSource.ScaleInverted) + offset.z);

                        // new voxel
                        BSVoxel voxel = pattern.voxelList[x, y, z];
                        voxel.materialType = materialType;
                        voxel.blockType    = BSVoxel.MakeBlockType(voxel.blockType >> 2, ((voxel.blockType & 0X3) + m_Rot) % 4);

                        //olde voxel
                        BSVoxel old_voxel = dataSource.SafeRead(inpos.x, inpos.y, inpos.z);

                        new_voxels.Add(voxel);
                        old_voxels.Add(old_voxel);
                        indexes.Add(inpos);
                    }
                }
            }

            // Modify
            if (indexes.Count != 0)
            {
                BSAction action = new BSAction();

                BSVoxelModify modify = new BSVoxelModify(indexes.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, mode);

                action.AddModify(modify);

                if (action.Do())
                {
                    BSHistory.AddAction(action);
                }
            }
        }
        else if (mode == EBSBrushMode.Subtract)
        {
            List <BSVoxel>    new_voxels = new List <BSVoxel>();
            List <IntVector3> indexes    = new List <IntVector3>();
            List <BSVoxel>    old_voxels = new List <BSVoxel>();

            for (int x = 0; x < cnt; x++)
            {
                for (int y = 0; y < cnt; y++)
                {
                    for (int z = 0; z < cnt; z++)
                    {
                        IntVector3 inpos = new IntVector3(Mathf.FloorToInt(m_Cursor.x * dataSource.ScaleInverted) + x,
                                                          Mathf.FloorToInt(m_Cursor.y * dataSource.ScaleInverted) + y,
                                                          Mathf.FloorToInt(m_Cursor.z * dataSource.ScaleInverted) + z);

                        BSVoxel voxel = dataSource.Read(inpos.x, inpos.y, inpos.z);
                        new_voxels.Add(new BSVoxel());
                        indexes.Add(inpos);
                        old_voxels.Add(voxel);
                    }
                }
            }

            // Action
            if (indexes.Count != 0)
            {
                BSAction action = new BSAction();

                BSVoxelModify modify = new BSVoxelModify(indexes.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, mode);

                action.AddModify(modify);

                if (action.Do())
                {
                    BSHistory.AddAction(action);
                }
            }
        }
    }
Пример #26
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);
    }
Пример #27
0
    protected override void Do()
    {
        if (gizmoTrigger.RayCast)
        {
            return;
        }


        Vector3 min = Min * dataSource.ScaleInverted;
        Vector3 max = Max * dataSource.ScaleInverted;

        int     cnt    = pattern.size;
        Vector3 center = new Vector3((cnt - 1) / 2.0f, 0, (cnt - 1) / 2.0f);

        if (mode == EBSBrushMode.Add)
        {
            List <BSVoxel>               new_voxels  = new List <BSVoxel>();
            List <IntVector3>            indexes     = new List <IntVector3>();
            List <BSVoxel>               old_voxels  = new List <BSVoxel>();
            Dictionary <IntVector3, int> refVoxelMap = new Dictionary <IntVector3, int>();

            for (int x = (int)min.x; x < (int)max.x; x += cnt)
            {
                for (int y = (int)min.y; y < (int)max.y; y += cnt)
                {
                    for (int z = (int)min.z; z < (int)max.z; z += cnt)
                    {
                        for (int ix = 0; ix < cnt; ix++)
                        {
                            for (int iy = 0; iy < cnt; iy++)
                            {
                                for (int iz = 0; iz < cnt; iz++)
                                {
                                    // rote if need
                                    Vector3 offset = Quaternion.Euler(0, 90 * m_Rot, 0) * new Vector3(ix - center.x, iy - center.y, iz - center.z) + center;

                                    IntVector3 inpos = new IntVector3(Mathf.FloorToInt(x) + offset.x,
                                                                      Mathf.FloorToInt(y) + offset.y,
                                                                      Mathf.FloorToInt(z) + offset.z);

                                    // new voxel
                                    BSVoxel voxel = pattern.voxelList[ix, iy, iz];
                                    voxel.materialType = materialType;
                                    voxel.blockType    = BSVoxel.MakeBlockType(voxel.blockType >> 2, ((voxel.blockType & 0X3) + m_Rot) % 4);

                                    //olde voxel
                                    BSVoxel old_voxel = dataSource.SafeRead(inpos.x, inpos.y, inpos.z);

                                    new_voxels.Add(voxel);
                                    old_voxels.Add(old_voxel);
                                    indexes.Add(inpos);
                                    refVoxelMap.Add(inpos, 0);
                                }
                            }
                        }
                    }
                }
            }

            // Extra Extendable
            FindExtraExtendableVoxels(dataSource, new_voxels, old_voxels, indexes, refVoxelMap);

            // new Modify ?
            if (indexes.Count != 0)
            {
                BSAction action = new BSAction();

                BSVoxelModify modify = new BSVoxelModify(indexes.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, mode);

                action.AddModify(modify);

                if (action.Do())
                {
                    BSHistory.AddAction(action);
                }
            }
        }
        else if (mode == EBSBrushMode.Subtract)
        {
            List <BSVoxel>               new_voxels  = new List <BSVoxel>();
            List <IntVector3>            indexes     = new List <IntVector3>();
            List <BSVoxel>               old_voxels  = new List <BSVoxel>();
            Dictionary <IntVector3, int> refVoxelMap = new Dictionary <IntVector3, int>();

            for (int x = (int)min.x; x < (int)max.x; x += cnt)
            {
                for (int y = (int)min.y; y < (int)max.y; y += cnt)
                {
                    for (int z = (int)min.z; z < (int)max.z; z += cnt)
                    {
                        for (int ix = 0; ix < cnt; ix++)
                        {
                            for (int iy = 0; iy < cnt; iy++)
                            {
                                for (int iz = 0; iz < cnt; iz++)
                                {
                                    IntVector3 inpos = new IntVector3(Mathf.FloorToInt(x) + ix,
                                                                      Mathf.FloorToInt(y) + iy,
                                                                      Mathf.FloorToInt(z) + iz);


                                    BSVoxel voxel = dataSource.Read(inpos.x, inpos.y, inpos.z);

                                    new_voxels.Add(new BSVoxel());
                                    indexes.Add(inpos);
                                    old_voxels.Add(voxel);
                                    refVoxelMap[inpos] = 0;

                                    // extendtable
                                    List <IntVector4> ext_posList = null;
                                    List <BSVoxel>    ext_voxels  = null;
                                    if (dataSource.ReadExtendableBlock(new IntVector4(inpos, 0), out ext_posList, out ext_voxels))
                                    {
                                        for (int i = 0; i < ext_voxels.Count; i++)
                                        {
                                            IntVector3 _ipos = new IntVector3(ext_posList[i].x, ext_posList[i].y, ext_posList[i].z);

                                            if (_ipos == inpos)
                                            {
                                                continue;
                                            }

                                            if (!refVoxelMap.ContainsKey(_ipos))
                                            {
                                                BSVoxel v = dataSource.Read(_ipos.x, _ipos.y, _ipos.z);
                                                old_voxels.Add(v);
                                                indexes.Add(_ipos);
                                                new_voxels.Add(new BSVoxel());
                                                refVoxelMap.Add(_ipos, 0);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Extra Extendable
            FindExtraExtendableVoxels(dataSource, new_voxels, old_voxels, indexes, refVoxelMap);

            // Action
            if (indexes.Count != 0)
            {
                BSAction action = new BSAction();

                BSVoxelModify modify = new BSVoxelModify(indexes.ToArray(), old_voxels.ToArray(), new_voxels.ToArray(), dataSource, mode);

                action.AddModify(modify);

                if (action.Do())
                {
                    BSHistory.AddAction(action);
                }
            }
        }
    }
Пример #28
0
    public static bool RayCastDrawTarget(Ray ray, IBSDataSource ds, out DrawTarget target, int minvol, bool ignoreDiagonal = true, params IBSDataSource[] relative_ds)
    {
        target = new DrawTarget();

        RaycastHit rch;
        float      dis = Upsilon;

//		bool cast = false;

        if (ds == null)
        {
            return(false);
        }

        IBSDataSource fds = null;

        if (RayCastV(ray, ds, out rch, minvol))
        {
            dis = rch.distance;
//			cast = true;
            fds = ds;
        }


        foreach (IBSDataSource _ds in relative_ds)
        {
            RaycastHit _rch;
            if (_ds != ds && RayCastV(ray, _ds, out _rch, minvol))
            {
                if (dis > _rch.distance)
                {
                    rch = _rch;
                    dis = _rch.distance;
                    fds = _ds;
                }
            }
        }

        target.ds = fds;
        if (fds != null)
        {
            if (fds == ds)
            {
                target.rch    = rch;
                target.snapto = new Vector3(Mathf.FloorToInt((rch.point.x - rch.normal.x * 0.5f) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.y - rch.normal.y * 0.5f) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.z - rch.normal.z * 0.5f) * ds.ScaleInverted) * ds.Scale);
                target.cursor = new Vector3(Mathf.FloorToInt((rch.point.x + rch.normal.x * 0.5f) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.y + rch.normal.y * 0.5f) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.z + rch.normal.z * 0.5f) * ds.ScaleInverted) * ds.Scale);


                IntVector3 offset = DiagonalOffset(rch.point * ds.ScaleInverted, rch.normal * ds.ScaleInverted, ds.DiagonalOffset);

                if (offset.x != 0)
                {
                    Vector3 _snap = target.snapto * ds.ScaleInverted + new Vector3(offset.x, 0, 0);
                    BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                    if (!ds.VoxelIsZero(voxel, minvol))
                    {
                        offset.x = 0;
                    }
                }


                if (offset.y != 0)
                {
                    Vector3 _snap = target.snapto * ds.ScaleInverted + new Vector3(0, offset.y, 0);
                    BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                    if (!ds.VoxelIsZero(voxel, minvol))
                    {
                        offset.y = 0;
                    }
                }


                if (offset.z != 0)
                {
                    Vector3 _snap = target.snapto * ds.ScaleInverted + new Vector3(0, 0, offset.z);
                    BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                    if (!ds.VoxelIsZero(voxel, minvol))
                    {
                        offset.z = 0;
                    }
                }

                if (offset.x != 0 && offset.y != 0)
                {
                    Vector3 _snap = target.snapto * ds.ScaleInverted + new Vector3(offset.x, offset.y, 0);
                    BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                    if (!ds.VoxelIsZero(voxel, minvol))
                    {
                        offset.x = 0;
                        offset.y = 0;
                    }
                }

                if (offset.x != 0 && offset.z != 0)
                {
                    Vector3 _snap = target.snapto * ds.ScaleInverted + new Vector3(offset.x, 0, offset.z);
                    BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                    if (!ds.VoxelIsZero(voxel, minvol))
                    {
                        offset.x = 0;
                        offset.z = 0;
                    }
                }

                if (offset.y != 0 && offset.z != 0)
                {
                    Vector3 _snap = target.snapto * ds.ScaleInverted + new Vector3(0, offset.y, offset.z);
                    BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                    if (!ds.VoxelIsZero(voxel, minvol))
                    {
                        offset.y = 0;
                        offset.z = 0;
                    }
                }

                target.cursor = target.cursor + (offset.ToVector3()) * ds.Scale;

                if (ds == BuildingMan.Blocks)
                {
                    if (!ignoreDiagonal)
                    {
                        Vector3 _snap = target.snapto * ds.ScaleInverted;
                        BSVoxel voxel = ds.SafeRead((int)_snap.x, (int)_snap.y, (int)_snap.z);
                        if (voxel.blockType > 128)
                        {
                            if (voxel.blockType >> 2 != 63)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }
            else
            {
                rch.point += fds.Offset;
                rch.point -= ds.Offset;

                target.rch = rch;
                float factor = Mathf.Min(fds.Scale, ds.Scale);
                target.snapto = new Vector3(Mathf.FloorToInt((rch.point.x - rch.normal.x * 0.5f * factor) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.y - rch.normal.y * 0.5f * factor) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.z - rch.normal.z * 0.5f * factor) * ds.ScaleInverted) * ds.Scale);
                target.cursor = new Vector3(Mathf.FloorToInt((rch.point.x + rch.normal.x * 0.5f * factor) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.y + rch.normal.y * 0.5f * factor) * ds.ScaleInverted) * ds.Scale,
                                            Mathf.FloorToInt((rch.point.z + rch.normal.z * 0.5f * factor) * ds.ScaleInverted) * ds.Scale);
            }

            return(true);
        }

        return(false);
    }
Пример #29
0
    public static bool RayCastV(Ray ray, IBSDataSource ds, out RaycastHit rch, int minvol = 1)
    {
        ray.origin -= ds.Offset;

        rch = new RaycastHit();

        IntVector3 origin_index = new IntVector3(Mathf.FloorToInt(ray.origin.x * ds.ScaleInverted),
                                                 Mathf.FloorToInt(ray.origin.y * ds.ScaleInverted),
                                                 Mathf.FloorToInt(ray.origin.z * ds.ScaleInverted));

        // ray origin inside a voxel
        BSVoxel voxel = ds.SafeRead(origin_index.x, origin_index.y, origin_index.z);

        if (!ds.VoxelIsZero(voxel, minvol))
        {
            return(false);
        }

        float xStart, yStart, zStart;
        float xAdder, yAdder, zAdder;
        float xMin, yMin, zMin;
        float xMax, yMax, zMax;

        if (ray.direction.x > Epsilon)
        {
            xAdder = ds.Scale;
        }
        else if (ray.direction.x < -Epsilon)
        {
            xAdder = -ds.Scale;
        }
        else
        {
            xAdder = 0;
        }

        if (ray.direction.y > Epsilon)
        {
            yAdder = ds.Scale;
        }
        else if (ray.direction.y < -Epsilon)
        {
            yAdder = -ds.Scale;
        }
        else
        {
            yAdder = 0;
        }

        if (ray.direction.z > Epsilon)
        {
            zAdder = ds.Scale;
        }
        else if (ray.direction.z < -Epsilon)
        {
            zAdder = -ds.Scale;
        }
        else
        {
            zAdder = 0;
        }

        Bounds bound = GetSafeBound(ds);

        xMin = bound.min.x;
        yMin = bound.min.y;
        zMin = bound.min.z;
        xMax = bound.max.x;
        yMax = bound.max.y;
        zMax = bound.max.z;

        xStart = (int)(((int)ray.origin.x) + ds.Scale + xAdder * 0.5f);
        yStart = (int)(((int)ray.origin.y) + ds.Scale + yAdder * 0.5f);
        zStart = (int)(((int)ray.origin.z) + ds.Scale + zAdder * 0.5f);

        // Clamp the start
        xStart = Mathf.Clamp(xStart, xMin, xMax);
        yStart = Mathf.Clamp(yStart, yMin, yMax);
        zStart = Mathf.Clamp(zStart, zMin, zMax);

        // Normalize the ray
        ray.direction = ray.direction.normalized;

        // Define the Enter
        float EnterX = Upsilon + 1;
        float EnterY = Upsilon + 1;
        float EnterZ = Upsilon + 1;

        // Find the X-cast Enter
        if (xAdder != 0)
        {
            for (float x = xStart; x > xMin - 0.1f && x <= xMax + 0.1f; x += xAdder)
            {
                float   enter = (x - ray.origin.x) / ray.direction.x;
                Vector3 Hit   = ray.origin + ray.direction * enter;

                // Cast Block?
                voxel = ds.Read(Mathf.FloorToInt((Hit.x + xAdder * 0.5f) * ds.ScaleInverted),
                                Mathf.FloorToInt(Hit.y * ds.ScaleInverted),
                                Mathf.FloorToInt(Hit.z * ds.ScaleInverted));
                if (!ds.VoxelIsZero(voxel, minvol))
                {
                    EnterX = enter;
                    break;
                }
            }
        }

        // Find the Y-cast Enter
        if (yAdder != 0)
        {
            for (float y = yStart; y >= yMin - 0.1f && y <= yMax + 0.1f; y += yAdder)
            {
                float   enter = (y - ray.origin.y) / ray.direction.y;
                Vector3 Hit   = ray.origin + ray.direction * enter;

                // Cast Block?
                voxel = ds.Read(Mathf.FloorToInt(Hit.x * ds.ScaleInverted),
                                Mathf.FloorToInt((Hit.y + yAdder * 0.5f) * ds.ScaleInverted),
                                Mathf.FloorToInt(Hit.z * ds.ScaleInverted));
                if (!ds.VoxelIsZero(voxel, minvol))
                {
                    EnterY = enter;
                    break;
                }
            }
        }

        // Find the Z-cast Enter
        if (zAdder != 0)
        {
            for (float z = zStart; z >= zMin - 0.1f && z <= zMax + 0.1f; z += zAdder)
            {
                float   enter = (z - ray.origin.z) / ray.direction.z;
                Vector3 Hit   = (ray.origin + ray.direction * enter);

                voxel = ds.Read(Mathf.FloorToInt(Hit.x * ds.ScaleInverted),
                                Mathf.FloorToInt(Hit.y * ds.ScaleInverted),
                                Mathf.FloorToInt((Hit.z + zAdder * 0.5f) * ds.ScaleInverted));
                if (!ds.VoxelIsZero(voxel, minvol))
                {
                    EnterZ = enter;
                    break;
                }
            }
        }

        // cull negative x,y,z cast result
        if (EnterX < 0)
        {
            EnterX = Upsilon + 1;
        }
        if (EnterY < 0)
        {
            EnterY = Upsilon + 1;
        }
        if (EnterZ < 0)
        {
            EnterZ = Upsilon + 1;
        }

        // no cast
        if (EnterX > Upsilon && EnterY > Upsilon && EnterZ > Upsilon)
        {
            return(false);
        }

        // x enter is the smallest
        if (EnterX < EnterY && EnterX < EnterZ)
        {
            rch.point    = ray.GetPoint(EnterX);
            rch.normal   = Vector3.left * xAdder;
            rch.distance = (rch.point - ray.origin).magnitude;
            return(true);
        }
        // y enter is the smallest
        else if (EnterY < EnterZ && EnterY < EnterX)
        {
            rch.point    = ray.GetPoint(EnterY);
            rch.normal   = Vector3.down * yAdder;
            rch.distance = (rch.point - ray.origin).magnitude;
            return(true);
        }
        // z enter is the smallest
        else if (EnterZ < EnterX && EnterZ < EnterY)
        {
            rch.point    = ray.GetPoint(EnterZ);
            rch.normal   = Vector3.back * zAdder;
            rch.distance = (rch.point - ray.origin).magnitude;
            return(true);
        }
        else
        {
            return(false);
        }
    }
Пример #30
0
    /// <summary>
    /// Calculates the selection
    /// </summary>
    /// <param name="mode">Mode : 0 = add, 1 = substract, 2 = cross</param>
    protected bool CalcSelection(int mode)
    {
        Vector3 iMin = Vector3.zero;
        Vector3 iMax = Vector3.zero;

        iMin.x = Mathf.Min(m_Begin.x, m_End.x);
        iMin.y = Mathf.Min(m_Begin.y, m_End.y);
        iMin.z = Mathf.Min(m_Begin.z, m_End.z);
        iMax.x = Mathf.Max(m_Begin.x, m_End.x);
        iMax.y = Mathf.Max(m_Begin.y, m_End.y);
        iMax.z = Mathf.Max(m_Begin.z, m_End.z);

        Dictionary <IntVector3, byte> selection = new Dictionary <IntVector3, byte>();
        Dictionary <IntVector3, byte> removes   = new Dictionary <IntVector3, byte>();


        if (mode == 0)
        {
            for (float x = iMin.x; x < iMax.x; x += dataSource.Scale)
            {
                for (float y = iMin.y; y < iMax.y; y += dataSource.Scale)
                {
                    for (float z = iMin.z; z < iMax.z; z += dataSource.Scale)
                    {
                        IntVector3 ipos = new IntVector3(Mathf.FloorToInt(x * dataSource.ScaleInverted),
                                                         Mathf.FloorToInt(y * dataSource.ScaleInverted),
                                                         Mathf.FloorToInt(z * dataSource.ScaleInverted));


                        List <IntVector4> ext_posList = null;
                        List <BSVoxel>    ext_voxels  = null;
                        if (dataSource.ReadExtendableBlock(new IntVector4(ipos, 0), out ext_posList, out ext_voxels))
                        {
                            for (int i = 0; i < ext_voxels.Count; i++)
                            {
                                IntVector3 _ipos = new IntVector3(ext_posList[i].x, ext_posList[i].y, ext_posList[i].z);
                                if (m_Selections.ContainsKey(_ipos))
                                {
                                    continue;
                                }

                                selection[_ipos] = (byte)0xfe;
                            }
                        }
                        else
                        {
                            if (m_Selections.ContainsKey(ipos))
                            {
                                continue;
                            }

                            BSVoxel voxel = dataSource.Read(ipos.x, ipos.y, ipos.z);
                            if (!dataSource.VoxelIsZero(voxel, 1))
                            {
                                selection[ipos] = (byte)0xfe;
                            }
                        }
                    }
                }
            }
        }
        else if (mode == 1)
        {
            for (float x = iMin.x; x < iMax.x; x += dataSource.Scale)
            {
                for (float y = iMin.y; y < iMax.y; y += dataSource.Scale)
                {
                    for (float z = iMin.z; z < iMax.z; z += dataSource.Scale)
                    {
                        IntVector3 ipos = new IntVector3(Mathf.FloorToInt(x * dataSource.ScaleInverted),
                                                         Mathf.FloorToInt(y * dataSource.ScaleInverted),
                                                         Mathf.FloorToInt(z * dataSource.ScaleInverted));

//						if (!m_Selections.ContainsKey(ipos))
//							continue;

                        List <IntVector4> ext_posList = null;
                        List <BSVoxel>    ext_voxels  = null;

                        if (dataSource.ReadExtendableBlock(new IntVector4(ipos, 0), out ext_posList, out ext_voxels))
                        {
                            for (int i = 0; i < ext_voxels.Count; i++)
                            {
                                IntVector3 _ipos = new IntVector3(ext_posList[i].x, ext_posList[i].y, ext_posList[i].z);
                                if (!m_Selections.ContainsKey(_ipos))
                                {
                                    continue;
                                }

                                removes[_ipos] = new byte();
                            }
                        }
                        else
                        {
                            if (!m_Selections.ContainsKey(ipos))
                            {
                                continue;
                            }

                            removes[ipos] = new byte();
                        }
                    }
                }
            }
        }
        else if (mode == 2)
        {
            for (float x = iMin.x; x < iMax.x; x += dataSource.Scale)
            {
                for (float y = iMin.y; y < iMax.y; y += dataSource.Scale)
                {
                    for (float z = iMin.z; z < iMax.z; z += dataSource.Scale)
                    {
                        IntVector3 ipos = new IntVector3(Mathf.FloorToInt(x * dataSource.ScaleInverted),
                                                         Mathf.FloorToInt(y * dataSource.ScaleInverted),
                                                         Mathf.FloorToInt(z * dataSource.ScaleInverted));

                        List <IntVector4> ext_posList = null;
                        List <BSVoxel>    ext_voxels  = null;

                        if (dataSource.ReadExtendableBlock(new IntVector4(ipos, 0), out ext_posList, out ext_voxels))
                        {
                            bool remove = false;
                            for (int i = 0; i < ext_voxels.Count; i++)
                            {
                                if (m_Selections.ContainsKey(new IntVector3(ext_posList[i].x, ext_posList[i].y, ext_posList[i].z)))
                                {
                                    for (int _i = 0; _i < ext_voxels.Count; _i++)
                                    {
                                        IntVector3 _ipos = new IntVector3(ext_posList[_i].x, ext_posList[_i].y, ext_posList[_i].z);
                                        removes[_ipos] = new byte();
                                    }
                                    remove = true;
                                    break;
                                }
                            }

                            if (!remove)
                            {
                                for (int i = 0; i < ext_voxels.Count; i++)
                                {
                                    selection[new IntVector3(ext_posList[i].x, ext_posList[i].y, ext_posList[i].z)] = (byte)0xfe;
                                }
                            }
                        }
                        else
                        {
                            if (m_Selections.ContainsKey(ipos))
                            {
                                removes[ipos] = (byte)0xfe;
                            }
                            else
                            {
                                BSVoxel voxel = dataSource.Read(ipos.x, ipos.y, ipos.z);
                                if (!dataSource.VoxelIsZero(voxel, 1))
                                {
                                    selection[ipos] = (byte)0xfe;
                                }
                            }
                        }
                    }
                }
            }
        }


        if (selection.Count - removes.Count + m_Selections.Count > MaxVoxelCount)
        {
            selection.Clear();
            removes.Clear();
            return(false);
        }

        // For History
        Dictionary <IntVector3, byte> old_seletion = new Dictionary <IntVector3, byte>(m_Selections);
        bool changed = false;


        foreach (IntVector3 key in removes.Keys)
        {
            changed = true;
            RemoveSelection(key);
        }


        foreach (KeyValuePair <IntVector3, byte> kvp in selection)
        {
            changed = true;
            AddSelection(kvp.Key, kvp.Value);
        }

        selection.Clear();
        removes.Clear();

        // For History
        if (changed)
        {
            Dictionary <IntVector3, byte> new_seletion = new Dictionary <IntVector3, byte>(m_Selections);
            BSSelectedBoxModify           modify       = new BSSelectedBoxModify(old_seletion, new_seletion, this);
            m_Action.AddModify(modify);
        }

        return(true);
    }