RoughnessMipmapper.js 6.4 KB

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