void GetCenterVoxel() { for (int vol = minVol; vol < BSMath.MC_ISO_VALUE + volAdder; vol += volAdder) { if (BSMath.RayCastDrawTarget(PeCamera.mouseRay, BuildingMan.Voxels, out m_DTar, vol)) { m_Center = new IntVector3(m_DTar.snapto); if (CheckVoxel(m_Center)) { m_FindVoxel = true; return; } } } //checkOriginNear if (CheckVoxel(PeCamera.mouseRay.origin - BuildingMan.Voxels.Offset)) { m_Center = new IntVector3(PeCamera.mouseRay.origin - BuildingMan.Voxels.Offset); m_FindVoxel = true; m_DTar = new BSMath.DrawTarget(); m_DTar.snapto = m_Center; return; } m_FindVoxel = false; m_Center = InvalidPos; }
protected static Vector3 CalcCursor(BSMath.DrawTarget target, IBSDataSource ds, int size) { Vector3 cursor = Vector3.zero; IntVector3 realPos = new IntVector3(Mathf.FloorToInt(target.cursor.x * ds.ScaleInverted), Mathf.FloorToInt(target.cursor.y * ds.ScaleInverted), Mathf.FloorToInt(target.cursor.z * ds.ScaleInverted)); float offset = Mathf.FloorToInt(size * 0.5f) * ds.Scale; cursor = realPos.ToVector3() * ds.Scale - new Vector3(offset, offset, offset); int sign = size % 2 == 0? 1 : 0; if (offset != 0) { Vector3 offset_v = Vector3.zero; if (target.rch.normal.x > 0) { offset_v.x += offset; } else if (target.rch.normal.x < 0) { offset_v.x -= (offset - ds.Scale * sign); } else { offset_v.x = 0; } if (target.rch.normal.y > 0) { offset_v.y += offset; } else if (target.rch.normal.y < 0) { offset_v.y -= (offset - ds.Scale * sign); } else { offset_v.y = 0; } if (target.rch.normal.z > 0) { offset_v.z += offset; } else if (target.rch.normal.z < 0) { offset_v.z -= (offset - ds.Scale * sign); } else { offset_v.z = 0; } cursor += offset_v; } return(cursor); }
protected static Vector3 CalcSnapto(BSMath.DrawTarget target, IBSDataSource ds, BSPattern pt) { Vector3 snapto = Vector3.zero; // Update gizmo Position IntVector3 realPos = new IntVector3(Mathf.FloorToInt(target.snapto.x * ds.ScaleInverted), Mathf.FloorToInt(target.snapto.y * ds.ScaleInverted), Mathf.FloorToInt(target.snapto.z * ds.ScaleInverted)); float offset = Mathf.FloorToInt(pt.size * 0.5f) * ds.Scale; snapto = realPos.ToVector3() * ds.Scale - new Vector3(offset, offset, offset); int sign = pt.size % 2 == 0? 1 : 0; if (offset != 0) { Vector3 offset_v = Vector3.zero; if (target.rch.normal.x > 0) { offset_v.x -= (offset - ds.Scale * sign); } else if (target.rch.normal.x < 0) { offset_v.x += offset; } else { offset_v.x = 0; } if (target.rch.normal.y > 0) { offset_v.y -= (offset - ds.Scale * sign); } else if (target.rch.normal.y < 0) { offset_v.y += offset; } else { offset_v.y = 0; } if (target.rch.normal.z > 0) { offset_v.z -= (offset - ds.Scale * sign); } else if (target.rch.normal.z < 0) { offset_v.z += offset; } else { offset_v.z = 0; } snapto += offset_v; } return(snapto); }
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(); } } }
public override void OnGL() { if (dataSource == null) { return; } if (!gameObject.activeInHierarchy || !enabled) { return; } if (m_Target.ds == null) { return; } if (!m_Drawing) { return; } if (m_Material == null) { m_Material = new Material(Shader.Find("Lines/Colored Blended")); m_Material.hideFlags = HideFlags.HideAndDontSave; m_Material.shader.hideFlags = HideFlags.HideAndDontSave; } // Draw selecting box if (m_Selecting) { // Save camera's matrix. GL.PushMatrix(); 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); iMax += dataSource.Offset; iMin += dataSource.Offset; float shrink = 0.02f; if (Camera.main != null) { float dist_min = (Camera.main.transform.position - iMin).magnitude; float dist_max = (Camera.main.transform.position - iMax).magnitude; dist_max = dist_max > dist_min ? dist_max : dist_min; shrink = Mathf.Clamp(dist_max * 0.001f, 0.02f, 0.1f); } iMax += new Vector3(shrink, shrink, shrink); iMin -= new Vector3(shrink, shrink, shrink); Vector3[] vec = new Vector3 [8] { new Vector3(iMax.x, iMax.y, iMax.z), new Vector3(iMin.x, iMax.y, iMax.z), new Vector3(iMin.x, iMin.y, iMax.z), new Vector3(iMax.x, iMin.y, iMax.z), new Vector3(iMax.x, iMax.y, iMin.z), new Vector3(iMin.x, iMax.y, iMin.z), new Vector3(iMin.x, iMin.y, iMin.z), new Vector3(iMax.x, iMin.y, iMin.z) }; for (int i = 0; i < 8; ++i) { vec[i] = vec[i]; } Color lineColor = new Color(0.0f, 0.3f, 0.6f, 1.0f); Color faceColor = new Color(0.0f, 0.3f, 0.6f, 1.0f); if (!m_IsValidBox) { lineColor = new Color(0.67f, 0.1f, 0.1f); faceColor = lineColor; } lineColor.a = 1.0f; faceColor.a *= 0.4f + Mathf.Sin(Time.time * 6f) * 0.1f; for (int i = 0; i < m_Material.passCount; i++) { m_Material.SetPass(i); GL.Begin(GL.LINES); GL.Color(lineColor); GL.Vertex(vec[0]); GL.Vertex(vec[1]); GL.Vertex(vec[1]); GL.Vertex(vec[2]); GL.Vertex(vec[2]); GL.Vertex(vec[3]); GL.Vertex(vec[3]); GL.Vertex(vec[0]); GL.Vertex(vec[4]); GL.Vertex(vec[5]); GL.Vertex(vec[5]); GL.Vertex(vec[6]); GL.Vertex(vec[6]); GL.Vertex(vec[7]); GL.Vertex(vec[7]); GL.Vertex(vec[4]); GL.Vertex(vec[0]); GL.Vertex(vec[4]); GL.Vertex(vec[1]); GL.Vertex(vec[5]); GL.Vertex(vec[2]); GL.Vertex(vec[6]); GL.Vertex(vec[3]); GL.Vertex(vec[7]); GL.End(); GL.Begin(GL.QUADS); GL.Color(faceColor); GL.Vertex(vec[0]); GL.Vertex(vec[1]); GL.Vertex(vec[2]); GL.Vertex(vec[3]); GL.Vertex(vec[4]); GL.Vertex(vec[5]); GL.Vertex(vec[6]); GL.Vertex(vec[7]); GL.Vertex(vec[0]); GL.Vertex(vec[4]); GL.Vertex(vec[5]); GL.Vertex(vec[1]); GL.Vertex(vec[1]); GL.Vertex(vec[5]); GL.Vertex(vec[6]); GL.Vertex(vec[2]); GL.Vertex(vec[2]); GL.Vertex(vec[6]); GL.Vertex(vec[7]); GL.Vertex(vec[3]); GL.Vertex(vec[3]); GL.Vertex(vec[7]); GL.Vertex(vec[4]); GL.Vertex(vec[0]); GL.End(); } // Restore camera's matrix. GL.PopMatrix(); } // Draw pre-select face direction else { if (BSInput.s_MouseOnUI) { return; } // Save camera's matrix. GL.PushMatrix(); BSMath.DrawTarget dtar = m_Target; Vector3 iMin = dtar.rch.point; Vector3 iMax = dtar.rch.point; Color indicator_color = Color.white; // zy plane if (Mathf.Abs(dtar.rch.normal.x) > 0.9f * dtar.ds.Scale) { iMin.y = Mathf.Floor(iMin.y * dataSource.ScaleInverted) * dataSource.Scale; iMin.z = Mathf.Floor(iMin.z * dataSource.ScaleInverted) * dataSource.Scale; iMax.y = Mathf.Floor(iMax.y * dataSource.ScaleInverted) * dataSource.Scale + dataSource.Scale; iMax.z = Mathf.Floor(iMax.z * dataSource.ScaleInverted) * dataSource.Scale + dataSource.Scale; indicator_color = new Color(0.9f, 0.1f, 0.2f, 1.0f); } // xz plane else if (Mathf.Abs(dtar.rch.normal.y) > 0.9f * dtar.ds.Scale) { iMin.x = Mathf.Floor(iMin.x * dataSource.ScaleInverted) * dataSource.Scale; iMin.z = Mathf.Floor(iMin.z * dataSource.ScaleInverted) * dataSource.Scale; iMax.x = Mathf.Floor(iMax.x * dataSource.ScaleInverted) * dataSource.Scale + dataSource.Scale; iMax.z = Mathf.Floor(iMax.z * dataSource.ScaleInverted) * dataSource.Scale + dataSource.Scale; indicator_color = new Color(0.5f, 1.0f, 0.1f, 1.0f); } // xy plane else if (Mathf.Abs(dtar.rch.normal.z) > 0.9f * dtar.ds.Scale) { iMin.y = Mathf.Floor(iMin.y * dataSource.ScaleInverted) * dataSource.Scale; iMin.x = Mathf.Floor(iMin.x * dataSource.ScaleInverted) * dataSource.Scale; iMax.y = Mathf.Floor(iMax.y * dataSource.ScaleInverted) * dataSource.Scale + dataSource.Scale; iMax.x = Mathf.Floor(iMax.x * dataSource.ScaleInverted) * dataSource.Scale + dataSource.Scale; indicator_color = new Color(0.1f, 0.6f, 1.0f, 1.0f); } iMax += dataSource.Offset; iMin += dataSource.Offset; float shrink = 0.02f; if (Camera.main != null) { float dist_min = (Camera.main.transform.position - iMin).magnitude; float dist_max = (Camera.main.transform.position - iMax).magnitude; dist_max = dist_max > dist_min ? dist_max : dist_min; shrink = Mathf.Clamp(dist_max * 0.002f, 0.02f, 0.1f); } iMax += new Vector3(shrink, shrink, shrink); iMin -= new Vector3(shrink, shrink, shrink); Vector3[] vec = new Vector3 [8] { new Vector3(iMax.x, iMax.y, iMax.z), new Vector3(iMin.x, iMax.y, iMax.z), new Vector3(iMin.x, iMin.y, iMax.z), new Vector3(iMax.x, iMin.y, iMax.z), new Vector3(iMax.x, iMax.y, iMin.z), new Vector3(iMin.x, iMax.y, iMin.z), new Vector3(iMin.x, iMin.y, iMin.z), new Vector3(iMax.x, iMin.y, iMin.z) }; for (int i = 0; i < 8; ++i) { vec[i] = vec[i]; } Color lineColor = indicator_color; Color faceColor = indicator_color; lineColor.a = 1.0f; faceColor.a *= 0.7f + Mathf.Sin(Time.time * 6f) * 0.1f; for (int i = 0; i < m_Material.passCount; i++) { m_Material.SetPass(i); GL.Begin(GL.LINES); GL.Color(lineColor); GL.Vertex(vec[0]); GL.Vertex(vec[1]); GL.Vertex(vec[1]); GL.Vertex(vec[2]); GL.Vertex(vec[2]); GL.Vertex(vec[3]); GL.Vertex(vec[3]); GL.Vertex(vec[0]); GL.Vertex(vec[4]); GL.Vertex(vec[5]); GL.Vertex(vec[5]); GL.Vertex(vec[6]); GL.Vertex(vec[6]); GL.Vertex(vec[7]); GL.Vertex(vec[7]); GL.Vertex(vec[4]); GL.Vertex(vec[0]); GL.Vertex(vec[4]); GL.Vertex(vec[1]); GL.Vertex(vec[5]); GL.Vertex(vec[2]); GL.Vertex(vec[6]); GL.Vertex(vec[3]); GL.Vertex(vec[7]); GL.End(); GL.Begin(GL.QUADS); GL.Color(faceColor); GL.Vertex(vec[0]); GL.Vertex(vec[1]); GL.Vertex(vec[2]); GL.Vertex(vec[3]); GL.Vertex(vec[4]); GL.Vertex(vec[5]); GL.Vertex(vec[6]); GL.Vertex(vec[7]); GL.Vertex(vec[0]); GL.Vertex(vec[4]); GL.Vertex(vec[5]); GL.Vertex(vec[1]); GL.Vertex(vec[1]); GL.Vertex(vec[5]); GL.Vertex(vec[6]); GL.Vertex(vec[2]); GL.Vertex(vec[2]); GL.Vertex(vec[6]); GL.Vertex(vec[7]); GL.Vertex(vec[3]); GL.Vertex(vec[3]); GL.Vertex(vec[7]); GL.Vertex(vec[4]); GL.Vertex(vec[0]); GL.End(); } // Restore camera's matrix. GL.PopMatrix(); } }
void Start() { m_Target = new BSMath.DrawTarget(); pattern = BSPattern.DefaultV1; }