Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local nearD = 0 //the real distance of nearD bounces around 3e-7. Can use 0 here
- local farD = 100000000000 //the real distance of farD is like 1e12. Can use a preferred distance.
- //a radius of 0.63 will create a sphere that is approximately a marines waist
- function IsSphereInFrustum(center,radius)
- //http://www.ijvr.org/issues/issue3-2008/7.pdf
- //http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-testing-points/
- //http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-implementation/
- //game programming gems 5 under the title "Improved Frustum Culling"
- local player = Client.GetLocalPlayer()
- //todo: can be moved out of function so it's not re-calculated for each point tested
- //local cameraCoords = player:GetCameraViewCoords() //more precise...more costly
- //local cameraOrigin = cameraCoords.origin
- local cameraCoords = player:GetViewAngles():GetCoords()
- local cameraOrigin = player:GetEyePos()
- //todo: these only needs to be calcuated during resolution change
- local rFactor = math.tan(player:GetRenderFov() / 2)
- local uFactor = rFactor / GetAspectRatio() //this approach came from game programming gems 5
- local v = center - cameraOrigin
- // compute and test the Z coordinate
- local P_z = Math.DotProduct(v, cameraCoords.zAxis)
- if P_z < (nearD - radius) or (farD + radius) < P_z then
- return false //behind near-plane or in front of far-plane
- end
- // compute and test the X coordinate
- local P_x = Math.DotProduct(v, cameraCoords.xAxis)
- local limit = (rFactor * P_z) + radius //half width of frustum at distance P_z from user
- if P_x < -limit or limit < P_x then
- return false //left of left-plane or right of right-plane
- end
- // compute and test the Y coordinate
- local P_y = Math.DotProduct(v, cameraCoords.yAxis)
- limit = (uFactor * P_z) + radius
- if P_y < -limit or limit < P_y then
- return false //above top-plane or below bottom-plane
- end
- return true //is visible
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement