MMDToonShader.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ( function () {
  2. /**
  3. * MMD Toon Shader
  4. *
  5. * This shader is extended from MeshPhongMaterial, and merged algorithms with
  6. * MeshToonMaterial and MeshMetcapMaterial.
  7. * Ideas came from https://github.com/mrdoob/three.js/issues/19609
  8. *
  9. * Combining steps:
  10. * * Declare matcap uniform.
  11. * * Add gradientmap_pars_fragment.
  12. * * Use gradient irradiances instead of dotNL irradiance from MeshPhongMaterial.
  13. * (Replace lights_phong_pars_fragment with lights_mmd_toon_pars_fragment)
  14. * * Add mmd_toon_matcap_fragment.
  15. */
  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. reflectedLight.directDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
  27. reflectedLight.directSpecular += irradiance * BRDF_BlinnPhong( directLight.direction, geometry.viewDir, geometry.normal, material.specularColor, material.specularShininess ) * material.specularStrength;
  28. }
  29. void RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {
  30. reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
  31. }
  32. #define RE_Direct RE_Direct_BlinnPhong
  33. #define RE_IndirectDiffuse RE_IndirectDiffuse_BlinnPhong
  34. #define Material_LightProbeLOD( material ) (0)
  35. `;
  36. const mmd_toon_matcap_fragment = `
  37. #ifdef USE_MATCAP
  38. vec3 viewDir = normalize( vViewPosition );
  39. vec3 x = normalize( vec3( viewDir.z, 0.0, - viewDir.x ) );
  40. vec3 y = cross( viewDir, x );
  41. vec2 uv = vec2( dot( x, normal ), dot( y, normal ) ) * 0.495 + 0.5; // 0.495 to remove artifacts caused by undersized matcap disks
  42. vec4 matcapColor = texture2D( matcap, uv );
  43. matcapColor = matcapTexelToLinear( matcapColor );
  44. #ifdef MATCAP_BLENDING_MULTIPLY
  45. outgoingLight *= matcapColor.rgb;
  46. #elif defined( MATCAP_BLENDING_ADD )
  47. outgoingLight += matcapColor.rgb;
  48. #endif
  49. #endif
  50. `;
  51. const MMDToonShader = {
  52. defines: {
  53. TOON: true,
  54. MATCAP: true,
  55. MATCAP_BLENDING_ADD: true
  56. },
  57. uniforms: THREE.UniformsUtils.merge( [ THREE.ShaderLib.toon.uniforms, THREE.ShaderLib.phong.uniforms, THREE.ShaderLib.matcap.uniforms ] ),
  58. vertexShader: THREE.ShaderLib.phong.vertexShader,
  59. fragmentShader: THREE.ShaderLib.phong.fragmentShader.replace( '#include <common>', `
  60. #ifdef USE_MATCAP
  61. uniform sampler2D matcap;
  62. #endif
  63. #include <common>
  64. ` ).replace( '#include <envmap_common_pars_fragment>', `
  65. #include <gradientmap_pars_fragment>
  66. #include <envmap_common_pars_fragment>
  67. ` ).replace( '#include <lights_phong_pars_fragment>', lights_mmd_toon_pars_fragment ).replace( '#include <envmap_fragment>', `
  68. #include <envmap_fragment>
  69. ${mmd_toon_matcap_fragment}
  70. ` )
  71. };
  72. THREE.MMDToonShader = MMDToonShader;
  73. } )();