// // Written by Andrew Chapman - July 2002 - chapman@technolumiere.com // // Returns how far from the center of the sphere the given point is, // so that values of 0 to 1 are within the sphere, and greater values // mean that the point lies outside the sphere. // // Returns the distance rather than a simple boolean so that you can // do falloff/attenuation, etc. // // Works by simply transforming the point given by the inverse transform // matrix of the named sphere, then calculating the distance of this // transformed point from the world origin. If the distance is less than // or equal to one, then the point is within the sphere, otherwise it is // outside of it. The actual value returned is the distance from the centre // of the sphere. global proc float isPointWithinSphere(vector $pt, string $sphereName) { float $pti[4] = {($pt.x), ($pt.y), ($pt.z), 1.0}; float $invMat[] = `getAttr ($sphereName + ".worldInverseMatrix")`; float $res[3] = { $invMat[0] * $pti[0] + $invMat[4] * $pti[1] + $invMat[8] * $pti[2] + $invMat[12] * $pti[3], $invMat[1] * $pti[0] + $invMat[5] * $pti[1] + $invMat[9] * $pti[2] + $invMat[13] * $pti[3], $invMat[2] * $pti[0] + $invMat[6] * $pti[1] + $invMat[10] * $pti[2] + $invMat[14] * $pti[3] }; return (`mag(<<$res[0],$res[1],$res[2]>>)`); }