/* * public static bool FindFirstWorldIntersection(ChiselModel model, Vector3 worldRayStart, Vector3 worldRayEnd, int visibleLayers, out ChiselIntersection foundIntersection) * { * return FindFirstWorldIntersection(model, worldRayStart, worldRayEnd, visibleLayers, null, null, out foundIntersection); * } */ public static bool FindFirstWorldIntersection(ChiselModel model, Vector3 worldRayStart, Vector3 worldRayEnd, int visibleLayers, GameObject[] ignore, GameObject[] filter, out ChiselIntersection foundIntersection) { foundIntersection = ChiselIntersection.None; if (!ChiselGeneratedComponentManager.IsValidModelToBeSelected(model)) { return(false); } s_FilterNodes.Clear(); s_IgnoreNodes.Clear(); s_IgnoreInstanceIDs.Clear(); s_FilterInstanceIDs.Clear(); if (ignore != null) { foreach (var go in ignore) { var node = go.GetComponent <ChiselNode>(); if (node) { ChiselNodeHierarchyManager.GetChildrenOfHierarchyItem(s_IgnoreNodes, node.hierarchyItem); s_IgnoreInstanceIDs.Add(node.GetInstanceID()); } } } if (filter != null) { foreach (var go in filter) { var node = go.GetComponent <ChiselNode>(); if (node) { ChiselNodeHierarchyManager.GetChildrenOfHierarchyItem(s_FilterNodes, node.hierarchyItem); s_FilterInstanceIDs.Add(node.GetInstanceID()); if (node.hierarchyItem != null && node.hierarchyItem.Model) { s_FilterInstanceIDs.Add(node.hierarchyItem.Model.GetInstanceID()); } } } } var tree = model.Node; if (s_IgnoreInstanceIDs.Contains(model.GetInstanceID()) || (s_FilterInstanceIDs.Count > 0 && !s_FilterInstanceIDs.Contains(model.GetInstanceID()))) { return(false); } if (((1 << model.gameObject.layer) & visibleLayers) == 0) { return(false); } var query = ChiselMeshQueryManager.GetMeshQuery(model); var visibleQueries = ChiselMeshQueryManager.GetVisibleQueries(query); // We only accept RayCasts into this model if it's visible if (visibleQueries == null || visibleQueries.Length == 0) { return(false); } Vector3 treeRayStart; Vector3 treeRayEnd; var transform = model.transform; if (transform) { var worldToLocalMatrix = transform.worldToLocalMatrix; treeRayStart = worldToLocalMatrix.MultiplyPoint(worldRayStart); treeRayEnd = worldToLocalMatrix.MultiplyPoint(worldRayEnd); } else { treeRayStart = worldRayStart; treeRayEnd = worldRayEnd; } var treeIntersections = CSGQueryManager.RayCastMulti(ChiselMeshQueryManager.GetMeshQuery(model), tree, treeRayStart, treeRayEnd, s_IgnoreNodes, s_FilterNodes, ignoreBackfaced: true, ignoreCulled: true); if (treeIntersections == null) { return(false); } bool found = false; for (var i = 0; i < treeIntersections.Length; i++) { var intersection = treeIntersections[i]; var brush = intersection.brush; var instanceID = brush.UserID; if ((s_FilterInstanceIDs.Count > 0 && !s_FilterInstanceIDs.Contains(instanceID)) || s_IgnoreInstanceIDs.Contains(instanceID)) { continue; } if (intersection.surfaceIntersection.distance < foundIntersection.brushIntersection.surfaceIntersection.distance) { foundIntersection = Convert(intersection); found = true; } } return(found); }
public static bool FindFirstWorldIntersection(ChiselModel model, Vector3 worldRayStart, Vector3 worldRayEnd, int filterLayerParameter0, int visibleLayers, GameObject[] ignore, GameObject[] filter, out CSGTreeBrushIntersection foundIntersection) { foundIntersection = new CSGTreeBrushIntersection(); foundIntersection.surfaceIntersection.distance = float.PositiveInfinity; if (!model || !model.isActiveAndEnabled) { return(false); } CSGTreeNode[] ignoreBrushes = null; HashSet <int> ignoreInstanceIDs = null; HashSet <int> filterInstanceIDs = null; if (ignore != null) { //var ignoreBrushList = new HashSet<CSGTreeBrush>(); ignoreInstanceIDs = new HashSet <int>(); foreach (var go in ignore) { var node = go.GetComponent <ChiselNode>(); if (node) { //node.GetAllTreeBrushes(ignoreBrushList); ignoreInstanceIDs.Add(node.GetInstanceID()); } }/* * if (ignoreBrushList.Count > 0) * { * // TODO: fix this, ignorebrushes doesn't remove the brush completely, but that's not necessarily correct * // for example: another brush that is not ignored, subtracts from a brush that's ignored (so its insides should be selectable) * ignoreBrushes = new CSGTreeNode[ignoreBrushList.Count]; * int index = 0; * foreach(var brush in ignoreBrushList) * { * ignoreBrushes[index] = brush; * index++; * } * }*/ } if (filter != null) { filterInstanceIDs = new HashSet <int>(); foreach (var go in filter) { var node = go.GetComponent <ChiselNode>(); if (node) { filterInstanceIDs.Add(node.GetInstanceID()); } } } var tree = model.Node; if ((ignoreInstanceIDs != null && ignoreInstanceIDs.Contains(model.GetInstanceID()))) { return(false); } if ((filterInstanceIDs != null && !filterInstanceIDs.Contains(model.GetInstanceID()))) { return(false); } if (((1 << model.gameObject.layer) & visibleLayers) == 0) { return(false); } var query = CSGMeshQueryManager.GetMeshQuery(model); // We only accept RayCasts into this model if it's visible if (!CSGMeshQueryManager.IsVisible(query)) { return(false); } Vector3 treeRayStart; Vector3 treeRayEnd; var transform = model.transform; if (transform) { var worldToLocalMatrix = transform.worldToLocalMatrix; treeRayStart = worldToLocalMatrix.MultiplyPoint(worldRayStart); treeRayEnd = worldToLocalMatrix.MultiplyPoint(worldRayEnd); } else { treeRayStart = worldRayStart; treeRayEnd = worldRayEnd; } var treeIntersections = tree.RayCastMulti(CSGMeshQueryManager.GetMeshQuery(model), treeRayStart, treeRayEnd, model.transform.localToWorldMatrix, filterLayerParameter0, ignoreBrushes); if (treeIntersections == null) { return(false); } bool found = false; for (var i = 0; i < treeIntersections.Length; i++) { var intersection = treeIntersections[i]; var brush = intersection.brush; var instanceID = brush.UserID; if ((filterInstanceIDs != null && !filterInstanceIDs.Contains(instanceID))) { continue; } if ((ignoreInstanceIDs != null && ignoreInstanceIDs.Contains(instanceID))) { continue; } if (intersection.surfaceIntersection.distance < foundIntersection.surfaceIntersection.distance) { foundIntersection = intersection; found = true; } } return(found); }
public static bool FindFirstWorldIntersection(ChiselModel model, Vector3 worldRayStart, Vector3 worldRayEnd, int filterLayerParameter0, int visibleLayers, GameObject[] ignore, GameObject[] filter, out ChiselIntersection foundIntersection) { foundIntersection = ChiselIntersection.None; if (!ChiselGeneratedComponentManager.IsValidModelToBeSelected(model)) { return(false); } CSGTreeNode[] ignoreBrushes = null; HashSet <int> ignoreInstanceIDs = null; HashSet <int> filterInstanceIDs = null; if (ignore != null) { //var ignoreBrushList = new HashSet<CSGTreeBrush>(); ignoreInstanceIDs = new HashSet <int>(); foreach (var go in ignore) { var node = go.GetComponent <ChiselNode>(); if (node) { //node.GetAllTreeBrushes(ignoreBrushList); ignoreInstanceIDs.Add(node.GetInstanceID()); } } } if (filter != null) { filterInstanceIDs = new HashSet <int>(); foreach (var go in filter) { var node = go.GetComponent <ChiselNode>(); if (node) { filterInstanceIDs.Add(node.GetInstanceID()); } } } var tree = model.Node; if ((ignoreInstanceIDs != null && ignoreInstanceIDs.Contains(model.GetInstanceID()))) { return(false); } if ((filterInstanceIDs != null && !filterInstanceIDs.Contains(model.GetInstanceID()))) { return(false); } if (((1 << model.gameObject.layer) & visibleLayers) == 0) { return(false); } var query = ChiselMeshQueryManager.GetMeshQuery(model); var visibleQueries = ChiselMeshQueryManager.GetVisibleQueries(query); // We only accept RayCasts into this model if it's visible if (visibleQueries == null || visibleQueries.Length == 0) { return(false); } Vector3 treeRayStart; Vector3 treeRayEnd; var transform = model.transform; if (transform) { var worldToLocalMatrix = transform.worldToLocalMatrix; treeRayStart = worldToLocalMatrix.MultiplyPoint(worldRayStart); treeRayEnd = worldToLocalMatrix.MultiplyPoint(worldRayEnd); } else { treeRayStart = worldRayStart; treeRayEnd = worldRayEnd; } var treeIntersections = CSGManager.RayCastMulti(ChiselMeshQueryManager.GetMeshQuery(model), tree, treeRayStart, treeRayEnd, ignoreBrushes); if (treeIntersections == null) { return(false); } bool found = false; for (var i = 0; i < treeIntersections.Length; i++) { var intersection = treeIntersections[i]; var brush = intersection.brush; var instanceID = brush.UserID; if ((filterInstanceIDs != null && !filterInstanceIDs.Contains(instanceID))) { continue; } if ((ignoreInstanceIDs != null && ignoreInstanceIDs.Contains(instanceID))) { continue; } if (intersection.surfaceIntersection.distance < foundIntersection.brushIntersection.surfaceIntersection.distance) { foundIntersection = Convert(intersection); found = true; } } return(found); }