@itwin/frontend-devtools
Version:
Debug menu and supporting UI widgets
100 lines (97 loc) • 4.01 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.FlipImageConfig = exports.FlipImageEffect = void 0;
const core_frontend_1 = require("@itwin/core-frontend");
const parseArgs_1 = require("../tools/parseArgs");
const EffectTools_1 = require("./EffectTools");
let flipHorizontal = false;
let flipVertical = false;
let flipColor = false;
/** An extremely simple and mostly useless effect intended to demonstrate the basics of creating a screen-space effect.
* It flips the Viewport's image on the x and/or y axis, and/or inverts the color of each pixel.
* @beta
*/
class FlipImageEffect extends EffectTools_1.AddEffectTool {
static toolId = "FlipImageEffect";
get effectName() { return "flip"; }
get textureCoordFromPosition() { return true; }
get source() {
return {
// Compute texture coordinate for use in fragment shader.
vertex: `
void effectMain(vec4 pos) {
vec2 uv = textureCoordFromPosition(pos);
if (u_flipHorizontal)
uv.x = 1.0 - uv.x;
if (u_flipVertical)
uv.y = 1.0 - uv.y;
v_uv = uv;
}`,
// Sample the original image to flip on x and/or y axis, then invert its color.
fragment: `
vec4 effectMain() {
vec4 color = sampleSourcePixel();
if (u_flipColor) {
color.r = 1.0 - color.r;
color.g = 1.0 - color.g;
color.b = 1.0 - color.b;
}
return color;
}`,
// Because we're moving pixels around, we must tell the render system where the source pixel was originally located - otherwise
// element locate will not work correctly.
sampleSourcePixel: "return TEXTURE(u_diffuse, v_uv);",
};
}
defineEffect(builder) {
// Don't bother applying the effect if nothing is to be flipped.
builder.shouldApply = (_context) => flipHorizontal || flipVertical || flipColor;
// Define the varying for the texture coordinate.
builder.addVarying("v_uv", core_frontend_1.VaryingType.Vec2);
// Hook up the uniforms.
builder.addUniform({
name: "u_flipHorizontal",
type: core_frontend_1.UniformType.Bool,
bind: (uniform, _context) => uniform.setUniform1i(flipHorizontal ? 1 : 0),
});
builder.addUniform({
name: "u_flipVertical",
type: core_frontend_1.UniformType.Bool,
bind: (uniform, _context) => uniform.setUniform1i(flipVertical ? 1 : 0),
});
builder.addUniform({
name: "u_flipColor",
type: core_frontend_1.UniformType.Bool,
bind: (uniform, _context) => uniform.setUniform1i(flipColor ? 1 : 0),
});
}
}
exports.FlipImageEffect = FlipImageEffect;
/** Configure the [[FlipImageEffect]].
* @beta
*/
class FlipImageConfig extends core_frontend_1.Tool {
static toolId = "FlipImageConfig";
static get minArgs() { return 0; }
static get maxArgs() { return 3; }
async run(horizontal, vertical, color) {
flipHorizontal = !!horizontal;
flipVertical = !!vertical;
flipColor = !!color;
(0, EffectTools_1.refreshViewportsForEffect)("fdt flip");
return true;
}
async parseAndRun(...input) {
const args = (0, parseArgs_1.parseArgs)(input);
return this.run(args.getBoolean("h"), args.getBoolean("v"), args.getBoolean("c"));
}
}
exports.FlipImageConfig = FlipImageConfig;
//# sourceMappingURL=FlipImage.js.map