RoughnessMipmapper.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @author Emmett Lalish / elalish
  3. *
  4. * This class generates custom mipmaps for a roughness map by encoding the lost variation in the
  5. * normal map mip levels as increased roughness in the corresponding roughness mip levels. This
  6. * helps with rendering accuracy for MeshStandardMaterial, and also helps with anti-aliasing when
  7. * using PMREM. If the normal map is larger than the roughness map, the roughness map will be
  8. * enlarged to match the dimensions of the normal map.
  9. */
  10. import {
  11. LinearMipMapLinearFilter,
  12. Math as _Math,
  13. Mesh,
  14. NoBlending,
  15. OrthographicCamera,
  16. PlaneBufferGeometry,
  17. RawShaderMaterial,
  18. Scene,
  19. Vector2,
  20. WebGLRenderTarget
  21. } from "../../../build/three.module.js";
  22. var RoughnessMipmapper = ( function () {
  23. var _mipmapMaterial = _getMipmapMaterial();
  24. var _scene = new Scene();
  25. _scene.add( new Mesh( new PlaneBufferGeometry( 2, 2 ), _mipmapMaterial ) );
  26. var _flatCamera = new OrthographicCamera( 0, 1, 0, 1, 0, 1 );
  27. var _tempTarget = null;
  28. var _renderer = null;
  29. // constructor
  30. var RoughnessMipmapper = function ( renderer ) {
  31. _renderer = renderer;
  32. };
  33. RoughnessMipmapper.prototype = {
  34. constructor: RoughnessMipmapper,
  35. generateMipmaps: function ( material ) {
  36. var { roughnessMap, normalMap } = material;
  37. if ( roughnessMap == null || normalMap == null || ! roughnessMap.generateMipmaps ||
  38. material.userData.roughnessUpdated ) return;
  39. material.userData.roughnessUpdated = true;
  40. var width = Math.max( roughnessMap.image.width, normalMap.image.width );
  41. var height = Math.max( roughnessMap.image.height, normalMap.image.height );
  42. if ( ! _Math.isPowerOfTwo( width ) || ! _Math.isPowerOfTwo( height ) ) return;
  43. var autoClear = _renderer.autoClear;
  44. _renderer.autoClear = false;
  45. if ( _tempTarget == null || _tempTarget.width !== width || _tempTarget.height !== height ) {
  46. if ( _tempTarget != null ) _tempTarget.dispose();
  47. _tempTarget = new WebGLRenderTarget( width, height, { depthBuffer: false, stencilBuffer: false } );
  48. }
  49. if ( width !== roughnessMap.image.width || height !== roughnessMap.image.height ) {
  50. var newRoughnessTarget = new WebGLRenderTarget( width, height, {
  51. minFilter: LinearMipMapLinearFilter,
  52. depthBuffer: false,
  53. stencilBuffer: false
  54. } );
  55. newRoughnessTarget.texture.generateMipmaps = true;
  56. // Setting the render target causes the memory to be allocated.
  57. _renderer.setRenderTarget( newRoughnessTarget );
  58. material.roughnessMap = newRoughnessTarget.texture;
  59. if ( material.metalnessMap == roughnessMap ) material.metalnessMap = material.roughnessMap;
  60. if ( material.aoMap == roughnessMap ) material.aoMap = material.roughnessMap;
  61. }
  62. _renderer.setRenderTarget( _tempTarget );
  63. _mipmapMaterial.uniforms.roughnessMap.value = roughnessMap;
  64. _mipmapMaterial.uniforms.normalMap.value = normalMap;
  65. var dpr = _renderer.getPixelRatio();
  66. var position = new Vector2( 0, 0 );
  67. var texelSize = _mipmapMaterial.uniforms.texelSize.value;
  68. for ( var mip = 0; width >= 1 && height >= 1;
  69. ++ mip, width /= 2, height /= 2 ) {
  70. // Rendering to a mip level is not allowed in webGL1. Instead we must set
  71. // up a secondary texture to write the result to, then copy it back to the
  72. // proper mipmap level.
  73. texelSize.set( 1.0 / width, 1.0 / height );
  74. if ( mip == 0 ) texelSize.set( 0.0, 0.0 );
  75. _renderer.setViewport( position.x, position.y, width / dpr, height / dpr );
  76. _renderer.render( _scene, _flatCamera );
  77. _renderer.copyFramebufferToTexture( position, material.roughnessMap, mip );
  78. _mipmapMaterial.uniforms.roughnessMap.value = material.roughnessMap;
  79. }
  80. if ( roughnessMap !== material.roughnessMap ) roughnessMap.dispose();
  81. _renderer.autoClear = autoClear;
  82. _renderer.setRenderTarget( null );
  83. var size = _renderer.getSize( new Vector2() );
  84. _renderer.setViewport( 0, 0, size.x, size.y );
  85. },
  86. dispose: function ( ) {
  87. _mipmapMaterial.dispose();
  88. _scene.children[ 0 ].geometry.dispose();
  89. if ( _tempTarget != null ) _tempTarget.dispose();
  90. }
  91. };
  92. function _getMipmapMaterial() {
  93. var shaderMaterial = new RawShaderMaterial( {
  94. uniforms: {
  95. roughnessMap: { value: null },
  96. normalMap: { value: null },
  97. texelSize: { value: new Vector2( 1, 1 ) }
  98. },
  99. vertexShader: `
  100. precision mediump float;
  101. precision mediump int;
  102. attribute vec3 position;
  103. attribute vec2 uv;
  104. varying vec2 vUv;
  105. void main() {
  106. vUv = uv;
  107. gl_Position = vec4( position, 1.0 );
  108. }
  109. `,
  110. fragmentShader: `
  111. precision mediump float;
  112. precision mediump int;
  113. varying vec2 vUv;
  114. uniform sampler2D roughnessMap;
  115. uniform sampler2D normalMap;
  116. uniform vec2 texelSize;
  117. #define ENVMAP_TYPE_CUBE_UV
  118. vec4 envMapTexelToLinear(vec4 a){return a;}
  119. #include <cube_uv_reflection_fragment>
  120. void main() {
  121. gl_FragColor = texture2D(roughnessMap, vUv, -1.0);
  122. if (texelSize.x == 0.0) return;
  123. float roughness = gl_FragColor.g;
  124. float variance = roughnessToVariance(roughness);
  125. vec3 avgNormal;
  126. for (float x = -1.0; x < 2.0; x += 2.0) {
  127. for (float y = -1.0; y < 2.0; y += 2.0) {
  128. vec2 uv = vUv + vec2(x, y) * 0.25 * texelSize;
  129. avgNormal += normalize(texture2D(normalMap, uv, -1.0).xyz - 0.5);
  130. }
  131. }
  132. variance += 1.0 - 0.25 * length(avgNormal);
  133. gl_FragColor.g = varianceToRoughness(variance);
  134. }
  135. `,
  136. blending: NoBlending,
  137. depthTest: false,
  138. depthWrite: false
  139. } );
  140. shaderMaterial.type = 'RoughnessMipmapper';
  141. return shaderMaterial;
  142. }
  143. return RoughnessMipmapper;
  144. } )();
  145. export { RoughnessMipmapper };