Bladeren bron

Mojo3d jsonifier tweaks.

Mark Sibly 7 jaren geleden
bovenliggende
commit
7125c7f8ec

+ 27 - 36
modules/mojo3d/scene/jsonifier/jsonifier.monkey2

@@ -197,55 +197,46 @@ Public
 		Local type:=value.Type
 		Assert( type )
 
-		'handle primitive types
-		Select type
-		Case Typeof<Bool>
-			Return New JsonBool( Cast<Bool>( value ) )
-		Case Typeof<Short>
-			Return New JsonNumber( Cast<Short>( value ) )
-		Case Typeof<Int>
-			Return New JsonNumber( Cast<Int>( value ) )
-		Case Typeof<Float>
-			Return New JsonNumber( Cast<Float>( value ) )
-		Case Typeof<String>
-			Return New JsonString( Cast<String>( value ) )
+		Select type.Kind
+		Case "Primitive"
+			Select type
+			Case Typeof<Bool>
+				Return New JsonBool( Cast<Bool>( value ) )
+			Case Typeof<Short>
+				Return New JsonNumber( Cast<Short>( value ) )
+			Case Typeof<Int>
+				Return New JsonNumber( Cast<Int>( value ) )
+			Case Typeof<Float>
+				Return New JsonNumber( Cast<Float>( value ) )
+			Case Typeof<String>
+				Return New JsonString( Cast<String>( value ) )
+			End
+		Case "Array"
+			Local n:=value.GetArrayLength()
+			Local jarray:=New JsonArray( n )
+			For Local i:=0 Until n
+				jarray[i]=Jsonify( value.GetArrayElement( i ) )
+			Next
+			Return jarray
+		Case "Enum"
+			Return New JsonNumber( value.EnumValue )
 		End
 		
-		'handle enums+references
-		Select type.Kind
-		Case "Class"
+		If IsInstanceType( type )
 			Local obj:=Cast<Object>( value )
 			If Not obj Return JsonValue.NullValue
 			Local inst:=_instsByObj[obj]
 			If inst Return New JsonString( "@"+inst.id )
-		Case "Enum"
-			Return New JsonNumber( value.EnumValue )
-		End
+			RuntimeError( "Can't jsonify instance of type '"+type+"'" )
+		Endif
 		
 		'try custom jsonifiers
 		For Local jext:=Eachin JsonifierExt.All
 			Local jvalue:=jext.Jsonify( value,Self )
 			If jvalue Return jvalue
 		Next
-
-		Select type.Kind
-		Case "Unknown"
-			Local obj:=Cast<Object>( value )
-			If Not obj Return JsonValue.NullValue
-			Local inst:=_instsByObj[obj]
-			If inst Return New JsonString( "@"+inst.id )
-		Case "Class"
-			Return JsonValue.NullValue
-		Case "Array"
-			Local n:=value.GetArrayLength()
-			Local jarray:=New JsonArray( n )
-			For Local i:=0 Until n
-				jarray[i]=Jsonify( value.GetArrayElement( i ) )
-			Next
-			Return jarray
-		End
 		
-		RuntimeError( "TODO: No jsonifier found for type '"+type+"'" )
+		RuntimeError( "No jsonifier found for type '"+type+"'" )
 		Return Null
 	End
 	

+ 1 - 1
modules/mojo3d/scene/scene.monkey2

@@ -310,7 +310,7 @@ Class Scene
 		Return _jsonifier
 	End
 	
-	Method LoadTexture:Texture( path:String,flags:TextureFlags,flipNormalY:Bool=False )
+	Method LoadTexture:Texture( path:String,flags:TextureFlags=TextureFlags.FilterMipmap,flipNormalY:Bool=False )
 		
 		Local texture:=Texture.Load( path,flags,flipNormalY )
 		If Not texture Return Null

+ 2 - 2
modules/mojo3d/tests/ducks.monkey2

@@ -6,7 +6,7 @@ Namespace myapp
 #Import "<mojo3d>"
 
 'uncomment this to create a mojo3d scene file in monkey2 dir!
-#Reflect mojo3d
+'#Reflect mojo3d
 
 #Import "assets/duck.gltf/@/duck.gltf"
 
@@ -105,7 +105,7 @@ Class MyWindow Extends Window
 		
 		CreateDucks()
 		
-		If _scene.Editable _scene.Save( "ducks-scene.mojo3d" ) ; _scene=Scene.Load( "ducks-scene.mojo3d" )
+		If _scene.Editable _scene.Save( "ducks-scene.mojo3d","modules/mojo3d/tests/assets/" ) ; _scene=Scene.Load( "ducks-scene.mojo3d" )
 	End
 	
 	Method OnRender( canvas:Canvas ) Override

+ 2 - 2
modules/mojo3d/tests/shapes.monkey2

@@ -6,7 +6,7 @@ Namespace myapp
 #Import "<mojo3d>"
 
 'uncomment this to create a mojo3d scene file in monkey2 dir!
-#Reflect mojo3d
+'#Reflect mojo3d
 
 Using std..
 Using mojo..
@@ -162,7 +162,7 @@ Class MyWindow Extends Window
 		CreateBodies()
 		
 		If _scene.Editable 
-			_scene.Save( "shapes-scene.mojo3d" )
+			_scene.Save( "shapes-scene.mojo3d","modules/mojo3d/tests/assets/" )
 			_scene=Scene.Load( "shapes-scene.mojo3d" )
 			_camera=Cast<Camera>( _scene.FindEntity( "Camera" ) )
 		Endif

+ 22 - 37
modules/mojo3d/tests/spotlight.monkey2

@@ -4,6 +4,9 @@ Namespace myapp3d
 #Import "<mojo>"
 #Import "<mojo3d>"
 
+'uncomment this to create a mojo3d scene file in monkey2 dir!
+'#Reflect mojo3d
+
 #Import "assets/monkey2-logo.png"
 
 Using std..
@@ -14,14 +17,6 @@ Class MyWindow Extends Window
 	
 	Field _scene:Scene
 	
-	Field _camera:Camera
-	
-	Field _light:Light
-	
-	Field _ground:Model
-
-	Field _donut:Model
-	
 	Method New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
 		
 		Super.New( title,width,height,flags )
@@ -30,53 +25,43 @@ Class MyWindow Extends Window
 	Method OnCreateWindow() Override
 		
 		'create (current) scene
-		_scene=New Scene
+		_scene=New Scene( True )
 		
 		'create camera
-		_camera=New Camera( Self )
-		_camera.AddComponent<FlyBehaviour>()
-		_camera.Move( 0,2.5,-10 )
+		Local camera:=New Camera( Self )
+		camera.AddComponent<FlyBehaviour>()
+		camera.Move( 0,2.5,-10 )
 		
 		'create light
-		_light=New Light
-		_light.Type=LightType.Spot
-		_light.Texture=Texture.Load( "asset::monkey2-logo.png" )'ColorTexture( Color.Red )
-		_light.Range=25
-		_light.InnerAngle=15
-		_light.OuterAngle=45
-		_light.CastsShadow=True
-		_light.Position=New Vec3f( 0,10,0 )
-		_light.Rotate( 90,0,0 )
+		Local light:=New Light
+		light.Type=LightType.Spot
+		light.Texture=_scene.LoadTexture( "asset::monkey2-logo.png" )
+		light.Range=25
+		light.InnerAngle=15
+		light.OuterAngle=45
+		light.CastsShadow=True
+		light.Position=New Vec3f( 0,10,0 )
+		light.Rotate( 90,0,0 )
 		
 		'create ground
 		Local groundBox:=New Boxf( -100,-1,-100,100,0,100 )
 		Local groundMaterial:=New PbrMaterial( Color.Brown,0,1 )
-		_ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )
-		_ground.CastsShadow=False
+		Local ground:=Model.CreateBox( groundBox,1,1,1,groundMaterial )
+		ground.CastsShadow=False
 		
 		'create donut
 		Local donutMaterial:=New PbrMaterial( Color.White,0,1 )
-		_donut=Model.CreateTorus( 2,.5,48,24,donutMaterial )
-		_donut.Move( 0,2.5,0 )
-		_donut.AddComponent<RotateBehaviour>().Speed=New Vec3f( .2,.4,.6 )
+		Local donut:=Model.CreateTorus( 2,.5,48,24,donutMaterial )
+		donut.Move( 0,2.5,0 )
+		donut.AddComponent<RotateBehaviour>().Speed=New Vec3f( .2,.4,.6 )
 		
+		If _scene.Editable _scene.Save( "spotlight-scene.mojo3d","modules/mojo3d/tests/assets/" ) ; _scene=Scene.Load( "spotlight-scene.mojo3d" )
 	End
 	
 	Method OnRender( canvas:Canvas ) Override
 		
 		RequestRender()
 		
-		If Keyboard.KeyHit( Key.Space )
-			Select _light.Type
-			Case LightType.Directional
-				_light.Type=LightType.Point
-			Case LightType.Point
-				_light.Type=LightType.Spot
-			Case LightType.Spot
-				_light.Type=LightType.Directional
-			End
-		Endif
-		
 		_scene.Update()
 		
 		_scene.Render( canvas )

+ 2 - 2
modules/mojo3d/tests/sprites.monkey2

@@ -6,7 +6,7 @@ Namespace myapp
 #Import "<mojo3d>"
 
 'uncomment this to create a mojo3d scene file in monkey2 dir!
-#Reflect mojo3d
+'#Reflect mojo3d
 
 #Import "assets/miramar-skybox.jpg"
 #Import "assets/Acadia-Tree-Sprite.png"
@@ -79,7 +79,7 @@ Class MyWindow Extends Window
 			box.Move( Rnd(-50,50),0,Rnd(-50,50) )
 		next	
 
-		If _scene.Editable _scene.Save( "sprites-scene.mojo3d" ) ; _scene=Scene.Load( "sprites-scene.mojo3d" )
+		If _scene.Editable _scene.Save( "sprites-scene.mojo3d","modules/mojo3d/tests/assets/" ) ; _scene=Scene.Load( "sprites-scene.mojo3d" )
 		
 	End
 	

+ 118 - 0
spotlight-scene.mojo3d

@@ -0,0 +1,118 @@
+{
+	"assetsDir":"modules/mojo3d/tests/assets/",
+	"instances":[{
+		"ctor":{
+			"args":[true],
+			"decl":"mojo3d.Scene.New",
+			"type":"Void(Bool)"
+		},
+		"id":0,
+		"type":"mojo3d.Scene"
+	},{
+		"ctor":{
+			"args":[null],
+			"decl":"mojo3d.Camera.New",
+			"type":"Void(mojo3d.Entity)"
+		},
+		"id":1,
+		"state":{
+			"LocalMatrix":[1,0,0,0,1,0,0,0,1,0,2.5,-10]
+		},
+		"type":"mojo3d.Camera"
+	},{
+		"ctor":{
+			"args":["@1"],
+			"decl":"mojo3d.FlyBehaviour.New",
+			"type":"Void(mojo3d.Entity)"
+		},
+		"id":2,
+		"type":"mojo3d.FlyBehaviour"
+	},{
+		"ctor":{
+			"args":[null],
+			"decl":"mojo3d.Light.New",
+			"type":"Void(mojo3d.Entity)"
+		},
+		"id":3,
+		"state":{
+			"CastsShadow":true,
+			"LocalMatrix":[1,0,0,0,-4.3711388286737929e-08,1,0,-1,-4.3711388286737929e-08,0,10,0],
+			"OuterAngle":45,
+			"Range":25,
+			"Texture":"@4",
+			"Type":3
+		},
+		"type":"mojo3d.Light"
+	},{
+		"ctor":{
+			"args":["asset::monkey2-logo.png",12,false],
+			"decl":"mojo3d.Scene.LoadTexture",
+			"inst":"@0",
+			"type":"mojo.graphics.Texture(String,mojo.graphics.TextureFlags,Bool)"
+		},
+		"id":4,
+		"type":"mojo.graphics.Texture"
+	},{
+		"ctor":{
+			"args":[[0.69999998807907104,0.40000000596046448,0.10000000149011612,1],0,1],
+			"decl":"mojo3d.PbrMaterial.New",
+			"type":"Void(std.graphics.Color,Float,Float)"
+		},
+		"id":5,
+		"type":"mojo3d.PbrMaterial"
+	},{
+		"ctor":{
+			"args":[[-100,-1,-100,100,0,100],1,1,1],
+			"decl":"mojo3d.Mesh.CreateBox",
+			"type":"mojo3d.Mesh(std.geom.Box<monkey.types.Float>,Int,Int,Int)"
+		},
+		"id":6,
+		"type":"mojo3d.Mesh"
+	},{
+		"ctor":{
+			"args":["@6","@5",null],
+			"decl":"mojo3d.Model.New",
+			"type":"Void(mojo3d.Mesh,mojo3d.Material,mojo3d.Entity)"
+		},
+		"id":7,
+		"type":"mojo3d.Model"
+	},{
+		"ctor":{
+			"args":[[1,1,1,1],0,1],
+			"decl":"mojo3d.PbrMaterial.New",
+			"type":"Void(std.graphics.Color,Float,Float)"
+		},
+		"id":8,
+		"type":"mojo3d.PbrMaterial"
+	},{
+		"ctor":{
+			"args":[2,0.5,48,24],
+			"decl":"mojo3d.Mesh.CreateTorus",
+			"type":"mojo3d.Mesh(Float,Float,Int,Int)"
+		},
+		"id":9,
+		"type":"mojo3d.Mesh"
+	},{
+		"ctor":{
+			"args":["@9","@8",null],
+			"decl":"mojo3d.Model.New",
+			"type":"Void(mojo3d.Mesh,mojo3d.Material,mojo3d.Entity)"
+		},
+		"id":10,
+		"state":{
+			"LocalMatrix":[1,0,0,0,1,0,0,0,1,0,2.5,0]
+		},
+		"type":"mojo3d.Model"
+	},{
+		"ctor":{
+			"args":["@10"],
+			"decl":"mojo3d.RotateBehaviour.New",
+			"type":"Void(mojo3d.Entity)"
+		},
+		"id":11,
+		"state":{
+			"Speed":[0.20000000298023224,0.40000000596046448,0.60000002384185791]
+		},
+		"type":"mojo3d.RotateBehaviour"
+	}]
+}

+ 1 - 1
sprites-scene.mojo3d

@@ -1,5 +1,5 @@
 {
-	"assetsDir":"D:/dev/monkey2/modules/mojo3d/tests/sprites.products/Windows/assets/",
+	"assetsDir":"modules/mojo3d/tests/assets/",
 	"instances":[{
 		"ctor":{
 			"args":[true],