cell.fragment.fx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. precision highp float;
  2. // Constants
  3. uniform vec3 vEyePosition;
  4. uniform vec4 vDiffuseColor;
  5. // Input
  6. varying vec3 vPositionW;
  7. #ifdef NORMAL
  8. varying vec3 vNormalW;
  9. #endif
  10. #ifdef VERTEXCOLOR
  11. varying vec4 vColor;
  12. #endif
  13. // Helper functions
  14. #include<helperFunctions>
  15. // Lights
  16. #include<__decl__lightFragment>[0..maxSimultaneousLights]
  17. #include<lightsFragmentFunctions>
  18. #include<shadowsFragmentFunctions>
  19. // Samplers
  20. #ifdef DIFFUSE
  21. varying vec2 vDiffuseUV;
  22. uniform sampler2D diffuseSampler;
  23. uniform vec2 vDiffuseInfos;
  24. #endif
  25. #include<clipPlaneFragmentDeclaration>
  26. // Fog
  27. #include<fogFragmentDeclaration>
  28. // Custom
  29. vec3 computeCustomDiffuseLighting(lightingInfo info, vec3 diffuseBase, float shadow)
  30. {
  31. diffuseBase = info.diffuse * shadow;
  32. #ifdef CELLBASIC
  33. float level = 1.0;
  34. if (info.ndl < 0.5)
  35. level = 0.5;
  36. diffuseBase.rgb * vec3(level, level, level);
  37. #else
  38. float ToonThresholds[4];
  39. ToonThresholds[0] = 0.95;
  40. ToonThresholds[1] = 0.5;
  41. ToonThresholds[2] = 0.2;
  42. ToonThresholds[3] = 0.03;
  43. float ToonBrightnessLevels[5];
  44. ToonBrightnessLevels[0] = 1.0;
  45. ToonBrightnessLevels[1] = 0.8;
  46. ToonBrightnessLevels[2] = 0.6;
  47. ToonBrightnessLevels[3] = 0.35;
  48. ToonBrightnessLevels[4] = 0.2;
  49. if (info.ndl > ToonThresholds[0])
  50. {
  51. diffuseBase.rgb *= ToonBrightnessLevels[0];
  52. }
  53. else if (info.ndl > ToonThresholds[1])
  54. {
  55. diffuseBase.rgb *= ToonBrightnessLevels[1];
  56. }
  57. else if (info.ndl > ToonThresholds[2])
  58. {
  59. diffuseBase.rgb *= ToonBrightnessLevels[2];
  60. }
  61. else if (info.ndl > ToonThresholds[3])
  62. {
  63. diffuseBase.rgb *= ToonBrightnessLevels[3];
  64. }
  65. else
  66. {
  67. diffuseBase.rgb *= ToonBrightnessLevels[4];
  68. }
  69. #endif
  70. return max(diffuseBase, vec3(0.2));
  71. }
  72. void main(void)
  73. {
  74. #include<clipPlaneFragment>
  75. vec3 viewDirectionW = normalize(vEyePosition - vPositionW);
  76. // Base color
  77. vec4 baseColor = vec4(1., 1., 1., 1.);
  78. vec3 diffuseColor = vDiffuseColor.rgb;
  79. // Alpha
  80. float alpha = vDiffuseColor.a;
  81. #ifdef DIFFUSE
  82. baseColor = texture2D(diffuseSampler, vDiffuseUV);
  83. #ifdef ALPHATEST
  84. if (baseColor.a < 0.4)
  85. discard;
  86. #endif
  87. #include<depthPrePass>
  88. baseColor.rgb *= vDiffuseInfos.y;
  89. #endif
  90. #ifdef VERTEXCOLOR
  91. baseColor.rgb *= vColor.rgb;
  92. #endif
  93. // Normal
  94. #ifdef NORMAL
  95. vec3 normalW = normalize(vNormalW);
  96. #else
  97. vec3 normalW = vec3(1.0, 1.0, 1.0);
  98. #endif
  99. // Lighting
  100. lightingInfo info;
  101. vec3 diffuseBase = vec3(0., 0., 0.);
  102. float shadow = 1.;
  103. float glossiness = 0.;
  104. #ifdef SPECULARTERM
  105. vec3 specularBase = vec3(0., 0., 0.);
  106. #endif
  107. #include<lightFragment>[0..maxSimultaneousLights]
  108. #ifdef VERTEXALPHA
  109. alpha *= vColor.a;
  110. #endif
  111. vec3 finalDiffuse = clamp(diffuseBase * diffuseColor, 0.0, 1.0) * baseColor.rgb;
  112. // Composition
  113. vec4 color = vec4(finalDiffuse, alpha);
  114. #include<fogFragment>
  115. gl_FragColor = color;
  116. }