molstar
Version:
A comprehensive macromolecular library.
88 lines • 3.09 kB
JavaScript
/**
* Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { Vec3, Vec4 } from '../../mol-math/linear-algebra';
export { Viewport };
function Viewport() {
return Viewport.zero();
}
(function (Viewport) {
function zero() {
return { x: 0, y: 0, width: 0, height: 0 };
}
Viewport.zero = zero;
function create(x, y, width, height) {
return { x: x, y: y, width: width, height: height };
}
Viewport.create = create;
function clone(viewport) {
return __assign({}, viewport);
}
Viewport.clone = clone;
function copy(target, source) {
return Object.assign(target, source);
}
Viewport.copy = copy;
function set(viewport, x, y, width, height) {
viewport.x = x;
viewport.y = y;
viewport.width = width;
viewport.height = height;
return viewport;
}
Viewport.set = set;
function toVec4(v4, viewport) {
v4[0] = viewport.x;
v4[1] = viewport.y;
v4[2] = viewport.width;
v4[3] = viewport.height;
return v4;
}
Viewport.toVec4 = toVec4;
function equals(a, b) {
return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
}
Viewport.equals = equals;
})(Viewport || (Viewport = {}));
//
var tmpVec4 = Vec4();
/** Transform point into 2D window coordinates. */
export function cameraProject(out, point, viewport, projectionView) {
var x = viewport.x, y = viewport.y, width = viewport.width, height = viewport.height;
// clip space -> NDC -> window coordinates, implicit 1.0 for w component
Vec4.set(tmpVec4, point[0], point[1], point[2], 1.0);
// transform into clip space
Vec4.transformMat4(tmpVec4, tmpVec4, projectionView);
// transform into NDC
var w = tmpVec4[3];
if (w !== 0) {
tmpVec4[0] /= w;
tmpVec4[1] /= w;
tmpVec4[2] /= w;
}
// transform into window coordinates, set fourth component to 1 / clip.w as in gl_FragCoord.w
out[0] = (tmpVec4[0] + 1) * width * 0.5 + x;
out[1] = (1 - tmpVec4[1]) * height * 0.5 + y; // flip Y
out[2] = (tmpVec4[2] + 1) * 0.5;
out[3] = w === 0 ? 0 : 1 / w;
return out;
}
/**
* Transform point from screen space to 3D coordinates.
* The point must have `x` and `y` set to 2D window coordinates
* and `z` between 0 (near) and 1 (far); the optional `w` is not used.
*/
export function cameraUnproject(out, point, viewport, inverseProjectionView) {
var x = viewport.x, y = viewport.y, width = viewport.width, height = viewport.height;
var px = point[0] - x;
var py = (height - point[1] - 1) - y;
var pz = point[2];
out[0] = (2 * px) / width - 1;
out[1] = (2 * py) / height - 1;
out[2] = 2 * pz - 1;
return Vec3.transformMat4(out, out, inverseProjectionView);
}
//# sourceMappingURL=util.js.map