|
@@ -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 );
|