molstar
Version:
A comprehensive macromolecular library.
95 lines (94 loc) • 4.98 kB
JavaScript
/**
* Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { StructureElement } from '../../../mol-model/structure';
import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
import { Vec3 } from '../../../mol-math/linear-algebra';
import { eachPolymerElement, getPolymerElementLoci, PolymerLocationIterator } from './util/polymer';
import { UnitsMeshParams, UnitsMeshVisual, UnitsSpheresVisual, UnitsSpheresParams } from '../units-visual';
import { BaseGeometry } from '../../../mol-geo/geometry/base';
import { Sphere3D } from '../../../mol-math/geometry';
import { addSphere } from '../../../mol-geo/geometry/mesh/builder/sphere';
import { sphereVertexCount } from '../../../mol-geo/primitive/sphere';
import { Spheres } from '../../../mol-geo/geometry/spheres/spheres';
import { SpheresBuilder } from '../../../mol-geo/geometry/spheres/spheres-builder';
import { eachPolymerBackboneElement } from './util/polymer/backbone';
export var PolymerBackboneSphereParams = __assign(__assign(__assign({}, UnitsMeshParams), UnitsSpheresParams), { sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }), detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }, BaseGeometry.CustomQualityParamInfo), tryUseImpostor: PD.Boolean(true) });
export function PolymerBackboneSphereVisual(materialId, structure, props, webgl) {
return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
? PolymerBackboneSphereImpostorVisual(materialId)
: PolymerBackboneSphereMeshVisual(materialId);
}
function createPolymerBackboneSphereImpostor(ctx, unit, structure, theme, props, spheres) {
var polymerElementCount = unit.polymerElements.length;
if (!polymerElementCount)
return Spheres.createEmpty(spheres);
var builder = SpheresBuilder.create(polymerElementCount, polymerElementCount / 2, spheres);
var pos = unit.conformation.invariantPosition;
var p = Vec3();
var add = function (index, group) {
pos(index, p);
builder.add(p[0], p[1], p[2], group);
};
eachPolymerBackboneElement(unit, add);
var s = builder.getSpheres();
var sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
s.setBoundingSphere(sphere);
return s;
}
export function PolymerBackboneSphereImpostorVisual(materialId) {
return UnitsSpheresVisual({
defaultProps: PD.getDefaultValues(PolymerBackboneSphereParams),
createGeometry: createPolymerBackboneSphereImpostor,
createLocationIterator: PolymerLocationIterator.fromGroup,
getLoci: getPolymerElementLoci,
eachLocation: eachPolymerElement,
setUpdateState: function (state, newProps, currentProps) { },
mustRecreate: function (structureGroup, props, webgl) {
return !props.tryUseImpostor || !webgl;
}
}, materialId);
}
function createPolymerBackboneSphereMesh(ctx, unit, structure, theme, props, mesh) {
var polymerElementCount = unit.polymerElements.length;
if (!polymerElementCount)
return Mesh.createEmpty(mesh);
var detail = props.detail, sizeFactor = props.sizeFactor;
var vertexCount = polymerElementCount * sphereVertexCount(detail);
var builderState = MeshBuilder.createState(vertexCount, vertexCount / 2, mesh);
var pos = unit.conformation.invariantPosition;
var p = Vec3();
var center = StructureElement.Location.create(structure, unit);
var add = function (index, group) {
center.element = index;
pos(center.element, p);
builderState.currentGroup = group;
addSphere(builderState, p, theme.size.size(center) * sizeFactor, detail);
};
eachPolymerBackboneElement(unit, add);
var m = MeshBuilder.getMesh(builderState);
var sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
m.setBoundingSphere(sphere);
return m;
}
export function PolymerBackboneSphereMeshVisual(materialId) {
return UnitsMeshVisual({
defaultProps: PD.getDefaultValues(PolymerBackboneSphereParams),
createGeometry: createPolymerBackboneSphereMesh,
createLocationIterator: PolymerLocationIterator.fromGroup,
getLoci: getPolymerElementLoci,
eachLocation: eachPolymerElement,
setUpdateState: function (state, newProps, currentProps) {
state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor ||
newProps.detail !== currentProps.detail);
},
mustRecreate: function (structureGroup, props, webgl) {
return props.tryUseImpostor && !!webgl;
}
}, materialId);
}