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); } }