public static int SortSelBox(SelBox a, SelBox b) { return((int)(a.m_Val) - (int)(b.m_Val)); }
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(); } }
public static void Calculate(Dictionary <IntVector3, byte> bitmap, ref List <SelBox> leastboxes) { // Clear old result leastboxes.Clear(); // Fetch key array IntVector3 [] keyarr = new IntVector3 [bitmap.Count]; int index = 0; foreach (IntVector3 k in bitmap.Keys) { keyarr[index] = new IntVector3(k); index++; } // Unmark all voxels foreach (IntVector3 key in keyarr) { (bitmap[key]) &= (0xfe); } // Traverse all voxels foreach (IntVector3 key in keyarr) { // bitmap[key] & 1 is the voxel mark. if ((bitmap[key] & 1) == 0) { // box value byte box_val = bitmap[key]; box_val = (byte)((box_val == 0xfe) ? (0xfe) : (box_val & 0xe0)); // Mark this voxel (bitmap[key]) |= (1); // Begin & end of this box IntVector3 begin = key; IntVector3 end = new IntVector3(begin); // Can extend on xyz direction bool can_extend_x = true; bool can_extend_y = true; bool can_extend_z = true; // Start extending, make box while (can_extend_x || can_extend_y || can_extend_z) { // Try to extend x if (can_extend_x) { bool ext_p = true; bool ext_n = true; List <IntVector3> ext_key_p = new List <IntVector3> (); List <IntVector3> ext_key_n = new List <IntVector3> (); for (int y = begin.y; y <= end.y; y++) { for (int z = begin.z; z <= end.z; z++) { // int _tst_key_p = VCIsoData.IPosToKey(end.x + 1,y,z); // int _tst_key_n = VCIsoData.IPosToKey(begin.x - 1,y,z); IntVector3 _tst_key_p = new IntVector3(end.x + 1, y, z); IntVector3 _tst_key_n = new IntVector3(begin.x - 1, y, z); byte _tst_val_p = 0xff; byte _tst_val_n = 0xff; if (bitmap.ContainsKey(_tst_key_p)) { _tst_val_p = bitmap[_tst_key_p]; } if (bitmap.ContainsKey(_tst_key_n)) { _tst_val_n = bitmap[_tst_key_n]; } _tst_val_p = (byte)((_tst_val_p == 0xfe) ? (0xfe) : (_tst_val_p & 0xe1)); _tst_val_n = (byte)((_tst_val_n == 0xfe) ? (0xfe) : (_tst_val_n & 0xe1)); if (_tst_val_p != box_val) { ext_p = false; } else { ext_key_p.Add(_tst_key_p); } if (_tst_val_n != box_val) { ext_n = false; } else { ext_key_n.Add(_tst_key_n); } if (!ext_p && !ext_n) { break; } } if (!ext_p && !ext_n) { break; } } if (ext_p) { foreach (IntVector3 __key in ext_key_p) { // Mark this voxel (bitmap[__key]) |= (1); } end.x++; } if (ext_n) { foreach (IntVector3 __key in ext_key_n) { // Mark this voxel (bitmap[__key]) |= (1); } begin.x--; } if (!ext_p && !ext_n) { can_extend_x = false; } ext_key_p.Clear(); ext_key_n.Clear(); } // Try to extend z if (can_extend_z) { bool ext_p = true; bool ext_n = true; List <IntVector3> ext_key_p = new List <IntVector3> (); List <IntVector3> ext_key_n = new List <IntVector3> (); for (int y = begin.y; y <= end.y; y++) { for (int x = begin.x; x <= end.x; x++) { // int _tst_key_p = VCIsoData.IPosToKey(x,y,end.z + 1); // int _tst_key_n = VCIsoData.IPosToKey(x,y,begin.z - 1); IntVector3 _tst_key_p = new IntVector3(x, y, end.z + 1); IntVector3 _tst_key_n = new IntVector3(x, y, begin.z - 1); byte _tst_val_p = 0xff; byte _tst_val_n = 0xff; if (bitmap.ContainsKey(_tst_key_p)) { _tst_val_p = bitmap[_tst_key_p]; } if (bitmap.ContainsKey(_tst_key_n)) { _tst_val_n = bitmap[_tst_key_n]; } _tst_val_p = (byte)((_tst_val_p == 0xfe) ? (0xfe) : (_tst_val_p & 0xe1)); _tst_val_n = (byte)((_tst_val_n == 0xfe) ? (0xfe) : (_tst_val_n & 0xe1)); if (_tst_val_p != box_val) { ext_p = false; } else { ext_key_p.Add(_tst_key_p); } if (_tst_val_n != box_val) { ext_n = false; } else { ext_key_n.Add(_tst_key_n); } if (!ext_p && !ext_n) { break; } } if (!ext_p && !ext_n) { break; } } if (ext_p) { foreach (IntVector3 __key in ext_key_p) { // Mark this voxel (bitmap[__key]) |= (1); } end.z++; } if (ext_n) { foreach (IntVector3 __key in ext_key_n) { // Mark this voxel (bitmap[__key]) |= (1); } begin.z--; } if (!ext_p && !ext_n) { can_extend_z = false; } ext_key_p.Clear(); ext_key_n.Clear(); } // Try to extend y if (can_extend_y) { bool ext_p = true; bool ext_n = true; List <IntVector3> ext_key_p = new List <IntVector3> (); List <IntVector3> ext_key_n = new List <IntVector3> (); for (int x = begin.x; x <= end.x; x++) { for (int z = begin.z; z <= end.z; z++) { // int _tst_key_p = VCIsoData.IPosToKey(x,end.y + 1,z); // int _tst_key_n = VCIsoData.IPosToKey(x,begin.y - 1,z); IntVector3 _tst_key_p = new IntVector3(x, end.y + 1, z); IntVector3 _tst_key_n = new IntVector3(x, begin.y - 1, z); byte _tst_val_p = 0xff; byte _tst_val_n = 0xff; if (bitmap.ContainsKey(_tst_key_p)) { _tst_val_p = bitmap[_tst_key_p]; } if (bitmap.ContainsKey(_tst_key_n)) { _tst_val_n = bitmap[_tst_key_n]; } _tst_val_p = (byte)((_tst_val_p == 0xfe) ? (0xfe) : (_tst_val_p & 0xe1)); _tst_val_n = (byte)((_tst_val_n == 0xfe) ? (0xfe) : (_tst_val_n & 0xe1)); if (_tst_val_p != box_val) { ext_p = false; } else { ext_key_p.Add(_tst_key_p); } if (_tst_val_n != box_val) { ext_n = false; } else { ext_key_n.Add(_tst_key_n); } if (!ext_p && !ext_n) { break; } } if (!ext_p && !ext_n) { break; } } if (ext_p) { foreach (IntVector3 __key in ext_key_p) { // Mark this voxel (bitmap[__key]) |= (1); } end.y++; } if (ext_n) { foreach (IntVector3 __key in ext_key_n) { // Mark this voxel (bitmap[__key]) |= (1); } begin.y--; } if (!ext_p && !ext_n) { can_extend_y = false; } ext_key_p.Clear(); ext_key_n.Clear(); } } // Create box SelBox box = new SelBox(); box.m_Box.xMin = (short)(begin.x); box.m_Box.yMin = (short)(begin.y); box.m_Box.zMin = (short)(begin.z); box.m_Box.xMax = (short)(end.x); box.m_Box.yMax = (short)(end.y); box.m_Box.zMax = (short)(end.z); box.m_Val = (byte)(box_val | 0x07); leastboxes.Add(box); } } // Sort leastboxes.Sort(SortSelBox); // Increase all voxels foreach (IntVector3 key in keyarr) { (bitmap[key]) |= (0x01); } }