public static bool RayCastDrawTarget(Ray ray, out DrawTarget target, int minvol, bool voxelbest = false) { target = new DrawTarget(); if (!VCEditor.DocumentOpen()) { return(false); } // Cast grid and voxel above minvol RaycastHit rch_voxel = new RaycastHit(); RaycastHit rch_grid = new RaycastHit(); float dist_voxel = 10000; float dist_grid = 10000; bool to_cast_grid = true; if (VCEMath.RayCastVoxel(VCEInput.s_PickRay, out rch_voxel, minvol)) { dist_voxel = rch_voxel.distance; if (voxelbest) { to_cast_grid = false; } } if (to_cast_grid && VCEMath.RayCastGrid(VCEInput.s_PickRay, out rch_grid)) { if (rch_grid.normal.y > 0) { dist_grid = rch_grid.distance; } } // cast voxel if (dist_voxel < dist_grid) { target.rch = rch_voxel; target.snapto = new IntVector3(Mathf.FloorToInt(rch_voxel.point.x - rch_voxel.normal.x * 0.5f), Mathf.FloorToInt(rch_voxel.point.y - rch_voxel.normal.y * 0.5f), Mathf.FloorToInt(rch_voxel.point.z - rch_voxel.normal.z * 0.5f)); target.cursor = new IntVector3(Mathf.FloorToInt(rch_voxel.point.x + rch_voxel.normal.x * 0.5f), Mathf.FloorToInt(rch_voxel.point.y + rch_voxel.normal.y * 0.5f), Mathf.FloorToInt(rch_voxel.point.z + rch_voxel.normal.z * 0.5f)); return(true); } // cast grid else if (dist_grid < dist_voxel) { target.rch = rch_grid; target.snapto = new IntVector3(Mathf.FloorToInt(rch_grid.point.x - rch_grid.normal.x * 0.5f), Mathf.FloorToInt(rch_grid.point.y - rch_grid.normal.y * 0.5f), Mathf.FloorToInt(rch_grid.point.z - rch_grid.normal.z * 0.5f)); target.cursor = new IntVector3(Mathf.FloorToInt(rch_grid.point.x + rch_grid.normal.x * 0.5f), Mathf.FloorToInt(rch_grid.point.y + rch_grid.normal.y * 0.5f), Mathf.FloorToInt(rch_grid.point.z + rch_grid.normal.z * 0.5f)); if (VCEditor.s_Scene.m_IsoData.GetVoxel(VCIsoData.IPosToKey(target.cursor)).Volume > VCEMath.MC_ISO_VALUE) { target.cursor = null; return(false); } return(true); } // cast nothing else { target.rch = new RaycastHit(); target.snapto = null; target.cursor = null; return(false); } }
void Update() { if (VCEditor.SelectedDecalGUID == 0) { return; } if (VCEditor.SelectedDecalIndex < 0) { return; } if (VCEditor.SelectedDecal == null) { return; } if (VCEInput.s_Cancel) { Cancel(); return; } m_DecalInst.m_Guid = VCEditor.SelectedDecalGUID; float voxel_size = VCEditor.s_Scene.m_Setting.m_VoxelSize; RaycastHit rch; if (!VCEInput.s_MouseOnUI && VCEMath.RayCastVoxel(VCEInput.s_PickRay, out rch, VCEMath.MC_ISO_VALUE)) { AdjustBrush(); m_DecalInst.gameObject.SetActive(true); Vector3 point = rch.point * 2; IntVector3 ipoint = new IntVector3(point); m_Tool.SetPivotPos(ipoint.ToVector3() * 0.5f * voxel_size); m_DecalInst.transform.localRotation = Quaternion.LookRotation(-rch.normal); m_DecalInst.transform.Rotate(-rch.normal, m_RotateAngle, Space.World); bool canput = true; List <VCComponentData> poscdatas = VCEditor.s_Scene.m_IsoData.FindComponentsAtPos(m_DecalInst.transform.localPosition, VCDecalData.s_ComponentId); foreach (VCComponentData cd in poscdatas) { VCDecalData ddata = cd as VCDecalData; if (ddata != null) { if (ddata.m_Guid == m_DecalInst.m_Guid) { canput = false; break; } } } // Mirror if (VCEditor.s_Mirror.Enabled_Masked) { VCEditor.s_Mirror.CalcPrepare(VCEditor.s_Scene.m_Setting.m_VoxelSize); if (m_DecalInst.transform.localPosition.x == VCEditor.s_Mirror.WorldPos.x && VCEditor.s_Mirror.XPlane_Masked) { canput = false; } if (m_DecalInst.transform.localPosition.y == VCEditor.s_Mirror.WorldPos.y && VCEditor.s_Mirror.YPlane_Masked) { canput = false; } if (m_DecalInst.transform.localPosition.z == VCEditor.s_Mirror.WorldPos.z && VCEditor.s_Mirror.ZPlane_Masked) { canput = false; } } if (canput) { m_Tool.m_SelBound.m_BoundColor = GLComponentBound.s_Green; m_Tool.m_SelBound.m_Highlight = false; } else { m_Tool.m_SelBound.m_BoundColor = GLComponentBound.s_Red; m_Tool.m_SelBound.m_Highlight = true; } if (Input.GetMouseButtonDown(0) && canput) { Do(); } } else { m_DecalInst.gameObject.SetActive(false); } }