About Monkey 2 › Forums › Monkey 2 Programming Help › How to import obj model and how to use color alpha
This topic contains 12 replies, has 4 voices, and was last updated by
swipis 1 year, 2 months ago.
-
AuthorPosts
-
January 24, 2018 at 8:23 pm #13205
Hello, I tried to import model (obj file made in blender) I did:
Monkey123Local shipmesh := Mesh.Load("test.obj")Local shipmat := New PbrMaterial(New Color(1,0,0))ship = New Model(shipmesh, shipmat)but I can’t see my model at run time.
Second question is how to use color alpha?
Monkey1New Color(1,0,0, ?)And last question can make an empty object and make it parent (in some other game engine can be called Position node, in unity – GameObject)?
Thank you!
January 24, 2018 at 8:27 pm #13206For OBJ loading you’ll have to do #Import “<mojo3d-loaders>”
But I would recommend exporting to GLTF or GLB using Blender, as those are supported by Mojo3D directly.
Other formats are loaded using “assimp”Alpha is just as you wrote it.
canvas.Color=New Color(1,0,0,0.5) where the last parameter is alpha.
Though I don’t think Mojo3D supports it?Mojo3D entities has “Parents” if that’s what you mean?
January 24, 2018 at 8:47 pm #13208Thank you for answers
Alpha looks like not working (at least for me, no matter alpha = 0 or =1 result the same).
I loaded GLTF model but still can’t see in my scene… What can be wrong?
January 24, 2018 at 8:54 pm #13209Head over to your Monkey2 folder, then go to ” modules/mojo3d/tests/ ”
You’ll have a bunch of examples there and some of them load models, so you could just copy that.January 24, 2018 at 9:04 pm #13210Than you, will look at these examples!
January 24, 2018 at 11:58 pm #13214It’s possible that your model is too small or too big. Try using “ship.Mesh.FitVertices( New Boxf )” to fit everything into a 1x1x1 box, then place your camera accordingly ( moving to “0,2,5” is a good starting point, then “camera.PointAt( ship )” to point at model ).
January 25, 2018 at 7:19 pm #13231OK when I did as you told but still can’t see my model and no errors what so ever… sometimes I getting “Memory access violation” here is code:
Monkey123456mycamera.Move(0,10,25)ship=Model.Load( "asset::test2.gltf" )ship.Mesh.FitVertices(New Boxf(1,1),False)mycamera.PointAt(ship)Any ideas what is wrong?
January 25, 2018 at 8:07 pm #13236This is often a sign that your model isn’t loading properly.
What you can do is put ‘Assert( model )’ after the Model.Load call to verify it did in fact load correctly.
Can you post the entire sourcec code? It’s generally much easier for us to spot problems if we can see the entire progam.
January 25, 2018 at 8:13 pm #13237Hi Mark, Assert(model) did not output anything, anyway here is all code:
Monkey1234567891011Namespace test3#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "assets/"Using std..Using mojo..Using mojo3d.. 'The new mojo 3d module.Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273Class MyWindow Extends WindowField myscene:SceneField mycamera:CameraField mylight:LightField ship:ModelField yawEntity:EntityField pitchEntity:EntityMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )' Get the current scenemyscene=Scene.GetCurrent()'create camera'mycamera = New Cameramycamera.Viewport=Rectmycamera.Near = 1 '??mycamera.Far = 1000 'how far should we rendermycamera.FOV = 60'create light'mylight = New Lightmylight.RotateX(Pi/2) 'aim directional light 'down' - Pi/2=90 degrees.ship=Model.Load( "asset::test2.gltf" )Assert(ship)ship.Mesh.FitVertices(New Boxf(1,1),False)mycamera.Move(0,10,25) 'move the camera to position x,y,zmycamera.PointAt(ship)'ship = Model.Load( "asset::test.gltf" )yawEntity = New EntitypitchEntity = New EntitypitchEntity.Parent = yawEntitymycamera.Parent = pitchEntityEnd Method' This method is the main loop.Method OnRender( canvas:Canvas ) OverrideRequestRender()myscene.Update()UpdateInput()mycamera.Render( canvas )'myscene.Render( canvas,mycamera ) 'render the 3d sceneIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()canvas.DrawText("FPS = " + App.FPS,0,0)End MethodEnd ClassFunction Main()New AppInstanceNew MyWindowApp.Run()End FunctionJanuary 25, 2018 at 8:36 pm #13238It’s missing the #imports at the top! Please post again…
January 25, 2018 at 8:39 pm #13239ohh sorry, here is full code
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150Namespace test3#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "assets/"Using std..Using mojo..Using mojo3d.. 'The new mojo 3d module.Class MyWindow Extends Window' What are we going to use.Field myscene:SceneField mycamera:CameraField mylight:LightField ship:ModelField yawEntity:EntityField pitchEntity:EntityMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )' Get the current scenemyscene=Scene.GetCurrent()'create camera'mycamera = New Cameramycamera.Viewport=Rectmycamera.Near = 1 '??mycamera.Far = 1000 'how far should we rendermycamera.FOV = 60'create light'mylight = New Lightmylight.RotateX(Pi/2) 'aim directional light 'down' - Pi/2=90 degrees.ship=Model.Load( "asset::test2.gltf" )Assert( ship )ship.Mesh.FitVertices(New Boxf(1,1),False)mycamera.Move(0,10,250) 'move the camera to position x,y,zmycamera.PointAt(ship)'ship = Model.Load( "asset::test.gltf" )yawEntity = New EntitypitchEntity = New EntitypitchEntity.Parent = yawEntitymycamera.Parent = pitchEntity'testing FMOD function (METHOD)End Method' This method is the main loop.Method OnRender( canvas:Canvas ) OverrideRequestRender()myscene.Update()UpdateInput()'mybox.Rotate(3,3,3) 'rotate the box (x,y,z)mycamera.Render( canvas )'myscene.Render( canvas,mycamera ) 'render the 3d sceneIf Keyboard.KeyReleased(Key.Escape) Then App.Terminate()canvas.DrawText("FPS = " + App.FPS,0,0)End MethodMethod UpdateInput()Local mousePos:Vec2f = Mouse.LocationmousePosDelta.x = mousePos.x - previousMousePos.xmousePosDelta.y = mousePos.y - previousMousePos.ypreviousMousePos = mousePos'Print mousePosDelta.x + " -- " + mousePos.xIf Mouse.ButtonDown(MouseButton.Right) Then'Local yaw:Float = mousePosDelta.x'Local pitch:Float = mousePosDelta.yPrint mousePosDelta.x'Mouse.PointerVisible = FalsemouseX = Mouse.X'FMOD(pitch - mouseX * 0.3 * 0.05, 360)'yaw = FMOD(yaw - mouseX * 0.3 * 0.05, 360)'pitch = pitch * (Pi/180)'Print FMOD(pitch - Mouse.X * 0.05, 360) + " -- " + RAD2DEG(pitch)yawEntity.Rotate(0,yaw,0)'pitchEntity.Rotate(-pitch,0,0)mousePosDelta = New Vec2f(0)mouseX = 0EndifEnd MethodMethod FMOD:Float(x:Float, y:Float)Local d:Floatd = x / yd = Floor(d)'d = Round(d)d = d * yd = x - dReturn dEnd MethodMethod DEG2RAD:Float (r:Float)r = (r * Pi) / 180Return rEnd MethodMethod RAD2DEG:Float (r:Float)r = (r * 180) / PiReturn rEnd MethodEnd ClassFunction Main()New AppInstanceNew MyWindowApp.Run()End FunctionJanuary 25, 2018 at 8:55 pm #13240ship.Mesh.FitVertices(New Boxf(1,1),False)
This looks wrong. New Boxf( 1,1 ) will create a 0 volume box as min and max are both 1. Try New Boxf( -1,1 ) instead.
January 25, 2018 at 8:59 pm #13241OK now my model is on the screen
thank you!
-
AuthorPosts
You must be logged in to reply to this topic.