matfrag.glsl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #extension GL_OES_standard_derivatives : enable
  2. precision mediump float;
  3. varying highp vec3 dv;
  4. varying mediump vec2 d;
  5. varying mediump vec3 dA;
  6. varying mediump vec3 dB;
  7. varying mediump vec3 dC;
  8. #ifdef VERTEX_COLOR
  9. varying lowp vec4 dD;
  10. #endif
  11. #ifdef TEXCOORD_SECONDARY
  12. varying mediump vec2 dE;
  13. #endif
  14. uniform sampler2D tAlbedo;
  15. uniform sampler2D tReflectivity;
  16. uniform sampler2D tNormal;
  17. uniform sampler2D tExtras;
  18. uniform sampler2D tSkySpecular;
  19. #ifdef REFRACTION
  20. uniform sampler2D tRefraction;
  21. #endif
  22. uniform vec4 uDiffuseCoefficients[9];
  23. uniform vec3 uCameraPosition;
  24. uniform float uAlphaTest;
  25. uniform vec3 uFresnel;
  26. uniform float uHorizonOcclude;
  27. uniform float uHorizonSmoothing;
  28. #ifdef EMISSIVE
  29. uniform float uEmissiveScale;
  30. uniform vec4 uTexRangeEmissive;
  31. #endif
  32. #ifdef AMBIENT_OCCLUSION
  33. uniform vec4 uTexRangeAO;
  34. #endif
  35. #ifdef REFRACTION
  36. uniform float uRefractionIOREntry;
  37. uniform float uRefractionRayDistance;
  38. uniform vec3 uRefractionTint;
  39. uniform float uRefractionAlbedoTint;
  40. uniform mat4 uRefractionViewProjection;
  41. uniform vec4 uTexRangeRefraction;
  42. #endif
  43. #ifdef LIGHT_COUNT
  44. uniform vec4 uLightPositions[LIGHT_COUNT];
  45. uniform vec3 uLightDirections[LIGHT_COUNT];
  46. uniform vec3 uLightColors[LIGHT_COUNT];
  47. uniform vec3 uLightParams[LIGHT_COUNT];
  48. uniform vec3 uLightSpot[LIGHT_COUNT];
  49. #endif
  50. #ifdef ANISO
  51. uniform float uAnisoStrength;
  52. uniform vec3 uAnisoTangent;
  53. uniform float uAnisoIntegral;
  54. uniform vec4 uTexRangeAniso;
  55. #endif
  56. #define saturate(x) clamp( x, 0.0, 1.0 )
  57. #include <matsampling.glsl>
  58. #include <matlighting.glsl>
  59. #include <matshadows.glsl>
  60. #include <matskin.glsl>
  61. #include <matmicrofiber.glsl>
  62. #include <matstrips.glsl>
  63. #ifdef TRANSPARENCY_DITHER
  64. #include <matdither.glsl>
  65. #endif
  66. void main(void)
  67. {
  68. vec4 m=texture2D(tAlbedo,d);
  69. vec3 dF=dG(m.xyz);
  70. float e=m.w;
  71. //顶点颜色
  72. #ifdef VERTEX_COLOR
  73. {
  74. vec3 dH=dD.xyz;
  75. #ifdef VERTEX_COLOR_SRGB
  76. dH=dH*(dH*(dH*0.305306011+vec3(0.682171111))+vec3(0.012522878)); //Gamma 校正
  77. #endif
  78. dF*=dH;
  79. #ifdef VERTEX_COLOR_ALPHA
  80. e*=dD.w;
  81. #endif
  82. }
  83. #endif
  84. //AlphaTest
  85. #ifdef ALPHA_TEST
  86. if(e<uAlphaTest)
  87. {
  88. discard;
  89. }
  90. #endif
  91. //透明抖动
  92. #ifdef TRANSPARENCY_DITHER
  93. e=(e>f(d.x))?1.0:e;
  94. #endif
  95. vec3 dI=dJ(texture2D(tNormal,d).xyz);
  96. //各向异性
  97. #ifdef ANISO
  98. #ifdef ANISO_NO_DIR_TEX
  99. vec3 dK=dL(uAnisoTangent);
  100. #else
  101. m=dM(d,uTexRangeAniso);
  102. vec3 dK=2.0*m.xyz-vec3(1.0);
  103. dK=dL(dK);
  104. #endif
  105. dK=dK-dI*dot(dK,dI);
  106. dK=normalize(dK);
  107. vec3 dN=dK*uAnisoStrength;
  108. #endif
  109. vec3 dO=normalize(uCameraPosition-dv);
  110. m=texture2D(tReflectivity,d);
  111. vec3 dP=dG(m.xyz);
  112. float dQ=m.w;
  113. float dR=dQ;
  114. #ifdef HORIZON_SMOOTHING
  115. float dS=dot(dO,dI);dS=uHorizonSmoothing-dS*uHorizonSmoothing;dQ=mix(dQ,1.0,dS*dS);
  116. #endif
  117. #ifdef STRIPVIEW
  118. dT dU;dV(dU,dQ,dP);
  119. #endif
  120. float dW=1.0;
  121. //环境光遮蔽
  122. #ifdef AMBIENT_OCCLUSION
  123. #ifdef AMBIENT_OCCLUSION_SECONDARY_UV
  124. dW=dM(dE,uTexRangeAO).x;
  125. #else
  126. dW=dM(d,uTexRangeAO).x;
  127. #endif
  128. dW*=dW;
  129. #endif
  130. //皮肤渲染
  131. #if defined(SKIN)
  132. dX dY;
  133. dZ(dY);
  134. dY.ec*=dW;
  135. #elif defined(MICROFIBER)
  136. ed ee;ef(ee,dI);ee.eh*=dW;
  137. #else
  138. vec3 ei=ej(dI);ei*=dW;
  139. #endif
  140. vec3 ek=reflect(-dO,dI);
  141. #ifdef ANISO
  142. vec3 rt=ek-(0.5*dN*dot(ek,dK));vec3 el=em(rt,mix(dQ,0.5*dQ,uAnisoStrength));
  143. #else
  144. vec3 el=em(ek,dQ);
  145. #endif
  146. el*=en(ek,dC);
  147. #ifdef LIGHT_COUNT
  148. highp float eo=10.0/log2(dQ*0.968+0.03);
  149. eo*=eo;
  150. float eu=eo*(1.0/(8.0*3.1415926))+(4.0/(8.0*3.1415926));
  151. eu=min(eu,1.0e3);
  152. #ifdef SHADOW_COUNT
  153. ev eA;
  154. #ifdef SKIN
  155. #ifdef SKIN_VERSION_1
  156. eB(eA,SHADOW_KERNEL+SHADOW_KERNEL*dY.eC);
  157. #else
  158. eD eE;
  159. float eF=SHADOW_KERNEL+SHADOW_KERNEL*dY.eC;
  160. eG(eE,eF);
  161. eB(eA,eF);
  162. #endif
  163. #else
  164. eB(eA,SHADOW_KERNEL);
  165. #endif
  166. #endif
  167. #ifdef ANISO
  168. eu*=uAnisoIntegral;
  169. #endif
  170. for(int k=0;k<LIGHT_COUNT;++k)
  171. {
  172. vec3 eH=uLightPositions[k].xyz-dv*uLightPositions[k].w;
  173. float eI=inversesqrt(dot(eH,eH));
  174. eH*=eI;
  175. float a=saturate(uLightParams[k].z/eI);
  176. a=1.0+a*(uLightParams[k].x+uLightParams[k].y*a);
  177. float s=saturate(dot(eH,uLightDirections[k]));
  178. s=saturate(uLightSpot[k].y-uLightSpot[k].z*(1.0-s*s));
  179. vec3 eJ=(a*s)*uLightColors[k].xyz;
  180. #if defined(SKIN)
  181. #ifdef SHADOW_COUNT
  182. #ifdef SKIN_VERSION_1
  183. eK(dY,eA.eL[k],1.0,eH,dI,eJ);
  184. #else
  185. eK(dY,eA.eL[k],eE.eE[k],eH,dI,eJ);
  186. #endif
  187. #else
  188. eK(dY,1.0,0.0,eH,dI,eJ);
  189. #endif
  190. #elif defined(MICROFIBER)
  191. #ifdef SHADOW_COUNT
  192. eM(ee,eA.eL[k],eH,dI,eJ);
  193. #else
  194. eM(ee,1.0,eH,dI,eJ);
  195. #endif
  196. #else
  197. float eN=saturate((1.0/3.1415926)*dot(eH,dI));
  198. #ifdef SHADOW_COUNT
  199. eN*=eA.eL[k];
  200. #endif
  201. ei+=eN*eJ;
  202. #endif
  203. vec3 eO=eH+dO;
  204. #ifdef ANISO
  205. eO=eO-(dN*dot(eO,dK));
  206. #endif
  207. eO=normalize(eO);
  208. float eP=eu*pow(saturate(dot(eO,dI)),eo);
  209. #ifdef SHADOW_COUNT
  210. eP*=eA.eL[k];
  211. #endif
  212. el+=eP*eJ;
  213. }
  214. #endif
  215. #if defined(SKIN)
  216. vec3 ei,diff_extra;
  217. eQ(ei,diff_extra,dY,dO,dI,dQ);
  218. #elif defined(MICROFIBER)
  219. vec3 ei,diff_extra;eR(ei,diff_extra,ee,dO,dI,dQ);
  220. #endif
  221. vec3 eS=eT(dO,dI,dP,dQ*dQ);
  222. el*=eS;
  223. //折射
  224. #ifdef REFRACTION
  225. vec4 eU;
  226. {
  227. vec3 G=refract(-dO,dI,uRefractionIOREntry);
  228. G=dv+G*uRefractionRayDistance;
  229. vec4 eV=uRefractionViewProjection[0]*G.x+(uRefractionViewProjection[1]*G.y+(uRefractionViewProjection[2]*G.z+uRefractionViewProjection[3]));
  230. vec2 c=eV.xy/eV.w;
  231. c=0.5*c+vec2(0.5,0.5);
  232. vec2 i=mod(floor(c),2.0);
  233. c=fract(c);
  234. c.x=i.x>0.0?1.0-c.x:c.x;
  235. c.y=i.y>0.0?1.0-c.y:c.y;
  236. eU.rgb=texture2D(tRefraction,c).xyz;
  237. eU.rgb=mix(eU.rgb,eU.rgb*dF,uRefractionAlbedoTint);
  238. eU.rgb=eU.rgb-eU.rgb*eS;
  239. eU.rgb*=uRefractionTint;
  240. #ifdef REFRACTION_NO_MASK_TEX
  241. eU.a=1.0;
  242. #else
  243. eU.a=dM(d,uTexRangeRefraction).x;
  244. #endif
  245. }
  246. #endif
  247. #ifdef DIFFUSE_UNLIT
  248. gl_FragColor.xyz=dF;
  249. #else
  250. gl_FragColor.xyz=ei*dF;
  251. #endif
  252. #ifdef REFRACTION
  253. gl_FragColor.xyz=mix(gl_FragColor.xyz,eU.rgb,eU.a);
  254. #endif
  255. gl_FragColor.xyz+=el;
  256. #if defined(SKIN) || defined(MICROFIBER)
  257. gl_FragColor.xyz+=diff_extra;
  258. #endif
  259. //自发光
  260. #ifdef EMISSIVE
  261. #ifdef EMISSIVE_SECONDARY_UV
  262. vec2 eW=dE;
  263. #else
  264. vec2 eW=d;
  265. #endif
  266. gl_FragColor.xyz+=uEmissiveScale*dG(dM(eW,uTexRangeEmissive).xyz);
  267. #endif
  268. //网格
  269. #ifdef STRIPVIEW
  270. gl_FragColor.xyz=eX(dU,dI,dF,dP,dR,ei,el,gl_FragColor.xyz);
  271. #endif
  272. #ifdef NOBLEND
  273. gl_FragColor.w=1.0;
  274. #else
  275. gl_FragColor.w=e;
  276. #endif
  277. }