コード例 #1
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();
        }
    }
コード例 #2
0
    void Update()
    {
        if (dataSource == null)
        {
            return;
        }

        if (GameConfig.IsInVCE)
        {
            return;
        }

        if (m_Phase == EPhase.Free)
        {
            if (BSInput.s_MouseOnUI)
            {
                gizmoBox.gameObject.SetActive(false);
                return;
            }

            if (BSInput.s_Cancel)
            {
                ResetDrawing();
            }

            // Ray cast voxel
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            BSMath.DrawTarget blockTgt = new BSMath.DrawTarget();
            BSMath.DrawTarget voxelTgt = new BSMath.DrawTarget();
            bool castBlock             = BSMath.RayCastDrawTarget(ray, dataSource, out blockTgt, minvol, true);
            bool castVoxel             = BSMath.RayCastDrawTarget(ray, dataSource, out voxelTgt, minvol, true, BuildingMan.Voxels);

            if (castBlock || castVoxel)
            {
                if (!gizmoBox.gameObject.activeSelf)
                {
                    gizmoBox.gameObject.SetActive(true);
                }

                Vector3 cursor = m_Target.cursor;
                if (castBlock && castVoxel)
                {
                    if (blockTgt.rch.distance <= voxelTgt.rch.distance)
                    {
                        m_Target = blockTgt;
                        cursor   = CalcSnapto(m_Target, dataSource, pattern);
                        m_Cursor = m_Target.rch.point;
                    }
                    else
                    {
                        m_Target = voxelTgt;
                        cursor   = CalcCursor(m_Target, dataSource, 1);
                        m_Cursor = m_Target.rch.point;
                    }
                }
                else if (castBlock)
                {
                    m_Target = blockTgt;
                    cursor   = CalcSnapto(m_Target, dataSource, pattern);
                    m_Cursor = m_Target.rch.point;
                }
                else if (castVoxel)
                {
                    m_Target = voxelTgt;
                    cursor   = CalcCursor(m_Target, dataSource, 1);
                    m_Cursor = m_Target.rch.point;
                }


                gizmoBox.size     = Vector3.one * dataSource.Scale;
                gizmoBox.position = cursor + dataSource.Offset;

                float   offset = dataSource.Scale;
                Vector3 begin  = cursor;
                Vector3 end    = begin + new Vector3(offset, offset, offset);

                // Click mouse and change phase
                if (Input.GetMouseButtonDown(0))
                {
                    m_Begin   = begin;
                    _beginPos = begin;
                    m_End     = end;

                    m_Phase       = EPhase.DragPlane;
                    _prevMousePos = Input.mousePosition;
                }
            }
            else
            {
                ResetDrawing();
            }
        }
        else if (m_Phase == EPhase.DragPlane)
        {
            if (BSInput.s_Cancel)
            {
                ResetDrawing();
                return;
            }

            RaycastHit rch;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            float position = 0;

            if (DragPlane == ECoordPlane.XY)
            {
                position = m_Cursor.z;
            }
            else if (DragPlane == ECoordPlane.XZ)
            {
                position = m_Cursor.y;
            }
            else if (DragPlane == ECoordPlane.ZY)
            {
                position = m_Cursor.x;
            }

            if (BSMath.RayCastCoordPlane(ray, DragPlane, position, out rch))
            {
                m_PointBeforeAdjustHeight = rch.point;
                if (!Vector3.Equals(_prevMousePos, Input.mousePosition))
                {
                    Vector3 point = rch.point - dataSource.Offset;

                    if (DragPlane == ECoordPlane.XZ)
                    {
                        // x
                        float x = 0;
                        m_Begin.x = CalcValue(point.x, _beginPos.x, out x);

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

                        m_End = new Vector3(x, _beginPos.y + (pattern.size) * dataSource.Scale, z);
                        m_End = Clamp(m_Begin, m_End);
                    }
                    else if (DragPlane == ECoordPlane.XY)
                    {
                        // x
                        float x = 0;
                        m_Begin.x = CalcValue(point.x, _beginPos.x, out x);

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

                        m_End = new Vector3(x, y, _beginPos.z + (pattern.size) * dataSource.Scale);
                        m_End = Clamp(m_Begin, m_End);
                    }
                    else if (DragPlane == ECoordPlane.ZY)
                    {
                        // y
                        float y = 0;
                        m_Begin.y = CalcValue(point.y, _beginPos.y, out y);

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

                        m_End = new Vector3(_beginPos.x + (pattern.size) * dataSource.Scale, y, z);
                        m_End = Clamp(m_Begin, m_End);
                    }

                    gizmoBox.position = Min + dataSource.Offset;

                    gizmoBox.size = Size * dataSource.Scale;
                }


                // Click mouse and change phase
                if (Input.GetMouseButtonUp(0))
                {
                    m_Phase       = EPhase.AdjustHeight;
                    _prevMousePos = new Vector3(-100, -100, -100);
                }
            }
        }
        else if (m_Phase == EPhase.AdjustHeight)
        {
            if (BSInput.s_Cancel)
            {
                ResetDrawing();
                return;
            }

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (DragPlane == ECoordPlane.XZ)
            {
                float adjustHeight = m_PointBeforeAdjustHeight.y;
                BSMath.RayAdjustHeight(ray, ECoordAxis.Y, m_PointBeforeAdjustHeight, out adjustHeight);

                // y
                float y = 0;
                m_Begin.y = CalcValue(adjustHeight, _beginPos.y, out y);
                m_End.y   = y;
            }
            else if (DragPlane == ECoordPlane.XY)
            {
                float adjustHeight = m_PointBeforeAdjustHeight.z;
                BSMath.RayAdjustHeight(ray, ECoordAxis.Z, m_PointBeforeAdjustHeight, out adjustHeight);


                // z
                float z = 0;
                m_Begin.z = CalcValue(adjustHeight, _beginPos.z, out z);
                m_End.z   = z;
            }
            else if (DragPlane == ECoordPlane.ZY)
            {
                float adjustHeight = m_PointBeforeAdjustHeight.x;
                BSMath.RayAdjustHeight(ray, ECoordAxis.X, m_PointBeforeAdjustHeight, out adjustHeight);

                // x
                float x = 0;
                m_Begin.x = CalcValue(adjustHeight, _beginPos.x, out x);
                m_End.x   = x;
            }

            m_End = Clamp(m_Begin, m_End);

            gizmoBox.position = Min + dataSource.Offset;

            gizmoBox.size = Size * dataSource.Scale;

            if (PeInput.Get(PeInput.LogicFunction.Build))
            {
                Do();
                m_Phase = EPhase.Drawing;
            }
        }
        else if (m_Phase == EPhase.Drawing)
        {
            if (BSInput.s_Cancel)
            {
                ResetDrawing();
                return;
            }

            if (BSInput.s_Delete)
            {
                DeleteVoxels();
            }
        }
    }
コード例 #3
0
    protected void Update()
    {
        if (dataSource == null)
        {
            return;
        }

        if (pattern == null)
        {
            return;
        }

        if (!ExtraAdjust())
        {
            return;
        }

        if (GameConfig.IsInVCE)
        {
            return;
        }

        gizmoCube.m_VoxelSize = dataSource.Scale;

        if (m_Phase == EPhase.Free)
        {
            if (BSInput.s_MouseOnUI)
            {
                gizmoCube.gameObject.SetActive(false);
                return;
            }

            if (BSInput.s_Cancel)
            {
                ResetDrawing();
            }

            // Ray cast voxel
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            bool ignoreDiagonal = (mode == EBSBrushMode.Add);
            bool cast           = BSMath.RayCastDrawTarget(ray, dataSource, out m_Target, minvol, ignoreDiagonal, BuildingMan.Datas);
            if (cast)
            {
                Vector3 cursor = Vector3.zero;

                if (mode == EBSBrushMode.Add)
                {
                    cursor = CalcCursor(m_Target, dataSource, pattern.size);

                    m_Cursor = m_Target.rch.point;
                }
                else if (mode == EBSBrushMode.Subtract)
                {
                    cursor = CalcSnapto(m_Target, dataSource, pattern);

                    m_Cursor = m_Target.rch.point;
                }
                m_GizmoCursor = cursor;

                _drawGL            = true;
                gizmoCube.CubeSize = new IntVector3(pattern.size, pattern.size, pattern.size);
                gizmoCube.gameObject.SetActive(true);
                gizmoCube.transform.position = cursor + dataSource.Offset;

                UpdateGizmoTrigger();

                float   offset = pattern.size * dataSource.Scale;
                Vector3 begin  = cursor;
                Vector3 end    = begin + new Vector3(offset, offset, offset);
                _glCenter   = cursor + new Vector3(offset, offset, offset) * 0.5f + dataSource.Offset;
                _glCenter.y = begin.y;

                // Click mouse and change phase
                if (Input.GetMouseButtonDown(0))
                {
                    m_Begin   = begin;
                    _beginPos = begin;
                    m_End     = end;

                    m_Phase       = EPhase.DragPlane;
                    _prevMousePos = Input.mousePosition;
                }
            }
            else
            {
                ResetDrawing();
            }

            // Switch the panel
            if (Input.GetKeyDown(KeyCode.G))
            {
                if (DragPlane == ECoordPlane.XZ)
                {
                    DragPlane = ECoordPlane.ZY;
                }
                else if (DragPlane == ECoordPlane.ZY)
                {
                    DragPlane = ECoordPlane.XY;
                }
                else if (DragPlane == ECoordPlane.XY)
                {
                    DragPlane = ECoordPlane.XZ;
                }

                Debug.Log("Switch the drag reference plane : " + DragPlane.ToString());
            }
        }
        else if (m_Phase == EPhase.DragPlane)
        {
            if (BSInput.s_Cancel)
            {
                ResetDrawing();
                return;
            }

            RaycastHit rch;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            float position = 0;

            if (DragPlane == ECoordPlane.XY)
            {
                position = m_Cursor.z;
            }
            else if (DragPlane == ECoordPlane.XZ)
            {
                position = m_Cursor.y;
            }
            else if (DragPlane == ECoordPlane.ZY)
            {
                position = m_Cursor.x;
            }

            if (BSMath.RayCastCoordPlane(ray, DragPlane, position, out rch))
            {
                m_PointBeforeAdjustHeight = rch.point;
                if (!Vector3.Equals(_prevMousePos, Input.mousePosition))
                {
                    Vector3 point = rch.point - dataSource.Offset;


                    if (DragPlane == ECoordPlane.XZ)
                    {
                        // x
                        float x = 0;
                        m_Begin.x = CalcValue(point.x, _beginPos.x, out x);

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

                        m_End = new Vector3(x, _beginPos.y + (pattern.size) * dataSource.Scale, z);
                        m_End = Clamp(m_Begin, m_End);
                    }
                    else if (DragPlane == ECoordPlane.XY)
                    {
                        // x
                        float x = 0;
                        m_Begin.x = CalcValue(point.x, _beginPos.x, out x);

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

                        m_End = new Vector3(x, y, _beginPos.z + (pattern.size) * dataSource.Scale);
                        m_End = Clamp(m_Begin, m_End);
                    }
                    else if (DragPlane == ECoordPlane.ZY)
                    {
                        // y
                        float y = 0;
                        m_Begin.y = CalcValue(point.y, _beginPos.y, out y);

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

                        m_End = new Vector3(_beginPos.x + (pattern.size) * dataSource.Scale, y, z);
                        m_End = Clamp(m_Begin, m_End);
                    }

                    // other phase Drag plane
                    DragPlaneExtraDo(DragPlane);

                    gizmoCube.transform.position = Min + dataSource.Offset;

                    gizmoCube.CubeSize = new IntVector3((int)Size.x, (int)Size.y, (int)Size.z);

                    UpdateGizmoTrigger();
                }



                // Click mouse and change phase
                if (Input.GetMouseButtonUp(0))
                {
                    m_Phase       = EPhase.AdjustHeight;
                    _prevMousePos = new Vector3(-100, -100, -100);
                }
            }
        }
        else if (m_Phase == EPhase.AdjustHeight)
        {
            if (BSInput.s_Cancel)
            {
                ResetDrawing();
                return;
            }

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

//			ECoordAxis axis = ECoordAxis.Y;
            if (DragPlane == ECoordPlane.XZ)
            {
                float adjustHeight = m_PointBeforeAdjustHeight.y;
                BSMath.RayAdjustHeight(ray, ECoordAxis.Y, m_PointBeforeAdjustHeight, out adjustHeight);

                // y
                float y = 0;
                m_Begin.y = CalcValue(adjustHeight, _beginPos.y, out y);
                m_End.y   = y + m_EndOffset.y;
            }
            else if (DragPlane == ECoordPlane.XY)
            {
                float adjustHeight = m_PointBeforeAdjustHeight.z;
                BSMath.RayAdjustHeight(ray, ECoordAxis.Z, m_PointBeforeAdjustHeight, out adjustHeight);


                // z
                float z = 0;
                m_Begin.z = CalcValue(adjustHeight, _beginPos.z, out z);
                m_End.z   = z + m_EndOffset.z;
            }
            else if (DragPlane == ECoordPlane.ZY)
            {
                float adjustHeight = m_PointBeforeAdjustHeight.x;
                BSMath.RayAdjustHeight(ray, ECoordAxis.X, m_PointBeforeAdjustHeight, out adjustHeight);

                // x
                float x = 0;
                m_Begin.x = CalcValue(adjustHeight, _beginPos.x, out x);

                m_End.x = x + m_EndOffset.x;
            }

            m_End = Clamp(m_Begin, m_End);

            // Other adjustHeight
            AdjustHeightExtraDo(DragPlane);


            gizmoCube.transform.position = Min + dataSource.Offset;

            gizmoCube.CubeSize = new IntVector3((int)Size.x, (int)Size.y, (int)Size.z);

            UpdateGizmoTrigger();

            // Click mouse and change phase
            //if (Input.GetMouseButtonDown(0))
            if (PeInput.Get(PeInput.LogicFunction.Build))
            {
                Do();
                m_Phase = EPhase.Free;
            }
        }


        AfterDo();
    }