示例#1
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();
            }
        }
    }
示例#2
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();
    }