std.glsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // **** MX2_RENDERPASS *****
  2. //
  3. // PASSTYPE 'mask=3, 0=quad, 1=deferred, 2=forward, 3=shadow
  4. // LIGHTTYPE 'mask=12, 0=none, 1=directional, 2=point, 3=spot
  5. // SHADOWTYPE 'mask=16, 0=no shadows, 1=shadows
  6. //
  7. #ifndef MX2_RENDERPASS
  8. #define MX2_RENDERPASS 0
  9. #endif
  10. #ifndef MX2_FORWARDRENDERER
  11. #define MX2_FORWARDRENDERER 0
  12. #endif
  13. #ifndef MX2_DEFERREDRENDERER
  14. #define MX2_DEFERREDRENDERER 0
  15. #endif
  16. #ifndef MX2_SRGBOUTPUT
  17. #define MX2_SRGBOUTPUT 0
  18. #endif
  19. #define MX2_PASSTYPE ((MX2_RENDERPASS & 3))
  20. #define MX2_LIGHTTYPE ((MX2_RENDERPASS & 12)>>2)
  21. #define MX2_SHADOWTYPE ((MX2_RENDERPASS & 16)>>4)
  22. #define MX2_DIRECTIONALLIGHT (MX2_LIGHTTYPE==1)
  23. #define MX2_POINTLIGHT (MX2_LIGHTTYPE==2)
  24. #define MX2_SPOTLIGHT (MX2_LIGHTTYPE==3)
  25. #define MX2_QUADPASS (MX2_PASSTYPE==0)
  26. #define MX2_DEFERREDPASS (MX2_PASSTYPE==1)
  27. #define MX2_FORWARDPASS (MX2_PASSTYPE==2)
  28. #define MX2_SHADOWPASS (MX2_PASSTYPE==3)
  29. #define MX2_AMBIENTPASS (MX2_PASSTYPE==1 || MX2_PASSTYPE==2)
  30. #define MX2_LIGHTINGPASS (MX2_LIGHTTYPE!=0 && MX2_SHADOWPASS==0)
  31. #define MX2_COLORPASS (MX2_AMBIENTPASS || MX2_LIGHTINGPASS)
  32. // ***** MX2_ATTRIBMASK *****
  33. //
  34. // Position 1
  35. // Normal 2
  36. // Color 4
  37. // TexCoord0 8
  38. // TexCoord1 16
  39. // Tangent 32
  40. // Weights 64
  41. // Bones 128
  42. #ifndef MX2_ATTRIBMASK
  43. #define MX2_ATTRIBMASK 0
  44. #endif
  45. #define MX2_TEXTURED ((MX2_ATTRIBMASK & 24)!=0)
  46. #define MX2_BUMPMAPPED ((MX2_ATTRIBMASK & 32)==32)
  47. #define MX2_BONED ((MX2_ATTRIBMASK & 192)==192)
  48. //***** CONSTS *****
  49. //
  50. const float pi=3.1415926535897932384626433832795;
  51. //***** RENDER *****
  52. //
  53. uniform float r_Time;
  54. uniform vec4 r_AmbientDiffuse;
  55. uniform samplerCube r_SkyTextureCube;
  56. uniform sampler2D r_SkyTexture2D;
  57. uniform bool r_SkyCube;
  58. uniform vec4 r_SkyColor;
  59. uniform samplerCube r_EnvTextureCube;
  60. uniform sampler2D r_EnvTexture2D;
  61. uniform bool r_EnvCube;
  62. uniform float r_EnvTextureMaxLod;
  63. uniform vec4 r_EnvColor;
  64. uniform mat3 r_EnvMatrix;
  65. uniform float r_DepthNear;
  66. uniform float r_DepthFar;
  67. uniform float r_FogNear;
  68. uniform float r_FogFar;
  69. uniform vec4 r_FogColor;
  70. uniform mat4 r_InverseProjectionMatrix;
  71. uniform mat4 r_ProjectionMatrix;
  72. uniform mat4 r_ViewMatrix;
  73. // These only available in deferred renderer!
  74. uniform sampler2D r_AccumBuffer;
  75. uniform sampler2D r_ColorBuffer;
  76. uniform sampler2D r_NormalBuffer;
  77. uniform sampler2D r_DepthBuffer;
  78. uniform vec2 r_BufferCoordScale;
  79. uniform vec2 r_QuadCoordScale;
  80. uniform vec2 r_QuadCoordTrans;
  81. //***** LIGHTING *****
  82. //
  83. uniform mat4 r_LightViewMatrix;
  84. uniform mat4 r_InverseLightViewMatrix;
  85. uniform samplerCube r_LightCubeTexture;
  86. uniform sampler2D r_LightTexture;
  87. uniform vec4 r_LightColor;
  88. uniform float r_LightRange;
  89. uniform float r_LightInnerAngle;
  90. uniform float r_LightOuterAngle;
  91. //***** SHADOWS *****
  92. //
  93. uniform sampler2D r_ShadowCSMTexture;
  94. uniform samplerCube r_ShadowCubeTexture;
  95. uniform vec4 r_ShadowCSMSplits;
  96. uniform mat4 r_ShadowMatrix0;
  97. uniform mat4 r_ShadowMatrix1;
  98. uniform mat4 r_ShadowMatrix2;
  99. uniform mat4 r_ShadowMatrix3;
  100. uniform float r_ShadowAlpha;
  101. //***** INSTANCE *****
  102. //
  103. uniform mat4 i_ModelMatrix;
  104. uniform mat4 i_ModelViewMatrix;
  105. uniform mat4 i_ModelViewProjectionMatrix;
  106. uniform mat3 i_ModelViewNormalMatrix;
  107. uniform mat4 i_ModelBoneMatrices[96];
  108. uniform vec4 i_Color;
  109. uniform float i_Alpha;
  110. //***** MATERIAL *****
  111. //
  112. uniform mat3 m_TextureMatrix;
  113. //***** VARYINGS *****
  114. //
  115. varying vec2 v_ClipPosition;
  116. varying vec2 v_BufferCoords;
  117. varying vec3 v_Position;
  118. varying vec3 v_Normal;
  119. varying vec4 v_Color;
  120. varying vec2 v_TexCoord0;
  121. varying vec2 v_TexCoord1;
  122. varying mat3 v_TanMatrix;
  123. //@vertex
  124. //***** ATTRIBUTES *****
  125. //
  126. attribute vec4 a_Position; //mask=1
  127. attribute vec3 a_Normal; //mask=2
  128. attribute vec4 a_Color; //mask=4
  129. attribute vec2 a_TexCoord0; //mask=8
  130. attribute vec2 a_TexCoord1; //mask=16
  131. attribute vec4 a_Tangent; //mask=32
  132. attribute vec4 a_Weights; //mask=64
  133. attribute vec4 a_Bones; //mask=128
  134. void transformLightQuadVertex(){
  135. //Careful! Bizarro angle/d3d bug...
  136. v_ClipPosition=a_Position.xy * r_QuadCoordScale + r_QuadCoordTrans;
  137. v_BufferCoords=v_ClipPosition * r_BufferCoordScale;
  138. gl_Position=vec4( v_ClipPosition * 2.0 - 1.0,-1.0,1.0 );
  139. }
  140. void transformQuadVertex(){
  141. v_ClipPosition=a_Position.xy;
  142. v_BufferCoords=v_ClipPosition.xy * r_BufferCoordScale;
  143. gl_Position=vec4( v_ClipPosition.xy * 2.0 - 1.0,-1.0,1.0 );
  144. }
  145. void transformSpriteVertex(){
  146. v_Position=(i_ModelViewMatrix * a_Position).xyz;
  147. v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
  148. v_Color=a_Color;
  149. gl_Position=i_ModelViewProjectionMatrix * a_Position;
  150. }
  151. void transformVertex(){
  152. #if MX2_BONED
  153. mat4 m0=i_ModelBoneMatrices[ int( a_Bones.x ) ];
  154. mat4 m1=i_ModelBoneMatrices[ int( a_Bones.y ) ];
  155. mat4 m2=i_ModelBoneMatrices[ int( a_Bones.z ) ];
  156. mat4 m3=i_ModelBoneMatrices[ int( a_Bones.a ) ];
  157. vec4 position=
  158. m0 * a_Position * a_Weights.x +
  159. m1 * a_Position * a_Weights.y +
  160. m2 * a_Position * a_Weights.z +
  161. m3 * a_Position * a_Weights.a;
  162. #if MX2_COLORPASS
  163. mat3 n0=mat3( m0[0].xyz,m0[1].xyz,m0[2].xyz );
  164. mat3 n1=mat3( m1[0].xyz,m1[1].xyz,m1[2].xyz );
  165. mat3 n2=mat3( m2[0].xyz,m2[1].xyz,m2[2].xyz );
  166. mat3 n3=mat3( m3[0].xyz,m3[1].xyz,m3[2].xyz );
  167. vec3 normal=normalize(
  168. n0 * a_Normal * a_Weights.x +
  169. n1 * a_Normal * a_Weights.y +
  170. n2 * a_Normal * a_Weights.z +
  171. n3 * a_Normal * a_Weights.a );
  172. #if MX2_BUMPMAPPED
  173. vec4 tangent=vec4( normalize(
  174. n0 * a_Tangent.xyz * a_Weights.x +
  175. n1 * a_Tangent.xyz * a_Weights.y +
  176. n2 * a_Tangent.xyz * a_Weights.z +
  177. n3 * a_Tangent.xyz * a_Weights.a ),a_Tangent.w );
  178. #endif
  179. #endif
  180. #else //MX2_BONED
  181. vec4 position=a_Position;
  182. #if MX2_COLORPASS
  183. vec3 normal=a_Normal;
  184. #if MX2_BUMPMAPPED
  185. vec4 tangent=a_Tangent;
  186. #endif
  187. #endif
  188. #endif
  189. // view space position
  190. v_Position=( i_ModelViewMatrix * position ).xyz;
  191. #if MX2_COLORPASS
  192. // viewspace normal
  193. v_Normal=i_ModelViewNormalMatrix * normal;
  194. // vertex color
  195. v_Color=a_Color * i_Color;
  196. v_Color.a*=i_Alpha;
  197. #if MX2_TEXTURED
  198. // texture coord0
  199. v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
  200. v_TexCoord1=a_TexCoord1;//(m_TextureMatrix * vec3(a_TexCoord1,1.0)).st;
  201. #if MX2_BUMPMAPPED
  202. // viewspace tangent matrix
  203. v_TanMatrix[2]=normalize( v_Normal );
  204. v_TanMatrix[0]=normalize( i_ModelViewNormalMatrix * tangent.xyz );
  205. v_TanMatrix[1]=cross( v_TanMatrix[0],v_TanMatrix[2] ) * tangent.a;
  206. #endif
  207. #endif
  208. #endif //MX2_COLORPASS
  209. gl_Position=i_ModelViewProjectionMatrix * position;
  210. }
  211. //@fragment
  212. vec4 FloatToRGBA( float value ){
  213. const float MaxFloat=0.9999999;
  214. value=clamp( value,0.0,MaxFloat );
  215. vec4 rgba=fract( vec4( 1.0, 255.0, 65025.0, 16581375.0 ) * value );
  216. return rgba-rgba.yzww * vec4( 1.0/255.0, 1.0/255.0, 1.0/255.0, 0.0 );
  217. }
  218. float RGBAToFloat( vec4 rgba ){
  219. return dot( rgba,vec4( 1.0, 1.0/255.0, 1.0/65025.0, 1.0/16581375.0 ) );
  220. }
  221. float viewDepth( float depthBufferDepth ){
  222. return r_DepthFar * r_DepthNear / ( r_DepthFar + depthBufferDepth * ( r_DepthNear - r_DepthFar ) );
  223. }
  224. #if MX2_QUADPASS
  225. vec3 fragmentPosition(){
  226. float depth=viewDepth( texture2D( r_DepthBuffer,v_BufferCoords ).r );
  227. vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition*2.0-1.0,-1.0,1.0 );
  228. vec3 vpos=vpos4.xyz/vpos4.w;
  229. //debug z coord...
  230. //
  231. if( abs( vpos.z-r_DepthNear)>0.00001 ){
  232. // gl_FragColor=vec4( 1.0,0.0,0.0,1.0 );
  233. // return vec3( 0.0 );
  234. }
  235. vec3 position=vpos/vpos.z*depth;
  236. return position;
  237. }
  238. #else
  239. vec3 fragmentPosition(){
  240. return v_Position;
  241. }
  242. #endif
  243. #if MX2_LIGHTINGPASS && MX2_SHADOWTYPE
  244. float shadowColor( vec3 position ){
  245. #if MX2_DIRECTIONALLIGHT
  246. if( position.z>=r_ShadowCSMSplits.w ) return 1.0;
  247. vec4 vpos=vec4( position,1.0 );
  248. vec2 off;
  249. if( vpos.z<r_ShadowCSMSplits.x ){
  250. vpos=r_ShadowMatrix0 * vpos;
  251. off=vec2( 0.0,0.0 );
  252. }else if( vpos.z<r_ShadowCSMSplits.y ){
  253. vpos=r_ShadowMatrix1 * vpos;
  254. off=vec2( 0.5,0.0 );
  255. }else if( vpos.z<r_ShadowCSMSplits.z ){
  256. vpos=r_ShadowMatrix2 * vpos;
  257. off=vec2( 0.0,0.5 );
  258. }else{
  259. vpos=r_ShadowMatrix3 * vpos;
  260. off=vec2( 0.5,0.5 );
  261. }
  262. vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
  263. float d=texture2D( r_ShadowCSMTexture,spos.xy+off ).r;
  264. if( spos.z>d ) return 1.0-r_ShadowAlpha;
  265. return 1.0;
  266. #elif MX2_POINTLIGHT
  267. vec4 vpos=vec4( position,1.0 );
  268. vec3 lpos=(r_ShadowMatrix0 * vpos).xyz;
  269. float d=RGBAToFloat( textureCube( r_ShadowCubeTexture,lpos ) );
  270. if( length(lpos) > d * r_LightRange ) return 1.0-r_ShadowAlpha;
  271. return 1.0;
  272. #elif MX2_SPOTLIGHT
  273. vec4 vpos=r_ShadowMatrix0 * vec4( position,1.0 );
  274. vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
  275. float d=texture2D( r_ShadowCSMTexture,spos.xy ).r;
  276. if( spos.z>d ) return 1.0-r_ShadowAlpha;
  277. return 1.0;
  278. #endif
  279. }
  280. #endif
  281. /*
  282. float mipmapLod( vec2 tc ){
  283. vec2 dx=dFdx( tc );
  284. vec2 dy=dFdy( tc );
  285. float dsqr=max( dot( dx,dx ),dot( dy,dy ) );
  286. float lod=log2( dsqr ) * 0.5;
  287. return max( lod,0.0 );
  288. }
  289. float mipmapLodCube( vec3 tv ){
  290. vec2 tc;
  291. vec3 at=abs( tv );
  292. if( at.x>at.y && at.x>at.z ){
  293. tc=vec2( tv.y,tv.z )/tv.x;
  294. }else if( at.y>at.z ){
  295. tc=vec2( tv.x,tv.z )/tv.y;
  296. }else{
  297. tc=vec2( tv.x,tv.y )/tv.z;
  298. }
  299. return mipmapLod( (tc+1.0)*0.5 );
  300. }
  301. */
  302. vec3 sampleEnv( vec3 viewVec,float roughness ){
  303. vec3 tv=r_EnvMatrix * viewVec;
  304. if( r_EnvCube ){
  305. //#ifdef GL_ES
  306. float lod=textureCube( r_EnvTextureCube,tv ).a * 255.0;
  307. if( lod==0.0 ) lod=textureCube( r_EnvTextureCube,tv,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
  308. return pow( textureCube( r_EnvTextureCube,tv,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  309. //#else
  310. // return pow( textureCube( r_EnvTextureCube,tv,roughness*r_EnvTextureMaxLod ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  311. //#endif
  312. }else{
  313. float p=-atan( tv.y,sqrt( tv.x*tv.x+tv.z*tv.z ) ) / pi + 0.5;
  314. float y=atan( tv.x,tv.z ) / pi * 0.5 + 0.5;
  315. vec2 tc=vec2( y,p );
  316. //#ifdef GL_ES
  317. float lod=texture2D( r_EnvTexture2D,tc ).a * 255.0;
  318. if( lod==0.0 ) lod=texture2D( r_EnvTexture2D,tc,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
  319. return pow( texture2D( r_EnvTexture2D,tc,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  320. //else
  321. // return pow( texture2DLod( r_EnvTexture2D,tc,max( mipmapLod( tc ),roughness*r_EnvTextureMaxLod ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  322. //#endif
  323. }
  324. }
  325. #if MX2_FORWARDRENDERER || MX2_FORWARDPASS
  326. void emitLinearFragment( vec4 color ){
  327. #if MX2_SRGBOUTPUT
  328. gl_FragColor=vec4( pow( color.rgb*color.a,vec3( 1.0/2.2 ) ),color.a );
  329. #else
  330. gl_FragColor=vec4( color.rgb*color.a,color.a );
  331. #endif
  332. }
  333. void emitColorFragment( vec4 color ){
  334. float fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
  335. color.rgb=mix( color.rgb,r_FogColor.rgb,fog );
  336. emitLinearFragment( color );
  337. }
  338. #endif
  339. #if MX2_SHADOWPASS
  340. void emitShadowFragment(){
  341. #if MX2_DIRECTIONALLIGHT || MX2_SPOTLIGHT
  342. gl_FragColor=vec4( vec3( gl_FragCoord.z ),1.0 );
  343. #elif MX2_POINTLIGHT
  344. gl_FragColor=FloatToRGBA( min( length( v_Position )/r_LightRange,1.0 ) );
  345. #endif
  346. }
  347. #endif