Parcourir la source

Merge branch 'develop' into develop-tmp

Mark Sibly il y a 7 ans
Parent
commit
463f6b44a7

+ 1 - 1
modules/mojo/graphics/glutil.monkey2

@@ -209,7 +209,7 @@ precision mediump float;
 			Print (i+1)+":~t"+lines[i]
 		Next
 		
-		RuntimeError( "Failed to compile fragment shader:"+glGetShaderInfoLogEx( shader ) )
+		RuntimeError( "Failed to compile shader:"+glGetShaderInfoLogEx( shader ) )
 	Endif
 	Return shader
 End

+ 30 - 16
modules/mojo3d-loaders/tests/castle.monkey2

@@ -6,8 +6,8 @@ Namespace myapp
 #Import "<mojo3d-loaders>"
 
 #Import "assets/castle/@/castle"
-#Import "../../mojo3d/tests/assets/heightmap_256.BMP"
-#Import "../../mojo3d/tests/assets/mossy-ground1.pbr@/mossy-ground1.pbr"
+#Import "../../mojo3d/tests/assets/terrain_256.png"
+#Import "../../mojo3d/tests/assets/mossy.pbr@/mossy.pbr"
 
 '#Import "../../mojo3d/tests/assets/miramar-skybox.jpg"
 
@@ -226,13 +226,17 @@ Class MyWindow Extends Window
 	
 	Field _camera:Camera
 	
+	Field _light:Light
+	
+	Field _godrays:GodraysEffect
+	
 	Method CreateTerrain:Model()
 		
 		Local box:=New Boxf( -256,-32,-256,256,0,256 )
 		
-		Local hmap:=Pixmap.Load( "asset::heightmap_256.BMP",PixelFormat.I8 )
+		Local hmap:=Pixmap.Load( "asset::terrain_256.png",PixelFormat.I8 )
 
-		Local material:=PbrMaterial.Load( "asset::mossy-ground1.pbr" )
+		Local material:=PbrMaterial.Load( "asset::mossy.pbr" )
 		material.ScaleTextureMatrix( 64,64 )
 		
 		'model+mesh
@@ -288,11 +292,13 @@ Class MyWindow Extends Window
 	
 	Method CreateScene:Scene()
 		
-		Local scene:=New Scene
+		_scene=New Scene
+		
+		_scene.AmbientLight=Color.Black
+		_scene.ClearColor=Color.Sky
+'		_scene.EnvColor=Color.Black
 		
-		Local light:=New Light
-		light.Rotate( 60,30,0 )	'aim directional light 'down' - Pi/2=90 degrees.
-		light.CastsShadow=True
+		_scene.ShadowAlpha=1
 		
 		Local terrain:=CreateTerrain()
 		
@@ -300,22 +306,30 @@ Class MyWindow Extends Window
 		
 		_player=CreatePlayer()
 		
-		Local camera:=New Camera( _player )
-		camera.View=Self
-		camera.LocalPosition=New Vec3f( 0,.5,0 )
-		camera.RotateX( 45 )
-		camera.Move( 0,0,-2 )
+		_camera=New Camera( _player )
+		_camera.View=Self
+		_camera.LocalPosition=New Vec3f( 0,.5,0 )
+		_camera.RotateX( 45 )
+		_camera.Move( 0,0,-2 )
+
+'		Return _scene
+		
+		_light=New Light
+		_light.Rotate( 60,30,0 )	'aim directional light 'down' - Pi/2=90 degrees.
+		_light.CastsShadow=True
 		
-		_camera=camera
+		_godrays=New GodraysEffect
+		_godrays.Light=_light
+		_scene.AddPostEffect( _godrays )
 		
-		Return scene
+		Return _scene
 	End
 	
 	Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
 
 		Super.New( title,width,height,flags )
 		
-		_scene=CreateScene()
+		CreateScene()
 	End
 		
 	Method OnRender( canvas:Canvas ) Override

+ 71 - 0
modules/mojo3d/assets/shaders/effects/godrays.glsl

@@ -0,0 +1,71 @@
+
+//@renderpasses 0
+
+uniform sampler2D r_SourceBuffer;
+
+uniform sampler2D r_DepthBuffer;
+
+uniform vec2 r_BufferCoordScale;
+
+varying vec2 v_BufferCoords;
+
+//@vertex
+
+attribute vec4 a_Position;
+	
+void main(){
+
+	v_BufferCoords=a_Position.xy * r_BufferCoordScale;
+
+	gl_Position=vec4( a_Position.xy * 2.0 - 1.0,0.0,1.0 );
+}
+
+//@fragment
+
+uniform vec2 m_LightPosBufferCoords;
+
+#ifdef GL_ES
+const int m_NumSamples=100;
+#else
+uniform int m_NumSamples;
+#endif
+
+uniform float m_Exposure;
+
+uniform float m_Decay;
+
+uniform float m_Density;
+
+uniform vec4 m_Color;
+
+uniform float m_Weight;
+
+void main(){
+
+	vec2 coords=v_BufferCoords;
+
+	vec2 delta=m_LightPosBufferCoords-coords;
+	
+	delta*=1.0/float( m_NumSamples ) * m_Density;
+	
+	vec3 fragColor=vec3( 0.0 );
+	
+	vec3 color=m_Color.rgb * m_Weight;
+	
+	for( int i=0;i<m_NumSamples;++i ){
+	
+		coords+=delta;
+
+		float depth=texture2D( r_DepthBuffer,coords ).r;
+		
+		vec3 sample=(depth==1.0) ? color : vec3( 0.0 );
+		
+		fragColor+=sample;
+	}
+
+	float depth=texture2D( r_DepthBuffer,v_BufferCoords * r_BufferCoordScale ).r;
+	
+//	gl_FragColor=vec4( v_SourceBufferCoords,0.0,1.0 );//fragColor*m_Exposure,1.0 );
+
+	gl_FragColor=vec4( depth,0.0,0.0,1.0 );//fragColor*m_Exposure,1.0 );
+}

+ 2 - 7
modules/mojo3d/assets/shaders/imports/pbr.glsl

@@ -7,10 +7,9 @@
 
 float pointAtten( float d,float r ){
 
-//	Doesn't work on ANGLE!
-//	float atten=1.0-min( (d*d)/(r*r),1.0 );atten*=atten;
+	float atten=1.0-min( (d*d)/(r*r),1.0 );atten*=atten;
 
-	float atten=1.0-min( d/r,1.0 );atten*=atten;
+//	float atten=1.0-min( d/r,1.0 );atten*=atten;
 	
 //	float atten=1.0/(1.0+d*d);
 
@@ -38,10 +37,6 @@ void emitPbrFragment( vec3 color,float metalness,float roughness,vec3 position,v
 
 	float atten=pointAtten( length( lvec ),r_LightRange );
 	
-#ifdef GL_ES
-	if( atten!=atten ) return;
-#endif
-	
 	lvec=normalize( lvec );
 	
 #elif MX2_SPOTLIGHT

+ 8 - 8
modules/mojo3d/assets/shaders/imports/std.glsl

@@ -156,22 +156,22 @@ attribute vec4 a_Bones;		//mask=128
 
 void transformLightQuadVertex(){
 
-	vec2 qposition=a_Position.xy * r_QuadCoordScale + r_QuadCoordTrans;
+	//Careful! Bizarro angle/d3d bug...
 
-	v_ClipPosition=qposition * 2.0 - 1.0;
+	v_ClipPosition=a_Position.xy * r_QuadCoordScale + r_QuadCoordTrans;
 	
-	v_BufferCoords=qposition * r_BufferCoordScale;
+	v_BufferCoords=v_ClipPosition * r_BufferCoordScale;
 	
-	gl_Position=vec4( v_ClipPosition,-1.0,1.0 );
+	gl_Position=vec4( v_ClipPosition * 2.0 - 1.0,-1.0,1.0 );
 }
 
 void transformQuadVertex(){
 
-	v_ClipPosition=a_Position.xy * 2.0 - 1.0;
+	v_ClipPosition=a_Position.xy;
 	
-	v_BufferCoords=a_Position.xy * r_BufferCoordScale;
+	v_BufferCoords=v_ClipPosition.xy * r_BufferCoordScale;
 	
-	gl_Position=vec4( v_ClipPosition,-1.0,1.0 );
+	gl_Position=vec4( v_ClipPosition.xy * 2.0 - 1.0,-1.0,1.0 );
 }
 
 void transformSpriteVertex(){
@@ -276,7 +276,7 @@ vec3 fragmentPosition(){
 
 	float depth=viewDepth( texture2D( r_DepthBuffer,v_BufferCoords ).r );
 
-	vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition,-1.0,1.0 );
+	vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition*2.0-1.0,-1.0,1.0 );
 	
 	vec3 vpos=vpos4.xyz/vpos4.w;
 	

+ 1 - 1
modules/mojo3d/assets/shaders/misc/fog-deferred.glsl

@@ -22,7 +22,7 @@ void main(){
 
 		depth=viewDepth( depth );
 		
-		vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition,-1.0,1.0 );
+		vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition*2.0-1.0,-1.0,1.0 );
 		
 		vec3 vpos=vpos4.xyz/vpos4.w;
 		

+ 1 - 1
modules/mojo3d/assets/shaders/misc/skybox.glsl

@@ -14,7 +14,7 @@ void main(){
 
 void main(){
 
-	vec4 clip=r_InverseProjectionMatrix * vec4( v_ClipPosition,1.0,1.0 );
+	vec4 clip=r_InverseProjectionMatrix * vec4( v_ClipPosition*2.0-1.0,1.0,1.0 );
 	
 	vec3 tv=r_EnvMatrix * (clip.xyz/clip.w);
 	

+ 1 - 0
modules/mojo3d/mojo3d.monkey2

@@ -53,6 +53,7 @@ Using reflection..
 #Import "scene/effects/bloomeffect"
 #Import "scene/effects/monochromeeffect"
 #Import "scene/effects/reflectioneffect"
+#Import "scene/effects/godrayseffect"
 
 #Import "scene/jsonifier/jsonifier"
 #Import "scene/jsonifier/invocation"

+ 146 - 0
modules/mojo3d/scene/effects/godrayseffect.monkey2

@@ -0,0 +1,146 @@
+
+Namespace mojo3d
+
+#rem monkeydoc The MonochromeEffect class.
+#end
+Class GodraysEffect Extends PostEffect
+
+	#rem monkeydoc Creates a new monochrome effect shader.
+	#end
+	Method New( light:Light=Null )
+		
+		_shader=Shader.Open( "effects/godrays" )
+		
+		_uniforms=New UniformBlock( 3 )
+		
+		Light=light
+		NumSamples=100
+		Exposure=.0034
+		Decay=1.0
+		Density=0.84
+		Color=Color.White
+		Weight=1
+	End
+	
+	Property Light:Light()
+		
+		Return _light
+	
+	Setter( light:Light )
+		
+		_light=light
+	End
+	
+	Property NumSamples:Int()
+		
+		Return _uniforms.GetInt( "NumSamples" )
+	
+	Setter( samples:Int )
+		
+		_uniforms.SetInt( "NumSamples",samples )
+	End
+	
+	Property Exposure:Float()
+		
+		Return _uniforms.GetFloat( "Exposure" )
+		
+	Setter( exposure:Float )
+		
+		_uniforms.SetFloat( "Exposure",exposure )
+	End
+	
+	Property Decay:Float()
+		
+		Return _uniforms.GetFloat( "Decay" )
+		
+	Setter( decay:Float )
+		
+		_uniforms.SetFloat( "Decay",decay )
+	End
+	
+	Property Density:Float()
+		
+		Return _uniforms.GetFloat( "Density" )
+		
+	Setter( density:Float )
+		
+		_uniforms.SetFloat( "Density",density )
+	End
+	
+	Property Color:Color()
+		
+		Return _uniforms.GetColor( "Color" )
+	
+	Setter( color:Color )
+		
+		_uniforms.SetColor( "Color",color )
+	End
+	
+	Property Weight:Float()
+		
+		Return _uniforms.GetFloat( "Weight" )
+	
+	Setter( weight:Float )
+		
+		_uniforms.SetFloat( "Weight",weight )
+	End
+	
+	Protected
+	
+	Method OnRender( target:RenderTarget,viewport:Recti ) Override
+		
+		If Not _light Return
+		
+		'computer light pos in buffer coords
+		Local viewProj:=Uniforms.GetMat4f( "ViewProjectionMatrix" )
+
+		Local lightClipPos:=viewProj * New Vec4f( -_light.Basis.k,0 )	'clip
+		
+		If lightClipPos.w<=0 return
+		
+'		If lightClipPos.x<-lightClipPos.w Or lightClipPos.x>lightClipPos.w Return
+'		If lightClipPos.y<-lightClipPos.w Or lightClipPos.y>lightClipPos.w Return
+		If lightClipPos.z<-lightClipPos.w Return 'Or lightClipPos.z>lightClipPos.w Return
+		
+		Local lightPos:=lightClipPos.XY/lightClipPos.w * 0.5 + 0.5		'NDC
+		
+		Local bcscale:=Uniforms.GetVec2f( "BufferCoordScale" )
+		
+'		Print "SourceBufferSize="+SourceBufferSize+" SourceBufferScale="+SourceBufferScale
+		
+		lightPos*=bcscale
+		
+		'set effect uniforms
+		_uniforms.SetVec2f( "LightPosBufferCoords",lightPos )
+		
+		'render!
+		#rem
+		Local size:=viewport.Size
+		Local source:=target.GetColorTexture( 0 )
+		
+		If Not _target Or size.x>_target.Size.x Or size.y>_target.Size.y
+			_target=CreateRenderTarget( size,source.Format,TextureFlags.Dynamic )
+		End
+		
+		Super.SetRenderTarget( _target,New Recti( 0,0,size ) )
+		#end
+
+		Device.Shader=_shader
+		Device.BindUniformBlock( _uniforms )
+		
+		Device.BlendMode=BlendMode.Additive
+		
+		RenderQuad()
+	End
+	
+	Private
+	
+	Field _light:Light
+	Field _color:Color
+	Field _weight:Float
+	
+	Field _shader:Shader
+	Field _uniforms:UniformBlock
+	Field _target:RenderTarget
+	
+End

+ 5 - 2
modules/mojo3d/scene/entity.monkey2

@@ -3,7 +3,7 @@ Namespace mojo3d
 
 #rem monkeydoc The Entity class.
 #end
-Class Entity Extends DynamicObject
+Class Entity Extends DynamicObject Abstract
 	
 	#rem monkeydoc Copied signal.
 	
@@ -99,6 +99,7 @@ Class Entity Extends DynamicObject
 	
 	#rem monkeydoc Parent entity.
 	#end
+	[jsonify=1]
 	Property Parent:Entity()
 		
 		Return _parent
@@ -476,7 +477,9 @@ Class Entity Extends DynamicObject
 	
 	Method OnCopy:Entity( parent:Entity ) Virtual
 		
-		Return New Entity( Self,parent )
+		RuntimeError( "Cannot copy Entity" )
+		
+		Return Null
 	End
 		
 	#rem monkeydoc Invoked when entity transitions from hidden->visible.

+ 35 - 6
modules/mojo3d/scene/posteffect.monkey2

@@ -24,6 +24,8 @@ Class PostEffect
 	#end	
 	Method Render()
 		
+		UpdateSourceUniforms()
+		
 		OnRender( _gdevice.RenderTarget,_gdevice.Viewport )
 	End
 	
@@ -50,6 +52,21 @@ Class PostEffect
 		Return _gdevice
 	End
 	
+	Property Uniforms:UniformBlock()
+		
+		Return _runiforms
+	End
+	
+	Property SourceBufferSize:Vec2f()
+		
+		Return _sourceBufferSize
+	End
+	
+	Property SourceBufferScale:Vec2f()
+		
+		Return _sourceBufferScale
+	End
+	
 	Method CreateRenderTarget:RenderTarget( size:Vec2i,format:PixelFormat,flags:TextureFlags )
 		
 		Local texture:=New Texture( size.x,size.y,format,flags )
@@ -59,12 +76,7 @@ Class PostEffect
 	
 	Method SetRenderTarget( target:RenderTarget,viewport:Recti )
 		
-		Local rsize:=_gdevice.Viewport.Size
-		Local rtarget:=_gdevice.RenderTarget
-		Local rtexture:=rtarget.GetColorTexture( 0 )
-		_runiforms.SetTexture( "SourceBuffer",rtexture )
-		_runiforms.SetVec2f( "SourceBufferSize",Cast<Vec2f>( rtexture.Size ) )'rsize ) )
-		_runiforms.SetVec2f( "SourceBufferScale",Cast<Vec2f>( rsize )/Cast<Vec2f>( rtexture.Size ) )
+		UpdateSourceUniforms()
 		
 		_gdevice.RenderTarget=target
 		_gdevice.Viewport=viewport
@@ -93,7 +105,24 @@ Class PostEffect
 	
 	Global _gdevice:GraphicsDevice
 	Global _runiforms:UniformBlock
+	Global _sourceBufferSize:Vec2f
+	Global _sourceBufferScale:Vec2f
 	
 	Field _enabled:Bool=True
 	
+	Function UpdateSourceUniforms()
+
+		Local rsize:=_gdevice.Viewport.Size
+		Local rtarget:=_gdevice.RenderTarget
+		Local rtexture:=rtarget.GetColorTexture( 0 )
+		
+		_sourceBufferSize=Cast<Vec2f>( rtexture.Size )
+		_sourceBufferScale=Cast<Vec2f>( rsize )/Cast<Vec2f>( rtexture.Size )
+		
+		_runiforms.SetTexture( "SourceBuffer",rtexture )
+		_runiforms.SetVec2f( "SourceBufferSize",_sourceBufferSize )
+		_runiforms.SetVec2f( "SourceBufferScale",_sourceBufferScale )
+	End
+	
+	
 End

+ 24 - 11
modules/mojo3d/tests/effects.monkey2

@@ -24,6 +24,8 @@ Class MyWindow Extends Window
 	
 	Field _mono:MonochromeEffect
 	
+	Field _godrays:GodraysEffect
+	
 	Method New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )
 
 		Super.New( title,width,height,flags )
@@ -31,14 +33,6 @@ Class MyWindow Extends Window
 		_scene=Scene.GetCurrent()
 		_scene.ClearColor=Color.Black
 		
-		_bloom=New BloomEffect
-		_bloom.Enabled=True
-		_scene.AddPostEffect( _bloom )
-		
-		_mono=New MonochromeEffect
-		_mono.Enabled=False
-		_scene.AddPostEffect( _mono )
-		
 		'create camera
 		'
 		_camera=New Camera( self )
@@ -48,7 +42,24 @@ Class MyWindow Extends Window
 		'create light
 		'
 		_light=New Light
-		_light.RotateX( 90 )
+		_light.Rotate( 120,0,0 )
+
+		'create effects
+		'
+		_bloom=New BloomEffect
+		_bloom.Enabled=True
+		_scene.AddPostEffect( _bloom )
+		
+		_mono=New MonochromeEffect
+		_mono.Enabled=False
+		_scene.AddPostEffect( _mono )
+
+		_godrays=New GodraysEffect
+		_godrays.Enabled=false
+		_godrays.Weight=4.0
+		_godrays.Light=_light
+		
+		_scene.AddPostEffect( _godrays )
 		
 		Local material:=New PbrMaterial( New Color( 2,.5,0,1 ),0,1 )
 		
@@ -63,14 +74,16 @@ Class MyWindow Extends Window
 		RequestRender()
 		
 		If Keyboard.KeyHit( Key.Key1 ) _bloom.Enabled=Not _bloom.Enabled
-
 		If Keyboard.KeyHit( Key.Key2 ) _mono.Enabled=Not _mono.Enabled
+		If Keyboard.KeyHit( Key.Key3 ) _godrays.Enabled=Not _godrays.Enabled
 		
 		_scene.Update()
 		
 		_scene.Render( canvas )
 		
-		canvas.DrawText( "Bloom="+_bloom.Enabled+" (1) monochrome="+_mono.Enabled+" (2)",0,0 )
+		canvas.DrawText( "(1) Bloom="+_bloom.Enabled,0,0 )
+		canvas.DrawText( "(2) Monochrome="+_mono.Enabled,0,16 )
+		canvas.DrawText( "(3) Godrays="+_godrays.Enabled,0,32 )
 	End
 	
 End

+ 4 - 1
modules/mojo3d/tests/pbrspheres.monkey2

@@ -43,7 +43,10 @@ Class MyWindow Extends Window
 		'create light
 		'
 		_light=New Light
-		_light.RotateX( 60 )	'aim directional light 'downish'.
+		_light.Rotate( 54,144,0 )	'calibrated so specular highlight matches sun on sky texture!
+		
+		Local godrays:=New GodraysEffect( _light )
+		_scene.AddPostEffect( godrays )
 		
 		'create ground
 		'

+ 8 - 0
modules/monkey/native/bbdebug.cpp

@@ -10,6 +10,10 @@
 
 #include <signal.h>
 
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#endif
+
 typedef void(*dbEmit_t)(void*);
 
 namespace bbDB{
@@ -130,6 +134,10 @@ namespace bbDB{
 		bb_printf( "\n" );
 		fflush( stdout );
 		
+#ifdef __EMSCRIPTEN__
+		emscripten_pause_main_loop();
+#endif
+		
 		for(;;){
 		
 			char buf[256];