About Monkey 2 › Forums › Monkey 2 Code Library › Very basic mojo3d application/template
This topic contains 3 replies, has 2 voices, and was last updated by
DruggedBunny
1 year, 8 months ago.
-
AuthorPosts
-
July 21, 2017 at 1:51 am #9453
[Updated]
Minimal mojo3d example. Just shows minimal setup needed, with an example cube and basic camera movement — use cursors plus A & Z.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122#Import "<std>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..' ----------------------------------------' Application name...' ----------------------------------------Global AppName:String = "My 3D Game"Class Game Extends Window' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:Light' Test cube...Field cube:ModelField cubemesh:MeshField cubematerial:MaterialMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)scene = Scene.GetCurrent () ' Important!camera = New Camera' Camera settings...camera.Near = 0.1' Camera position...camera.Move (0, 0, -2)camera.Rotate (0, 0, 0)light = New Lightlight.Move (-10, 10, -10)light.Rotate (0, 0, 0)cubemesh = Mesh.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5)) ' 1 x 1 x 1cubematerial = New PbrMaterial (Color.Aluminum)cube = New Model (cubemesh, cubematerial)cube.Rotate (0, 45, 0)EndMethod OnRender (canvas:Canvas) OverrideIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.A)camera.Move (0.0, 0.0, 0.1)EndifIf Keyboard.KeyDown (Key.Z)camera.Move (0.0, 0.0, -0.1)EndifIf Keyboard.KeyDown (Key.Left)camera.Rotate (0.0, 1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Right)camera.Rotate (0.0, -1.0, 0.0)EndifIf Keyboard.KeyDown (Key.Up)camera.Rotate (1.0, 0.0, 0.0)EndifIf Keyboard.KeyDown (Key.Down)camera.Rotate (-1.0, 0.0, 0.0)Endifcube.Rotate (0.0, 1.0, 0.0)' Render scene to canvas (passed to OnRender by mojo), from camera...scene.Render (canvas, camera)' Tell app to draw frame when ready...RequestRender ()EndEndFunction Main ()' Windowed mode...Local width:Int = 640Local height:Int = 480Local flags:WindowFlags = WindowFlags.Center' Full-screen mode (comment out above)...' Local width:Int = 1920' Local height:Int = 1080' Local flags:WindowFlags = WindowFlags.FullscreenNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()EndI think you definitely need to have your own template for mojo3d, as the app setup is a LOT more complicated than Blitz3D. (Which was, er, Graphics3D 640, 480!)
However, once set up, it should be just as easy to use for simple messing about.
July 21, 2017 at 1:54 am #9454… and a stripped-down version without the demo cube or camera controls. Shows nothing, just ready for you to take over! [Updated]
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#Import "<std>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..' ----------------------------------------' Application name...' ----------------------------------------Global AppName:String = "My 3D Game"Class Game Extends Window' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:LightMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)scene = Scene.GetCurrent () ' Important!camera = New Cameralight = New LightEndMethod OnRender (canvas:Canvas) Overridescene.Render (canvas, camera)RequestRender ()EndEndFunction Main ()' Windowed mode...Local width:Int = 640Local height:Int = 480Local flags:WindowFlags = WindowFlags.CenterNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()EndJuly 21, 2017 at 11:31 am #9460At this point I will add (if and when I have time!) some Blitz3d equivalent ‘command’ like
EntityPosition entity,x,y,z
EntityRotate entity,x,y,zwhere (at least for Rotate) x,y,z are internally converted using Degrees(x)
ps: never touched at the moment, I dont’ know if ‘entity’ can be any thing (like Light, Camera, Mesh etc)
August 1, 2017 at 9:56 pm #9643Been having a bit of fun with this. Add/remove 100 cubes at a time via plus and minus keys:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159#Import "<std>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..' ----------------------------------------' Application name...' ----------------------------------------Global AppName:String = "My 3D Game"' ----------------------------------------Function CreateEasyBox:Model (color:Color = Color.Aluminum)Local box:ModelLocal boxmesh:MeshLocal boxmaterial:Materialboxmesh = Mesh.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5))boxmaterial = New PbrMaterial (color)box = New Model (boxmesh, boxmaterial)Return boxEndFunction AddBoxes (stack:Stack <Model>, number:Int)For Local loop:Int = 1 To numberLocal box:Model = CreateEasyBox (New Color (Rnd (0.4, 1.0), Rnd (0.4, 1.0), Rnd (0.4, 1.0)))box.Move (Rnd (-100, 100), Rnd (-100, 100), Rnd (-100, 100))box.SetScale (Rnd (1, 10), Rnd (1, 10), Rnd (1, 10))stack.Push (box)NextEndFunction RemoveBoxes (stack:Stack <Model>, number:Int)number = Min (stack.Length, number)For Local loop:Int = 1 To numberstack.Pop ().Destroy ()NextEndClass Game Extends Window' Basic 3D scene requirements...Field scene:SceneField camera:CameraField light:LightField boxes:Stack<Model>Method New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags)scene = Scene.GetCurrent ()camera = New Cameralight = New Lightlight.Move (-50, 50, 0)light.Rotate (0, 90, 0)light.ShadowsEnabled = Falseboxes = New Stack<Model>EndMethod ProcessInput:Void ()' If Keyboard.KeyHit (Key.Space) Then light.ShadowsEnabled = Not light.ShadowsEnabledIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()If Keyboard.KeyDown (Key.A)camera.Move (0.0, 0.0, 0.5)EndIf Keyboard.KeyDown (Key.Z)camera.Move (0.0, 0.0, -0.5)EndIf Keyboard.KeyDown (Key.Left)camera.Rotate (0.0, 1.0, 0.0, True)EndIf Keyboard.KeyDown (Key.Right)camera.Rotate (0.0, -1.0, 0.0, true)EndIf Keyboard.KeyDown (Key.Up)camera.Rotate (1.0, 0.0, 0.0, True)EndIf Keyboard.KeyDown (Key.Down)camera.Rotate (-1.0, 0.0, 0.0, True)EndIf Keyboard.KeyHit (Key.Equals) Or Keyboard.KeyHit (Key.KeypadPlus)AddBoxes (boxes, 100)EndifIf Keyboard.KeyHit (Key.Minus) Or Keyboard.KeyHit (Key.KeypadMinus)RemoveBoxes (boxes, 100)EndifEndMethod OnRender (canvas:Canvas) OverrideProcessInput ()For Local box:Model = Eachin boxesbox.Rotate (1.0, 2.0, 4.0)Nextscene.Render (canvas, camera)canvas.DrawText ("Cursors plus A/Z to move", 20, 20)canvas.DrawText ("Use - and + to add/remove 100 boxes (currently " + boxes.Length + ")", 20, 60)RequestRender ()EndEndFunction Main ()' Windowed mode...Local width:Int = 1920Local height:Int = 1080Local flags:WindowFlags = WindowFlags.FullscreenNew AppInstanceNew Game (AppName, width, height, flags)App.Run ()EndI notice that it gets slow very quickly (like, around 1,000 cubes), but I think it’s at least partly because mojo3d isn’t doing visibility culling yet — a 10,000-cube demo in Blitz3D runs very well when the camera is in the middle of the cube ‘cloud’, but slows down as you back out to view more, while similar numbers cripple mojo3d at the moment, regardless of how many are in view!
Here it is online via Emscripten — works for me in Firefox and Opera; Vivaldi didn’t like it and I don’t have any other up-to-date browsers installed!
http://www.hi-toro.com/temp/mx2/Incube.html
Click within the canvas and hit + to get started!
Oh, and I LOVE that this worked for removing cubes from the stack and the scene!
Monkey1stack.Pop ().Destroy () -
AuthorPosts
You must be logged in to reply to this topic.