toonFrag.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import common from './common';
  2. const toonInfo = `
  3. uniform highp vec4 u_baseColorFactor;
  4. uniform sampler2D u_baseColorMap;
  5. uniform sampler2D u_gradientMap;
  6. struct ToonMaterial {
  7. vec3 diffuseColor;
  8. };
  9. vec3 diffuse = vec3( 1.0 );
  10. float opacity = 1.0;
  11. vec3 BRDF_Lambert( const in vec3 diffuseColor ) {
  12. return RECIPROCAL_PI * diffuseColor;
  13. } // validated
  14. vec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) {
  15. // dotNL will be from -1.0 to 1.0
  16. float dotNL = dot( normal, lightDirection );
  17. vec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 );
  18. return vec3( texture2D( u_gradientMap, coord ).r );
  19. }
  20. void Direct_Toon( const in IncidentLight directLight, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {
  21. vec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;
  22. reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
  23. }
  24. void IndirectDiffuse_Toon( const in vec3 irradiance, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {
  25. reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
  26. }
  27. `
  28. export default
  29. /* glsl */
  30. `#version 100
  31. precision highp float;
  32. precision highp int;
  33. uniform float u_gameTime;
  34. uniform highp mat4 u_world;
  35. uniform highp mat4 u_view;
  36. uniform highp mat4 u_projection;
  37. uniform highp mat4 u_viewInverse;
  38. varying highp vec2 v_UV;
  39. varying highp vec2 v_UV1;
  40. varying highp vec3 v_ViewPosition;
  41. #ifdef WX_USE_NORMAL
  42. varying highp vec3 v_Normal;
  43. #endif
  44. ${common}
  45. ${toonInfo}
  46. void main()
  47. {
  48. vec4 diffuseColor = vec4( diffuse, opacity );
  49. // ReflectedLight
  50. ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
  51. // Emissive
  52. vec3 totalEmissiveRadiance = vec3(0.0);
  53. #ifdef WX_USE_BASECOLORMAP
  54. #ifdef WX_BASECOLORMAP_USE_UV1
  55. vec2 uvBaseColor = v_UV1;
  56. #elif defined(WX_BASECOLORMAP_USE_UV2)
  57. vec2 uvBaseColor = v_UV2;
  58. #elif defined(WX_BASECOLORMAP_USE_UV3)
  59. vec2 uvBaseColor = v_UV3;
  60. #else
  61. vec2 uvBaseColor = v_UV;
  62. #endif
  63. vec4 baseColor = texture2D(u_baseColorMap, uvBaseColor) * u_baseColorFactor;
  64. #else
  65. vec4 baseColor = u_baseColorFactor;
  66. #endif
  67. // Normal
  68. #ifdef WX_USE_NORMAL
  69. vec3 normal = normalize(v_Normal);
  70. #else
  71. vec3 normal = vec3(0.0, 0.0, 1.0);
  72. #endif
  73. // Accumulation
  74. ToonMaterial material;
  75. material.diffuseColor = diffuseColor.rgb;
  76. GeometricContext geometry;
  77. geometry.position = v_ViewPosition;
  78. geometry.normal = normal;
  79. geometry.viewDir = normalize( vec3(0.0) - v_ViewPosition );
  80. // AddLights
  81. #ifdef WX_USE_ADD_LIGHTS
  82. for(int i = 0; i < WX_ADD_LIGHTS_COUNT; ++i)
  83. {
  84. }
  85. #endif
  86. vec3 color = baseColor.rgb;
  87. // color = (v_Normal + vec3(1.0, 1.0, 1.0)) / 2.0;
  88. gl_FragData[0] = vec4(color, 1.0);
  89. }
  90. `;