std.glsl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #define MX2_PASSTYPE ((MX2_RENDERPASS & 3))
  11. #define MX2_LIGHTTYPE ((MX2_RENDERPASS & 12)>>2)
  12. #define MX2_SHADOWTYPE ((MX2_RENDERPASS & 16)>>4)
  13. #define MX2_DIRECTIONALLIGHT (MX2_LIGHTTYPE==1)
  14. #define MX2_POINTLIGHT (MX2_LIGHTTYPE==2)
  15. #define MX2_SPOTLIGHT (MX2_LIGHTTYPE==3)
  16. #define MX2_QUADPASS (MX2_PASSTYPE==0)
  17. #define MX2_DEFERREDPASS (MX2_PASSTYPE==1)
  18. #define MX2_FORWARDPASS (MX2_PASSTYPE==2)
  19. #define MX2_SHADOWPASS (MX2_PASSTYPE==3)
  20. #define MX2_AMBIENTPASS (MX2_PASSTYPE==1 || MX2_PASSTYPE==2)
  21. #define MX2_LIGHTINGPASS (MX2_LIGHTTYPE!=0 && MX2_SHADOWPASS==0)
  22. #define MX2_COLORPASS (MX2_AMBIENTPASS || MX2_LIGHTINGPASS)
  23. // ***** MX2_ATTRIBMASK *****
  24. //
  25. // Position 1
  26. // Normal 2
  27. // Color 4
  28. // TexCoord0 8
  29. // TexCoord1 16
  30. // Tangent 32
  31. // Weights 64
  32. // Bones 128
  33. #ifndef MX2_ATTRIBMASK
  34. #define MX2_ATTRIBMASK 0
  35. #endif
  36. #define MX2_TEXTURED ((MX2_ATTRIBMASK & 24)!=0)
  37. #define MX2_BUMPMAPPED ((MX2_ATTRIBMASK & 32)==32)
  38. #define MX2_BONED ((MX2_ATTRIBMASK & 192)==192)
  39. //***** RENDER *****
  40. //
  41. uniform float r_Time;
  42. uniform vec4 r_AmbientDiffuse;
  43. uniform samplerCube r_SkyTexture;
  44. uniform samplerCube r_EnvTexture;
  45. uniform float r_EnvTextureMaxLod;
  46. uniform vec4 r_EnvColor;
  47. uniform mat3 r_EnvMatrix;
  48. uniform float r_DepthNear;
  49. uniform float r_DepthFar;
  50. uniform float r_FogNear;
  51. uniform float r_FogFar;
  52. uniform vec4 r_FogColor;
  53. uniform mat4 r_InverseProjectionMatrix;
  54. uniform mat4 r_ProjectionMatrix;
  55. uniform mat4 r_ViewMatrix;
  56. // These available in deferred renderer!
  57. uniform sampler2D r_AccumBuffer;
  58. uniform sampler2D r_ColorBuffer;
  59. uniform sampler2D r_NormalBuffer;
  60. uniform sampler2D r_DepthBuffer;
  61. uniform vec2 r_BufferCoordScale;
  62. //***** LIGHTING *****
  63. //
  64. uniform mat4 r_LightViewMatrix;
  65. uniform mat4 r_InverseLightViewMatrix;
  66. uniform samplerCube r_LightCubeTexture;
  67. uniform sampler2D r_LightTexture;
  68. uniform vec4 r_LightColor;
  69. uniform float r_LightRange;
  70. uniform float r_LightInnerAngle;
  71. uniform float r_LightOuterAngle;
  72. //***** SHADOWS *****
  73. //
  74. uniform sampler2D r_ShadowCSMTexture;
  75. uniform samplerCube r_ShadowCubeTexture;
  76. uniform vec4 r_ShadowCSMSplits;
  77. uniform mat4 r_ShadowMatrix0;
  78. uniform mat4 r_ShadowMatrix1;
  79. uniform mat4 r_ShadowMatrix2;
  80. uniform mat4 r_ShadowMatrix3;
  81. uniform float r_ShadowAlpha;
  82. //***** INSTANCE *****
  83. //
  84. uniform mat4 i_ModelMatrix;
  85. uniform mat4 i_ModelViewMatrix;
  86. uniform mat4 i_ModelViewProjectionMatrix;
  87. uniform mat3 i_ModelViewNormalMatrix;
  88. uniform mat4 i_ModelBoneMatrices[96];
  89. uniform vec4 i_Color;
  90. uniform float i_Alpha;
  91. //***** MATERIAL *****
  92. //
  93. uniform mat3 m_TextureMatrix;
  94. //***** VARYINGS *****
  95. //
  96. varying vec2 v_ClipPosition;
  97. varying vec2 v_BufferCoords;
  98. varying vec3 v_Position;
  99. varying vec3 v_Normal;
  100. varying vec4 v_Color;
  101. varying vec2 v_TexCoord0;
  102. varying vec2 v_TexCoord1;
  103. varying mat3 v_TanMatrix;
  104. //@vertex
  105. //***** ATTRIBUTES *****
  106. //
  107. attribute vec4 a_Position; //mask=1
  108. attribute vec3 a_Normal; //mask=2
  109. attribute vec4 a_Color; //mask=4
  110. attribute vec2 a_TexCoord0; //mask=8
  111. attribute vec2 a_TexCoord1; //mask=16
  112. attribute vec4 a_Tangent; //mask=32
  113. attribute vec4 a_Weights; //mask=64
  114. attribute vec4 a_Bones; //mask=128
  115. void transformQuadVertex(){
  116. v_ClipPosition=a_Position.xy * 2.0 - 1.0;
  117. v_BufferCoords=a_Position.xy * r_BufferCoordScale;
  118. gl_Position=vec4( v_ClipPosition,-1.0,1.0 );
  119. }
  120. void transformSpriteVertex(){
  121. v_Position=(i_ModelViewMatrix * a_Position).xyz;
  122. v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
  123. gl_Position=i_ModelViewProjectionMatrix * a_Position;
  124. }
  125. void transformVertex(){
  126. #if MX2_BONED
  127. mat4 m0=i_ModelBoneMatrices[ int( a_Bones.x ) ];
  128. mat4 m1=i_ModelBoneMatrices[ int( a_Bones.y ) ];
  129. mat4 m2=i_ModelBoneMatrices[ int( a_Bones.z ) ];
  130. mat4 m3=i_ModelBoneMatrices[ int( a_Bones.a ) ];
  131. vec4 position=
  132. m0 * a_Position * a_Weights.x +
  133. m1 * a_Position * a_Weights.y +
  134. m2 * a_Position * a_Weights.z +
  135. m3 * a_Position * a_Weights.a;
  136. #if MX2_COLORPASS
  137. mat3 n0=mat3( m0[0].xyz,m0[1].xyz,m0[2].xyz );
  138. mat3 n1=mat3( m1[0].xyz,m1[1].xyz,m1[2].xyz );
  139. mat3 n2=mat3( m2[0].xyz,m2[1].xyz,m2[2].xyz );
  140. mat3 n3=mat3( m3[0].xyz,m3[1].xyz,m3[2].xyz );
  141. vec3 normal=normalize(
  142. n0 * a_Normal * a_Weights.x +
  143. n1 * a_Normal * a_Weights.y +
  144. n2 * a_Normal * a_Weights.z +
  145. n3 * a_Normal * a_Weights.a );
  146. #if MX2_BUMPMAPPED
  147. vec4 tangent=vec4( normalize(
  148. n0 * a_Tangent.xyz * a_Weights.x +
  149. n1 * a_Tangent.xyz * a_Weights.y +
  150. n2 * a_Tangent.xyz * a_Weights.z +
  151. n3 * a_Tangent.xyz * a_Weights.a ),a_Tangent.w );
  152. #endif
  153. #endif
  154. #else //MX2_BONED
  155. vec4 position=a_Position;
  156. #if MX2_COLORPASS
  157. vec3 normal=a_Normal;
  158. #if MX2_BUMPMAPPED
  159. vec4 tangent=a_Tangent;
  160. #endif
  161. #endif
  162. #endif
  163. // view space position
  164. v_Position=( i_ModelViewMatrix * position ).xyz;
  165. #if MX2_COLORPASS
  166. // viewspace normal
  167. v_Normal=i_ModelViewNormalMatrix * normal;
  168. // vertex color
  169. v_Color=a_Color * i_Color;
  170. v_Color.a*=i_Alpha;
  171. #if MX2_TEXTURED
  172. // texture coord0
  173. v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
  174. v_TexCoord1=a_TexCoord1;//(m_TextureMatrix * vec3(a_TexCoord1,1.0)).st;
  175. #if MX2_BUMPMAPPED
  176. // viewspace tangent matrix
  177. v_TanMatrix[2]=normalize( v_Normal );
  178. v_TanMatrix[0]=normalize( i_ModelViewNormalMatrix * tangent.xyz );
  179. v_TanMatrix[1]=cross( v_TanMatrix[0],v_TanMatrix[2] ) * tangent.a;
  180. #endif
  181. #endif
  182. #endif //MX2_COLORPASS
  183. gl_Position=i_ModelViewProjectionMatrix * position;
  184. }
  185. //@fragment
  186. vec4 FloatToRGBA( float value ){
  187. const float MaxFloat=0.9999999;
  188. value=clamp( value,0.0,MaxFloat );
  189. vec4 rgba=fract( vec4( 1.0, 255.0, 65025.0, 16581375.0 ) * value );
  190. return rgba-rgba.yzww * vec4( 1.0/255.0, 1.0/255.0, 1.0/255.0, 0.0 );
  191. }
  192. float RGBAToFloat( vec4 rgba ){
  193. return dot( rgba,vec4( 1.0, 1.0/255.0, 1.0/65025.0, 1.0/16581375.0 ) );
  194. }
  195. float viewDepth( float depthBufferDepth ){
  196. return r_DepthFar * r_DepthNear / ( r_DepthFar + depthBufferDepth * ( r_DepthNear - r_DepthFar ) );
  197. }
  198. #if MX2_QUADPASS
  199. vec3 fragmentPosition(){
  200. float depth=viewDepth( texture2D( r_DepthBuffer,v_BufferCoords ).r );
  201. vec4 vpos4=r_InverseProjectionMatrix * vec4( v_ClipPosition,-1.0,1.0 );
  202. vec3 vpos=vpos4.xyz/vpos4.w;
  203. //debug z coord...
  204. //
  205. if( abs( vpos.z-r_DepthNear)>0.00001 ){
  206. // gl_FragColor=vec4( 1.0,0.0,0.0,1.0 );
  207. // return vec3( 0.0 );
  208. }
  209. vec3 position=vpos/vpos.z*depth;
  210. return position;
  211. }
  212. #else
  213. vec3 fragmentPosition(){
  214. return v_Position;
  215. }
  216. #endif
  217. #if MX2_LIGHTINGPASS && MX2_SHADOWTYPE
  218. float shadowColor( vec3 position ){
  219. #if MX2_DIRECTIONALLIGHT
  220. if( position.z>=r_ShadowCSMSplits.w ) return 1.0;
  221. vec4 vpos=vec4( position,1.0 );
  222. vec2 off;
  223. if( vpos.z<r_ShadowCSMSplits.x ){
  224. vpos=r_ShadowMatrix0 * vpos;
  225. off=vec2( 0.0,0.0 );
  226. }else if( vpos.z<r_ShadowCSMSplits.y ){
  227. vpos=r_ShadowMatrix1 * vpos;
  228. off=vec2( 0.5,0.0 );
  229. }else if( vpos.z<r_ShadowCSMSplits.z ){
  230. vpos=r_ShadowMatrix2 * vpos;
  231. off=vec2( 0.0,0.5 );
  232. }else{
  233. vpos=r_ShadowMatrix3 * vpos;
  234. off=vec2( 0.5,0.5 );
  235. }
  236. vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
  237. float d=texture2D( r_ShadowCSMTexture,spos.xy+off ).r;
  238. if( spos.z>d ) return 1.0-r_ShadowAlpha;
  239. return 1.0;
  240. #elif MX2_POINTLIGHT
  241. vec4 vpos=vec4( position,1.0 );
  242. vec3 lpos=(r_ShadowMatrix0 * vpos).xyz;
  243. float d=RGBAToFloat( textureCube( r_ShadowCubeTexture,lpos ) );
  244. if( length(lpos) > d * r_LightRange ) return 1.0-r_ShadowAlpha;
  245. return 1.0;
  246. #elif MX2_SPOTLIGHT
  247. vec4 vpos=r_ShadowMatrix0 * vec4( position,1.0 );
  248. vec3 spos=vpos.xyz/vpos.w * vec3( 0.25,0.25,0.5 ) + vec3( 0.25,0.25,0.5 );
  249. float d=texture2D( r_ShadowCSMTexture,spos.xy ).r;
  250. if( spos.z>d ) return 1.0-r_ShadowAlpha;
  251. return 1.0;
  252. #endif
  253. }
  254. #endif
  255. #if MX2_FORWARDPASS
  256. void emitLinearFragment( vec4 color ){
  257. #if defined( MX2_SRGBOUTPUT )
  258. gl_FragColor=vec4( pow( color.rgb*color.a,vec3( 1.0/2.2 ) ),color.a );
  259. #else
  260. gl_FragColor=vec4( color.rgb*color.a,color.a );
  261. #endif
  262. }
  263. void emitColorFragment( vec4 color ){
  264. float fog=clamp( (length( v_Position )-r_FogNear)/(r_FogFar-r_FogNear),0.0,1.0 ) * r_FogColor.a;
  265. color.rgb=mix( color.rgb,r_FogColor.rgb,fog );
  266. emitLinearFragment( color );
  267. }
  268. #endif
  269. #if MX2_SHADOWPASS
  270. void emitShadowFragment(){
  271. #if MX2_DIRECTIONALLIGHT || MX2_SPOTLIGHT
  272. gl_FragColor=vec4( vec3( gl_FragCoord.z ),1.0 );
  273. #elif MX2_POINTLIGHT
  274. gl_FragColor=FloatToRGBA( min( length( v_Position )/r_LightRange,1.0 ) );
  275. #endif
  276. }
  277. #endif