RoughnessMipmapper.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * This class generates custom mipmaps for a roughness map by encoding the lost variation in the
  3. * normal map mip levels as increased roughness in the corresponding roughness mip levels. This
  4. * helps with rendering accuracy for MeshStandardMaterial, and also helps with anti-aliasing when
  5. * using PMREM. If the normal map is larger than the roughness map, the roughness map will be
  6. * enlarged to match the dimensions of the normal map.
  7. */
  8. import {
  9. MathUtils,
  10. Mesh,
  11. NoBlending,
  12. OrthographicCamera,
  13. PlaneGeometry,
  14. RawShaderMaterial,
  15. Vector2,
  16. WebGLRenderTarget
  17. } from '../../../build/three.module.js';
  18. const _mipmapMaterial = _getMipmapMaterial();
  19. const _mesh = new Mesh( new PlaneGeometry( 2, 2 ), _mipmapMaterial );
  20. const _flatCamera = new OrthographicCamera( 0, 1, 0, 1, 0, 1 );
  21. let _tempTarget = null;
  22. let _renderer = null;
  23. class RoughnessMipmapper {
  24. constructor( renderer ) {
  25. _renderer = renderer;
  26. _renderer.compile( _mesh, _flatCamera );
  27. }
  28. generateMipmaps( material ) {
  29. if ( 'roughnessMap' in material === false ) return;
  30. const { roughnessMap, normalMap } = material;
  31. if ( roughnessMap === null || normalMap === null || ! roughnessMap.generateMipmaps || material.userData.roughnessUpdated ) return;
  32. material.userData.roughnessUpdated = true;
  33. let width = Math.max( roughnessMap.image.width, normalMap.image.width );
  34. let height = Math.max( roughnessMap.image.height, normalMap.image.height );
  35. if ( ! MathUtils.isPowerOfTwo( width ) || ! MathUtils.isPowerOfTwo( height ) ) return;
  36. const oldTarget = _renderer.getRenderTarget();
  37. const autoClear = _renderer.autoClear;
  38. _renderer.autoClear = false;
  39. if ( _tempTarget === null || _tempTarget.width !== width || _tempTarget.height !== height ) {
  40. if ( _tempTarget !== null ) _tempTarget.dispose();
  41. _tempTarget = new WebGLRenderTarget( width, height, { depthBuffer: false } );
  42. _tempTarget.scissorTest = true;
  43. }
  44. if ( width !== roughnessMap.image.width || height !== roughnessMap.image.height ) {
  45. const params = {
  46. wrapS: roughnessMap.wrapS,
  47. wrapT: roughnessMap.wrapT,
  48. magFilter: roughnessMap.magFilter,
  49. minFilter: roughnessMap.minFilter,
  50. depthBuffer: false
  51. };
  52. const newRoughnessTarget = new WebGLRenderTarget( width, height, params );
  53. newRoughnessTarget.texture.generateMipmaps = true;
  54. // Setting the render target causes the memory to be allocated.
  55. _renderer.setRenderTarget( newRoughnessTarget );
  56. material.roughnessMap = newRoughnessTarget.texture;
  57. if ( material.metalnessMap == roughnessMap ) material.metalnessMap = material.roughnessMap;
  58. if ( material.aoMap == roughnessMap ) material.aoMap = material.roughnessMap;
  59. // Copy UV transform parameters
  60. material.roughnessMap.offset.copy( roughnessMap.offset );
  61. material.roughnessMap.repeat.copy( roughnessMap.repeat );
  62. material.roughnessMap.center.copy( roughnessMap.center );
  63. material.roughnessMap.rotation = roughnessMap.rotation;
  64. material.roughnessMap.image = roughnessMap.image;
  65. material.roughnessMap.matrixAutoUpdate = roughnessMap.matrixAutoUpdate;
  66. material.roughnessMap.matrix.copy( roughnessMap.matrix );
  67. }
  68. _mipmapMaterial.uniforms.roughnessMap.value = roughnessMap;
  69. _mipmapMaterial.uniforms.normalMap.value = normalMap;
  70. const position = new Vector2( 0, 0 );
  71. const texelSize = _mipmapMaterial.uniforms.texelSize.value;
  72. for ( let mip = 0; width >= 1 && height >= 1; ++ mip, width /= 2, height /= 2 ) {
  73. // Rendering to a mip level is not allowed in webGL1. Instead we must set
  74. // up a secondary texture to write the result to, then copy it back to the
  75. // proper mipmap level.
  76. texelSize.set( 1.0 / width, 1.0 / height );
  77. if ( mip == 0 ) texelSize.set( 0.0, 0.0 );
  78. _tempTarget.viewport.set( position.x, position.y, width, height );
  79. _tempTarget.scissor.set( position.x, position.y, width, height );
  80. _renderer.setRenderTarget( _tempTarget );
  81. _renderer.render( _mesh, _flatCamera );
  82. _renderer.copyFramebufferToTexture( position, material.roughnessMap, mip );
  83. _mipmapMaterial.uniforms.roughnessMap.value = material.roughnessMap;
  84. }
  85. if ( roughnessMap !== material.roughnessMap ) roughnessMap.dispose();
  86. _renderer.setRenderTarget( oldTarget );
  87. _renderer.autoClear = autoClear;
  88. }
  89. dispose() {
  90. _mipmapMaterial.dispose();
  91. _mesh.geometry.dispose();
  92. if ( _tempTarget != null ) _tempTarget.dispose();
  93. }
  94. }
  95. function _getMipmapMaterial() {
  96. const shaderMaterial = new RawShaderMaterial( {
  97. uniforms: {
  98. roughnessMap: { value: null },
  99. normalMap: { value: null },
  100. texelSize: { value: new Vector2( 1, 1 ) }
  101. },
  102. vertexShader: /* glsl */`
  103. precision mediump float;
  104. precision mediump int;
  105. attribute vec3 position;
  106. attribute vec2 uv;
  107. varying vec2 vUv;
  108. void main() {
  109. vUv = uv;
  110. gl_Position = vec4( position, 1.0 );
  111. }
  112. `,
  113. fragmentShader: /* glsl */`
  114. precision mediump float;
  115. precision mediump int;
  116. varying vec2 vUv;
  117. uniform sampler2D roughnessMap;
  118. uniform sampler2D normalMap;
  119. uniform vec2 texelSize;
  120. #define ENVMAP_TYPE_CUBE_UV
  121. vec4 envMapTexelToLinear( vec4 a ) { return a; }
  122. #include <cube_uv_reflection_fragment>
  123. float roughnessToVariance( float roughness ) {
  124. float variance = 0.0;
  125. if ( roughness >= r1 ) {
  126. variance = ( r0 - roughness ) * ( v1 - v0 ) / ( r0 - r1 ) + v0;
  127. } else if ( roughness >= r4 ) {
  128. variance = ( r1 - roughness ) * ( v4 - v1 ) / ( r1 - r4 ) + v1;
  129. } else if ( roughness >= r5 ) {
  130. variance = ( r4 - roughness ) * ( v5 - v4 ) / ( r4 - r5 ) + v4;
  131. } else {
  132. float roughness2 = roughness * roughness;
  133. variance = 1.79 * roughness2 * roughness2;
  134. }
  135. return variance;
  136. }
  137. float varianceToRoughness( float variance ) {
  138. float roughness = 0.0;
  139. if ( variance >= v1 ) {
  140. roughness = ( v0 - variance ) * ( r1 - r0 ) / ( v0 - v1 ) + r0;
  141. } else if ( variance >= v4 ) {
  142. roughness = ( v1 - variance ) * ( r4 - r1 ) / ( v1 - v4 ) + r1;
  143. } else if ( variance >= v5 ) {
  144. roughness = ( v4 - variance ) * ( r5 - r4 ) / ( v4 - v5 ) + r4;
  145. } else {
  146. roughness = pow( 0.559 * variance, 0.25 ); // 0.559 = 1.0 / 1.79
  147. }
  148. return roughness;
  149. }
  150. void main() {
  151. gl_FragColor = texture2D( roughnessMap, vUv, - 1.0 );
  152. if ( texelSize.x == 0.0 ) return;
  153. float roughness = gl_FragColor.g;
  154. float variance = roughnessToVariance( roughness );
  155. vec3 avgNormal;
  156. for ( float x = - 1.0; x < 2.0; x += 2.0 ) {
  157. for ( float y = - 1.0; y < 2.0; y += 2.0 ) {
  158. vec2 uv = vUv + vec2( x, y ) * 0.25 * texelSize;
  159. avgNormal += normalize( texture2D( normalMap, uv, - 1.0 ).xyz - 0.5 );
  160. }
  161. }
  162. variance += 1.0 - 0.25 * length( avgNormal );
  163. gl_FragColor.g = varianceToRoughness( variance );
  164. }
  165. `,
  166. blending: NoBlending,
  167. depthTest: false,
  168. depthWrite: false
  169. } );
  170. shaderMaterial.type = 'RoughnessMipmapper';
  171. return shaderMaterial;
  172. }
  173. export { RoughnessMipmapper };