src/materials/SMAAColorEdgesMaterial.js
import { ShaderMaterial, Uniform, Vector2 } from "three";
import fragment from "./glsl/smaa-color-edges/shader.frag";
import vertex from "./glsl/smaa-color-edges/shader.vert";
/**
* Subpixel Morphological Antialiasing.
*
* This material detects edges in a color texture.
*/
export class SMAAColorEdgesMaterial extends ShaderMaterial {
/**
* Constructs a new SMAA color edges material.
*
* @param {Vector2} [texelSize] - The absolute screen texel size.
*/
constructor(texelSize = new Vector2()) {
super({
type: "SMAAColorEdgesMaterial",
defines: {
EDGE_THRESHOLD: "0.1"
},
uniforms: {
tDiffuse: new Uniform(null),
texelSize: new Uniform(texelSize)
},
fragmentShader: fragment,
vertexShader: vertex,
depthWrite: false,
depthTest: false
});
}
/**
* Sets the edge detection sensitivity.
*
* A lower value results in more edges being detected at the expense of
* performance.
*
* 0.1 is a reasonable value, and allows to catch most visible edges.
* 0.05 is a rather overkill value, that allows to catch 'em all.
*
* If temporal supersampling is used, 0.2 could be a reasonable value,
* as low contrast edges are properly filtered by just 2x.
*
* @param {Number} threshold - The edge detection sensitivity. Range: [0, 0.5].
*/
setEdgeDetectionThreshold(threshold) {
this.defines.EDGE_THRESHOLD = threshold.toFixed("2");
this.needsUpdate = true;
}
}