molstar
Version:
A comprehensive macromolecular library.
123 lines (122 loc) • 6.63 kB
JavaScript
"use strict";
/**
* Copyright (c) 2019-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
* @author Ludovic Autin <ludovic.autin@gmail.com>
* @author Gianluca Tomasello <giagitom@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutlinesSchema = exports.OutlinePass = exports.OutlineParams = void 0;
exports.getOutlinesRenderable = getOutlinesRenderable;
const util_1 = require("../../mol-gl/compute/util");
const schema_1 = require("../../mol-gl/renderable/schema");
const shader_code_1 = require("../../mol-gl/shader-code");
const mol_util_1 = require("../../mol-util");
const render_item_1 = require("../../mol-gl/webgl/render-item");
const renderable_1 = require("../../mol-gl/renderable");
const linear_algebra_1 = require("../../mol-math/linear-algebra");
const param_definition_1 = require("../../mol-util/param-definition");
const quad_vert_1 = require("../../mol-gl/shader/quad.vert");
const outlines_frag_1 = require("../../mol-gl/shader/outlines.frag");
const debug_1 = require("../../mol-util/debug");
const color_1 = require("../../mol-util/color");
exports.OutlineParams = {
scale: param_definition_1.ParamDefinition.Numeric(1, { min: 1, max: 5, step: 1 }),
threshold: param_definition_1.ParamDefinition.Numeric(0.33, { min: 0.01, max: 1, step: 0.01 }),
color: param_definition_1.ParamDefinition.Color((0, color_1.Color)(0x000000)),
includeTransparent: param_definition_1.ParamDefinition.Boolean(true, { description: 'Whether to show outline for transparent objects' }),
};
class OutlinePass {
static isEnabled(props) {
return props.outline.name !== 'off';
}
constructor(webgl, width, height, depthTextureTransparent, depthTextureOpaque) {
this.webgl = webgl;
this.target = webgl.createRenderTarget(width, height, false);
this.renderable = getOutlinesRenderable(webgl, depthTextureOpaque, depthTextureTransparent, true);
}
setSize(width, height) {
const [w, h] = this.renderable.values.uTexSize.ref.value;
if (width !== w || height !== h) {
this.target.setSize(width, height);
mol_util_1.ValueCell.update(this.renderable.values.uTexSize, linear_algebra_1.Vec2.set(this.renderable.values.uTexSize.ref.value, width, height));
}
}
update(camera, props, depthTextureTransparent, depthTextureOpaque) {
var _a;
let needsUpdate = false;
const orthographic = camera.state.mode === 'orthographic' ? 1 : 0;
const invProjection = this.renderable.values.uInvProjection.ref.value;
linear_algebra_1.Mat4.invert(invProjection, camera.projection);
const transparentOutline = (_a = props.includeTransparent) !== null && _a !== void 0 ? _a : true;
const outlineScale = Math.max(1, Math.round(props.scale * this.webgl.pixelRatio)) - 1;
const outlineThreshold = 50 * props.threshold * this.webgl.pixelRatio;
mol_util_1.ValueCell.updateIfChanged(this.renderable.values.uNear, camera.near);
mol_util_1.ValueCell.updateIfChanged(this.renderable.values.uFar, camera.far);
mol_util_1.ValueCell.update(this.renderable.values.uInvProjection, invProjection);
if (this.renderable.values.dTransparentOutline.ref.value !== transparentOutline) {
needsUpdate = true;
mol_util_1.ValueCell.update(this.renderable.values.dTransparentOutline, transparentOutline);
}
if (this.renderable.values.dOrthographic.ref.value !== orthographic) {
needsUpdate = true;
mol_util_1.ValueCell.update(this.renderable.values.dOrthographic, orthographic);
}
mol_util_1.ValueCell.updateIfChanged(this.renderable.values.uOutlineThreshold, outlineThreshold);
if (this.renderable.values.tDepthTransparent.ref.value !== depthTextureTransparent) {
needsUpdate = true;
mol_util_1.ValueCell.update(this.renderable.values.tDepthTransparent, depthTextureTransparent);
}
if (this.renderable.values.tDepthOpaque.ref.value !== depthTextureOpaque) {
needsUpdate = true;
mol_util_1.ValueCell.update(this.renderable.values.tDepthOpaque, depthTextureOpaque);
}
if (needsUpdate) {
this.renderable.update();
}
return { transparentOutline, outlineScale };
}
render() {
if (debug_1.isTimingMode)
this.webgl.timer.mark('OutlinePass.render');
this.target.bind();
this.renderable.render();
if (debug_1.isTimingMode)
this.webgl.timer.markEnd('OutlinePass.render');
}
}
exports.OutlinePass = OutlinePass;
exports.OutlinesSchema = {
...util_1.QuadSchema,
tDepthOpaque: (0, schema_1.TextureSpec)('texture', 'rgba', 'ubyte', 'nearest'),
tDepthTransparent: (0, schema_1.TextureSpec)('texture', 'rgba', 'ubyte', 'nearest'),
uTexSize: (0, schema_1.UniformSpec)('v2'),
dOrthographic: (0, schema_1.DefineSpec)('number'),
uNear: (0, schema_1.UniformSpec)('f'),
uFar: (0, schema_1.UniformSpec)('f'),
uInvProjection: (0, schema_1.UniformSpec)('m4'),
uOutlineThreshold: (0, schema_1.UniformSpec)('f'),
dTransparentOutline: (0, schema_1.DefineSpec)('boolean'),
};
function getOutlinesRenderable(ctx, depthTextureOpaque, depthTextureTransparent, transparentOutline) {
const width = depthTextureOpaque.getWidth();
const height = depthTextureOpaque.getHeight();
const values = {
...util_1.QuadValues,
tDepthOpaque: mol_util_1.ValueCell.create(depthTextureOpaque),
tDepthTransparent: mol_util_1.ValueCell.create(depthTextureTransparent),
uTexSize: mol_util_1.ValueCell.create(linear_algebra_1.Vec2.create(width, height)),
dOrthographic: mol_util_1.ValueCell.create(0),
uNear: mol_util_1.ValueCell.create(1),
uFar: mol_util_1.ValueCell.create(10000),
uInvProjection: mol_util_1.ValueCell.create(linear_algebra_1.Mat4.identity()),
uOutlineThreshold: mol_util_1.ValueCell.create(0.33),
dTransparentOutline: mol_util_1.ValueCell.create(transparentOutline),
};
const schema = { ...exports.OutlinesSchema };
const shaderCode = (0, shader_code_1.ShaderCode)('outlines', quad_vert_1.quad_vert, outlines_frag_1.outlines_frag);
const renderItem = (0, render_item_1.createComputeRenderItem)(ctx, 'triangles', shaderCode, schema, values);
return (0, renderable_1.createComputeRenderable)(renderItem, values);
}