/// Intersect ray public override bool Invoke(Ray ray, uint idx, ref float maxDist, bool stopAtFirst) { if (idx >= objects_size) { return(false); } T obj = objects[idx]; if (obj != null) { return(_callback.Invoke(ray, obj, ref maxDist)); } return(false); }
public void IntersectPoint(Vector3 p, WorkerCallback intersectCallback) { if (!bounds.contains(p)) { return; } StackNode[] stack = new StackNode[64]; int stackPos = 0; int node = 0; while (true) { while (true) { uint tn = tree[node]; uint axis = (uint)(tn & (3 << 30)) >> 30; bool BVH2 = Convert.ToBoolean(tn & (1 << 29)); int offset = (int)(tn & ~(7 << 29)); if (!BVH2) { if (axis < 3) { // "normal" interior node float tl = IntBitsToFloat(tree[node + 1]); float tr = IntBitsToFloat(tree[node + 2]); // point is between clip zones if (tl < p[(int)axis] && tr > p[axis]) { break; } int right = offset + 3; node = right; // point is in right node only if (tl < p[(int)axis]) { continue; } node = offset; // left // point is in left node only if (tr > p[axis]) { continue; } // point is in both nodes // push back right node stack[stackPos].node = (uint)right; stackPos++; continue; } else { // leaf - test some objects uint n = tree[node + 1]; while (n > 0) { intersectCallback.Invoke(p, objects[offset]); // !!! --n; ++offset; } break; } } else // BVH2 node (empty space cut off left and right) { if (axis > 2) { return; // should not happen } float tl = IntBitsToFloat(tree[node + 1]); float tr = IntBitsToFloat(tree[node + 2]); node = offset; if (tl > p[axis] || tr < p[axis]) { break; } continue; } } // traversal loop // stack is empty? if (stackPos == 0) { return; } // move back up the stack stackPos--; node = (int)stack[stackPos].node; } }
public void IntersectRay(Ray r, WorkerCallback intersectCallback, ref float maxDist, bool stopAtFirst = false) { float intervalMin = -1.0f; float intervalMax = -1.0f; Vector3 org = r.Origin; Vector3 dir = r.Direction; Vector3 invDir = new(); for (int i = 0; i < 3; ++i) { invDir[i] = 1.0f / dir[i]; if (MathFunctions.fuzzyNe(dir[i], 0.0f)) { float t1 = (bounds.Lo[i] - org[i]) * invDir[i]; float t2 = (bounds.Hi[i] - org[i]) * invDir[i]; if (t1 > t2) { MathFunctions.Swap <float>(ref t1, ref t2); } if (t1 > intervalMin) { intervalMin = t1; } if (t2 < intervalMax || intervalMax < 0.0f) { intervalMax = t2; } // intervalMax can only become smaller for other axis, // and intervalMin only larger respectively, so stop early if (intervalMax <= 0 || intervalMin >= maxDist) { return; } } } if (intervalMin > intervalMax) { return; } intervalMin = Math.Max(intervalMin, 0.0f); intervalMax = Math.Min(intervalMax, maxDist); uint[] offsetFront = new uint[3]; uint[] offsetBack = new uint[3]; uint[] offsetFront3 = new uint[3]; uint[] offsetBack3 = new uint[3]; // compute custom offsets from direction sign bit for (int i = 0; i < 3; ++i) { offsetFront[i] = FloatToRawIntBits(dir[i]) >> 31; offsetBack[i] = offsetFront[i] ^ 1; offsetFront3[i] = offsetFront[i] * 3; offsetBack3[i] = offsetBack[i] * 3; // avoid always adding 1 during the inner loop ++offsetFront[i]; ++offsetBack[i]; } StackNode[] stack = new StackNode[64]; int stackPos = 0; int node = 0; while (true) { while (true) { uint tn = tree[node]; uint axis = (uint)(tn & (3 << 30)) >> 30; bool BVH2 = Convert.ToBoolean(tn & (1 << 29)); int offset = (int)(tn & ~(7 << 29)); if (!BVH2) { if (axis < 3) { // "normal" interior node float tf = (IntBitsToFloat(tree[(int)(node + offsetFront[axis])]) - org[axis]) * invDir[axis]; float tb = (IntBitsToFloat(tree[(int)(node + offsetBack[axis])]) - org[axis]) * invDir[axis]; // ray passes between clip zones if (tf < intervalMin && tb > intervalMax) { break; } int back = (int)(offset + offsetBack3[axis]); node = back; // ray passes through far node only if (tf < intervalMin) { intervalMin = (tb >= intervalMin) ? tb : intervalMin; continue; } node = offset + (int)offsetFront3[axis]; // front // ray passes through near node only if (tb > intervalMax) { intervalMax = (tf <= intervalMax) ? tf : intervalMax; continue; } // ray passes through both nodes // push back node stack[stackPos].node = (uint)back; stack[stackPos].tnear = (tb >= intervalMin) ? tb : intervalMin; stack[stackPos].tfar = intervalMax; stackPos++; // update ray interval for front node intervalMax = (tf <= intervalMax) ? tf : intervalMax; continue; } else { // leaf - test some objects int n = (int)tree[node + 1]; while (n > 0) { bool hit = intersectCallback.Invoke(r, objects[offset], ref maxDist, stopAtFirst); if (stopAtFirst && hit) { return; } --n; ++offset; } break; } } else { if (axis > 2) { return; // should not happen } float tf = (IntBitsToFloat(tree[(int)(node + offsetFront[axis])]) - org[axis]) * invDir[axis]; float tb = (IntBitsToFloat(tree[(int)(node + offsetBack[axis])]) - org[axis]) * invDir[axis]; node = offset; intervalMin = (tf >= intervalMin) ? tf : intervalMin; intervalMax = (tb <= intervalMax) ? tb : intervalMax; if (intervalMin > intervalMax) { break; } continue; } } // traversal loop do { // stack is empty? if (stackPos == 0) { return; } // move back up the stack stackPos--; intervalMin = stack[stackPos].tnear; if (maxDist < intervalMin) { continue; } node = (int)stack[stackPos].node; intervalMax = stack[stackPos].tfar; break; } while (true); } }