public static bool GetNodesInFrustum(Frustum frustum, int visibleLayers, ref HashSet <CSGTreeNode> rectFoundNodes) { rectFoundNodes.Clear(); var planes = new Plane[6]; Vector4 srcVector; var allTrees = CSGManager.AllTrees; for (var t = 0; t < allTrees.Length; t++) { var tree = allTrees[t]; var model = ChiselNodeHierarchyManager.FindChiselNodeByTreeNode(tree) as ChiselModel; if (!ChiselModelManager.IsVisible(model)) { continue; } if (((1 << model.gameObject.layer) & visibleLayers) == 0) { continue; } 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) { continue; } // Transform the frustum into the space of the tree var transform = model.transform; var worldToLocalMatrixInversed = transform.localToWorldMatrix; // localToWorldMatrix == worldToLocalMatrix.inverse var worldToLocalMatrixInversedTransposed = worldToLocalMatrixInversed.transpose; for (int p = 0; p < 6; p++) { var srcPlane = frustum.Planes[p]; srcVector.x = srcPlane.normal.x; srcVector.y = srcPlane.normal.y; srcVector.z = srcPlane.normal.z; srcVector.w = srcPlane.distance; srcVector = worldToLocalMatrixInversedTransposed * srcVector; planes[p].normal = srcVector; planes[p].distance = srcVector.w; } var treeNodesInFrustum = CSGManager.GetNodesInFrustum(tree, query, planes); if (treeNodesInFrustum == null) { continue; } for (int n = 0; n < treeNodesInFrustum.Length; n++) { var treeNode = treeNodesInFrustum[n]; rectFoundNodes.Add(treeNode); } } return(rectFoundNodes.Count > 0); }