About Monkey 2 › Forums › Monkey 2 Code Library › Simple mojo3d view culler
This topic contains 4 replies, has 4 voices, and was last updated by
Mark Sibly
1 year, 1 month ago.
-
AuthorPosts
-
February 24, 2018 at 4:28 am #13713
Simple view culling logic – soon to be added to engine.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137Namespace myapp3d#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Function TransformBox:Boxf( box:Boxf,matrix:AffineMat4f )Local r:Boxf=Boxf.EmptyBoundsFor Local i:=0 Until 8r|=matrix * box.Corner( i )NextReturn rEndFunction GetClipPlanes:Planef[]( p:Mat4f )Return New Planef[](New Planef( p.i.w+p.i.x, p.j.w+p.j.x, p.k.w+p.k.x, p.t.w+p.t.x ).Normalize(),New Planef( p.i.w-p.i.x, p.j.w-p.j.x, p.k.w-p.k.x, p.t.w-p.t.x ).Normalize(),New Planef( p.i.w+p.i.y, p.j.w+p.j.y, p.k.w+p.k.y, p.t.w+p.t.y ).Normalize(),New Planef( p.i.w-p.i.y, p.j.w-p.j.y, p.k.w-p.k.y, p.t.w-p.t.y ).Normalize(),New Planef( p.i.w+p.i.z, p.j.w+p.j.z, p.k.w+p.k.z, p.t.w+p.t.z ).Normalize(),New Planef( p.i.w-p.i.z, p.j.w-p.j.z, p.k.w-p.k.z, p.t.w-p.t.z ).Normalize() )End'Simple conservative approach, just check all box corners are outside one plane.'Function CheckVisible:Bool( box:Boxf,planes:Planef[] )For Local p:=Eachin planesLocal allbehind:=TrueFor Local i:=0 Until 8If p.Distance( box.Corner( i ) )<0 Continueallbehind=FalseExitNextIf allbehind Return FalseNextReturn TrueEndFunction CheckVisible:Bool( model:Model,camera:Camera )'model space->view space matrixLocal modelView:=-camera.Matrix * model.Matrix'view space clip planesLocal clipPlanes:=GetClipPlanes( camera.ProjectionMatrix )'view space mesh boundsLocal bounds:=TransformBox( model.Mesh.Bounds,modelView )'check!Return CheckVisible( bounds,clipPlanes )EndClass MyWindow Extends WindowField _scene:SceneField _camera:CameraField _light:LightField _ground:ModelField _donut:ModelMethod New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )'create (current) scene'_scene=New Scene_scene.ShadowAlpha=.75'create camera'_camera=New Camera( Self )_camera.AddComponent<FlyBehaviour>()_camera.Move( 0,10,-5 )'create light'_light=New Light_light.RotateX( 90 )_light.CastsShadow=True'create ground'Local groundBox:=New Boxf( -50,-1,-50,50,0,50 )Local groundMaterial:=New PbrMaterial( Color.Green,0,1 )_ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )_ground.CastsShadow=False'create dount'Local donutMaterial:=New PbrMaterial( Color.Brown )_donut=Model.CreateTorus( 2,.5,48,24,donutMaterial )_donut.Move( 0,10,0 )EndMethod OnRender( canvas:Canvas ) Override'check if in view?_donut.Visible=CheckVisible( _donut,_camera )RequestRender()_donut.Rotate( .2,.4,.6 )_scene.Update()_camera.Render( canvas )canvas.DrawText( "Visible="+_donut.Visible+", FPS="+App.FPS,0,0 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndFebruary 24, 2018 at 10:23 am #13716I get a similar error to the one I got with pyro.
/untitled1.monkey2 [25] : Error : Can’t find overload for ‘new’ with argument types (monkey.types.Float,monkey.types.Float,monkey.types.Float,monkey.types.Float)
February 24, 2018 at 2:36 pm #13718Yeah, same here, doesn’t like the first New Planef. (Latest dev.)
February 25, 2018 at 10:13 am #13720Edited the code as follows and was able to run the code
Monkey1234567Return New Planef[](New Planef( New Vec3<Float>(p.i.w+p.i.x, p.j.w+p.j.x, p.k.w+p.k.x), p.t.w+p.t.x ),New Planef( New Vec3<Float>(p.i.w-p.i.x, p.j.w-p.j.x, p.k.w-p.k.x), p.t.w-p.t.x ),New Planef( New Vec3<Float>(p.i.w+p.i.y, p.j.w+p.j.y, p.k.w+p.k.y), p.t.w+p.t.y ),New Planef( New Vec3<Float>(p.i.w-p.i.y, p.j.w-p.j.y, p.k.w-p.k.y), p.t.w-p.t.y ),New Planef( New Vec3<Float>(p.i.w+p.i.z, p.j.w+p.j.z, p.k.w+p.k.z), p.t.w+p.t.z ),New Planef( New Vec3<Float>(p.i.w-p.i.z, p.j.w-p.j.z, p.k.w-p.k.z), p.t.w-p.t.z ))Object inside frustum Object outside frustum

February 25, 2018 at 11:23 pm #13727Develop branch should be updated now.
Note: if git starts giving you weird errors http messages on windows when you ‘git push’, install this:
https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/tag/v1.14.0
Modern software is rubbish…
-
AuthorPosts
You must be logged in to reply to this topic.