MeshPostProcessingMaterial.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @augments MeshPhysicalMaterial
  25. */
  26. class MeshPostProcessingMaterial extends MeshPhysicalMaterial {
  27. /**
  28. * Constructs a new conditional line material.
  29. *
  30. * @param {Object} [parameters] - An object with one or more properties
  31. * defining the material's appearance. Any property of the material
  32. * (including any property from inherited materials) can be passed
  33. * in here. Color values can be passed any type of value accepted
  34. * by {@link Color#set}.
  35. */
  36. constructor( parameters ) {
  37. const aoPassMap = parameters.aoPassMap;
  38. const aoPassMapScale = parameters.aoPassMapScale || 1.0;
  39. delete parameters.aoPassMap;
  40. delete parameters.aoPassMapScale;
  41. super( parameters );
  42. this.onBeforeCompile = this._onBeforeCompile;
  43. this.customProgramCacheKey = this._customProgramCacheKey;
  44. this._aoPassMap = aoPassMap;
  45. /**
  46. * The scale of the AO pass.
  47. *
  48. * @type {number}
  49. * @default 1
  50. */
  51. this.aoPassMapScale = aoPassMapScale;
  52. this._shader = null;
  53. }
  54. /**
  55. * A texture representing the AO pass.
  56. *
  57. * @type {Texture}
  58. */
  59. get aoPassMap() {
  60. return this._aoPassMap;
  61. }
  62. set aoPassMap( aoPassMap ) {
  63. this._aoPassMap = aoPassMap;
  64. this.needsUpdate = true;
  65. this._setUniforms();
  66. }
  67. _customProgramCacheKey() {
  68. return this._aoPassMap !== undefined && this._aoPassMap !== null ? 'aoPassMap' : '';
  69. }
  70. _onBeforeCompile( shader ) {
  71. this._shader = shader;
  72. if ( this._aoPassMap !== undefined && this._aoPassMap !== null ) {
  73. shader.fragmentShader = shader.fragmentShader.replace(
  74. '#include <aomap_pars_fragment>',
  75. aomap_pars_fragment_replacement
  76. );
  77. shader.fragmentShader = shader.fragmentShader.replace(
  78. '#include <aomap_fragment>',
  79. aomap_fragment_replacement
  80. );
  81. }
  82. this._setUniforms();
  83. }
  84. _setUniforms() {
  85. if ( this._shader ) {
  86. this._shader.uniforms.tAoPassMap = { value: this._aoPassMap };
  87. this._shader.uniforms.aoPassMapScale = { value: this.aoPassMapScale };
  88. }
  89. }
  90. }
  91. const aomap_pars_fragment_replacement = /* glsl */`
  92. #ifdef USE_AOMAP
  93. uniform sampler2D aoMap;
  94. uniform float aoMapIntensity;
  95. #endif
  96. uniform sampler2D tAoPassMap;
  97. uniform float aoPassMapScale;
  98. `;
  99. const aomap_fragment_replacement = /* glsl */`
  100. #ifndef AOPASSMAP_SWIZZLE
  101. #define AOPASSMAP_SWIZZLE r
  102. #endif
  103. float ambientOcclusion = texelFetch( tAoPassMap, ivec2( gl_FragCoord.xy * aoPassMapScale ), 0 ).AOPASSMAP_SWIZZLE;
  104. #ifdef USE_AOMAP
  105. // reads channel R, compatible with a combined OcclusionRoughnessMetallic (RGB) texture
  106. ambientOcclusion = min( ambientOcclusion, texture2D( aoMap, vAoMapUv ).r );
  107. ambientOcclusion *= ( ambientOcclusion - 1.0 ) * aoMapIntensity + 1.0;
  108. #endif
  109. reflectedLight.indirectDiffuse *= ambientOcclusion;
  110. #if defined( USE_CLEARCOAT )
  111. clearcoatSpecularIndirect *= ambientOcclusion;
  112. #endif
  113. #if defined( USE_SHEEN )
  114. sheenSpecularIndirect *= ambientOcclusion;
  115. #endif
  116. #if defined( USE_ENVMAP ) && defined( STANDARD )
  117. float dotNV = saturate( dot( geometryNormal, geometryViewDir ) );
  118. reflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.roughness );
  119. #endif
  120. `;
  121. export { MeshPostProcessingMaterial };
粤ICP备19079148号