MeshPostProcessingMaterial.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { MeshPhysicalMaterial } from 'three';
  2. /**
  3. * The aim of this mesh material is to use information from a post processing pass in the diffuse color pass.
  4. * This material is based on the MeshPhysicalMaterial.
  5. *
  6. * In the current state, only the information of a screen space AO pass can be used in the material.
  7. * Actually, the output of any screen space AO (SSAO, GTAO) can be used,
  8. * as it is only necessary to provide the AO in one color channel of a texture,
  9. * however the AO pass must be rendered prior to the color pass,
  10. * which makes the post-processing pass somewhat of a pre-processing pass.
  11. * Fot this purpose a new map (`aoPassMap`) is added to the material.
  12. * The value of the map is used the same way as the `aoMap` value.
  13. *
  14. * Motivation to use the outputs AO pass directly in the material:
  15. * The incident light of a fragment is composed of ambient light, direct light and indirect light
  16. * Ambient Occlusion only occludes ambient light and environment light, but not direct light.
  17. * Direct light is only occluded by geometry that casts shadows.
  18. * And of course the emitted light should not be darkened by ambient occlusion either.
  19. * This cannot be achieved if the AO post processing pass is simply blended with the diffuse render pass.
  20. *
  21. * Further extension work might be to use the output of an SSR pass or an HBIL pass from a previous frame.
  22. * This would then create the possibility of SSR and IR depending on material properties such as `roughness`, `metalness` and `reflectivity`.
  23. **/
  24. class MeshPostProcessingMaterial extends MeshPhysicalMaterial {
  25. constructor( parameters ) {
  26. const aoPassMap = parameters.aoPassMap;
  27. const aoPassMapScale = parameters.aoPassMapScale || 1.0;
  28. delete parameters.aoPassMap;
  29. delete parameters.aoPassMapScale;
  30. super( parameters );
  31. this.onBeforeCompile = this._onBeforeCompile;
  32. this.customProgramCacheKey = this._customProgramCacheKey;
  33. this._aoPassMap = aoPassMap;
  34. this.aoPassMapScale = aoPassMapScale;
  35. this._shader = null;
  36. }
  37. get aoPassMap() {
  38. return this._aoPassMap;
  39. }
  40. set aoPassMap( aoPassMap ) {
  41. this._aoPassMap = aoPassMap;
  42. this.needsUpdate = true;
  43. this._setUniforms();
  44. }
  45. _customProgramCacheKey() {
  46. return this._aoPassMap !== undefined && this._aoPassMap !== null ? 'aoPassMap' : '';
  47. }
  48. _onBeforeCompile( shader ) {
  49. this._shader = shader;
  50. if ( this._aoPassMap !== undefined && this._aoPassMap !== null ) {
  51. shader.fragmentShader = shader.fragmentShader.replace(
  52. '#include <aomap_pars_fragment>',
  53. aomap_pars_fragment_replacement
  54. );
  55. shader.fragmentShader = shader.fragmentShader.replace(
  56. '#include <aomap_fragment>',
  57. aomap_fragment_replacement
  58. );
  59. }
  60. this._setUniforms();
  61. }
  62. _setUniforms() {
  63. if ( this._shader ) {
  64. this._shader.uniforms.tAoPassMap = { value: this._aoPassMap };
  65. this._shader.uniforms.aoPassMapScale = { value: this.aoPassMapScale };
  66. }
  67. }
  68. }
  69. const aomap_pars_fragment_replacement = /* glsl */`
  70. #ifdef USE_AOMAP
  71. uniform sampler2D aoMap;
  72. uniform float aoMapIntensity;
  73. #endif
  74. uniform sampler2D tAoPassMap;
  75. uniform float aoPassMapScale;
  76. `;
  77. const aomap_fragment_replacement = /* glsl */`
  78. #ifndef AOPASSMAP_SWIZZLE
  79. #define AOPASSMAP_SWIZZLE r
  80. #endif
  81. float ambientOcclusion = texelFetch( tAoPassMap, ivec2( gl_FragCoord.xy * aoPassMapScale ), 0 ).AOPASSMAP_SWIZZLE;
  82. #ifdef USE_AOMAP
  83. // reads channel R, compatible with a combined OcclusionRoughnessMetallic (RGB) texture
  84. ambientOcclusion = min( ambientOcclusion, texture2D( aoMap, vAoMapUv ).r );
  85. ambientOcclusion *= ( ambientOcclusion - 1.0 ) * aoMapIntensity + 1.0;
  86. #endif
  87. reflectedLight.indirectDiffuse *= ambientOcclusion;
  88. #if defined( USE_CLEARCOAT )
  89. clearcoatSpecularIndirect *= ambientOcclusion;
  90. #endif
  91. #if defined( USE_SHEEN )
  92. sheenSpecularIndirect *= ambientOcclusion;
  93. #endif
  94. #if defined( USE_ENVMAP ) && defined( STANDARD )
  95. float dotNV = saturate( dot( geometryNormal, geometryViewDir ) );
  96. reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );
  97. #endif
  98. `;
  99. export { MeshPostProcessingMaterial };