1 line
9.9 KiB
Plaintext
1 line
9.9 KiB
Plaintext
|
|
{"version":3,"file":"RenderPixelatedPass.cjs","sources":["../../src/postprocessing/RenderPixelatedPass.js"],"sourcesContent":["import {\n WebGLRenderTarget,\n RGBAFormat,\n MeshNormalMaterial,\n ShaderMaterial,\n Vector2,\n Vector4,\n DepthTexture,\n NearestFilter,\n} from 'three'\nimport { Pass, FullScreenQuad } from './Pass'\n\nclass RenderPixelatedPass extends Pass {\n constructor(resolution, pixelSize, scene, camera, options = {}) {\n super()\n\n this.pixelSize = pixelSize\n this.resolution = new Vector2()\n this.renderResolution = new Vector2()\n this.setSize(resolution.x, resolution.y)\n\n this.fsQuad = new FullScreenQuad(this.material())\n this.scene = scene\n this.camera = camera\n\n this.normalEdgeStrength = options.normalEdgeStrength ?? 0.3\n this.depthEdgeStrength = options.depthEdgeStrength ?? 0.4\n\n this.rgbRenderTarget = pixelRenderTarget(this.renderResolution, RGBAFormat, true)\n this.normalRenderTarget = pixelRenderTarget(this.renderResolution, RGBAFormat, false)\n\n this.normalMaterial = new MeshNormalMaterial()\n }\n\n dispose() {\n this.rgbRenderTarget.dispose()\n this.normalRenderTarget.dispose()\n this.fsQuad.dispose()\n }\n\n setSize(width, height) {\n this.resolution.set(width, height)\n this.renderResolution.set((width / this.pixelSize) | 0, (height / this.pixelSize) | 0)\n const { x, y } = this.renderResolution\n this.rgbRenderTarget?.setSize(x, y)\n this.normalRenderTarget?.setSize(x, y)\n this.fsQuad?.material.uniforms.resolution.value.set(x, y, 1 / x, 1 / y)\n }\n\n setPixelSize(pixelSize) {\n this.pixelSize = pixelSize\n this.setSize(this.resolution.x, this.resolution.y)\n }\n\n render(renderer, writeBuffer) {\n const uniforms = this.fsQuad.material.uniforms\n uniforms.normalEdgeStrength.value = this.normalEdgeStrength\n uniforms.depthEdgeStrength.value = this.depthEdgeStrength\n\n renderer.setRenderTarget(this.rgbRenderTarget)\n renderer.render(this.scene, this.camera)\n\n const overrideMaterial_old = this.scene.overrideMaterial\n renderer.setRenderTarget(this.normalRenderTarget)\n this.scene.overrideMaterial = this.normalMaterial\n renderer.render(this.scene, this.camera)\n this.scene.overrideMaterial = overrideMaterial_old\n\n uniforms.tDiffuse.value = this.rgbRenderTarget.texture\n uniforms.tDepth.value = this.rgbRenderTarget.depthTexture\n uniforms.tNormal.value = this.normalRenderTarget.texture\n\n if (this.renderToScreen) {\n renderer.setRenderTarget(null)\n } else {\n renderer.setRenderTarget(writeBuffer)\n\n if (this.clear) renderer.clear()\n }\n\n this.fsQuad.render(renderer)\n }\n\n material() {\n return new ShaderMaterial({\n uniforms: {\n tDiffuse: { value: null },\n tDepth: { value: null },\n tNormal: { value: null },\n resolution: {\n value: new Vector4(\n this.renderResolution.x,\n this.renderResolution.y,\n 1 / this.renderResolution.x,\n 1 / this.renderResolution.y,\n ),\n },\n normalEdgeStrength: { value: 0 },\n depthEdgeStrength: { value: 0 },\n },\n vertexShader: `\n\t\t\t\tvarying vec2 vUv;\n\n\t\t\t\tvoid main() {\n\n\t\t\t\t\tvUv = uv;\n\t\t\t\t\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\n\t\t\t\t}\n\t\t\t\t`,\n fragmentShader: `\n\t\t\t\tuniform sampler2D tDiffuse;\n\t\t\t\tuniform sampler2D tDepth;\n\t\t\t\tuniform sampler2D tNormal;\n\t\t\t\tuniform vec4 resolution;\n\t\t\t\tuniform float normalEdgeStrength;\n\t\t\t\tuniform float depthEdgeStrength;\n\t\t\t\tvarying vec2 vUv;\n\n\t\t\t\tfloat getDepth(int x, int y) {\n\n\t\t\t\t\treturn texture2D( tDepth, vUv + vec2(x, y) * resolution.zw ).r;\n\n\t\t\t\t}\n\n\t\t\t\tvec3 getNormal(int x, int y) {\n\n\t\t\t\t\treturn texture2D( tNormal, vUv + vec2(x, y) * resolution.zw ).rgb * 2.0 - 1.0;\n\n\t\t\t\t}\n\n\t\t\t\tfloat depthEdgeIndicator(float depth, vec3 normal) {\n\n\t\t\t\t\tfloat diff = 0.0;\n\t
|