Browse Source

Updated ted2go SceneDocument so it can load .mojo3d files.

Mark Sibly 7 years ago
parent
commit
48f2eb6702
2 changed files with 66 additions and 50 deletions
  1. 10 2
      VERSIONS.TXT
  2. 56 48
      src/ted2go/document/SceneDocument.monkey2

+ 10 - 2
VERSIONS.TXT

@@ -1,7 +1,15 @@
 
-***** Monkey2 v2018.03 *****
+***** Monkey2 v2018.03 Mx2cc v1.1.12 *****
 
-Big version numbering system change! Now just YYYY.VV Uses 2 digits for version so stringized versions can sort properly in github.
+Experimental mojo3d scene loading and saving added. Only very minimal support right now, eg: physics components are not yet supported and no texture or model loading support.
+
+See mojo3d/tests/loadscene.monkey2 for a demo. This should also create a test-scene.mojo3d file in your monkey2 dir which you can double click to open in latest ted2go (the version included in the monkey2 repos).
+
+To save a scene, you need to enabled mojo3d reflection by adding this to your file:
+
+#Reflect mojo3d
+
+You also need to enable 'editing' mode in your code with something like _scene.Editing=True before creating scene contents. Once you've created your scene, save it using the Scene.Save( path:String ) method. Ditto you can load a scene using the Scene.Load:Scene( path:String ) function.
 
 Typeinfo.Name now always returns proper typename even if reflection info disabled and even for generic types. Typeinfo.Name used to return a 'unknown@12345678' style string.
 

+ 56 - 48
src/ted2go/document/SceneDocument.monkey2

@@ -2,6 +2,13 @@ Namespace ted2go
 
 #Import "<mojo3d>"
 #Import "<mojo3d-loaders>"
+#Import "<reflection>"
+
+'just too brutal in debug mode!
+#If __RELEASE__
+#Reflect mojo.graphics
+#Reflect mojo3d
+#Endif
 
 Using mojo3d..
 
@@ -17,38 +24,31 @@ Class SceneDocumentView Extends View
 	
 	Method OnRender( canvas:Canvas ) Override
 	
-		For Local x:=0 Until Width Step 64
-			For Local y:=0 Until Height Step 64
-				canvas.Color=(x~y) & 64 ? New Color( .1,.1,.1 ) Else New Color( .05,.05,.05 )
-				canvas.DrawRect( x,y,64,64 )
-			Next
-		Next
+		RequestRender()
 		
-		Local model:=_doc.Model
-		If Not model
-			canvas.Clear( Color.Sky )
+		If Not _doc.Scene Or Not _doc.Camera
+			canvas.Clear( Color.Red )
 			Return
 		Endif
 		
-		RequestRender()
-		
-		Global _anim:Float=0
-		
-		If Keyboard.KeyDown( Key.A )
-			If _doc.Model.Animator
+		If _doc.Model And _doc.Model.Animator
+			Global _anim:Float=0
+			If Keyboard.KeyDown( Key.A )
 				_anim+=12.0/60.0
 				_doc.Model.Animator.Animate( 0,_anim )
+			Else
+				_anim=0
 			Endif
-		Else
-			_anim=0
 		Endif
 		
+		_doc.Scene.Update()
+		
 		_doc.Camera.Render( canvas )
 	End
 	
 	Method OnMouseEvent( event:MouseEvent ) Override
 		
-		If Not _doc.Model Return
+		If Not _doc.Camera Or Not _doc.Model Return
 		
 		Global _v:Vec2i
 		Global _f:Bool
@@ -74,13 +74,15 @@ Class SceneDocumentView Extends View
 	
 	Method OnKeyEvent( event:KeyEvent ) Override
 		
+		If Not _doc.Camera Or Not _doc.Model Return
+		
 		If event.Type=EventType.KeyDown
 			Select event.Key
 			Case Key.R
 				_doc.Camera.Position=New Vec3f(0,0,-2.5)
 				_doc.Model.Rotation=New Vec3f(0,0,0)
 			Case Key.S
-				_doc.Light.CastsShadow=Not _doc.Light.CastsShadow
+				If _doc.Light _doc.Light.CastsShadow=Not _doc.Light.CastsShadow
 			Case Key.A
 				
 			End
@@ -99,22 +101,6 @@ Class SceneDocument Extends Ted2Document
 		Super.New( path )
 		
 		_view=New SceneDocumentView( Self )
-		
-		_scene=New Scene
-		
-		Scene.SetCurrent( _scene )
-		
-		_camera=New Camera( _view )
-		_camera.Near=.01
-		_camera.Far=10
-		_camera.MoveZ( -2.5 )
-		
-		_light=New Light
-		_light.RotateX( Pi/2 )
-		
-		_model=null
-		
-		Scene.SetCurrent( Null )
 	End
 	
 	Property Scene:Scene()
@@ -127,30 +113,50 @@ Class SceneDocument Extends Ted2Document
 		Return _camera
 	End
 	
-	Property Light:Light()
-		
-		Return _light
-	End
-	
 	Property Model:Model()
 	
 		Return _model
 	End
 	
+	Property Light:Light()
+		
+		Return _light
+	End
+	
 	Protected
 	
 	Method OnLoad:Bool() Override
 		
-		If _model _model.Destroy()
+		If ExtractExt( Path )=".mojo3d"		
+			
+			Print "Loading scene from "+Path
+			
+			_scene=Scene.Load( Path )
+			
+			_camera=Cast<Camera>( _scene.FindEntity( "Camera" ) )
+'			If _camera _camera.View=_view
+				
+			Return True
+		Endif
+		
+		_scene=New Scene
 		
 		Scene.SetCurrent( _scene )
+
+		_camera=New Camera( _view )
+		_camera.Near=.01
+		_camera.Far=10
+		_camera.MoveZ( -2.5 )
+		_camera.AddComponent<FlyBehaviour>()
+
+		_light=New Light
+		_light.RotateX( Pi/2 )
 		
 		_model=Model.Load( Path )
-		
-		Scene.SetCurrent( Null )
-
 		If _model _model.Mesh.FitVertices( New Boxf( -1,1 ) )
-	
+			
+		Scene.SetCurrent( Null )
+		
 		Return True
 	End
 	
@@ -162,7 +168,6 @@ Class SceneDocument Extends Ted2Document
 	Method OnClose() Override
 		
 		_scene.DestroyAllEntities()
-		
 	End
 	
 	Method OnCreateView:SceneDocumentView() Override
@@ -190,7 +195,11 @@ Class SceneDocumentType Extends Ted2DocumentType
 	Method New()
 		AddPlugin( Self )
 		
-		Extensions=New String[]( ".gltf",".glb",".b3d",".3ds",".obj",".dae",".fbx",".blend",".x" )
+		#If __RELEASE__
+			Extensions=New String[]( ".mojo3d",".gltf",".glb",".b3d",".3ds",".obj",".dae",".fbx",".blend",".x" )
+		#Else
+			Extensions=New String[]( ".gltf",".glb",".b3d",".3ds",".obj",".dae",".fbx",".blend",".x" )
+		#Endif
 	End
 	
 	Method OnCreateDocument:Ted2Document( path:String ) Override
@@ -201,5 +210,4 @@ Class SceneDocumentType Extends Ted2DocumentType
 	Private
 	
 	Global _instance:=New SceneDocumentType
-	
 End