Bläddra i källkod

Updated mega renderer shaders.

Mark Sibly 7 år sedan
förälder
incheckning
3a8e63afd9

+ 4 - 4
modules/mojo3d/assets/shaders/copy.glsl

@@ -1,9 +1,9 @@
 
 //@renderpasses 0
 
-uniform sampler2D r_SourceTexture;
+uniform sampler2D r_AccumBuffer;
 
-uniform vec2 r_SourceCoordScale;
+uniform vec2 r_BufferCoordScale;
 
 varying vec2 v_TexCoord0;
 
@@ -13,7 +13,7 @@ attribute vec2 a_Position;	//0...1 (1=viewport size)
 
 void main(){
 
-	v_TexCoord0=a_Position * r_SourceCoordScale;
+	v_TexCoord0=a_Position * r_BufferCoordScale;
 
 	gl_Position=vec4( a_Position * 2.0 - 1.0,-1.0,1.0 );
 }
@@ -22,5 +22,5 @@ void main(){
 
 void main(){
 
-	gl_FragColor=vec4( pow( texture2D( r_SourceTexture,v_TexCoord0 ).rgb,vec3( 1.0/2.2 ) ),1.0 );
+	gl_FragColor=vec4( pow( texture2D( r_AccumBuffer,v_TexCoord0 ).rgb,vec3( 1.0/2.2 ) ),1.0 );
 }

+ 0 - 41
modules/mojo3d/assets/shaders/effect-fog.glsl

@@ -1,41 +0,0 @@
-
-//@renderpasses 0
-
-varying vec2 v_TexCoord0;
-
-//@vertex
-
-uniform vec2 r_BufferCoordScale;
-
-attribute vec2 a_Position;	//0...1 (1=viewport size)
-
-void main(){
-
-	v_TexCoord0=a_Position * r_BufferCoordScale;
-	
-	gl_Position=vec4( a_Position * 2.0 - 1.0,-1.0,1.0 );
-}
-
-//@fragment
-
-uniform sampler2D r_DepthBuffer;
-uniform float r_DepthNear;
-uniform float r_DepthFar;
-
-uniform vec4 m_Color;
-uniform float m_Near;
-uniform float m_Far;
-
-float viewDepth( float depth ){
-
-	return r_DepthFar * r_DepthNear / ( r_DepthFar + depth * ( r_DepthNear - r_DepthFar ) );
-}
-
-void main(){
-
-	float depth=viewDepth( texture2D( r_DepthBuffer,v_TexCoord0 ).r );
-	
-	float fog=clamp( (depth-m_Near)/(m_Far-m_Near),0.0,1.0 ) * m_Color.a;
-	
-	gl_FragColor=vec4( m_Color.rgb * fog,fog );	//premultiplied alpha!
-}

+ 60 - 0
modules/mojo3d/assets/shaders/fog-deferred.glsl

@@ -0,0 +1,60 @@
+
+//@renderpasses 0
+
+varying vec2 v_ClipPosition;
+
+varying vec2 v_TexCoord0;
+
+//@vertex
+
+uniform vec2 r_BufferCoordScale;
+
+attribute vec2 a_Position;	//0...1 (1=viewport size)
+
+void main(){
+
+	v_ClipPosition=a_Position * 2.0 - 1.0;
+	
+	v_TexCoord0=a_Position * r_BufferCoordScale;
+	
+	gl_Position=vec4( v_ClipPosition,-1.0,1.0 );
+}
+
+//@fragment
+
+uniform mat4 r_InverseProjectionMatrix;
+
+uniform sampler2D r_DepthBuffer;
+uniform float r_DepthNear;
+uniform float r_DepthFar;
+
+uniform vec4 r_FogColor;
+uniform float r_FogNear;
+uniform float r_FogFar;
+
+float viewDepth( float depth ){
+
+	return r_DepthFar * r_DepthNear / ( r_DepthFar + depth * ( r_DepthNear - r_DepthFar ) );
+}
+
+void main(){
+
+	float fog=0.0;
+
+	float depth=texture2D( r_DepthBuffer,v_TexCoord0 ).r;
+	
+	if( depth<1.0 ){
+
+		depth=viewDepth( depth );
+		
+		vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition,-1.0,1.0 );
+		
+		vec3 vpos=vpos4.xyz/vpos4.w;
+		
+		vec3 v_Position=vpos/vpos.z*depth;
+		
+		fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
+	}
+	
+	gl_FragColor=vec4( r_FogColor.rgb * fog,fog );
+}

+ 63 - 60
modules/mojo3d/assets/shaders/lighting-deferred.glsl

@@ -1,15 +1,18 @@
-//@renderpasses 0,1,2,3
 
-//renderpasses:
-// 0=directional light without shadow.
-// 1=point light without shadow.
-// 2=directional light with shadow.
-// 3=point light with shadow.
+//@renderpasses 2,6,10,14
 
+//renderpasses:
+//
+// 2=directional without shadows
+// 6=directional with  shadows
+// 10=point without shadows
+// 14=point with shadows
+ 
 uniform mat4 r_InverseProjectionMatrix;
 
-#define MX2_DIRECTIONALPASS (MX2_RENDERPASS==0 || MX2_RENDERPASS==2)
-#define MX2_SHADOWEDPASS (MX2_RENDERPASS==2 || MX2_RENDERPASS==3)
+#define MX2_POINT_LIGHT ((MX2_RENDERPASS&8)!=0)
+#define MX2_DIRECTIONAL_LIGHT ((MX2_RENDERPASS&8)==0)
+#define MX2_SHADOWED_LIGHT ((MX2_RENDERPASS&4)!=0)
 
 uniform vec2 r_BufferCoordScale;
 uniform sampler2D r_ColorBuffer;
@@ -23,18 +26,24 @@ uniform mat4 r_LightViewMatrix;
 uniform float r_LightRange;
 uniform vec4 r_LightColor;
 
-#if MX2_RENDERPASS==2
+#if MX2_SHADOWED_LIGHT
+
+uniform float r_ShadowAlpha;
+
+#if MX2_DIRECTIONAL_LIGHT
 uniform sampler2D r_ShadowCSMTexture;
 uniform vec4 r_ShadowCSMSplits;
 uniform mat4 r_ShadowMatrix0;
 uniform mat4 r_ShadowMatrix1;
 uniform mat4 r_ShadowMatrix2;
 uniform mat4 r_ShadowMatrix3;
-#elif MX2_RENDERPASS==3
+#else
 uniform samplerCube r_ShadowCubeTexture;
 uniform mat4 r_ShadowMatrix0;
 #endif
 
+#endif
+
 varying vec2 v_ClipPosition;
 varying vec2 v_TexCoord0;
 
@@ -78,7 +87,9 @@ float viewDepth( float depth ){
 	return r_DepthFar * r_DepthNear / ( r_DepthFar + depth * ( r_DepthNear - r_DepthFar ) );
 }
 
-#if MX2_RENDERPASS==2
+#if MX2_SHADOWED_LIGHT
+
+#if MX2_DIRECTIONAL_LIGHT
 
 float shadowColor(){
 
@@ -87,30 +98,26 @@ float shadowColor(){
 //	vec3 vn=vec4( cross( dx,dy ),0.0 );
 //	vec4 vpos=vec4( v_Position + v_Normal*0.01,1.0 );
 
-	vec4 vpos=vec4( v_Position,1.0 );
-
-	if( vpos.z>=r_ShadowCSMSplits.w ) return 0.5;
+	if( v_Position.z>=r_ShadowCSMSplits.w ) return r_ShadowAlpha*0.5;
 	
-	vec4 llpos,lpos;
+	vec4 vpos=vec4( v_Position,1.0 );
 	vec2 off;
 	
 	if( vpos.z<r_ShadowCSMSplits.x ){
-		lpos=r_ShadowMatrix0 * vpos;
+		vpos=r_ShadowMatrix0 * vpos;
 		off=vec2( 0.0,0.0 );
 	}else if( vpos.z<r_ShadowCSMSplits.y ){
-		lpos=r_ShadowMatrix1 * vpos;
+		vpos=r_ShadowMatrix1 * vpos;
 		off=vec2( 0.5,0.0 );
 	}else if( vpos.z<r_ShadowCSMSplits.z ){
-		lpos=r_ShadowMatrix2 * vpos;
+		vpos=r_ShadowMatrix2 * vpos;
 		off=vec2( 0.0,0.5 );
 	}else{
-		lpos=r_ShadowMatrix3 * vpos;
+		vpos=r_ShadowMatrix3 * vpos;
 		off=vec2( 0.5,0.5 );
 	}
 	
-	vec3 spos=lpos.xyz/lpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
-
-//	spos.z*=0.999;
+	vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
 	
 #if defined( MX2_RGBADEPTHTEXTURES )
 	float d=RGBAToFloat( texture2D( r_ShadowCSMTexture,spos.xy+off ) );
@@ -118,27 +125,30 @@ float shadowColor(){
 	float d=texture2D( r_ShadowCSMTexture,spos.xy+off ).r;
 #endif
 	
-	if( spos.z>d ) return 0.0;
+	if( spos.z>d ) return 1.0-r_ShadowAlpha;
 	
 	return 1.0;
 }
 
-#elif MX2_RENDERPASS==3
+#else
 
 float shadowColor(){
 
-	vec4 vpos=vec4( v_Position + v_Normal * .05,1.0 );
+	vec4 vpos=vec4( v_Position,1.0 );
+	
 	vec3 lpos=(r_ShadowMatrix0 * vpos).xyz;
 	
 	float d=RGBAToFloat( textureCube( r_ShadowCubeTexture,lpos ) );
 	
-	if( length( lpos )>=d*r_LightRange ) return 0.0;
+	if( length(lpos) > d * r_LightRange ) return 1.0-r_ShadowAlpha;
 	
 	return 1.0;
 }
 
 #endif
 
+#endif
+
 vec3 lightColor( vec3 color,float metalness,float roughness ){
 
 	vec3 normal=v_Normal;
@@ -146,46 +156,39 @@ vec3 lightColor( vec3 color,float metalness,float roughness ){
 	vec3 color0=vec3( 0.04,0.04,0.04 );
 	vec3 diffuse=color * (1.0-metalness);
 	vec3 specular=(color-color0) * metalness + color0;
-	float atten=1.0;
 	
-#if MX2_DIRECTIONALPASS
+#if MX2_DIRECTIONAL_LIGHT
 	vec3 lvec=normalize( -r_LightViewMatrix[2].xyz );
+	float atten=1.0;
 #else
-	// https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
-	//
+	// TODO: https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
 	vec3 lvec=r_LightViewMatrix[3].xyz-v_Position;
-	float ldist=length( lvec );
-    lvec/=ldist;
-	atten=max( 1.0-(ldist/r_LightRange),0.0 );
-    
-//    atten=1.0;
-	
-//	atten=1.0/( pow( length( lvec/r_LightRange ),2.0 ) );	//Cool! https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
-//	atten=1.0/( pow( length( lvec )/r_LightRange,2.0 ) );	//Cool! https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
-//	lvec=normalize( lvec );
-
+	float atten=max( 1.0-length( lvec )/r_LightRange,0.0 );
+	lvec=normalize( lvec );
 #endif
+
 	vec3 vvec=normalize( -v_Position );
 	vec3 hvec=normalize( lvec+vvec );
 
+	float hdotl=max( dot( hvec,lvec ),0.0 );
+	float ndotl=max( dot( normal,lvec ),0.0 );
+	float ndoth=max( dot( normal,hvec ),0.0 );
+	
 	float spow=pow( 2.0,glosiness * 12.0 );
-//	float spow=pow( 4096.0,glosiness );
-//	float spow=exp2( 12.0 * glosiness + 1.0 );
-	float fnorm=(spow+2.0)/8.0;
+//	float spow=pow( 2048.0,glosiness );
 	
-	float hdotl=max( dot( hvec,lvec ),0.0 );
+	float fnorm=(spow+2.0)/8.0;
 	
 	vec3 fschlick=specular + (1.0-specular) * pow( 1.0-hdotl,5.0 ) * glosiness;
 	
-	float ndotl=max( dot( normal,lvec ),0.0 );
-	float ndoth=max( dot( normal,hvec ),0.0 );
-	
-	specular=pow( ndoth,spow ) * fnorm * fschlick;
+	specular=fschlick * pow( ndoth,spow ) * fnorm;
 
 	vec3 light=r_LightColor.rgb * ndotl * atten;
 	
-#if MX2_SHADOWEDPASS
+#if MX2_SHADOWED_LIGHT
+
 	light*=shadowColor();
+	
 #endif
 	
 	return (diffuse+specular) * light;
@@ -205,23 +208,23 @@ void main(){
 
 	//debug vpos x/y
 	//
-	//if( abs( vpos.x )>=1.0 || abs( vpos.y )>=1.0 ){
-	//	gl_FragColor=vec4( 0.0,0.0,1.0,1.0 );
-	//	return;
-	//}
+	if( abs( vpos.x )>=1.0 || abs( vpos.y )>=1.0 ){
+		gl_FragColor=vec4( 0.0,0.0,1.0,1.0 );
+		return;
+	}
 	
 	//debug z
 	//
-	//if( abs( vpos.z-r_DepthNear)>0.00001 ){
-	//	gl_FragColor=vec4( 1.0,0.0,0.0,1.0 );
-	//	return;
-	//}
+	if( abs( vpos.z-r_DepthNear)>0.00001 ){
+		gl_FragColor=vec4( 1.0,0.0,0.0,1.0 );
+		return;
+	}
 	
 	v_Position=vpos/vpos.z*depth;
 	
 	v_Normal=normalize( normal_r.xyz * 2.0 - 1.0 );
 	
-	vec3 light=lightColor( color_m.rgb,color_m.a,normal_r.a );
-
-	gl_FragColor=vec4( min( light,8.0 ),1.0 );
+	vec3 frag=lightColor( color_m.rgb,color_m.a,normal_r.a );
+	
+	gl_FragColor=vec4( min( frag,8.0 ),1.0 );
 }

+ 34 - 11
modules/mojo3d/assets/shaders/material-particle.glsl

@@ -1,17 +1,28 @@
-//@renderpasses 0
+
+//@renderpasses 1,3
 
 //renderer uniforms
 
 uniform float r_Time;
 
-uniform mat4 r_ViewProjectionMatrix;
+uniform mat4 r_ProjectionMatrix;
+
+uniform mat4 r_ViewMatrix;
+
+uniform vec4 r_FogColor;
+
+uniform float r_FogNear;
+
+uniform float r_FogFar;
 
 //instance uniforms
 
-//uniform mat4 i_ModelViewProjectionMatrix;
+uniform mat4 i_ModelViewMatrix;
 
 uniform mat4 i_ModelMatrix;
 
+uniform float i_Alpha;
+
 //material uniforms
 
 uniform sampler2D m_ColorTexture;
@@ -28,6 +39,8 @@ uniform float x_Fade;		//start fade out time
 
 //varyings...
 
+varying vec3 v_Position;
+
 varying vec4 v_Color;
 
 //@vertex
@@ -43,14 +56,18 @@ void main(){
 	float t=(r_Time-a_TexCoord0.s);
 	
 	float a=1.0-clamp( (t-x_Fade)/(x_Duration-x_Fade),0.0,1.0 );
-	
-	v_Color=m_ColorFactor * vec4( a_Tangent.rgb,a_Tangent.a * a );
+
+	v_Color=m_ColorFactor * vec4( a_Tangent.rgb,a_Tangent.a * a * i_Alpha );
 	
 	vec4 position=i_ModelMatrix * vec4( a_Position * t,1.0 );
 	
 	position.xyz+=x_Gravity * t * t * .5;
 	
-	gl_Position=r_ViewProjectionMatrix * position;
+	position=r_ViewMatrix * position;
+	
+	v_Position=position.xyz;
+	
+	gl_Position=r_ProjectionMatrix * position;
 	
 	gl_PointSize=a_TexCoord0.t/gl_Position.w;
 }
@@ -59,15 +76,21 @@ void main(){
 
 void main(){
 
-	vec4 tcolor=texture2D( m_ColorTexture,gl_PointCoord );
+	vec4 color=texture2D( m_ColorTexture,gl_PointCoord );
 
-	vec3 color=pow( tcolor.rgb,vec3( 2.2 ) ) * v_Color.rgb * v_Color.a;
+	float alpha=color.a * v_Color.a;
+	
+	vec3 frag=pow( color.rgb,vec3( 2.2 ) ) * v_Color.rgb;
+	
+	float fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
+	
+	frag=mix( frag,r_FogColor.rgb,fog );
 	
-	float alpha=tcolor.a * v_Color.a;
+	frag*=alpha;
 	
 #if defined( MX2_SRGBOUTPUT )
-	gl_FragColor=vec4( pow( color,vec3( 1.0/2.2 ) ),alpha );
+	gl_FragColor=vec4( pow( frag,vec3( 1.0/2.2 ) ),alpha );
 #else
-	gl_FragColor=vec4( color,alpha );
+	gl_FragColor=vec4( frag,alpha );
 #endif
 }

+ 34 - 24
modules/mojo3d/assets/shaders/material-pbr-deferred.glsl

@@ -1,13 +1,14 @@
-
-//@renderpasses 0,16,17
+//@renderpasses 1,4,12
 
 //renderpasses:
 //
-// 0 = ambient + gbuffers.
-// 16 = directional light shadowcasters.
-// 17 = point light shadowcasters.
+// 1 = ambient
+// 4 = directional shadows
+// 12 = point shadows
 
-#define MX2_COLORPASS (MX2_RENDERPASS==0)
+#define MX2_COLORPASS ((MX2_RENDERPASS&3)!=0)
+#define MX2_POINT_LIGHT ((MX2_RENDERPASS&8)!=0)
+#define MX2_DIRECTIONAL_LIGHT ((MX2_RENDERPASS&8)==0)
  
 //material uniforms
 
@@ -42,7 +43,7 @@ varying mat3 v_TanMatrix;
 
 #else	//MX2_COLORPASS
 
-#if MX2_RENDERPASS==17
+#if MX2_POINT_LIGHT
 uniform float r_LightRange;
 #endif
 
@@ -191,26 +192,29 @@ void pbrWriteFragData( vec3 color,vec3 emissive,float metalness,float roughness,
 	vec3 diffuse=color * (1.0-metalness);
 	vec3 specular=(color-color0) * metalness + color0;
 	
-	vec3 rvec=r_EnvMatrix * reflect( v_Position,normal );
+	vec3 vvec=normalize( -v_Position );
+	float ndotv=max( dot( normal,vvec ),0.0 );
 	
+	vec3 rvec=r_EnvMatrix * reflect( v_Position,normal );
+
 	float lod=textureCube( r_EnvTexture,rvec,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
-	
 	if( lod>0.0 ) lod=textureCube( r_EnvTexture,rvec ).a * 255.0;
-	
-	vec3 env=pow( textureCube( r_EnvTexture,rvec,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
 
-	vec3 vvec=normalize( -v_Position );
-	
-	float ndotv=max( dot( normal,vvec ),0.0 );
-	
-	vec3 fschlick=specular + (1.0-specular) * pow( 1.0-ndotv,5.0 ) * glosiness;
+//	float lod=textureCube( r_EnvTexture,rvec ).a * 255.0;
+//	if( lod==0.0 ) lod=textureCube( r_EnvTexture,rvec,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
 
-	vec3 ambdiff=diffuse * r_AmbientDiffuse.rgb;
-		
-	vec3 ambspec=env * fschlick;
+	vec3 ambEnv=pow( textureCube( r_EnvTexture,rvec,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
+
+	vec3 fschlick0=specular + (1.0-specular) * pow( 1.0-ndotv,5.0 ) * glosiness;
 
+	vec3 ambDiffuse=diffuse * r_AmbientDiffuse.rgb;
+		
+	vec3 ambSpecular=fschlick0 * ambEnv;
+	
+	vec3 frag=(ambDiffuse + ambSpecular) * occlusion + emissive;
+	
 	//write ambient
-	gl_FragData[0]=vec4( min( (ambdiff+ambspec) * occlusion + emissive,8.0 ),1.0 );
+	gl_FragData[0]=vec4( min( frag,8.0 ),1.0 );
 	
 	//write color/metalness
 	gl_FragData[1]=vec4( color,metalness );
@@ -229,8 +233,10 @@ uniform sampler2D m_EmissiveTexture;
 uniform sampler2D m_MetalnessTexture;
 uniform sampler2D m_RoughnessTexture;
 uniform sampler2D m_OcclusionTexture;
+#ifdef MX2_BUMPMAPPED
 uniform sampler2D m_NormalTexture;
 #endif
+#endif
 
 uniform vec4 m_ColorFactor;
 uniform vec4 m_EmissiveFactor;
@@ -247,15 +253,13 @@ void main(){
 	float roughness=texture2D( m_RoughnessTexture,v_TexCoord0 ).g * m_RoughnessFactor;
 	float occlusion=texture2D( m_OcclusionTexture,v_TexCoord0 ).r;
 	
-//	emissive=vec3( 1.0,1.0,0.0 );
-	
 #ifdef MX2_BUMPMAPPED
 	vec3 normal=texture2D( m_NormalTexture,v_TexCoord0 ).xyz * 2.0 - 1.0;
 	normal=normalize( v_TanMatrix * normal );
 #else
 	vec3 normal=normalize( v_Normal );
 #endif
-	
+
 #else
 
 	vec3 color=m_ColorFactor.rgb;
@@ -274,12 +278,18 @@ void main(){
 
 void main(){
 
-#if MX2_RENDERPASS==17
+#if MX2_POINT_LIGHT
+
 	gl_FragColor=FloatToRGBA( length( v_Position )/r_LightRange );
+	
 #elif defined( MX2_RGBADEPTHTEXTURES )
+
 	gl_FragColor=FloatToRGBA( gl_FragCoord.z );
+
 #else
+
 	gl_FragColor=vec4( vec3( gl_FragCoord.z ),1.0 );
+	
 #endif
 
 }

+ 95 - 62
modules/mojo3d/assets/shaders/material-pbr-forward.glsl

@@ -1,5 +1,4 @@
-
-//@renderpasses 1,2,3,6,7,10,11,14,15,16,17
+//@renderpasses 1, 2,3,6,7, 10,11,14,15, 4,12
 
 //render passes:
 //
@@ -7,14 +6,15 @@
 // 2  = lighting.
 // 4  = shadowed.
 // 8  = light type - 0=directional, 1=point
-// 16 = directional shadow casters.
-// 17 = point shadow casters.
 
-#define MX2_COLORPASS ((MX2_RENDERPASS&16)==0)
+#define MX2_COLORPASS ((MX2_RENDERPASS&3)!=0)
+
 #define MX2_AMBIENTPASS (MX2_RENDERPASS&1)
 #define MX2_LIGHTINGPASS (MX2_RENDERPASS&2)
 #define MX2_SHADOWEDPASS (MX2_RENDERPASS&4)
-#define MX2_DIRECTIONALLIGHT ((MX2_RENDERPASS&8)==0)
+
+#define MX2_POINT_LIGHT ((MX2_RENDERPASS&8)!=0)
+#define MX2_DIRECTIONAL_LIGHT ((MX2_RENDERPASS&8)==0)
  
 //instance uniforms
 //
@@ -28,6 +28,10 @@ uniform mat4 i_ModelBoneMatrices[96];
 
 //material uniforms
 
+#if MX2_COLORPASS
+uniform float i_Alpha;
+#endif
+
 #if MX2_COLORPASS && defined( MX2_TEXTURED )
 uniform mat3 m_TextureMatrix;
 #endif
@@ -42,7 +46,6 @@ uniform float r_EnvTextureMaxLod;
 uniform vec4 r_EnvColor;
 uniform mat3 r_EnvMatrix;
 
-
 #endif
 
 #if MX2_LIGHTINGPASS
@@ -53,7 +56,9 @@ uniform float r_LightRange;
 
 #if MX2_SHADOWEDPASS
 
-#if MX2_DIRECTIONALLIGHT
+uniform float r_ShadowAlpha;
+
+#if MX2_DIRECTIONAL_LIGHT
 uniform sampler2D r_ShadowCSMTexture;
 uniform vec4 r_ShadowCSMSplits;
 uniform mat4 r_ShadowMatrix0;
@@ -69,7 +74,7 @@ uniform mat4 r_ShadowMatrix0;
 
 #endif	//MX2_LIGHTINGPASS
 
-#if MX2_RENDERPASS==17
+#if !MX2_COLORPASS && MX2_POINT_LIGHT
 uniform float r_LightRange;
 #endif
 
@@ -209,33 +214,36 @@ float RGBAToFloat( vec4 rgba ){
 
 #if MX2_COLORPASS
 
+uniform float r_FogNear;
+uniform float r_FogFar;
+uniform vec4 r_FogColor;
+
 #if MX2_SHADOWEDPASS
 
-#if MX2_DIRECTIONALLIGHT
+#if MX2_DIRECTIONAL_LIGHT
 
-float shadowColor( vec3 normal ){
+float shadowColor(){
 
-	vec4 vpos=vec4( v_Position + normal * .05,1.0 );
-	vec4 lpos;
+	if( v_Position.z>=r_ShadowCSMSplits.w ) return r_ShadowAlpha*0.5;
+
+	vec4 vpos=vec4( v_Position,1.0 );
 	vec2 off;
 	
 	if( vpos.z<r_ShadowCSMSplits.x ){
-		lpos=r_ShadowMatrix0 * vpos;
+		vpos=r_ShadowMatrix0 * vpos;
 		off=vec2( 0.0,0.0 );
 	}else if( vpos.z<r_ShadowCSMSplits.y ){
-		lpos=r_ShadowMatrix1 * vpos;
+		vpos=r_ShadowMatrix1 * vpos;
 		off=vec2( 0.5,0.0 );
 	}else if( vpos.z<r_ShadowCSMSplits.z ){
-		lpos=r_ShadowMatrix2 * vpos;
+		vpos=r_ShadowMatrix2 * vpos;
 		off=vec2( 0.0,0.5 );
 	}else{
-		lpos=r_ShadowMatrix3 * vpos;
+		vpos=r_ShadowMatrix3 * vpos;
 		off=vec2( 0.5,0.5 );
 	}
 	
-	vec3 spos=lpos.xyz/lpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
-
-//	spos.z*=0.999;
+	vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
 
 #if defined( MX2_RGBADEPTHTEXTURES )
 	float d=RGBAToFloat( texture2D( r_ShadowCSMTexture,spos.xy+off ) );
@@ -243,104 +251,109 @@ float shadowColor( vec3 normal ){
 	float d=texture2D( r_ShadowCSMTexture,spos.xy+off ).r;
 #endif
 	
-	if( spos.z>d ) return 0.0;
+	if( spos.z>d ) return 1.0-r_ShadowAlpha;
 	
 	return 1.0;
 }
 
-#else	//MX2_DIRECTIONALLIGHT
+#else	//MX2_DIRECTIONAL_LIGHT
 
-float shadowColor( vec3 normal ){
+float shadowColor(){
 
-	vec4 vpos=vec4( v_Position + normal * .05,1.0 );
+	vec4 vpos=vec4( v_Position,1.0 );
 	
 	vec3 lpos=(r_ShadowMatrix0 * vpos).xyz;
 	
 	float d=RGBAToFloat( textureCube( r_ShadowCubeTexture,lpos ) );
 	
-	if( length(lpos) > d * r_LightRange ) return 0.0;
+	if( length(lpos) > d * r_LightRange ) return 1.0-r_ShadowAlpha;
 	
 	return 1.0;
 }
 
-#endif	//MX2_DIRECTIONALLIGHT
+#endif	//MX2_DIRECTIONAL_LIGHT
 
 #endif	//MX2_SHADOWEDPASS
 
-vec3 fragColor( vec3 color,vec3 emissive,float metalness,float roughness,float occlusion,vec3 normal ){
+vec3 lighting( vec3 color,vec3 emissive,float metalness,float roughness,float occlusion,vec3 normal ){
 
+	const vec3 color0=vec3( 0.04,0.04,0.04 );
+	
 	float glosiness=1.0-roughness;
-	vec3 color0=vec3( 0.04,0.04,0.04 );
 	vec3 diffuse=color * (1.0-metalness);
 	vec3 specular=(color-color0) * metalness + color0;
+	
 	vec3 vvec=normalize( -v_Position );
+	float ndotv=dot( normal,vvec );
 	
 	vec3 frag=vec3( 0.0 );
-
-#if MX2_AMBIENTPASS
 	
+#if MX2_AMBIENTPASS
+
 	//ambient color
-	
-	
 	vec3 rvec=r_EnvMatrix * reflect( v_Position,normal );
+	
 	float lod=textureCube( r_EnvTexture,rvec,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
 	if( lod>0.0 ) lod=textureCube( r_EnvTexture,rvec ).a * 255.0;
-	vec3 ambenv=pow( textureCube( r_EnvTexture,rvec,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
-	
-	float ndotv=max( dot( normal,vvec ),0.0 );
+//	float lod=textureCube( r_EnvTexture,rvec ).a * 255.0;
+//	if( lod==0.0 ) lod=textureCube( r_EnvTexture,rvec,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
 
-	vec3 fschlick1=specular + (1.0-specular) * pow( 1.0-ndotv,5.0 ) * glosiness;
+	vec3 ambEnv=pow( textureCube( r_EnvTexture,rvec,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
+	
+	vec3 fschlick0=specular + (1.0-specular) * pow( 1.0-ndotv,5.0 ) * glosiness;
+	
+	vec3 ambDiffuse=diffuse * r_AmbientDiffuse.rgb;
+	
+	vec3 ambSpecular=fschlick0 * ambEnv;
 
-	vec3 ambdiff=diffuse * r_AmbientDiffuse.rgb;
-	vec3 ambspec=ambenv * fschlick1;
-	frag+=(ambdiff+ambspec) * occlusion + emissive;
+	frag+=( ambDiffuse + ambSpecular ) * occlusion + emissive;
 
-#endif	//MX2_AMBIENTPASS
+#endif
 
 #if MX2_LIGHTINGPASS
 
 	//lighting color
-	
 	float spow=pow( 2.0,glosiness * 12.0 );				//specular power
+//	float spow=pow( 4096.0,glosiness );
 	float fnorm=(spow+2.0)/8.0;							//normalization factor
-	float atten=1.0;
 	
-#if MX2_DIRECTIONALLIGHT
+#if MX2_DIRECTIONAL_LIGHT
 	vec3 lvec=normalize( -r_LightViewMatrix[2].xyz );
+	float atten=1.0;
 #else
 	vec3 lvec=r_LightViewMatrix[3].xyz-v_Position;
-	float ldist=length( lvec );
-    lvec/=ldist;
-	atten=max( 1.0-(ldist/r_LightRange),0.0 );
+	float atten=max( 1.0-length( lvec )/r_LightRange,0.0 );
+	lvec=normalize( lvec );
 #endif
-	
-	vec3 hvec=normalize( lvec+vvec );					//halfway vector
 
-	float hdotl=max( dot( hvec,lvec ),0.0 );
-	
-	vec3 fschlick2=specular + (1.0-specular) * pow( 1.0-hdotl,5.0 ) * glosiness;
+	vec3 hvec=normalize( lvec+vvec );
 
 	float ndotl=max( dot( normal,lvec ),0.0 );
 	float ndoth=max( dot( normal,hvec ),0.0 );
+	float hdotl=max( dot( hvec,lvec ),0.0 );
+	
+	vec3 fschlick=specular + (1.0-specular) * pow( 1.0-hdotl,5.0 ) * glosiness;
 	
-	vec3 lightspec=pow( ndoth,spow ) * fschlick2 * fnorm;
+	vec3 fspecular=fschlick * pow( ndoth,spow ) * fnorm;
 	
-	vec3 light=(diffuse+lightspec) * r_LightColor.rgb * ndotl * atten;
+	vec3 light=r_LightColor.rgb * ndotl * atten;
+	
+	light=(diffuse+fspecular) * light;
 
 #if MX2_SHADOWEDPASS
-	float shadow=shadowColor( normal );
+
+	light*=shadowColor();
 	
-	light*=shadow;
 #endif
 
 	frag+=light;
-	
+
 #endif	//MX2_LIGHTINGPASS
 
 	return frag;
 }
 
-#if defined( MX2_TEXTURED)
+#if defined( MX2_TEXTURED )
 uniform sampler2D m_ColorTexture;
 uniform sampler2D m_EmissiveTexture;
 uniform sampler2D m_MetalnessTexture;
@@ -357,11 +370,13 @@ uniform float m_RoughnessFactor;
 void main(){
 
 #if defined( MX2_TEXTURED )
-	vec3 color=pow( texture2D( m_ColorTexture,v_TexCoord0 ).rgb,vec3( 2.2 ) ) * m_ColorFactor.rgb;
+	vec4 rgba=texture2D( m_ColorTexture,v_TexCoord0 );
+	vec3 color=pow( rgba.rgb,vec3( 2.2 ) ) * m_ColorFactor.rgb;
 	vec3 emissive=pow( texture2D( m_EmissiveTexture,v_TexCoord0 ).rgb,vec3( 2.2 ) ) * m_EmissiveFactor.rgb;
 	float metalness=texture2D( m_MetalnessTexture,v_TexCoord0 ).b * m_MetalnessFactor;
 	float roughness=texture2D( m_RoughnessTexture,v_TexCoord0 ).g * m_RoughnessFactor;
 	float occlusion=texture2D( m_OcclusionTexture,v_TexCoord0 ).r;
+	float alpha=rgba.a * m_ColorFactor.a;
 	
 #if defined( MX2_BUMPMAPPED )
 	vec3 normal=texture2D( m_NormalTexture,v_TexCoord0 ).xyz * 2.0 - 1.0;
@@ -376,23 +391,41 @@ void main(){
 	float metalness=m_MetalnessFactor;
 	float roughness=m_RoughnessFactor;
 	float occlusion=1.0;
+	float alpha=m_ColorFactor.a;
+	
 	vec3 normal=normalize( v_Normal );
 #endif
 
-	vec3 frag=fragColor( color,emissive,metalness,roughness,occlusion,normal );
+	//perform lighting
+	vec3 frag=lighting( color,emissive,metalness,roughness,occlusion,normal );
 	
+	alpha*=i_Alpha;
+	
+	frag*alpha;
+	
+	//apply fog
+	float fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
+
+#if MX2_AMBIENTPASS
+	frag=mix( frag,r_FogColor.rgb,fog );
+#else
+	frag=mix( frag,vec3(0.0),fog );
+#endif
+	alpha=mix( alpha,1.0,fog );
+
 #if defined( MX2_SRGBOUTPUT )
-	gl_FragColor=vec4( pow( frag,vec3( 1.0/2.2 ) ),1.0 );
+	gl_FragColor=vec4( pow( frag,vec3( 1.0/2.2 ) ),alpha );
 #else
-	gl_FragColor=vec4( frag,1.0 );
+	gl_FragColor=vec4( frag,alpha );
 #endif
+
 }
 
 #else	//MX2_COLORPASS
 
 void main(){
 
-#if MX2_RENDERPASS==17
+#if MX2_POINT_LIGHT
 	gl_FragColor=FloatToRGBA( min( length( v_Position )/r_LightRange,1.0 ) );
 #elif defined( MX2_RGBADEPTHTEXTURES )
 	gl_FragColor=FloatToRGBA( gl_FragCoord.z ),1.0 );

+ 32 - 7
modules/mojo3d/assets/shaders/material-sprite.glsl

@@ -1,15 +1,30 @@
-//@renderpasses 0
+
+//@renderpasses 1,3
+
+//render uniforms
+
+uniform vec4 r_FogColor;
+
+uniform float r_FogNear;
+
+uniform float r_FogFar;
 
 //material uniforms
 
 uniform mat3 m_TextureMatrix;
 
-//renderer uniforms...
+//instance uniforms...
 
 uniform mat4 i_ModelViewProjectionMatrix;
 
+uniform mat4 i_ModelViewMatrix;
+
+uniform float i_Alpha;
+
 //varyings...
 
+varying vec3 v_Position;
+
 varying vec2 v_TexCoord0;
 
 //@vertex
@@ -20,6 +35,8 @@ attribute vec2 a_TexCoord0;
 
 void main(){
 
+	v_Position=(i_ModelViewMatrix * a_Position).xyz;
+
 	v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
 	
 	gl_Position=i_ModelViewProjectionMatrix * a_Position;
@@ -35,17 +52,25 @@ uniform float m_AlphaDiscard;
 
 void main(){
 
-	vec4 tcolor=texture2D( m_ColorTexture,v_TexCoord0 );
+	vec4 color=texture2D( m_ColorTexture,v_TexCoord0 );
 
-	float alpha=tcolor.a * m_ColorFactor.a;
+	float alpha=color.a * m_ColorFactor.a * i_Alpha;
 	
 	if( alpha<m_AlphaDiscard ) discard;
 
-	vec3 color=pow( tcolor.rgb,vec3( 2.2 ) ) * m_ColorFactor.rgb;
+	vec3 frag=pow( color.rgb,vec3( 2.2 ) ) * m_ColorFactor.rgb;
+	
+	float fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
+	
+	frag=mix( frag,r_FogColor.rgb,fog );
+	
+	alpha*=1.0-fog;
+	
+	frag*=alpha;
 	
 #if defined( MX2_SRGBOUTPUT )
-	gl_FragColor=vec4( pow( color,vec3( 1.0/2.2 ) ),alpha );
+	gl_FragColor=vec4( pow( frag,vec3( 1.0/2.2 ) ),alpha );
 #else
-	gl_FragColor=vec4( color * alpha,alpha );
+	gl_FragColor=vec4( frag,alpha );
 #endif
 }

+ 5 - 5
modules/mojo3d/assets/shaders/material-water.glsl

@@ -1,5 +1,4 @@
-
-//@renderpasses 0
+//@renderpasses 1
 
 //material uniforms
 
@@ -80,8 +79,10 @@ void main0( vec3 color,vec3 emissive,float metalness,float roughness,float occlu
 	vec3 ambdiff=diffuse * r_AmbientDiffuse.rgb;
 		
 	vec3 ambspec=env * fschlick;
-
-	gl_FragData[0]=vec4( min( (ambdiff+ambspec) * occlusion + emissive,8.0 ),1.0 );
+	
+	vec3 frag=(ambdiff+ambspec) * occlusion + emissive;
+	
+	gl_FragData[0]=vec4( min( frag,8.0 ),1.0 );
 	
 	gl_FragData[1]=vec4( color,metalness );
 	
@@ -121,5 +122,4 @@ void main(){
 	float occlusion=1.0;
 
 	main0( color,emissive,metalness,roughness,occlusion,normal );
-
 }

+ 38 - 0
modules/mojo3d/assets/shaders/skybox-deferred.glsl

@@ -0,0 +1,38 @@
+
+//@renderpasses 0
+
+varying vec2 v_ClipPosition;
+
+//@vertex
+
+attribute vec2 a_Position;	//0...1
+
+void main(){
+
+	v_ClipPosition=a_Position * 2.0 - 1.0;
+
+	gl_Position=vec4( v_ClipPosition,1.0,1.0 );
+}
+
+//@fragment
+
+uniform mat3 r_EnvMatrix;
+
+uniform samplerCube r_SkyTexture;
+
+uniform mat4 r_InverseProjectionMatrix;
+
+void main(){
+
+	vec4 clip=r_InverseProjectionMatrix * vec4( v_ClipPosition,1.0,1.0 );
+
+	vec3 tv=r_EnvMatrix * (clip.xyz/clip.w);
+	
+	vec3 frag=pow( textureCube( r_SkyTexture,tv ).rgb,vec3( 2.2 ) );
+	
+	gl_FragData[0]=vec4( frag,1.0 );					//accum
+	
+	gl_FragData[1]=vec4( 0.0,0.0,0.0,1.0 );				//color_m
+	
+	gl_FragData[2]=vec4( 0.5,0.5,1.0,1.0 );				//normal_r
+}