src/materials/DepthDownsamplingMaterial.js
import { BasicDepthPacking, NoBlending, ShaderMaterial, Uniform, Vector2 } from "three";
import fragmentShader from "./glsl/depth-downsampling.frag";
import vertexShader from "./glsl/depth-downsampling.vert";
/**
* A depth downsampling shader material.
*
* Based on an article by Eleni Maria Stea:
* https://eleni.mutantstargoat.com/hikiko/depth-aware-upsampling-6
*
* @implements {Resizable}
*/
export class DepthDownsamplingMaterial extends ShaderMaterial {
/**
* Constructs a new depth downsampling material.
*/
constructor() {
super({
name: "DepthDownsamplingMaterial",
defines: {
DEPTH_PACKING: "0"
},
uniforms: {
depthBuffer: new Uniform(null),
normalBuffer: new Uniform(null),
texelSize: new Uniform(new Vector2())
},
blending: NoBlending,
toneMapped: false,
depthWrite: false,
depthTest: false,
fragmentShader,
vertexShader
});
}
/**
* The depth buffer.
*
* @type {Texture}
*/
set depthBuffer(value) {
this.uniforms.depthBuffer.value = value;
}
/**
* The depth packing strategy.
*
* @type {DepthPackingStrategies}
*/
set depthPacking(value) {
this.defines.DEPTH_PACKING = value.toFixed(0);
this.needsUpdate = true;
}
/**
* Sets the depth buffer.
*
* @deprecated Use depthBuffer and depthPacking instead.
* @param {Texture} buffer - The depth texture.
* @param {DepthPackingStrategies} [depthPacking=BasicDepthPacking] - The depth packing strategy.
*/
setDepthBuffer(buffer, depthPacking = BasicDepthPacking) {
this.depthBuffer = buffer;
this.depthPacking = depthPacking;
}
/**
* The normal buffer.
*
* @type {Texture}
*/
set normalBuffer(value) {
this.uniforms.normalBuffer.value = value;
if(value !== null) {
this.defines.DOWNSAMPLE_NORMALS = "1";
} else {
delete this.defines.DOWNSAMPLE_NORMALS;
}
this.needsUpdate = true;
}
/**
* Sets the normal buffer.
*
* @deprecated Use normalBuffer instead.
* @param {Texture} value - The normal buffer.
*/
setNormalBuffer(value) {
this.normalBuffer = value;
}
/**
* Sets the texel size.
*
* @deprecated Use setSize() instead.
* @param {Number} x - The texel width.
* @param {Number} y - The texel height.
*/
setTexelSize(x, y) {
this.uniforms.texelSize.value.set(x, y);
}
/**
* Sets the size of this object.
*
* @param {Number} width - The width.
* @param {Number} height - The height.
*/
setSize(width, height) {
this.uniforms.texelSize.value.set(1.0 / width, 1.0 / height);
}
}