blitz-research/monkey2

Camera.MousePick does not work correctly with Layout="letterbox"

Closed this issue · 2 comments

Hi,

Camera.MousePick works fine with fill layout, but stops working correctly when I switch the layout to "letterbox" or similar.

Here's some source code. I am setting the camera view property, so that should work, right? Donut turns black when mouse hovers it. Try changing the window aspect ratio after it's been created to see the problem.

Namespace myapp3d

#Import "<std>"
#Import "<mojo>"
#Import "<mojo3d>"

Using std..
Using mojo..
Using mojo3d..


Class MyWindow Extends Window
	
	Const res := New Vec2i( 1280, 720 )
	Const flags:= WindowFlags.Resizable
	Const title := "Simple mojo3d app"
	
	Field _scene:Scene
	Field _camera:Camera
	Field _light:Light
	Field _ground:Model
	Field _donut:Model
	

	Method New()
		Super.New( title ,res.X, res.Y, flags )
		Layout = "letterbox"	'<--- If you comment this line out, MousePick will work fine
	End

	
	Method OnCreateWindow() Override
		
		'create (current) scene
		_scene=New Scene
		_scene.ClearColor = New Color( 0.2, 0.6, 1.0 )
		_scene.AmbientLight = _scene.ClearColor * 0.25
		_scene.FogColor = _scene.ClearColor
		_scene.FogNear = 1.0
		_scene.FogFar = 200.0
		
		'create camera
		_camera=New Camera( Self )
		_camera.AddComponent<FlyBehaviour>()
		_camera.Move( 0,2.5,-5 )
		
		'create light
		_light=New Light
		_light.CastsShadow=True
		_light.Rotate( 45, 45, 0 )
		
		'create ground
		Local groundBox:=New Boxf( -100,-1,-100,100,0,100 )
		Local groundMaterial:=New PbrMaterial( Color.Lime )
		_ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )
		_ground.CastsShadow=False
		
		'create donut and physics components
		Local donutMaterial:=New PbrMaterial( Color.Red, 0.05, 0.2 )
		_donut=Model.CreateTorus( 2,.5,24,16,donutMaterial )
		_donut.Move( 0,2.5,0 )
		_donut.RotateX( 90 )
		
		Local body := _donut.AddComponent<RigidBody>()
		body.Mass = 0
		
		Local collider := _donut.AddComponent<MeshCollider>()
		collider.Mesh = _donut.Mesh
	End
	
	
	Method OnRender( canvas:Canvas ) Override
		RequestRender()
		_camera.View = Self
		
		Local ray := _camera.MousePick()
		If ray
			_donut.Color = Color.Black
		Else
			_donut.Color = Color.White
		End
		
		_scene.Update()
		_camera.Render( canvas )
		canvas.DrawText( "Hover mouse over donut to see MousePick work", 0, 0 )
		canvas.DrawText( "Try resizing the window to change its aspect ratio. Mousepick will stop working!",Width,0, 1, 0)
	End
	
	
	Method OnMeasure:Vec2i() Override
		Return res
	End
	
End


Function Main()
	New AppInstance
	New MyWindow
	App.Run()
End

Thanks!

Works now, thanks!