std.glsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. gl_Position=i_ModelViewProjectionMatrix * a_Position;
  149. }
  150. void transformVertex(){
  151. #if MX2_BONED
  152. mat4 m0=i_ModelBoneMatrices[ int( a_Bones.x ) ];
  153. mat4 m1=i_ModelBoneMatrices[ int( a_Bones.y ) ];
  154. mat4 m2=i_ModelBoneMatrices[ int( a_Bones.z ) ];
  155. mat4 m3=i_ModelBoneMatrices[ int( a_Bones.a ) ];
  156. vec4 position=
  157. m0 * a_Position * a_Weights.x +
  158. m1 * a_Position * a_Weights.y +
  159. m2 * a_Position * a_Weights.z +
  160. m3 * a_Position * a_Weights.a;
  161. #if MX2_COLORPASS
  162. mat3 n0=mat3( m0[0].xyz,m0[1].xyz,m0[2].xyz );
  163. mat3 n1=mat3( m1[0].xyz,m1[1].xyz,m1[2].xyz );
  164. mat3 n2=mat3( m2[0].xyz,m2[1].xyz,m2[2].xyz );
  165. mat3 n3=mat3( m3[0].xyz,m3[1].xyz,m3[2].xyz );
  166. vec3 normal=normalize(
  167. n0 * a_Normal * a_Weights.x +
  168. n1 * a_Normal * a_Weights.y +
  169. n2 * a_Normal * a_Weights.z +
  170. n3 * a_Normal * a_Weights.a );
  171. #if MX2_BUMPMAPPED
  172. vec4 tangent=vec4( normalize(
  173. n0 * a_Tangent.xyz * a_Weights.x +
  174. n1 * a_Tangent.xyz * a_Weights.y +
  175. n2 * a_Tangent.xyz * a_Weights.z +
  176. n3 * a_Tangent.xyz * a_Weights.a ),a_Tangent.w );
  177. #endif
  178. #endif
  179. #else //MX2_BONED
  180. vec4 position=a_Position;
  181. #if MX2_COLORPASS
  182. vec3 normal=a_Normal;
  183. #if MX2_BUMPMAPPED
  184. vec4 tangent=a_Tangent;
  185. #endif
  186. #endif
  187. #endif
  188. // view space position
  189. v_Position=( i_ModelViewMatrix * position ).xyz;
  190. #if MX2_COLORPASS
  191. // viewspace normal
  192. v_Normal=i_ModelViewNormalMatrix * normal;
  193. // vertex color
  194. v_Color=a_Color * i_Color;
  195. v_Color.a*=i_Alpha;
  196. #if MX2_TEXTURED
  197. // texture coord0
  198. v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
  199. v_TexCoord1=a_TexCoord1;//(m_TextureMatrix * vec3(a_TexCoord1,1.0)).st;
  200. #if MX2_BUMPMAPPED
  201. // viewspace tangent matrix
  202. v_TanMatrix[2]=normalize( v_Normal );
  203. v_TanMatrix[0]=normalize( i_ModelViewNormalMatrix * tangent.xyz );
  204. v_TanMatrix[1]=cross( v_TanMatrix[0],v_TanMatrix[2] ) * tangent.a;
  205. #endif
  206. #endif
  207. #endif //MX2_COLORPASS
  208. gl_Position=i_ModelViewProjectionMatrix * position;
  209. }
  210. //@fragment
  211. vec4 FloatToRGBA( float value ){
  212. const float MaxFloat=0.9999999;
  213. value=clamp( value,0.0,MaxFloat );
  214. vec4 rgba=fract( vec4( 1.0, 255.0, 65025.0, 16581375.0 ) * value );
  215. return rgba-rgba.yzww * vec4( 1.0/255.0, 1.0/255.0, 1.0/255.0, 0.0 );
  216. }
  217. float RGBAToFloat( vec4 rgba ){
  218. return dot( rgba,vec4( 1.0, 1.0/255.0, 1.0/65025.0, 1.0/16581375.0 ) );
  219. }
  220. float viewDepth( float depthBufferDepth ){
  221. return r_DepthFar * r_DepthNear / ( r_DepthFar + depthBufferDepth * ( r_DepthNear - r_DepthFar ) );
  222. }
  223. #if MX2_QUADPASS
  224. vec3 fragmentPosition(){
  225. float depth=viewDepth( texture2D( r_DepthBuffer,v_BufferCoords ).r );
  226. vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition*2.0-1.0,-1.0,1.0 );
  227. vec3 vpos=vpos4.xyz/vpos4.w;
  228. //debug z coord...
  229. //
  230. if( abs( vpos.z-r_DepthNear)>0.00001 ){
  231. // gl_FragColor=vec4( 1.0,0.0,0.0,1.0 );
  232. // return vec3( 0.0 );
  233. }
  234. vec3 position=vpos/vpos.z*depth;
  235. return position;
  236. }
  237. #else
  238. vec3 fragmentPosition(){
  239. return v_Position;
  240. }
  241. #endif
  242. #if MX2_LIGHTINGPASS && MX2_SHADOWTYPE
  243. float shadowColor( vec3 position ){
  244. #if MX2_DIRECTIONALLIGHT
  245. if( position.z>=r_ShadowCSMSplits.w ) return 1.0;
  246. vec4 vpos=vec4( position,1.0 );
  247. vec2 off;
  248. if( vpos.z<r_ShadowCSMSplits.x ){
  249. vpos=r_ShadowMatrix0 * vpos;
  250. off=vec2( 0.0,0.0 );
  251. }else if( vpos.z<r_ShadowCSMSplits.y ){
  252. vpos=r_ShadowMatrix1 * vpos;
  253. off=vec2( 0.5,0.0 );
  254. }else if( vpos.z<r_ShadowCSMSplits.z ){
  255. vpos=r_ShadowMatrix2 * vpos;
  256. off=vec2( 0.0,0.5 );
  257. }else{
  258. vpos=r_ShadowMatrix3 * vpos;
  259. off=vec2( 0.5,0.5 );
  260. }
  261. vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
  262. float d=texture2D( r_ShadowCSMTexture,spos.xy+off ).r;
  263. if( spos.z>d ) return 1.0-r_ShadowAlpha;
  264. return 1.0;
  265. #elif MX2_POINTLIGHT
  266. vec4 vpos=vec4( position,1.0 );
  267. vec3 lpos=(r_ShadowMatrix0 * vpos).xyz;
  268. float d=RGBAToFloat( textureCube( r_ShadowCubeTexture,lpos ) );
  269. if( length(lpos) > d * r_LightRange ) return 1.0-r_ShadowAlpha;
  270. return 1.0;
  271. #elif MX2_SPOTLIGHT
  272. vec4 vpos=r_ShadowMatrix0 * vec4( position,1.0 );
  273. vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
  274. float d=texture2D( r_ShadowCSMTexture,spos.xy ).r;
  275. if( spos.z>d ) return 1.0-r_ShadowAlpha;
  276. return 1.0;
  277. #endif
  278. }
  279. #endif
  280. /*
  281. float mipmapLod( vec2 tc ){
  282. vec2 dx=dFdx( tc );
  283. vec2 dy=dFdy( tc );
  284. float dsqr=max( dot( dx,dx ),dot( dy,dy ) );
  285. float lod=log2( dsqr ) * 0.5;
  286. return max( lod,0.0 );
  287. }
  288. float mipmapLodCube( vec3 tv ){
  289. vec2 tc;
  290. vec3 at=abs( tv );
  291. if( at.x>at.y && at.x>at.z ){
  292. tc=vec2( tv.y,tv.z )/tv.x;
  293. }else if( at.y>at.z ){
  294. tc=vec2( tv.x,tv.z )/tv.y;
  295. }else{
  296. tc=vec2( tv.x,tv.y )/tv.z;
  297. }
  298. return mipmapLod( (tc+1.0)*0.5 );
  299. }
  300. */
  301. vec3 sampleEnv( vec3 viewVec,float roughness ){
  302. vec3 tv=r_EnvMatrix * viewVec;
  303. if( r_EnvCube ){
  304. //#ifdef GL_ES
  305. float lod=textureCube( r_EnvTextureCube,tv ).a * 255.0;
  306. if( lod==0.0 ) lod=textureCube( r_EnvTextureCube,tv,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
  307. return pow( textureCube( r_EnvTextureCube,tv,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  308. //#else
  309. // return pow( textureCube( r_EnvTextureCube,tv,roughness*r_EnvTextureMaxLod ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  310. //#endif
  311. }else{
  312. float p=-atan( tv.y,sqrt( tv.x*tv.x+tv.z*tv.z ) ) / pi + 0.5;
  313. float y=atan( tv.x,tv.z ) / pi * 0.5 + 0.5;
  314. vec2 tc=vec2( y,p );
  315. //#ifdef GL_ES
  316. float lod=texture2D( r_EnvTexture2D,tc ).a * 255.0;
  317. if( lod==0.0 ) lod=texture2D( r_EnvTexture2D,tc,r_EnvTextureMaxLod ).a * 255.0 - r_EnvTextureMaxLod;
  318. return pow( texture2D( r_EnvTexture2D,tc,max( roughness*r_EnvTextureMaxLod-lod,0.0 ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  319. //else
  320. // return pow( texture2DLod( r_EnvTexture2D,tc,max( mipmapLod( tc ),roughness*r_EnvTextureMaxLod ) ).rgb,vec3( 2.2 ) ) * r_EnvColor.rgb;
  321. //#endif
  322. }
  323. }
  324. #if MX2_FORWARDRENDERER || MX2_FORWARDPASS
  325. void emitLinearFragment( vec4 color ){
  326. #if MX2_SRGBOUTPUT
  327. gl_FragColor=vec4( pow( color.rgb*color.a,vec3( 1.0/2.2 ) ),color.a );
  328. #else
  329. gl_FragColor=vec4( color.rgb*color.a,color.a );
  330. #endif
  331. }
  332. void emitColorFragment( vec4 color ){
  333. float fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
  334. color.rgb=mix( color.rgb,r_FogColor.rgb,fog );
  335. emitLinearFragment( color );
  336. }
  337. #endif
  338. #if MX2_SHADOWPASS
  339. void emitShadowFragment(){
  340. #if MX2_DIRECTIONALLIGHT || MX2_SPOTLIGHT
  341. gl_FragColor=vec4( vec3( gl_FragCoord.z ),1.0 );
  342. #elif MX2_POINTLIGHT
  343. gl_FragColor=FloatToRGBA( min( length( v_Position )/r_LightRange,1.0 ) );
  344. #endif
  345. }
  346. #endif