浏览代码

Added some new mojo3d tests.

Mark Sibly 7 年之前
父节点
当前提交
6fa5e94739
共有 3 个文件被更改,包括 526 次插入0 次删除
  1. 87 0
      modules/mojo3d/tests/alphacubes.monkey2
  2. 324 0
      modules/mojo3d/tests/bouncyboxes.monkey2
  3. 115 0
      modules/mojo3d/tests/room.monkey2

+ 87 - 0
modules/mojo3d/tests/alphacubes.monkey2

@@ -0,0 +1,87 @@
+Namespace myapp
+
+#Import "<std>"
+#Import "<mojo>"
+#Import "<mojo3d>"
+
+#Import "assets/"
+
+Using std..
+Using mojo..
+Using mojo3d..
+
+Class MyWindow Extends Window
+	
+	Field _scene:Scene
+	
+	Field _camera:Camera
+	
+	Field _light:Light
+	
+	Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
+
+		Super.New( title,width,height,flags )
+		
+		SetConfig( "MOJO3D_RENDERER","forward" )
+
+		'create scene
+		'		
+		_scene=New Scene
+		_scene.ClearColor=Color.Sky
+		_scene.FogColor=Color.Sky
+		_scene.FogNear=25
+		_scene.FogFar=50
+		
+		'create camera
+		'
+		_camera=New Camera( Self )
+		_camera.Near=.1
+		_camera.Far=100
+		_camera.Move( 0,10,-10 )
+		
+		New FlyBehaviour( _camera )
+		
+		'create light
+		'
+		_light=New Light
+		_light.Rotate( 30,60,0 )
+		
+		'Create cube
+		'
+		Local cube:=Model.CreateBox( New Boxf( -1,1 ),1,1,1,New PbrMaterial( Color.Grey ) )
+		
+		cube.CastsShadow=False
+		
+		For Local x:=-50.0 To 50.0 Step 2.5
+			For Local z:=-50.0 To 50.0 Step 2.5
+				Local copy:=cube.Copy()
+				copy.Move( x,0,z )
+				
+				copy.Alpha=(x+50)/100
+			Next
+		Next
+		
+		cube.Destroy()
+	End
+	
+	Method OnRender( canvas:Canvas ) Override
+	
+		RequestRender()
+		
+		_scene.Update()
+		
+		_camera.Render( canvas )
+		
+		canvas.DrawText( "FPS="+App.FPS,Width,0,1,0 )
+	End
+	
+End
+
+Function Main()
+	
+	New AppInstance
+	
+	New MyWindow
+	
+	App.Run()
+End

+ 324 - 0
modules/mojo3d/tests/bouncyboxes.monkey2

@@ -0,0 +1,324 @@
+
+#Import "<std>"
+#Import "<mojo>"
+#Import "<mojo3d>"
+#Import "<bullet>"
+ 
+Using std..
+Using mojo..
+Using mojo3d..
+ 
+Const WALL_WIDTH:Int	= 20
+Const WALL_HEIGHT:Int	= 20
+ 
+Class PhysBox
+ 
+	Class PhysParams
+		Field mass:Float
+		Field group:Short
+		Field mask:Short
+	End
+	
+	Field box:Boxf
+	Field model:Model
+	Field collider:BoxCollider
+	Field body:RigidBody
+ 
+	Field init:PhysParams
+	 
+	Method New (width:Float = 1, height:Float = 1, depth:Float = 1, mass:Float = 1, material:Material = Null, group:Int = 1, mask:Int = 1)
+ 
+		' Store setup params for PhysBox.Start ()
+		
+'		SetConfig( "MOJO3D_RENDERER","forward" )
+		
+		init = New PhysParams
+		
+		init.mass	= mass
+		init.group	= group
+		init.mask	= mask
+		
+		If Not material
+			material = New PbrMaterial (Color.Red)
+		Endif
+		
+		box					= New Boxf (-width * 0.5, -height * 0.5, -depth * 0.5, width * 0.5, height * 0.5, depth * 0.5)
+		model				= Model.CreateBox (box, 1, 1, 1, material)
+ 
+	End
+ 
+	Method Start ()
+ 
+		collider			= New BoxCollider (model)
+		body				= New RigidBody (model)
+ 
+		collider.Box		= box
+	
+		body.Mass			= init.mass
+		body.CollisionGroup	= init.group
+		body.CollisionMask	= init.mask
+		
+		box					= Null
+		init				= Null
+		
+	End
+	 
+	Method Move (x:Float, y:Float, z:Float)
+		model.Move (x, y, z)
+	End
+ 
+	Method Rotate (pitch:Float, roll:Float, yaw:Float, localspace:Bool = False)
+		model.Rotate (pitch, roll, yaw, localspace)
+	End
+	
+End
+ 
+Class Game Extends Window
+ 
+	Const CAMERA_MOVE:Float = 0.05
+	Const CAMERA_BOOST:Float = 4.0
+	
+	Field cam_boost:Float = 1.0
+	 
+	Field scene:Scene
+	Field cam:Camera
+	Field light:Light
+	
+	Field ground:PhysBox
+ 
+	Field boxes:List <PhysBox>
+	Field bullets:List <PhysBox>
+	
+	Method New (title:String, width:Int, height:Int, flags:WindowFlags)
+ 
+		Super.New (title, width, height, flags)
+ 
+		SwapInterval = 1
+		
+		CreateArena (50)
+		
+	End
+	
+	Method CreateArena:Void (ground_size:Float = 100)
+ 
+		SeedRnd (Millisecs ())
+		
+		scene					= Scene.GetCurrent ()
+		ground					= New PhysBox (ground_size, 1, ground_size, 0, New PbrMaterial (Color.Green * 0.25))
+		cam						= New Camera( Self )
+		light					= New Light
+ 
+		scene.AmbientLight	= Color.White * 0.75
+		scene.ClearColor	= Color.Sky * 0.75
+		scene.ShadowAlpha	= .5
+ 
+		cam.FOV = 100
+		cam.Move (0, 10, -10)
+		
+		ground.Start ()
+		
+		cam.Near				= 0.01
+		cam.Far					= 1000
+	
+		light.CastsShadow		= True
+		light.Range				= 1000
+ 
+		light.Move (0, 20, 10)
+		
+'		cam.PointAt (ground.model)
+		light.PointAt (ground.model)
+ 
+		boxes	= New List <PhysBox>
+		bullets	= New List <PhysBox>
+ 
+		BuildWall (WALL_WIDTH, WALL_HEIGHT)
+		
+	End
+ 
+	Method BuildWall (width:Int, height:Int)
+	
+		For Local y:Int = 0 Until height
+			For Local x:Int = 0 Until width
+				
+				Local color:Color = New Color (Rnd (0.4, 1.0), Rnd (0.4, 1.0), Rnd (0.4, 1.0))
+				
+				' Create new PhysBox...
+				
+				Local pb:PhysBox = New PhysBox (1, 1, 1, 1, New PbrMaterial (color))
+				
+				' Position PhysBox...
+				
+	 			pb.Move (x - (width / 2.0), (y + 1), 0)
+	
+				boxes.Add (pb)
+				
+				' Start its physics...
+				
+				pb.Start ()
+				
+			Next
+		Next
+ 
+	End
+ 
+	Method DropBox ()
+ 
+		Local pb:PhysBox = New PhysBox (1, 1, 1, 1, New PbrMaterial (Color.Red))
+		
+		pb.Move (0, 50, 0)
+ 
+		boxes.Add (pb)
+		
+		pb.Start ()
+ 
+	End
+	
+	Method BumpWall ()
+ 
+		Local vec:Vec3f = New Vec3f (0.0, 0.5, 0.0)
+	
+		'Local count:Int
+		
+		For Local pb:PhysBox = Eachin boxes
+			'If Not pb.collider Then Print "No collider"
+			'If Not pb.body Then Print "No body"
+			pb.body.ApplyImpulse (vec)
+			'pb.body.LinearVelocity=vec
+			'Print "Body count: " + count
+			'count = count + 1
+		Next
+		
+		'Print Millisecs ()
+		
+	End
+	
+	Method UpdateBoxes ()
+ 
+		For Local pb:PhysBox = Eachin boxes
+ 
+			If pb.model.Y < -20
+ 
+				pb.model.Scale = pb.model.Scale * 0.75
+ 
+				If pb.model.Scale.x < 0.01
+					pb.model.Destroy ()
+					boxes.Remove (pb)
+				Endif
+ 
+			Endif
+ 
+		Next
+ 
+	End
+ 
+	Method UpdateGame ()
+		
+		UpdateBoxes ()
+		
+		If Keyboard.KeyDown (Key.D)
+			DropBox ()
+		Endif
+ 	
+		If Keyboard.KeyDown (Key.B)
+			BumpWall ()
+		Endif
+ 	
+		If Keyboard.KeyHit (Key.Escape)
+			App.Terminate ()
+		Endif
+ 	
+ 		If Keyboard.KeyHit (Key.S)
+			light.CastsShadow = Not light.CastsShadow
+		Endif
+	 
+ 		If Keyboard.KeyHit (Key.Space)
+ 			
+ 			For Local pb:PhysBox = Eachin boxes
+				pb.model.Destroy ()
+				boxes.Remove (pb)
+			Next
+			
+			BuildWall (WALL_WIDTH, WALL_HEIGHT)
+			
+		Endif
+	 
+		If Keyboard.KeyDown (Key.LeftShift)
+			cam_boost = CAMERA_BOOST
+		Else
+			cam_boost = 1.0
+		Endif
+		
+		If Keyboard.KeyDown (Key.A)
+			cam.Move (0.0, 0.0, CAMERA_MOVE * cam_boost)
+		Endif
+ 
+		If Keyboard.KeyDown (Key.Z)
+			cam.Move (0.0, 0.0, -CAMERA_MOVE * cam_boost)
+		Endif
+ 
+		If Keyboard.KeyDown (Key.Left)
+			cam.Rotate (0.0, 1.0, 0.0)
+		Endif
+ 
+		If Keyboard.KeyDown (Key.Right)
+			cam.Rotate (0.0, -1.0, 0.0)
+		Endif
+ 
+		If Keyboard.KeyDown (Key.Up)
+			cam.Rotate (1.0, 0.0, 0.0, True)
+		Endif
+ 
+		If Keyboard.KeyDown (Key.Down)
+			cam.Rotate (-1.0, 0.0, 0.0, True)
+		Endif
+		
+	End
+ 
+	Method ShadowText:Void (canvas:Canvas, s:String, x:Float, y:Float)
+		canvas.Color = Color.Black
+		canvas.DrawText	(s, x + 1, y + 1)
+		canvas.Color = Color.White
+		canvas.DrawText	(s, x, y)
+	End
+ 
+	Method RenderText (canvas:Canvas)
+		
+		ShadowText (canvas, "FPS: " + App.FPS, 20.0, 20.0)
+		ShadowText (canvas, "A/Z + Cursors to move camera", 20.0, 40.0)
+		ShadowText (canvas, "SHIFT to boost", 20.0, 60.0)
+		ShadowText (canvas, "SPACE to rebuild wall", 20.0, 80.0)
+		ShadowText (canvas, "S to toggle shadows", 20.0, 100.0)
+		ShadowText (canvas, "B to boost boxes", 20.0, 120.0)
+ 		ShadowText (canvas, "Boxes: " + boxes.Count (), 20.0, 160.0)
+		
+	End
+	
+	Method OnRender (canvas:Canvas) Override
+ 
+		RequestRender ()
+		
+		UpdateGame ()
+		
+		scene.Update ()
+		
+		scene.Render (canvas)
+ 
+		RenderText (canvas)
+		
+	End
+	
+End
+ 
+Function Run3D (title:String, width:Int, height:Int, flags:WindowFlags = WindowFlags.Center)
+ 
+	New AppInstance
+	New Game (title, width, height, flags)
+ 
+	App.Run ()
+ 
+End
+ 
+Function Main ()
+	Run3D ("3D Scene", 960, 540, WindowFlags.Center)		' 1/4 HD!
+'	Run3D ("3D Scene", 1920, 1080, WindowFlags.Fullscreen) 
+End

+ 115 - 0
modules/mojo3d/tests/room.monkey2

@@ -0,0 +1,115 @@
+Namespace myapp
+
+#Import "<std>"
+#Import "<mojo>"
+#Import "<mojo3d>"
+
+#Import "assets/"
+
+Using std..
+Using mojo..
+Using mojo3d..
+
+Function Main()
+	
+	New AppInstance
+	
+	New MyWindow
+	
+	App.Run()
+End
+
+Class MyWindow Extends Window
+	
+	Field _scene:Scene
+	
+	Field _camera:Camera
+	
+	Field _light:Light
+	
+	Field _room:Model
+	
+	Field _fish:Entity
+	
+	Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
+
+		Super.New( title,width,height,flags )
+		
+		SetConfig( "MOJO3D_RENDERER","forward" )
+		
+		'Create scene
+		_scene=New Scene
+		_scene.ClearColor=Color.Black
+		_scene.AmbientLight=Color.Black'DarkGrey*.2
+		_scene.EnvColor=Color.DarkGrey
+		_scene.ShadowAlpha=.7
+		
+		'Create camera
+		_camera=New Camera( Self )
+		_camera.Move( 0,0,-5 )
+		
+		New FlyBehaviour( _camera )
+		
+		'Create light
+		_light=New Light
+		_light.Type=LightType.Point
+		_light.CastsShadow=True
+		_light.Range=15
+		
+		Local sphereMaterial:=New PbrMaterial( Color.Black,0,1 )
+		sphereMaterial.EmissiveFactor=_light.Color
+		
+		'Local sphere:=Model.CreateSphere( .1,24,12,sphereMaterial,_light )
+		'sphere.CastsShadow=False
+		
+		'_light.Visible=False
+		
+		'Create room
+		Local roomBox:=New Boxf( -10,10 )
+		Local roomMaterial:=New PbrMaterial( Color.Orange )
+'		Local roomMaterial:=PbrMaterial.Load( "asset::blocksrough.pbr" )
+		_room=Model.CreateBox( roomBox,1,1,1,roomMaterial )
+		_room.Mesh.FlipTriangles()
+		_room.Mesh.UpdateNormals()
+		_room.Mesh.UpdateTangents()
+		_room.CastsShadow=False
+		
+		Local fish:=Model.Load( "asset::fish.glb" )
+'		Local fish:=Model.Load( "asset::barramundi.gltf/BarramundiFish.gltf" )
+		fish.Mesh.FitVertices( New Boxf( -1,1 ) )
+		
+		_fish=New Entity
+		_fish.Visible=True
+		
+		For Local an:=0 Until 360 Step 15
+			
+			local copy:=fish.Copy( _fish )
+			
+			copy.Rotate( 0,an,0 )
+			copy.Move( 0,Sin( an )*3,Rnd( 2.5,7.5 ) )
+			
+			copy.Rotate( 0,-90,0 )
+			
+		Next
+		
+		fish.Destroy()
+		
+	End
+	
+	Method OnRender( canvas:Canvas ) Override
+	
+		RequestRender()
+		
+		_fish.Rotate( 0,.1,0 )
+		
+		_scene.Update()
+		
+		_camera.Render( canvas )
+		
+		canvas.DrawText(	"POS="+_camera.Position,0,0 )
+		
+		canvas.DrawText( "FPS "+App.FPS,Width,0,1,0 )
+	End
+	
+End
+