Browse Source

Lighting tweaks.

Mark Sibly 7 years ago
parent
commit
b3686635ba

+ 26 - 8
modules/mojo3d/assets/shaders/imports/pbr.glsl

@@ -5,6 +5,15 @@
 
 
 //@fragment
 //@fragment
 
 
+float pointAtten( float d,float r ){
+
+	float atten=max( 1.0-d*d/(r*r),0.0 );atten*=atten;
+	
+//	float d=length( lvec ),atten=1.0/(1.0+d*d);
+
+	return atten;
+}
+
 #if MX2_QUADPASS && MX2_LIGHTINGPASS
 #if MX2_QUADPASS && MX2_LIGHTINGPASS
 
 
 void emitPbrFragment( vec3 color,float metalness,float roughness,vec3 position,vec3 normal ){
 void emitPbrFragment( vec3 color,float metalness,float roughness,vec3 position,vec3 normal ){
@@ -17,19 +26,23 @@ void emitPbrFragment( vec3 color,float metalness,float roughness,vec3 position,v
 #if MX2_DIRECTIONALLIGHT
 #if MX2_DIRECTIONALLIGHT
 
 
 	vec3 lvec=normalize( -r_LightViewMatrix[2].xyz );
 	vec3 lvec=normalize( -r_LightViewMatrix[2].xyz );
+
 	float atten=1.0;
 	float atten=1.0;
 	
 	
 #elif MX2_POINTLIGHT
 #elif MX2_POINTLIGHT
 
 
-	// TODO: https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
 	vec3 lvec=r_LightViewMatrix[3].xyz-position;
 	vec3 lvec=r_LightViewMatrix[3].xyz-position;
-	float atten=max( 1.0-length( lvec )/r_LightRange,0.0 );
+
+	float atten=pointAtten( length( lvec ),r_LightRange );
+
 	lvec=normalize( lvec );
 	lvec=normalize( lvec );
 	
 	
 #elif MX2_SPOTLIGHT
 #elif MX2_SPOTLIGHT
 
 
 	vec3 lvec=r_LightViewMatrix[3].xyz-position;
 	vec3 lvec=r_LightViewMatrix[3].xyz-position;
-	float atten=max( 1.0-length( lvec )/r_LightRange,0.0 );
+	
+	float atten=pointAtten( length( lvec ),r_LightRange );
+	
 	lvec=normalize( lvec );
 	lvec=normalize( lvec );
 	
 	
 	float cosangle=dot( -lvec,r_LightViewMatrix[2].xyz );
 	float cosangle=dot( -lvec,r_LightViewMatrix[2].xyz );
@@ -51,11 +64,11 @@ void emitPbrFragment( vec3 color,float metalness,float roughness,vec3 position,v
 	
 	
 	float spow=pow( 2.0,glosiness * 12.0 );
 	float spow=pow( 2.0,glosiness * 12.0 );
 //	float spow=pow( 2048.0,glosiness );
 //	float spow=pow( 2048.0,glosiness );
-	
+
 	float fnorm=(spow+2.0)/8.0;
 	float fnorm=(spow+2.0)/8.0;
 	
 	
 	vec3 fschlick=specular + (1.0-specular) * pow( 1.0-hdotl,5.0 ) * glosiness;
 	vec3 fschlick=specular + (1.0-specular) * pow( 1.0-hdotl,5.0 ) * glosiness;
-	
+
 	specular=fschlick * pow( ndoth,spow ) * fnorm;
 	specular=fschlick * pow( ndoth,spow ) * fnorm;
 	
 	
 	vec3 light=r_LightColor.rgb * ndotl * atten;
 	vec3 light=r_LightColor.rgb * ndotl * atten;
@@ -80,7 +93,7 @@ void emitPbrFragment( vec3 color,float metalness,float roughness,vec3 position,v
 
 
 	vec3 frag=(diffuse+specular) * light;
 	vec3 frag=(diffuse+specular) * light;
 		
 		
-	gl_FragColor=vec4( min( frag,8.0 ),1.0 );
+	gl_FragColor=vec4( frag,1.0 );
 }
 }
 
 
 #endif
 #endif
@@ -189,18 +202,23 @@ void emitPbrFragment( vec4 color,vec3 ambient,vec3 emissive,float metalness,floa
 #if MX2_DIRECTIONALLIGHT
 #if MX2_DIRECTIONALLIGHT
 
 
 	vec3 lvec=normalize( -r_LightViewMatrix[2].xyz );
 	vec3 lvec=normalize( -r_LightViewMatrix[2].xyz );
+	
 	float atten=1.0;
 	float atten=1.0;
 	
 	
 #elif MX2_POINTLIGHT
 #elif MX2_POINTLIGHT
 
 
 	vec3 lvec=r_LightViewMatrix[3].xyz-v_Position;
 	vec3 lvec=r_LightViewMatrix[3].xyz-v_Position;
-	float atten=max( 1.0-length( lvec )/r_LightRange,0.0 );
+	
+	float atten=pointAtten( length( lvec ),r_LightRange );
+	
 	lvec=normalize( lvec );
 	lvec=normalize( lvec );
 	
 	
 #elif MX2_SPOTLIGHT
 #elif MX2_SPOTLIGHT
 
 
 	vec3 lvec=r_LightViewMatrix[3].xyz-v_Position;
 	vec3 lvec=r_LightViewMatrix[3].xyz-v_Position;
-	float atten=max( 1.0-length( lvec )/r_LightRange,0.0 );
+	
+	float atten=pointAtten( length( lvec ),r_LightRange );
+	
 	lvec=normalize( lvec );
 	lvec=normalize( lvec );
 	
 	
 	float cosangle=dot( -lvec,r_LightViewMatrix[2].xyz );
 	float cosangle=dot( -lvec,r_LightViewMatrix[2].xyz );

+ 15 - 2
modules/mojo3d/assets/shaders/imports/std.glsl

@@ -91,6 +91,8 @@ uniform sampler2D r_ColorBuffer;
 uniform sampler2D r_NormalBuffer;
 uniform sampler2D r_NormalBuffer;
 uniform sampler2D r_DepthBuffer;
 uniform sampler2D r_DepthBuffer;
 uniform vec2 r_BufferCoordScale;
 uniform vec2 r_BufferCoordScale;
+uniform vec2 r_QuadCoordScale;
+uniform vec2 r_QuadCoordTrans;
 
 
 //***** LIGHTING *****
 //***** LIGHTING *****
 //
 //
@@ -152,6 +154,17 @@ attribute vec4 a_Tangent;	//mask=32
 attribute vec4 a_Weights;	//mask=64
 attribute vec4 a_Weights;	//mask=64
 attribute vec4 a_Bones;		//mask=128
 attribute vec4 a_Bones;		//mask=128
 
 
+void transformLightQuadVertex(){
+
+	vec2 qposition=a_Position.xy * r_QuadCoordScale + r_QuadCoordTrans;
+
+	v_ClipPosition=qposition * 2.0 - 1.0;
+	
+	v_BufferCoords=qposition * r_BufferCoordScale;
+	
+	gl_Position=vec4( v_ClipPosition,-1.0,1.0 );
+}
+
 void transformQuadVertex(){
 void transformQuadVertex(){
 
 
 	v_ClipPosition=a_Position.xy * 2.0 - 1.0;
 	v_ClipPosition=a_Position.xy * 2.0 - 1.0;
@@ -382,7 +395,7 @@ vec3 sampleEnv( vec3 viewVec,float roughness ){
 //#ifdef GL_ES
 //#ifdef GL_ES
 		float lod=textureCube( r_EnvTextureCube,tv ).a * 255.0;
 		float lod=textureCube( r_EnvTextureCube,tv ).a * 255.0;
 		if( lod==0.0 ) lod=textureCube( r_EnvTextureCube,tv,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
 		if( lod==0.0 ) lod=textureCube( r_EnvTextureCube,tv,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
-		return pow( textureCube( r_EnvTextureCube,tv,max( roughness*(r_EnvTextureMaxLod-1.0)-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
+		return pow( textureCube( r_EnvTextureCube,tv,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 //#else
 //#else
 //		return pow( textureCube( r_EnvTextureCube,tv,roughness*r_EnvTextureMaxLod ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 //		return pow( textureCube( r_EnvTextureCube,tv,roughness*r_EnvTextureMaxLod ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 //#endif
 //#endif
@@ -396,7 +409,7 @@ vec3 sampleEnv( vec3 viewVec,float roughness ){
 //#ifdef GL_ES
 //#ifdef GL_ES
 		float lod=texture2D( r_EnvTexture2D,tc ).a * 255.0;
 		float lod=texture2D( r_EnvTexture2D,tc ).a * 255.0;
 		if( lod==0.0 ) lod=texture2D( r_EnvTexture2D,tc,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
 		if( lod==0.0 ) lod=texture2D( r_EnvTexture2D,tc,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
-		return pow( texture2D( r_EnvTexture2D,tc,max( roughness*(r_EnvTextureMaxLod-1.0)-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
+		return pow( texture2D( r_EnvTexture2D,tc,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 //else
 //else
 //		return pow( texture2DLod( r_EnvTexture2D,tc,max( mipmapLod( tc ),roughness*r_EnvTextureMaxLod ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 //		return pow( texture2DLod( r_EnvTexture2D,tc,max( mipmapLod( tc ),roughness*r_EnvTextureMaxLod ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 	
 	

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

@@ -7,7 +7,7 @@
 
 
 void main(){
 void main(){
 
 
-	transformQuadVertex();
+	transformLightQuadVertex();
 }
 }
 
 
 //@fragment
 //@fragment
@@ -23,6 +23,8 @@ void main(){
 	vec3 normal=normalize( normal_r.xyz * 2.0 - 1.0 );
 	vec3 normal=normalize( normal_r.xyz * 2.0 - 1.0 );
 	
 	
 	emitPbrFragment( color_m.rgb,color_m.a,normal_r.a,position,normal );
 	emitPbrFragment( color_m.rgb,color_m.a,normal_r.a,position,normal );
+	
+//	gl_FragColor=vec4( 1.0,1.0,0.0,1.0 );
 }
 }
 
 
 
 

+ 34 - 2
modules/mojo3d/render/renderer.monkey2

@@ -169,22 +169,54 @@ When a new renderer is created, the config setting `MOJO3D\_RENDERER` can be use
 		
 		
 		Local renderPass:=0
 		Local renderPass:=0
 		
 		
+		Local lvmatrix:=_viewMatrix * light.Matrix
+		
+		Local qscale:=New Vec2f( 1 )
+		Local qtrans:=New Vec2f( 0 )
+		
 		Select light.Type
 		Select light.Type
 		Case LightType.Directional
 		Case LightType.Directional
+			
 			If light.CastsShadow RenderDirectionalShadows( light ) ; renderPass|=16
 			If light.CastsShadow RenderDirectionalShadows( light ) ; renderPass|=16
 			renderPass|=4
 			renderPass|=4
+			
 		Case LightType.Point
 		Case LightType.Point
+
+			Local r:=light.Range
+			
+			If lvmatrix.t.z<-r 
+'				Print "clip1, z="+lvmatrix.t.z
+				Return
+			Endif
+			
+			If lvmatrix.t.z>r
+				Local box:=New Boxf( lvmatrix.t+New Vec3f( -r ),lvmatrix.t+New Vec3f( r ) )
+				Local cmin:=New Vec2f( 1 ),cmax:=New Vec2f( 0 )
+				For Local i:=0 Until 8
+					Local v:=(_projMatrix * box.Corner( i ) ).XY * 0.5 + 0.5
+					cmin.x=Min( cmin.x,v.x );cmin.y=Min( cmin.y,v.y )
+					cmax.x=Max( cmax.x,v.x );cmax.y=Max( cmax.y,v.y )
+				Next
+				If cmin.x>=1.0 Or cmin.y>=1.0 Return
+				If cmax.x<0.0 Or cmax.y<0.0 Return
+				qscale=cmax-cmin
+				qtrans=cmin
+			Endif
+			
 			If light.CastsShadow RenderPointShadows( light ) ; renderPass|=16
 			If light.CastsShadow RenderPointShadows( light ) ; renderPass|=16
 			renderPass|=8
 			renderPass|=8
+			
 		Case LightType.Spot
 		Case LightType.Spot
+			
 			If light.CastsShadow RenderSpotShadows( light ) ; renderPass|=16
 			If light.CastsShadow RenderSpotShadows( light ) ; renderPass|=16
 			renderPass|=12
 			renderPass|=12
+
 		Default
 		Default
 			Return
 			Return
 		End
 		End
 		
 		
-		Local lvmatrix:=_viewMatrix * light.Matrix
-		
+		_runiforms.SetVec2f( "QuadCoordScale",qscale )
+		_runiforms.SetVec2f( "QuadCoordTrans",qtrans )
 		_runiforms.SetMat4f( "LightViewMatrix",lvmatrix )
 		_runiforms.SetMat4f( "LightViewMatrix",lvmatrix )
 		_runiforms.SetMat4f( "InverseLightViewMatrix",-lvmatrix )
 		_runiforms.SetMat4f( "InverseLightViewMatrix",-lvmatrix )
 		_runiforms.SetColor( "LightColor",light.Color )
 		_runiforms.SetColor( "LightColor",light.Color )

+ 4 - 2
modules/mojo3d/tests/room.monkey2

@@ -41,7 +41,7 @@ Class MyWindow Extends Window
 	
 	
 	Method CreateRoom()
 	Method CreateRoom()
 
 
-		Local box:=New Boxf( -10,10 )
+		Local box:=New Boxf( -15,15 )
 		
 		
 		Local material:=New PbrMaterial( Color.Orange )
 		Local material:=New PbrMaterial( Color.Orange )
 		
 		
@@ -88,13 +88,15 @@ Class MyWindow Extends Window
 		Local camera:=New Camera( Self )
 		Local camera:=New Camera( Self )
 		camera.Move( 0,1,-5 )
 		camera.Move( 0,1,-5 )
 		camera.AddComponent<FlyBehaviour>()
 		camera.AddComponent<FlyBehaviour>()
+		camera.Far=100
 		
 		
 		'Create light
 		'Create light
 		Local light:=New Light
 		Local light:=New Light
 		light.Type=LightType.Point
 		light.Type=LightType.Point
+		light.Color=Color.White
 		light.Texture=Texture.Load( "asset::monkey2-logo.png",TextureFlags.Filter|TextureFlags.Cubemap )
 		light.Texture=Texture.Load( "asset::monkey2-logo.png",TextureFlags.Filter|TextureFlags.Cubemap )
 		light.CastsShadow=True
 		light.CastsShadow=True
-		light.Range=15
+		light.Range=25
 		light.AddComponent<RotateBehaviour>().Speed=New Vec3f( 0,-.05,0 )
 		light.AddComponent<RotateBehaviour>().Speed=New Vec3f( 0,-.05,0 )
 		
 		
 		CreateRoom()
 		CreateRoom()

+ 3 - 1
modules/mojo3d/tests/spotlight.monkey2

@@ -26,6 +26,7 @@ Class MyWindow Extends Window
 		
 		
 		'create (current) scene
 		'create (current) scene
 		_scene=New Scene( True )
 		_scene=New Scene( True )
+		_scene.ShadowAlpha=.75
 		
 		
 		'create camera
 		'create camera
 		Local camera:=New Camera( Self )
 		Local camera:=New Camera( Self )
@@ -36,7 +37,8 @@ Class MyWindow Extends Window
 		Local light:=New Light
 		Local light:=New Light
 		light.Type=LightType.Spot
 		light.Type=LightType.Spot
 		light.Texture=_scene.LoadTexture( "asset::monkey2-logo.png" )
 		light.Texture=_scene.LoadTexture( "asset::monkey2-logo.png" )
-		light.Range=25
+		light.Color=Color.White * 8
+		light.Range=15
 		light.InnerAngle=15
 		light.InnerAngle=15
 		light.OuterAngle=45
 		light.OuterAngle=45
 		light.CastsShadow=True
 		light.CastsShadow=True