@itwin/frontend-devtools
Version:
Debug menu and supporting UI widgets
166 lines • 6.03 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Effects
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EdgeDetectionEffect = exports.SharpnessEffect = exports.SharpenEffect = exports.EmbossEffect = exports.UnsharpenEffect = exports.GaussianBlurEffect = exports.ConvolutionEffect = void 0;
const core_frontend_1 = require("@itwin/core-frontend");
const EffectTools_1 = require("./EffectTools");
/** Adds one of a collection of "convolution kernels" that alter a [Viewport]($frontend)'s image by blending neighboring pixels.
* Based on https://webglfundamentals.org/webgl/lessons/webgl-image-processing-continued.html
* @beta
*/
class ConvolutionEffect extends EffectTools_1.AddEffectTool {
static get minArgs() { return 0; }
static get maxArgs() { return 0; }
get textureCoordFromPosition() { return true; }
get source() {
return {
// The vertex shader simply computes the texture coordinate for use in the fragment shader.
vertex: `
void effectMain(vec4 pos) {
v_texCoord = textureCoordFromPosition(pos);
}`,
// The fragment shader samples the pixel and its neighbors and applies the kernel.
fragment: `
vec4 effectMain() {
vec2 onePixel = vec2(1.0, 1.0) / u_textureSize;
vec4 colorSum =
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2(-1, -1)) * u_kernel[0] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2( 0, -1)) * u_kernel[1] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2( 1, -1)) * u_kernel[2] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2(-1, 0)) * u_kernel[3] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2( 0, 0)) * u_kernel[4] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2( 1, 0)) * u_kernel[5] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2(-1, 1)) * u_kernel[6] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2( 0, 1)) * u_kernel[7] +
TEXTURE(u_diffuse, v_texCoord + onePixel * vec2( 1, 1)) * u_kernel[8] ;
return vec4((colorSum / u_kernelWeight).rgb, 1);
}`,
};
}
defineEffect(builder) {
// Define the varying for the texture coordinate.
builder.addVarying("v_texCoord", core_frontend_1.VaryingType.Vec2);
// Hook up the uniforms.
const matrix = this.matrix;
builder.addUniform({
name: "u_textureSize",
type: core_frontend_1.UniformType.Vec2,
bind: (uniform, context) => {
const rect = context.viewport.viewRect;
uniform.setUniform2fv([rect.width, rect.height]);
},
});
builder.addUniformArray({
name: "u_kernel",
type: core_frontend_1.UniformType.Float,
length: matrix.length,
bind: (uniform) => uniform.setUniform1fv(matrix),
});
let weight = matrix.reduce((prev, curr) => prev + curr);
if (weight <= 0)
weight = 1;
builder.addUniform({
name: "u_kernelWeight",
type: core_frontend_1.UniformType.Float,
bind: (uniform) => uniform.setUniform1f(weight),
});
}
}
exports.ConvolutionEffect = ConvolutionEffect;
/** Adds a gaussian blur screen-space effect to the selected Viewport.
* @beta
*/
class GaussianBlurEffect extends ConvolutionEffect {
static toolId = "GaussianBlurEffect";
get effectName() { return "blur"; }
get matrix() {
return [
0.045, 0.122, 0.045,
0.122, 0.332, 0.122,
0.045, 0.122, 0.045,
];
}
}
exports.GaussianBlurEffect = GaussianBlurEffect;
/** Adds a screen-space unsharpen effect to the selected Viewport.
* @beta
*/
class UnsharpenEffect extends ConvolutionEffect {
static toolId = "UnsharpenEffect";
get effectName() { return "unsharpen"; }
get matrix() {
return [
-1, -1, -1,
-1, 9, -1,
-1, -1, -1,
];
}
}
exports.UnsharpenEffect = UnsharpenEffect;
/** Adds a screen-space emboss effect to the selected Viewport.
* @beta
*/
class EmbossEffect extends ConvolutionEffect {
static toolId = "EmbossEffect";
get effectName() { return "emboss"; }
get matrix() {
return [
-2, -1, 0,
-1, 1, 1,
0, 1, 2,
];
}
}
exports.EmbossEffect = EmbossEffect;
/** Adds a screen-space sharpen effect to the selected Viewport.
* @beta
*/
class SharpenEffect extends ConvolutionEffect {
static toolId = "SharpenEffect";
get effectName() { return "sharpen"; }
get matrix() {
return [
0, -1, 0,
-1, 5, -1,
0, -1, 0,
];
}
}
exports.SharpenEffect = SharpenEffect;
/** Adds a screen-space sharpness effect to the selected Viewport.
* @beta
*/
class SharpnessEffect extends ConvolutionEffect {
static toolId = "SharpnessEffect";
get effectName() { return "sharpness"; }
get matrix() {
return [
0, -1, 0,
-1, 5, -1,
0, -1, 0,
];
}
}
exports.SharpnessEffect = SharpnessEffect;
/** Adds a screen-space edge-detection effect to the selected Viewport.
* @beta
*/
class EdgeDetectionEffect extends ConvolutionEffect {
static toolId = "EdgeDetectionEffect";
get effectName() { return "edgedetect"; }
get matrix() {
return [
-5, 0, 0,
0, 0, 0,
0, 0, 5,
];
}
}
exports.EdgeDetectionEffect = EdgeDetectionEffect;
//# sourceMappingURL=Convolution.js.map