RoughnessMipmapper.js 6.2 KB

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