// src/index.ts
import { KernelSize as KernelSize2, Pass as Pass4 } from "postprocessing";
import * as THREE5 from "three";

// src/bilateralFilter.ts
import { KernelSize, Pass } from "postprocessing";
import * as THREE from "three";

// src/bilateralFilter.frag
var bilateralFilter_default = "uniform sampler2D tInput;uniform vec2 resolution;uniform float bSigma;varying vec2 vUv;\n#if (KSIZE_ENUM == 0)\n#define KSIZE 2\n#define MSIZE 5\nconst float kernel[MSIZE]=float[MSIZE](0.,0.24196934138575799,0.39894,0.24196934138575799,0.);\n#elif (KSIZE_ENUM == 1)\n#define KSIZE 3\n#define MSIZE 7\nconst float kernel[MSIZE]=float[MSIZE](0.,0.39104045872899694,0.3969502784491287,0.39894,0.3969502784491287,0.39104045872899694,0.);\n#elif (KSIZE_ENUM == 2)\n#define KSIZE 4\n#define MSIZE 9\nconst float kernel[MSIZE]=float[MSIZE](0.,0.3813856354024969,0.39104045872899694,0.3969502784491287,0.39894,0.3969502784491287,0.39104045872899694,0.3813856354024969,0.);\n#elif (KSIZE_ENUM == 3)\n#define KSIZE 5\n#define MSIZE 11\nconst float kernel[MSIZE]=float[MSIZE](0.,0.03682680352274845,0.03813856354024969,0.039104045872899694,0.03969502784491287,0.039894,0.03969502784491287,0.039104045872899694,0.03813856354024969,0.03682680352274845,0.);\n#elif (KSIZE_ENUM == 4)\n#define KSIZE 6\n#define MSIZE 13\nconst float kernel[MSIZE]=float[MSIZE](0.,0.035206331431709856,0.03682680352274845,0.03813856354024969,0.039104045872899694,0.03969502784491287,0.039894,0.03969502784491287,0.039104045872899694,0.03813856354024969,0.03682680352274845,0.035206331431709856,0.);\n#elif (KSIZE_ENUM == 5)\n#define KSIZE 7\n#define MSIZE 15\nconst float kernel[MSIZE]=float[MSIZE](0.031225216,0.033322271,0.035206333,0.036826804,0.038138565,0.039104044,0.039695028,0.039894000,0.039695028,0.039104044,0.038138565,0.036826804,0.035206333,0.033322271,0.031225216);\n#endif\nfloat normpdf(in float x,in float sigma){return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;}float normpdf3(in vec3 v,in float sigma){return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;}void main(){vec3 c=texture(tInput,vUv).rgb;ivec2 fragCoord=ivec2(vUv*resolution);vec3 finalColor=vec3(0.);float bZ=1.0/normpdf(0.0,bSigma);float totalFactor=0.;for(int i=-KSIZE;i<=KSIZE;++i){for(int j=-KSIZE;j<=KSIZE;++j){vec3 cc=texelFetch(tInput,fragCoord+ivec2(i,j),0).rgb;float factor=normpdf3(cc-c,bSigma)*bZ*kernel[KSIZE+j]*kernel[KSIZE+i];totalFactor+=factor;finalColor+=factor*cc;}}gl_FragColor=vec4(finalColor/totalFactor,1.);}";

// src/compositor.vert
var compositor_default = "varying vec2 vUv;void main(){vUv=uv;gl_Position=projectionMatrix*modelViewMatrix*vec4(position,1.0);}";

// src/bilateralFilter.ts
var GODRAYS_BLUR_RESOLUTION_SCALE = 1;
var BilateralFilterMaterial = class extends THREE.ShaderMaterial {
  constructor(input) {
    super({
      uniforms: {
        tInput: { value: input },
        resolution: {
          value: new THREE.Vector2(
            input.image.width * GODRAYS_BLUR_RESOLUTION_SCALE,
            input.image.height * GODRAYS_BLUR_RESOLUTION_SCALE
          )
        },
        bSigma: { value: 0 }
      },
      defines: {
        KSIZE_ENUM: KernelSize.SMALL
      },
      vertexShader: compositor_default,
      fragmentShader: bilateralFilter_default
    });
  }
};
var BilateralFilterPass = class extends Pass {
  constructor(input) {
    super("BilateralFilterPass");
    this.needsSwap = false;
    this.material = new BilateralFilterMaterial(input);
    this.fullscreenMaterial = this.material;
  }
  setSize(width, height) {
    this.material.uniforms.resolution.value.set(width, height);
  }
  render(renderer, _inputBuffer, outputBuffer, _deltaTime, _stencilTest) {
    renderer.setRenderTarget(outputBuffer);
    renderer.render(this.scene, this.camera);
  }
  updateUniforms(params) {
    this.material.uniforms.bSigma.value = params.variance;
    this.material.defines.KSIZE_ENUM = params.kernelSize;
  }
  dispose() {
    this.material.dispose();
    super.dispose();
  }
};

// src/compositorPass.ts
import { CopyPass, Pass as Pass2 } from "postprocessing";
import * as THREE2 from "three";

// src/compositor.frag
var compositor_default2 = "#include <common>\nuniform sampler2D godrays;uniform sampler2D sceneDiffuse;uniform sampler2D sceneDepth;uniform float edgeStrength;uniform float edgeRadius;uniform vec2 resolution;uniform float near;uniform float far;uniform vec3 color;uniform bool gammaCorrection;varying vec2 vUv;\n#define DITHERING\n#include <dithering_pars_fragment>\nfloat linearize_depth(float depth,float zNear,float zFar){\n#if defined( USE_LOGDEPTHBUF )\nfloat d=pow(2.0,depth*log2(far+1.0))-1.0;float a=far/(far-near);float b=far*near/(near-far);depth=a+b/d;\n#endif\nreturn zNear*zFar/(zFar+depth*(zNear-zFar));}vec4 LinearTosRGB_(in vec4 value){return vec4(mix(pow(value.rgb,vec3(0.41666))*1.055-vec3(0.055),value.rgb*12.92,vec3(lessThanEqual(value.rgb,vec3(0.0031308)))),value.a);}void main(){float rawDepth=texture2D(sceneDepth,vUv).x;float correctDepth=linearize_depth(rawDepth,near,far);vec2 pushDir=vec2(0.0);float count=0.0;for(float x=-edgeRadius;x<=edgeRadius;x++){for(float y=-edgeRadius;y<=edgeRadius;y++){vec2 sampleUv=(vUv*resolution+vec2(x,y))/resolution;float sampleDepth=texelFetch(sceneDepth,ivec2(sampleUv*resolution),0).x;sampleDepth=linearize_depth(sampleDepth,near,far);if(abs(sampleDepth-correctDepth)<0.05*correctDepth){pushDir+=vec2(x,y);count+=1.0;}}}if(count==0.0){count=1.0;}pushDir/=count;pushDir=normalize(pushDir);vec2 sampleUv=length(pushDir)>0.0?vUv+edgeStrength*(pushDir/resolution):vUv;float bestChoice=texture2D(godrays,sampleUv).x;vec3 diffuse=texture2D(sceneDiffuse,vUv).rgb;gl_FragColor=vec4(mix(diffuse,color,bestChoice),1.0);\n#include <dithering_fragment>\nif(gammaCorrection){gl_FragColor=LinearTosRGB_(gl_FragColor);}}";

// src/compositorPass.ts
var GodraysCompositorMaterial = class extends THREE2.ShaderMaterial {
  constructor({
    godrays,
    edgeStrength,
    edgeRadius,
    color,
    camera,
    gammaCorrection
  }) {
    const uniforms = {
      godrays: { value: godrays },
      sceneDiffuse: { value: null },
      sceneDepth: { value: null },
      edgeStrength: { value: edgeStrength },
      edgeRadius: { value: edgeRadius },
      near: { value: 0.1 },
      far: { value: 1e3 },
      color: { value: color },
      resolution: { value: new THREE2.Vector2(1, 1) },
      gammaCorrection: { value: 1 }
    };
    super({
      name: "GodraysCompositorMaterial",
      uniforms,
      depthWrite: false,
      depthTest: false,
      fragmentShader: compositor_default2,
      vertexShader: compositor_default
    });
    this.updateUniforms(edgeStrength, edgeRadius, color, gammaCorrection, camera.near, camera.far);
  }
  updateUniforms(edgeStrength, edgeRadius, color, gammaCorrection, near, far) {
    this.uniforms.edgeStrength.value = edgeStrength;
    this.uniforms.edgeRadius.value = edgeRadius;
    this.uniforms.color.value = color;
    this.uniforms.near.value = near;
    this.uniforms.far.value = far;
    this.uniforms.gammaCorrection.value = gammaCorrection ? 1 : 0;
  }
  setSize(width, height) {
    this.uniforms.resolution.value.set(width, height);
  }
};
var GodraysCompositorPass = class extends Pass2 {
  constructor(props) {
    super("GodraysCompositorPass");
    this.depthCopyRenderTexture = null;
    this.depthTextureCopyPass = null;
    this.fullscreenMaterial = new GodraysCompositorMaterial(props);
    this.sceneCamera = props.camera;
  }
  updateUniforms(params) {
    this.fullscreenMaterial.updateUniforms(
      params.edgeStrength,
      params.edgeRadius,
      params.color,
      params.gammaCorrection,
      this.sceneCamera.near,
      this.sceneCamera.far
    );
  }
  render(renderer, inputBuffer, outputBuffer, _deltaTime, _stencilTest) {
    this.fullscreenMaterial.uniforms.sceneDiffuse.value = inputBuffer.texture;
    const sceneDepth = this.fullscreenMaterial.uniforms.sceneDepth.value;
    if (sceneDepth && outputBuffer && outputBuffer.depthTexture && sceneDepth === outputBuffer.depthTexture) {
      if (!this.depthCopyRenderTexture) {
        this.depthCopyRenderTexture = new THREE2.WebGLRenderTarget(
          outputBuffer.depthTexture.image.width,
          outputBuffer.depthTexture.image.height,
          {
            minFilter: outputBuffer.depthTexture.minFilter,
            magFilter: outputBuffer.depthTexture.magFilter,
            format: outputBuffer.depthTexture.format,
            generateMipmaps: outputBuffer.depthTexture.generateMipmaps
          }
        );
      }
      if (!this.depthTextureCopyPass) {
        this.depthTextureCopyPass = new CopyPass();
      }
      this.depthTextureCopyPass.render(
        renderer,
        this.fullscreenMaterial.uniforms.sceneDepth.value,
        this.depthCopyRenderTexture
      );
      this.fullscreenMaterial.uniforms.sceneDepth.value = this.depthCopyRenderTexture.texture;
    }
    renderer.setRenderTarget(outputBuffer);
    renderer.render(this.scene, this.camera);
    this.fullscreenMaterial.uniforms.sceneDepth.value = sceneDepth;
  }
  setDepthTexture(depthTexture, depthPacking) {
    if (depthPacking && depthPacking !== THREE2.BasicDepthPacking) {
      throw new Error("Only BasicDepthPacking is supported");
    }
    this.fullscreenMaterial.uniforms.sceneDepth.value = depthTexture;
  }
  setSize(width, height) {
    this.fullscreenMaterial.setSize(width, height);
  }
};

// src/illumPass.ts
import { Pass as Pass3 } from "postprocessing";
import * as THREE4 from "three";

// src/bluenoise.ts
import * as THREE3 from "three";
var BlueNoiseTextureDataURI = "data:image/png;base64,[encoded data, 291584 characters]";
var getBlueNoiseTexture = async () => {
  const textureLoader = new THREE3.TextureLoader();
  const blueNoiseTexture = await textureLoader.loadAsync(BlueNoiseTextureDataURI);
  blueNoiseTexture.wrapS = THREE3.RepeatWrapping;
  blueNoiseTexture.wrapT = THREE3.RepeatWrapping;
  blueNoiseTexture.magFilter = THREE3.NearestFilter;
  blueNoiseTexture.minFilter = THREE3.NearestFilter;
  blueNoiseTexture.generateMipmaps = false;
  return blueNoiseTexture;
};

// src/godrays.frag
var godrays_default = "varying vec2 vUv;uniform sampler2D sceneDepth;uniform sampler2D blueNoise;uniform vec3 lightPos;uniform vec3 cameraPos;uniform vec2 resolution;uniform mat4 cameraProjectionMatrixInv;uniform mat4 cameraMatrixWorld;uniform sampler2D shadowMap;uniform vec2 noiseResolution;uniform float texelSizeY;uniform float lightCameraNear;uniform float lightCameraFar;uniform float near;uniform float far;uniform float density;uniform float maxDensity;uniform float distanceAttenuation;uniform vec3[6]fNormals;uniform float[6]fConstants;uniform float raymarchSteps;uniform mat4 premultipliedLightCameraMatrix;\n#include <packing>\nvec3 WorldPosFromDepth(float depth,vec2 coord){\n#if defined( USE_LOGDEPTHBUF )\nfloat d=pow(2.0,depth*log2(far+1.0))-1.0;float a=far/(far-near);float b=far*near/(near-far);depth=a+b/d;\n#endif\nfloat z=depth*2.0-1.0;vec4 clipSpacePosition=vec4(coord*2.0-1.0,z,1.0);vec4 viewSpacePosition=cameraProjectionMatrixInv*clipSpacePosition;viewSpacePosition/=viewSpacePosition.w;vec4 worldSpacePosition=cameraMatrixWorld*viewSpacePosition;return worldSpacePosition.xyz;}vec2 cubeToUV(vec3 v){vec3 absV=abs(v);float scaleToCube=1.0/max(absV.x,max(absV.y,absV.z));absV*=scaleToCube;v*=scaleToCube*(1.0-2.0*texelSizeY);vec2 planar=v.xy;float almostATexel=1.5*texelSizeY;float almostOne=1.0-almostATexel;if(absV.z>=almostOne){if(v.z>0.0)planar.x=4.0-v.x;}else if(absV.x>=almostOne){float signX=sign(v.x);planar.x=v.z*signX+2.0*signX;}else if(absV.y>=almostOne){float signY=sign(v.y);planar.x=v.x+2.0*signY+2.0;planar.y=v.z*signY-2.0;}return vec2(0.125,0.25)*planar+vec2(0.375,0.75);}vec3 projectToShadowMap(vec3 worldPos){vec4 lightSpacePos=premultipliedLightCameraMatrix*vec4(worldPos,1.0);lightSpacePos/=lightSpacePos.w;lightSpacePos=lightSpacePos*0.5+0.5;return lightSpacePos.xyz;}vec2 inShadow(vec3 worldPos){\n#if defined(IS_POINT_LIGHT)\nvec2 shadowMapUV=cubeToUV(normalize(worldPos-lightPos));\n#elif defined(IS_DIRECTIONAL_LIGHT)\nvec3 shadowMapUV=projectToShadowMap(worldPos);bool isOutsideShadowMap=shadowMapUV.x<0.0||shadowMapUV.x>1.0||shadowMapUV.y<0.0||shadowMapUV.y>1.0||shadowMapUV.z<0.0||shadowMapUV.z>1.0;if(isOutsideShadowMap){return vec2(1.0,0.0);}\n#endif\nvec4 packedDepth=texture2D(shadowMap,shadowMapUV.xy);float depth=unpackRGBAToDepth(packedDepth);depth=lightCameraNear+(lightCameraFar-lightCameraNear)*depth;\n#if defined(IS_POINT_LIGHT)\nfloat lightDist=distance(worldPos,lightPos);\n#elif defined(IS_DIRECTIONAL_LIGHT)\nfloat lightDist=(lightCameraNear+(lightCameraFar-lightCameraNear)*shadowMapUV.z);\n#endif\nfloat difference=lightDist-depth;return vec2(float(difference>0.0),lightDist);}float sdPlane(vec3 p,vec3 n,float h){return dot(p,n)+h;}float intersectRayPlane(vec3 rayOrigin,vec3 rayDirection,vec3 planeNormal,float planeDistance){float denom=dot(planeNormal,rayDirection);return-(sdPlane(rayOrigin,planeNormal,planeDistance)/denom);}void main(){float depth=texture2D(sceneDepth,vUv).x;vec3 worldPos=WorldPosFromDepth(depth,vUv);float inBoxDist=-10000.0;for(int i=0;i<6;i++){inBoxDist=max(inBoxDist,sdPlane(cameraPos,fNormals[i],fConstants[i]));}bool cameraIsInBox=inBoxDist<0.0;vec3 startPos=cameraPos;if(cameraIsInBox){for(int i=0;i<6;i++){if(sdPlane(worldPos,fNormals[i],fConstants[i])>0.0){vec3 direction=normalize(worldPos-cameraPos);float t=intersectRayPlane(cameraPos,direction,fNormals[i],fConstants[i]);worldPos=cameraPos+t*direction;}}}else{vec3 direction=normalize(worldPos-cameraPos);float minT=10000.0;for(int i=0;i<6;i++){float t=intersectRayPlane(cameraPos,direction,fNormals[i],fConstants[i]);if(t<minT&&t>0.0){minT=t;}}if(minT==10000.0){gl_FragColor=vec4(0.0,0.0,0.0,1.0);return;}startPos=cameraPos+(minT+0.001)*direction;float endInBoxDist=-10000.0;for(int i=0;i<6;i++){endInBoxDist=max(endInBoxDist,sdPlane(worldPos,fNormals[i],fConstants[i]));}bool endInBox=false;if(endInBoxDist<0.0){endInBox=true;}if(!endInBox){float minT=10000.0;for(int i=0;i<6;i++){if(sdPlane(worldPos,fNormals[i],fConstants[i])>0.0){float t=intersectRayPlane(startPos,direction,fNormals[i],fConstants[i]);if(t<minT&&t>0.0){minT=t;}}}if(minT<distance(worldPos,startPos)){worldPos=startPos+minT*direction;}}}float illum=0.0;vec4 blueNoiseSample=texture2D(blueNoise,vUv*(resolution/noiseResolution));float samplesFloat=round(raymarchSteps+((raymarchSteps/8.)+2.)*blueNoiseSample.x);int samples=int(samplesFloat);for(int i=0;i<samples;i++){vec3 samplePos=mix(startPos,worldPos,float(i)/samplesFloat);vec2 shadowInfo=inShadow(samplePos);float shadowAmount=1.0-shadowInfo.x;illum+=shadowAmount*(distance(startPos,worldPos)*density)*pow(1.0-shadowInfo.y/lightCameraFar,distanceAttenuation);}illum/=samplesFloat;gl_FragColor=vec4(vec3(clamp(1.0-exp(-illum),0.0,maxDensity)),depth);}";

// src/godrays.vert
var godrays_default2 = "varying vec2 vUv;void main(){vUv=position.xy*0.5+0.5;gl_Position=vec4(position.xy,1.0,1.0);}";

// src/illumPass.ts
var GODRAYS_RESOLUTION_SCALE = 1 / 2;
var GodraysMaterial = class extends THREE4.ShaderMaterial {
  constructor(light) {
    const uniforms = {
      density: { value: 1 / 128 },
      maxDensity: { value: 0.5 },
      distanceAttenuation: { value: 2 },
      sceneDepth: { value: null },
      lightPos: { value: new THREE4.Vector3(0, 0, 0) },
      cameraPos: { value: new THREE4.Vector3(0, 0, 0) },
      resolution: { value: new THREE4.Vector2(1, 1) },
      premultipliedLightCameraMatrix: { value: new THREE4.Matrix4() },
      cameraProjectionMatrixInv: { value: new THREE4.Matrix4() },
      cameraMatrixWorld: { value: new THREE4.Matrix4() },
      shadowMap: { value: null },
      texelSizeY: { value: 1 },
      lightCameraNear: { value: 0.1 },
      lightCameraFar: { value: 1e3 },
      near: { value: 0.1 },
      far: { value: 1e3 },
      blueNoise: { value: null },
      noiseResolution: { value: new THREE4.Vector2(1, 1) },
      fNormals: { value: DIRECTIONS.map(() => new THREE4.Vector3()) },
      fConstants: { value: DIRECTIONS.map(() => 0) },
      raymarchSteps: { value: 60 }
    };
    const defines = {};
    if (light instanceof THREE4.PointLight || light.isPointLight) {
      defines.IS_POINT_LIGHT = "";
    } else if (light instanceof THREE4.DirectionalLight || light.isDirectionalLight) {
      defines.IS_DIRECTIONAL_LIGHT = "";
    }
    super({
      name: "GodraysMaterial",
      uniforms,
      fragmentShader: godrays_default,
      vertexShader: godrays_default2,
      defines
    });
    getBlueNoiseTexture().then((blueNoiseTexture) => {
      uniforms.blueNoise.value = blueNoiseTexture;
      uniforms.noiseResolution.value.set(
        blueNoiseTexture.image.width,
        blueNoiseTexture.image.height
      );
    });
  }
};
var DIRECTIONS = [
  new THREE4.Vector3(1, 0, 0),
  new THREE4.Vector3(-1, 0, 0),
  new THREE4.Vector3(0, 1, 0),
  new THREE4.Vector3(0, -1, 0),
  new THREE4.Vector3(0, 0, 1),
  new THREE4.Vector3(0, 0, -1)
];
var PLANES = DIRECTIONS.map(() => new THREE4.Plane());
var SCRATCH_VECTOR = new THREE4.Vector3();
var SCRATCH_FRUSTUM = new THREE4.Frustum();
var SCRATCH_MAT4 = new THREE4.Matrix4();
var GodraysIllumPass = class extends Pass3 {
  constructor(props, params) {
    super("GodraysPass");
    this.shadowMapSet = false;
    this.lightWorldPos = new THREE4.Vector3();
    this.props = props;
    this.lastParams = params;
    this.material = new GodraysMaterial(props.light);
    this.updateUniforms(props, params);
    this.fullscreenMaterial = this.material;
  }
  setSize(width, height) {
    this.material.uniforms.resolution.value.set(
      Math.ceil(width * GODRAYS_RESOLUTION_SCALE),
      Math.ceil(height * GODRAYS_RESOLUTION_SCALE)
    );
    this.material.uniforms.near.value = this.props.camera.near;
    this.material.uniforms.far.value = this.props.camera.far;
  }
  render(renderer, _inputBuffer, outputBuffer, _deltaTime, _stencilTest) {
    var _a;
    if (!this.shadowMapSet && ((_a = this.props.light.shadow.map) == null ? void 0 : _a.texture)) {
      this.updateUniforms(this.props, this.lastParams);
      this.shadowMapSet = true;
    }
    this.updateLightParams(this.props);
    renderer.setRenderTarget(outputBuffer);
    renderer.render(this.scene, this.camera);
  }
  setDepthTexture(depthTexture, depthPacking) {
    this.material.uniforms.sceneDepth.value = depthTexture;
    if (depthPacking && depthPacking !== THREE4.BasicDepthPacking) {
      throw new Error("Only BasicDepthPacking is supported");
    }
  }
  updateLightParams({ light }) {
    light.getWorldPosition(this.lightWorldPos);
    const uniforms = this.material.uniforms;
    uniforms.premultipliedLightCameraMatrix.value.multiplyMatrices(
      light.shadow.camera.projectionMatrix,
      light.shadow.camera.matrixWorldInverse
    );
    if (light instanceof THREE4.PointLight || light.isPointLight) {
      for (let i = 0; i < DIRECTIONS.length; i += 1) {
        const direction = DIRECTIONS[i];
        const plane = PLANES[i];
        SCRATCH_VECTOR.copy(light.position);
        SCRATCH_VECTOR.addScaledVector(direction, uniforms.lightCameraFar.value);
        plane.setFromNormalAndCoplanarPoint(direction, SCRATCH_VECTOR);
        uniforms.fNormals.value[i].copy(plane.normal);
        uniforms.fConstants.value[i] = plane.constant;
      }
    } else if (light instanceof THREE4.DirectionalLight || light.isDirectionalLight) {
      SCRATCH_MAT4.multiplyMatrices(
        light.shadow.camera.projectionMatrix,
        light.shadow.camera.matrixWorldInverse
      );
      SCRATCH_FRUSTUM.setFromProjectionMatrix(SCRATCH_MAT4);
      for (let planeIx = 0; planeIx < 6; planeIx += 1) {
        const plane = SCRATCH_FRUSTUM.planes[planeIx];
        uniforms.fNormals.value[planeIx].copy(plane.normal).multiplyScalar(-1);
        uniforms.fConstants.value[planeIx] = plane.constant * -1;
      }
    }
  }
  updateUniforms({ light, camera }, params) {
    var _a, _b, _c, _d, _e, _f;
    const shadow = light.shadow;
    if (!shadow) {
      throw new Error("Light used for godrays must have shadow");
    }
    const shadowMap = (_b = (_a = shadow.map) == null ? void 0 : _a.texture) != null ? _b : null;
    const mapSize = (_d = (_c = shadow.map) == null ? void 0 : _c.height) != null ? _d : 1;
    const uniforms = this.material.uniforms;
    uniforms.density.value = params.density;
    uniforms.maxDensity.value = params.maxDensity;
    uniforms.lightPos.value = this.lightWorldPos;
    uniforms.cameraPos.value = camera.position;
    uniforms.cameraProjectionMatrixInv.value = camera.projectionMatrixInverse;
    uniforms.cameraMatrixWorld.value = camera.matrixWorld;
    uniforms.shadowMap.value = shadowMap;
    uniforms.texelSizeY.value = 1 / (mapSize * 2);
    uniforms.lightCameraNear.value = (_e = shadow == null ? void 0 : shadow.camera.near) != null ? _e : 0.1;
    uniforms.lightCameraFar.value = (_f = shadow == null ? void 0 : shadow.camera.far) != null ? _f : 1e3;
    uniforms.near.value = camera.near;
    uniforms.far.value = camera.far;
    uniforms.density.value = params.density;
    uniforms.maxDensity.value = params.maxDensity;
    uniforms.distanceAttenuation.value = params.distanceAttenuation;
    uniforms.raymarchSteps.value = params.raymarchSteps;
  }
};

// src/index.ts
var defaultParams = {
  density: 1 / 128,
  maxDensity: 0.5,
  edgeStrength: 2,
  edgeRadius: 2,
  distanceAttenuation: 2,
  color: new THREE5.Color(16777215),
  raymarchSteps: 60,
  blur: true,
  gammaCorrection: true
};
var populateParams = (partialParams) => {
  var _a;
  return {
    ...defaultParams,
    ...partialParams,
    color: new THREE5.Color((_a = partialParams.color) != null ? _a : defaultParams.color)
  };
};
var defaultGodraysBlurParams = {
  variance: 0.1,
  kernelSize: KernelSize2.SMALL
};
var populateGodraysBlurParams = (blur) => {
  if (typeof blur === "boolean") {
    return { ...defaultGodraysBlurParams };
  }
  return { ...defaultGodraysBlurParams, ...blur };
};
var GodraysPass = class extends Pass4 {
  /**
   * Constructs a new GodraysPass.  Casts godrays from a point light source.  Add to your scene's composer like this:
   *
   * ```ts
   * import { EffectComposer, RenderPass } from 'postprocessing';
   * import { GodraysPass } from 'three-good-godrays';
   *
   * const composer = new EffectComposer(renderer, { frameBufferType: THREE.HalfFloatType });
   * const renderPass = new RenderPass(scene, camera);
   * renderPass.renderToScreen = false;
   * composer.addPass(renderPass);
   *
   * const godraysPass = new GodraysPass(pointLight, camera);
   * godraysPass.renderToScreen = true;
   * composer.addPass(godraysPass);
   *
   * function animate() {
   *   composer.render(scene, camera);
   * }
   * ```
   *
   * @param light The light source to use for the godrays.
   * @param camera The camera used to render the scene.
   * @param partialParams The parameters to use for the godrays effect.  Will use default values for any parameters not specified.
   */
  constructor(light, camera, partialParams = {}) {
    super("GodraysPass");
    this.depthTexture = null;
    this.depthPacking = null;
    this.godraysRenderTarget = new THREE5.WebGLRenderTarget(1, 1, {
      minFilter: THREE5.LinearFilter,
      magFilter: THREE5.LinearFilter,
      format: THREE5.RGBAFormat,
      type: THREE5.HalfFloatType,
      generateMipmaps: false
    });
    this.enableBlurPass = true;
    this.blurPass = null;
    this.blurRenderTarget = null;
    this.props = {
      light,
      camera
    };
    const params = populateParams(partialParams);
    this.lastParams = params;
    this.illumPass = new GodraysIllumPass(this.props, params);
    this.illumPass.needsDepthTexture = true;
    this.compositorPass = new GodraysCompositorPass({
      godrays: this.godraysRenderTarget.texture,
      edgeStrength: params.edgeStrength,
      edgeRadius: params.edgeRadius,
      color: params.color,
      camera,
      gammaCorrection: params.gammaCorrection
    });
    this.compositorPass.needsDepthTexture = true;
    this.needsDepthTexture = true;
    this.setParams(params);
  }
  /**
   * Updates the parameters used for the godrays effect.  Will use default values for any parameters not specified.
   */
  setParams(partialParams) {
    const params = populateParams(partialParams);
    this.lastParams = params;
    this.illumPass.updateUniforms(this.props, params);
    this.compositorPass.updateUniforms(params);
    this.enableBlurPass = !!params.blur;
    if (params.blur && this.blurPass) {
      const blurParams = populateGodraysBlurParams(params.blur);
      if (this.blurPass.material.defines.KSIZE_ENUM !== blurParams.kernelSize) {
        this.blurPass.dispose();
        this.maybeInitBlur(this.godraysRenderTarget.texture);
      }
      this.blurPass.updateUniforms(blurParams);
    }
  }
  maybeInitBlur(input) {
    var _a;
    if (!this.blurPass) {
      this.blurPass = new BilateralFilterPass(input);
      const blurParams = populateGodraysBlurParams(this.lastParams.blur);
      this.blurPass.updateUniforms(blurParams);
      if (this.depthTexture) {
        this.blurPass.setDepthTexture(this.depthTexture, (_a = this.depthPacking) != null ? _a : void 0);
      }
    }
    if (!this.blurRenderTarget) {
      this.blurRenderTarget = new THREE5.WebGLRenderTarget(
        Math.ceil(this.godraysRenderTarget.width * GODRAYS_BLUR_RESOLUTION_SCALE),
        Math.ceil(this.godraysRenderTarget.height * GODRAYS_BLUR_RESOLUTION_SCALE),
        {
          minFilter: THREE5.LinearFilter,
          magFilter: THREE5.LinearFilter,
          format: THREE5.RGBAFormat,
          type: THREE5.HalfFloatType,
          generateMipmaps: false
        }
      );
    }
  }
  render(renderer, inputBuffer, outputBuffer, _deltaTime, _stencilTest) {
    this.illumPass.render(renderer, inputBuffer, this.godraysRenderTarget);
    if (this.enableBlurPass) {
      this.maybeInitBlur(this.godraysRenderTarget.texture);
      this.blurPass.render(renderer, this.godraysRenderTarget, this.blurRenderTarget);
      this.compositorPass.fullscreenMaterial.uniforms.godrays.value = this.blurRenderTarget.texture;
    } else {
      this.compositorPass.fullscreenMaterial.uniforms.godrays.value = this.godraysRenderTarget.texture;
    }
    this.compositorPass.render(renderer, inputBuffer, this.renderToScreen ? null : outputBuffer);
  }
  setDepthTexture(depthTexture, depthPacking) {
    this.illumPass.setDepthTexture(depthTexture, depthPacking);
    this.compositorPass.setDepthTexture(depthTexture, depthPacking);
    this.depthTexture = depthTexture;
    this.depthPacking = depthPacking;
  }
  setSize(width, height) {
    var _a, _b;
    this.godraysRenderTarget.setSize(
      Math.ceil(width * GODRAYS_RESOLUTION_SCALE),
      Math.ceil(height * GODRAYS_RESOLUTION_SCALE)
    );
    this.illumPass.setSize(width, height);
    this.compositorPass.setSize(width, height);
    (_a = this.blurPass) == null ? void 0 : _a.setSize(
      Math.ceil(width * GODRAYS_RESOLUTION_SCALE),
      Math.ceil(height * GODRAYS_RESOLUTION_SCALE)
    );
    (_b = this.blurRenderTarget) == null ? void 0 : _b.setSize(
      Math.ceil(width * GODRAYS_RESOLUTION_SCALE * GODRAYS_BLUR_RESOLUTION_SCALE),
      Math.ceil(height * GODRAYS_RESOLUTION_SCALE * GODRAYS_BLUR_RESOLUTION_SCALE)
    );
  }
  dispose() {
    var _a;
    this.godraysRenderTarget.dispose();
    this.illumPass.dispose();
    this.compositorPass.dispose();
    (_a = this.blurPass) == null ? void 0 : _a.dispose;
    super.dispose();
  }
};
export {
  GodraysPass
};
