mojo2_program.glsl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //***** Mojo2 Mega Shader *****
  2. //***** USER OPTIONS ******
  3. //enable less accurate but faster rendering - make sure yer normals are right when enabling this!
  4. #define OPT_FAST 0
  5. //Apply cheesy_hdr to lighting - slower, but nice for colored lights as they saturate to white
  6. #define CHEESY_HDR 1
  7. //enable bump tangents, for correct bump mapping of rotating drawops - can only really handle uniform scaling/rotation, but will probably look OK otherwise...
  8. #define BUMP_TANGENTS 1
  9. //make sure shadows are working!
  10. #define DEBUG_SHADOWS 0
  11. //enable ortho lighting - faster + makes lights easier to position
  12. #define ORTHO_LIGHTING 1
  13. //enable fresnel effect - slower
  14. #define FRESNEL_EFFECT 1
  15. //enable gamma correction - slower. Don't use yet! Messes up with >4 lights...
  16. #define GAMMA_CORRECTION 0
  17. //***** ENABLED VARS *****
  18. #if NUM_LIGHTS && !defined(B3D_CLIPPOSITION)
  19. #define B3D_CLIPPOSITION 1
  20. #endif
  21. #if NUM_LIGHTS && !defined(B3D_VIEWPOSITION)
  22. #define B3D_VIEWPOSITION 1
  23. #endif
  24. #if NUM_LIGHTS && BUMP_TANGENTS && !defined(B3D_VIEWTANGENT)
  25. #define B3D_VIEWTANGENT 1
  26. #endif
  27. #ifdef B3D_TEXCOORD0
  28. varying vec2 b3d_Texcoord0;
  29. #endif
  30. #ifdef B3D_COLOR
  31. varying vec4 b3d_Color;
  32. #endif
  33. #ifdef B3D_VIEWPOSITION
  34. varying vec3 b3d_ViewPosition;
  35. #endif
  36. #ifdef B3D_VIEWNORMAL
  37. varying vec3 b3d_ViewNormal;
  38. #endif
  39. #ifdef B3D_VIEWTANGENT
  40. varying vec3 b3d_ViewTangent;
  41. #endif
  42. #ifdef B3D_CLIPPOSITION
  43. varying vec4 b3d_ClipPosition;
  44. #endif
  45. //@vertex
  46. uniform mat4 ModelViewProjectionMatrix;
  47. uniform mat4 ModelViewMatrix;
  48. uniform vec4 ClipPosScale; //hack to handle inverted y when rendering to display
  49. uniform vec4 GlobalColor;
  50. attribute vec4 Position;
  51. attribute vec2 Texcoord0;
  52. attribute vec3 Tangent;
  53. attribute vec4 Color;
  54. void main(){
  55. gl_Position=ModelViewProjectionMatrix * Position;
  56. gl_PointSize=1.0;
  57. #ifdef B3D_CLIPPOSITION
  58. b3d_ClipPosition=gl_Position * ClipPosScale;
  59. #endif
  60. #ifdef B3D_VIEWPOSITION
  61. b3d_ViewPosition=(ModelViewMatrix * Position).xyz;
  62. #endif
  63. #ifdef B3D_VIEWNORMAL
  64. b3d_ViewNormal=(ModelViewMatrix * vec4( 0.0,0.0,-1.0 )).xyz;
  65. #endif
  66. #ifdef B3D_VIEWTANGENT
  67. #if OPT_FAST
  68. b3d_ViewTangent=normalize( (ModelViewMatrix * vec4( Tangent,0.0 )).xyz );
  69. #else
  70. b3d_ViewTangent=(ModelViewMatrix * vec4( Tangent,0.0 )).xyz;
  71. #endif
  72. #endif
  73. #ifdef B3D_TEXCOORD0
  74. b3d_Texcoord0=Texcoord0;
  75. #endif
  76. #ifdef B3D_COLOR
  77. b3d_Color=Color * GlobalColor;
  78. #endif
  79. }
  80. //@fragment
  81. uniform vec4 FogColor;
  82. uniform vec4 AmbientLight;
  83. #if NUM_LIGHTS
  84. uniform sampler2D ShadowTexture;
  85. uniform vec4 LightColors[NUM_LIGHTS];
  86. uniform vec4 LightVectors[NUM_LIGHTS];
  87. #endif
  88. #define b3d_FragColor gl_FragColor
  89. float b3d_Alpha;
  90. float b3d_Roughness;
  91. vec4 b3d_Ambient;
  92. vec4 b3d_Diffuse;
  93. vec4 b3d_Specular;
  94. vec3 b3d_Normal;
  95. ${SHADER}
  96. #if NUM_LIGHTS
  97. float gloss;
  98. float spow;
  99. float fnorm;
  100. float ndotv;
  101. #if ORTHO_LIGHTING
  102. const vec3 eyevec=vec3( 0.0,0.0,-1.0 );
  103. #else
  104. vec3 eyevec;
  105. #endif
  106. vec4 diffuse;
  107. vec4 specular;
  108. mat3 tanMatrix;
  109. void light( in vec4 lightVector,in vec4 lightColor,float shadow ){
  110. #if DEBUG_SHADOWS
  111. diffuse+=1.0-shadow;
  112. return;
  113. #endif
  114. vec3 v=lightVector.xyz-b3d_ViewPosition;
  115. #if BUMP_TANGENTS
  116. v=tanMatrix * v;
  117. #endif
  118. float falloff=max( 1.0-length( v )/lightVector.w,0.0 );
  119. vec3 lvec=normalize( v );
  120. vec3 hvec=normalize( lvec + eyevec );
  121. float ndotl=max( dot( b3d_Normal,lvec ),0.0 );
  122. float ndoth=max( dot( b3d_Normal,hvec ),0.0 );
  123. vec4 icolor=lightColor * ndotl * falloff * shadow;
  124. diffuse+=icolor;
  125. specular+=icolor * pow( ndoth,spow ) * fnorm;
  126. }
  127. #if CHEESY_HDR
  128. vec4 cheesy_hdr( in vec4 color ){
  129. vec4 ov=max( color-1.0,0.0 );
  130. return color+( (ov.r+ov.g+ov.b)/3.0 );
  131. }
  132. #endif
  133. #endif
  134. void main(){
  135. shader();
  136. #ifndef B3D_FRAGCOLOR
  137. #if NUM_LIGHTS
  138. #if !ORTHO_LIGHTING
  139. eyevec=normalize( -b3d_ViewPosition );
  140. #endif
  141. //specular power
  142. gloss=1.0-Roughness;
  143. spow=pow( 2.0,gloss*12.0 ); //specular power
  144. fnorm=spow*2.0/8.0; //energy conservation - sharper highlights are brighter coz they're smaller.
  145. #if FRESNEL_EFFECT //fresnel effect - reflectivity approaches 1 as surface grazing angle approaches 0 for glossy surfaces.
  146. ndotv=max( dot( b3d_Normal,eyevec ),0.0 );
  147. b3d_Specular+=(1.0-b3d_Specular) * pow( 1.0-ndotv,5.0 ) * gloss;
  148. #endif
  149. diffuse=AmbientLight;
  150. specular=vec4( 0.0 );
  151. #if BUMP_TANGENTS
  152. #if OPT_FAST
  153. vec3 vtan=b3d_ViewTangent;
  154. #else
  155. vec3 vtan=normalize( b3d_ViewTangent );
  156. #endif
  157. tanMatrix=mat3( vec3( vtan.x,-vtan.y,0.0 ),vec3( vtan.y,vtan.x,0.0 ),vec3( 0.0,0.0,1.0 ) );
  158. #endif
  159. vec4 clip=b3d_ClipPosition/b3d_ClipPosition.w;
  160. vec4 shadow=texture2D( ShadowTexture,clip.xy*0.5+0.5 );
  161. light( LightVectors[0],LightColors[0],shadow.r );
  162. #if NUM_LIGHTS>1
  163. light( LightVectors[1],LightColors[1],shadow.g );
  164. #if NUM_LIGHTS>2
  165. light( LightVectors[2],LightColors[2],shadow.b );
  166. #if NUM_LIGHTS>3
  167. light( LightVectors[3],LightColors[3],shadow.a );
  168. #endif
  169. #endif
  170. #endif
  171. #if CHEESY_HDR
  172. diffuse=cheesy_hdr( diffuse );
  173. specular=cheesy_hdr( specular );
  174. #endif
  175. vec4 color=(b3d_Diffuse * diffuse) + (b3d_Specular * specular) + (b3d_Ambient * AmbientLight.a);
  176. #else // NUM_LIGHTS
  177. #ifdef B3D_DIFFUSE
  178. vec4 color=(b3d_Diffuse * AmbientLight) + (b3d_Ambient * AmbientLight.a);
  179. #else
  180. vec4 color=b3d_Ambient * AmbientLight.a;
  181. //vec4 color=vec4(.5, .5, .5, 1);
  182. #endif
  183. #endif // NUM_LIGHTS
  184. color.rgb=mix( color.rgb,FogColor.rgb,FogColor.a * b3d_Alpha );
  185. #if GAMMA_CORRECTION
  186. gl_FragColor=vec4( pow( color.rgb,vec3( 1.0/2.2 ) ),b3d_Alpha );
  187. #else
  188. gl_FragColor=vec4( color.rgb,b3d_Alpha );
  189. #endif
  190. #endif //B3D_FRAGCOLOR
  191. }