summit/frontend/node_modules/three-stdlib/utils/RoughnessMipmapper.cjs.map

1 line
10 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"RoughnessMipmapper.cjs","sources":["../../src/utils/RoughnessMipmapper.js"],"sourcesContent":["/**\n * This class generates custom mipmaps for a roughness map by encoding the lost variation in the\n * normal map mip levels as increased roughness in the corresponding roughness mip levels. This\n * helps with rendering accuracy for MeshStandardMaterial, and also helps with anti-aliasing when\n * using PMREM. If the normal map is larger than the roughness map, the roughness map will be\n * enlarged to match the dimensions of the normal map.\n */\n\nimport {\n MathUtils,\n Mesh,\n NoBlending,\n OrthographicCamera,\n PlaneGeometry,\n RawShaderMaterial,\n Vector2,\n WebGLRenderTarget,\n} from 'three'\n\nvar _mipmapMaterial = /* @__PURE__ */ _getMipmapMaterial()\n\nvar _mesh = /* @__PURE__ */ new Mesh(/* @__PURE__ */ new PlaneGeometry(2, 2), _mipmapMaterial)\n\nvar _flatCamera = /* @__PURE__ */ new OrthographicCamera(0, 1, 0, 1, 0, 1)\n\nvar _tempTarget = null\n\nclass RoughnessMipmapper {\n constructor(renderer) {\n this._renderer = renderer\n\n this._renderer.compile(_mesh, _flatCamera)\n }\n\n generateMipmaps = function (material) {\n if ('roughnessMap' in material === false) return\n\n var { roughnessMap, normalMap } = material\n\n if (\n roughnessMap === null ||\n normalMap === null ||\n !roughnessMap.generateMipmaps ||\n material.userData.roughnessUpdated\n ) {\n return\n }\n\n material.userData.roughnessUpdated = true\n\n var width = Math.max(roughnessMap.image.width, normalMap.image.width)\n\n var height = Math.max(roughnessMap.image.height, normalMap.image.height)\n\n if (!MathUtils.isPowerOfTwo(width) || !MathUtils.isPowerOfTwo(height)) return\n\n var oldTarget = this._renderer.getRenderTarget()\n\n var autoClear = this._renderer.autoClear\n\n this._renderer.autoClear = false\n\n if (_tempTarget === null || _tempTarget.width !== width || _tempTarget.height !== height) {\n if (_tempTarget !== null) _tempTarget.dispose()\n\n _tempTarget = new WebGLRenderTarget(width, height, {\n depthBuffer: false,\n })\n\n _tempTarget.scissorTest = true\n }\n\n if (width !== roughnessMap.image.width || height !== roughnessMap.image.height) {\n var params = {\n wrapS: roughnessMap.wrapS,\n wrapT: roughnessMap.wrapT,\n magFilter: roughnessMap.magFilter,\n minFilter: roughnessMap.minFilter,\n depthBuffer: false,\n }\n\n var newRoughnessTarget = new WebGLRenderTarget(width, height, params)\n\n newRoughnessTarget.texture.generateMipmaps = true\n\n // Setting the render target causes the memory to be allocated.\n\n this._renderer.setRenderTarget(newRoughnessTarget)\n\n material.roughnessMap = newRoughnessTarget.texture\n\n if (material.metalnessMap == roughnessMap) material.metalnessMap = material.roughnessMap\n\n if (material.aoMap == roughnessMap) material.aoMap = material.roughnessMap\n }\n\n _mipmapMaterial.uniforms.roughnessMap.value = roughnessMap\n\n _mipmapMaterial.uniforms.normalMap.value = normalMap\n\n var position = new Vector2(0, 0)\n\n var texelSize = _mipmapMaterial.uniforms.texelSize.value\n\n for (let mip = 0; width >= 1 && height >= 1; ++mip, width /= 2, height /= 2) {\n // Rendering to a mip level is not allowed in webGL1. Instead we must set\n // up a secondary texture to write the result to, then copy it back to the\n // proper mipmap level.\n\n texelSize.set(1.0 / width, 1.0 / height)\n\n if (mip == 0) texelSize.set(0.0, 0.0)\n\n _tempTarget.viewport.set(position.x, position.y, width, height)\n\n _tempTarget.scissor.set(position.x, position.y, width, height)\n\n this._renderer.setRenderTarget(_tempTarget)\n\n this._renderer.render(_mesh, _flatCamera)\n\n this._renderer.copyFramebufferToTexture(position, material.roughnessMap, mip)\n\n _mipmapMaterial.uniforms.roughnessMap.value = material.roughnessMap\n }\n\n if (roughnessMap !==