MMDToonShader.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * MMD Toon Shader
  3. *
  4. * This shader is extended from MeshPhongMaterial, and merged algorithms with
  5. * MeshToonMaterial and MeshMetcapMaterial.
  6. * Ideas came from https://github.com/mrdoob/three.js/issues/19609
  7. *
  8. * Combining steps:
  9. * * Declare matcap uniform.
  10. * * Add gradientmap_pars_fragment.
  11. * * Use gradient irradiances instead of dotNL irradiance from MeshPhongMaterial.
  12. * (Replace lights_phong_pars_fragment with lights_mmd_toon_pars_fragment)
  13. * * Add mmd_toon_matcap_fragment.
  14. */
  15. import { UniformsUtils, ShaderLib } from '../../../build/three.module.js';
  16. const lights_mmd_toon_pars_fragment = `
  17. varying vec3 vViewPosition;
  18. struct BlinnPhongMaterial {
  19. vec3 diffuseColor;
  20. vec3 specularColor;
  21. float specularShininess;
  22. float specularStrength;
  23. };
  24. void RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
  25. vec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;
  26. #ifndef PHYSICALLY_CORRECT_LIGHTS
  27. irradiance *= PI; // punctual light
  28. #endif
  29. reflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
  30. reflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;
  31. }
  32. void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
  33. reflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );
  34. }
  35. #define RE_Direct RE_Direct_BlinnPhong
  36. #define RE_IndirectDiffuse RE_IndirectDiffuse_BlinnPhong
  37. #define Material_LightProbeLOD( material ) (0)
  38. `;
  39. const mmd_toon_matcap_fragment = `
  40. #ifdef USE_MATCAP
  41. vec3 viewDir = normalize( vViewPosition );
  42. vec3 x = normalize( vec3( viewDir.z, 0.0, - viewDir.x ) );
  43. vec3 y = cross( viewDir, x );
  44. vec2 uv = vec2( dot( x, normal ), dot( y, normal ) ) * 0.495 + 0.5; // 0.495 to remove artifacts caused by undersized matcap disks
  45. vec4 matcapColor = texture2D( matcap, uv );
  46. matcapColor = matcapTexelToLinear( matcapColor );
  47. #ifdef MATCAP_BLENDING_MULTIPLY
  48. outgoingLight *= matcapColor.rgb;
  49. #elif defined( MATCAP_BLENDING_ADD )
  50. outgoingLight += matcapColor.rgb;
  51. #endif
  52. #endif
  53. `;
  54. const MMDToonShader = {
  55. defines: {
  56. TOON: true,
  57. MATCAP: true,
  58. MATCAP_BLENDING_ADD: true,
  59. },
  60. uniforms: UniformsUtils.merge( [
  61. ShaderLib.toon.uniforms,
  62. ShaderLib.phong.uniforms,
  63. ShaderLib.matcap.uniforms,
  64. ] ),
  65. vertexShader: ShaderLib.phong.vertexShader,
  66. fragmentShader:
  67. ShaderLib.phong.fragmentShader
  68. .replace(
  69. '#include <common>',
  70. `
  71. #ifdef USE_MATCAP
  72. uniform sampler2D matcap;
  73. #endif
  74. #include <common>
  75. `
  76. )
  77. .replace(
  78. '#include <envmap_common_pars_fragment>',
  79. `
  80. #include <gradientmap_pars_fragment>
  81. #include <envmap_common_pars_fragment>
  82. `
  83. )
  84. .replace(
  85. '#include <lights_phong_pars_fragment>',
  86. lights_mmd_toon_pars_fragment
  87. )
  88. .replace(
  89. '#include <envmap_fragment>',
  90. `
  91. #include <envmap_fragment>
  92. ${mmd_toon_matcap_fragment}
  93. `
  94. ),
  95. };
  96. export { MMDToonShader };