Ejemplo n.º 1
0
        public static IntBox CalculateBound(List <SelBox> boxes)
        {
            int count = boxes.Count;

            int[] xMins  = new int[count];
            int[] yMins  = new int[count];
            int[] zMins  = new int[count];
            int[] xMaxes = new int[count];
            int[] yMaxes = new int[count];
            int[] zMaxes = new int[count];

            for (int i = 0; i < count; i++)
            {
                BSTools.IntBox box = boxes[i].m_Box;
                xMins[i]  = box.xMin;
                yMins[i]  = box.yMin;
                zMins[i]  = box.zMin;
                xMaxes[i] = box.xMax;
                yMaxes[i] = box.yMax;
                zMaxes[i] = box.zMax;
            }

            IntBox output = new IntBox();

            output.xMin = (short)Mathf.Min(xMins);
            output.yMin = (short)Mathf.Min(yMins);
            output.zMin = (short)Mathf.Min(zMins);
            output.xMax = (short)Mathf.Max(xMaxes);
            output.yMax = (short)Mathf.Max(yMaxes);
            output.zMax = (short)Mathf.Max(zMaxes);

            return(output);
        }
Ejemplo n.º 2
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);
        }
    }
Ejemplo n.º 3
0
    public override void OnGL()
    {
        if (m_Material == null)
        {
            m_Material                  = new Material(Shader.Find("Lines/Colored Blended"));
            m_Material.hideFlags        = HideFlags.HideAndDontSave;
            m_Material.shader.hideFlags = HideFlags.HideAndDontSave;
        }

        // Save camera's matrix.
        GL.PushMatrix();


        for (int i = 0; i < m_Material.passCount; i++)
        {
            m_Material.SetPass(i);
            foreach (BSTools.SelBox sbox in m_Boxes)
            {
                BSTools.IntBox ibox = sbox.m_Box;
                Bounds         box  = new Bounds(Vector3.zero, Vector3.zero);

                box.SetMinMax(new Vector3(ibox.xMin, ibox.yMin, ibox.zMin) * scale + offset,
                              new Vector3(ibox.xMax + 1f, ibox.yMax + 1f, ibox.zMax + 1f) * scale + offset);

                float shrink = 0.03f;
                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.03f, 0.1f);
                }

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

                float sv = (float)(sbox.m_Val) / 255.0f;

                // Edge
                if (sv > 0.99f)
                {
                    GL.Begin(GL.LINES);
                    GL.Color(m_LineColor.Evaluate(sv));
                    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();
                }

                // Face
                GL.Begin(GL.QUADS);
                GL.Color(m_BoxColor.Evaluate(sv));
                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();
            }
        }

        // Restore camera's matrix.
        GL.PopMatrix();
    }
Ejemplo n.º 4
0
    protected void Update()
    {
        if (dataSource == null)
        {
            return;
        }

        if (BSInput.s_MouseOnUI)
        {
            return;
        }

        if (_datasource == dataSource)
        {
            // Clear and rebuild;
            ClearSelection(null);
            _datasource = dataSource;
        }

        if (seletionBoxeRenderer != null)
        {
            seletionBoxeRenderer.m_Boxes = m_SelectionBoxes;
            seletionBoxeRenderer.scale   = dataSource.Scale;
            seletionBoxeRenderer.offset  = dataSource.Offset;
        }

        if (m_RecalcBoxes)
        {
            CalcBoxes();
            m_RecalcBoxes = false;
        }

        if (GameConfig.IsInVCE)
        {
            return;
        }

        // Depth
        if (!BSInput.s_Shift && Input.GetKeyDown(KeyCode.UpArrow))
        {
            Depth      = ++Depth >= maxDragSize ? maxDragSize : Depth;
            m_GUIAlpha = 5;
        }
        else if (!BSInput.s_Shift && Input.GetKeyDown(KeyCode.DownArrow))
        {
            Depth      = --Depth >= 1 ? Depth : 1;
            m_GUIAlpha = 5;
        }

        m_GUIAlpha = Mathf.Lerp(m_GUIAlpha, 0, 0.05f);



        // Drag Box
        //
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (!m_Selecting)
        {
            // Cancel
            if (BSInput.s_Cancel)
            {
                if (!canDo)
                {
                    return;
                }

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

                // For History
                if (old_seletion.Count != 0)
                {
                    BSSelectedBoxModify modify = new BSSelectedBoxModify(old_seletion, new_selection, this);
                    m_Action.AddModify(modify);

                    DoHistory();
                }

                return;
            }

            if (BSMath.RayCastDrawTarget(ray, dataSource, out m_Target, minvol, true, BuildingMan.Datas))
            {
                m_Drawing = true;

                m_Begin   = m_Target.snapto;
                _beginPos = m_Begin;
                m_End     = m_Begin;

                float vx = Mathf.Abs(m_Target.rch.normal.x);
                float vy = Mathf.Abs(m_Target.rch.normal.y);
                float vz = Mathf.Abs(m_Target.rch.normal.z);

                if (vx > 0.9f * m_Target.ds.Scale)
                {
                    m_Coord    = ECoordPlane.ZY;
                    m_PlanePos = m_Target.rch.point.x;
                }
                else if (vy > 0.9f * m_Target.ds.Scale)
                {
                    m_Coord    = ECoordPlane.XZ;
                    m_PlanePos = m_Target.rch.point.y;
                }
                else if (vz > 0.9f * m_Target.ds.Scale)
                {
                    m_Coord    = ECoordPlane.XY;
                    m_PlanePos = m_Target.rch.point.z;
                }

                if (Input.GetMouseButtonDown(0))
                {
                    if (canDo)
                    {
                        m_Selecting = true;
                    }
                }
            }
            else
            {
                m_Drawing = false;
            }
        }
        // Selecting
        else
        {
            m_Drawing = true;

            // Cancel
            if (BSInput.s_Cancel)
            {
                if (!canDo)
                {
                    return;
                }
                m_Selecting = false;
                m_Begin     = new Vector3(-10000, -10000, -10000);
                m_End       = new Vector3(-10000, -10000, -10000);
                return;
            }

            RaycastHit rch;
            if (BSMath.RayCastCoordPlane(ray, m_Coord, m_PlanePos, out rch))
            {
                Vector3 point = rch.point - dataSource.Offset;
                if (m_Coord == ECoordPlane.XY)
                {
                    // x componet
                    float x = 0;
                    m_Begin.x = CalcValue(point.x, _beginPos.x, out x);

                    // y componet
                    float y = 0;
                    m_Begin.y = CalcValue(point.y, _beginPos.y, out y);

                    m_End.x = x;
                    m_End.y = y;
                    m_End.z = Mathf.FloorToInt(point.z * dataSource.ScaleInverted) * dataSource.Scale;


                    if (m_Target.rch.normal.z > 0)
                    {
                        m_Begin.z = m_PlanePos - Depth * dataSource.Scale;
                    }
                    else
                    {
                        m_Begin.z = m_PlanePos + Depth * dataSource.Scale;
                    }

                    m_End = Clamp(m_Begin, m_End);
                }
                else if (m_Coord == ECoordPlane.XZ)
                {
                    // x componet
                    float x = 0;
                    m_Begin.x = CalcValue(point.x, _beginPos.x, out x);

                    // z componet
                    float z = 0;
                    m_Begin.z = CalcValue(point.z, _beginPos.z, out z);

                    m_End.x = x;
                    m_End.y = Mathf.FloorToInt(point.y * dataSource.ScaleInverted) * dataSource.Scale;
                    m_End.z = z;

                    if (m_Target.rch.normal.y > 0)
                    {
                        m_Begin.y = m_PlanePos - Depth * dataSource.Scale;
                    }
                    else
                    {
                        m_Begin.y = m_PlanePos + Depth * dataSource.Scale;
                    }

                    m_End = Clamp(m_Begin, m_End);
                }
                else if (m_Coord == ECoordPlane.ZY)
                {
                    // y componet
                    float y = 0;
                    m_Begin.y = CalcValue(point.y, _beginPos.y, out y);

                    // z componet
                    float z = 0;
                    m_Begin.z = CalcValue(point.z, _beginPos.z, out z);

                    m_End.x = Mathf.FloorToInt(point.x * dataSource.ScaleInverted) * dataSource.Scale;
                    m_End.y = y;
                    m_End.z = z;

                    if (m_Target.rch.normal.x > 0)
                    {
                        m_Begin.x = m_PlanePos - Depth * dataSource.Scale;
                    }
                    else
                    {
                        m_Begin.x = m_PlanePos + Depth * dataSource.Scale;
                    }

                    m_End = Clamp(m_Begin, m_End);
                }
            }

            // In Valid area ?
            if (m_PrevDS != null && m_PrevDS == dataSource)
            {
                if (m_Selections.Count != 0)
                {
                    BSTools.SelBox box = new BSTools.SelBox();
                    box.m_Box.xMin = (short)Mathf.FloorToInt(Mathf.Min(m_Begin.x, m_End.x) * dataSource.ScaleInverted);
                    box.m_Box.yMin = (short)Mathf.FloorToInt(Mathf.Min(m_Begin.y, m_End.y) * dataSource.ScaleInverted);
                    box.m_Box.zMin = (short)Mathf.FloorToInt(Mathf.Min(m_Begin.z, m_End.z) * dataSource.ScaleInverted);
                    box.m_Box.xMax = (short)Mathf.FloorToInt(Mathf.Max(m_Begin.x, m_End.x) * dataSource.ScaleInverted);
                    box.m_Box.yMax = (short)Mathf.FloorToInt(Mathf.Max(m_Begin.y, m_End.y) * dataSource.ScaleInverted);
                    box.m_Box.zMax = (short)Mathf.FloorToInt(Mathf.Max(m_Begin.z, m_End.z) * dataSource.ScaleInverted);

                    // Add current box to list temporarily
                    m_SelectionBoxes.Add(box);
                    BSTools.IntBox bound = BSTools.SelBox.CalculateBound(m_SelectionBoxes);

                    Vector3 size = new Vector3(bound.xMax - bound.xMin, bound.yMax - bound.yMin, bound.zMax - bound.zMin);

                    if (size.x > maxSelectBoxSize.x || size.y > maxSelectBoxSize.y || size.z > maxSelectBoxSize.z)
                    {
                        m_IsValidBox = false;
                    }
                    else
                    {
                        m_IsValidBox = true;
                    }

                    // revert the list
                    m_SelectionBoxes.RemoveAt(m_SelectionBoxes.Count - 1);
                }
                else
                {
                    m_IsValidBox = true;
                }
            }
            else
            {
                m_IsValidBox = true;
            }

            if (Input.GetMouseButtonUp(0))
            {
                if (!canDo)
                {
                    return;
                }

//				bool result = false;

                if (m_IsValidBox)
                {
                    // Add
                    if (m_PrevDS == dataSource)
                    {
                        if (BSInput.s_Shift)
                        {
                            if (m_PrevDS == dataSource)
                            {
                                /*result = */ CalcSelection(0);
                            }
                        }
                        // Substract
                        else if (BSInput.s_Alt)
                        {
                            /*result = */ CalcSelection(1);
                        }
                        // Cross
                        else if (BSInput.s_Control)
                        {
                            /*result = */ CalcSelection(2);
                        }
                        else
                        {
                            ClearSelection(m_Action);
                            /*result = */ CalcSelection(0);
                        }

                        m_PrevDS = dataSource;
                    }
                    else
                    {
                        ClearSelection(m_Action);
                        /*result = */ CalcSelection(0);

                        m_PrevDS = dataSource;
                    }
                }

                m_Selecting = false;
                ResetValue();
            }
        }
        // Other Ajust
        if (AfterSelectionUpdate())
        {
            DoHistory();
        }
    }