molstar
Version:
A comprehensive macromolecular library.
153 lines • 7.82 kB
JavaScript
/**
* Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { createRenderObject, getNextMaterialId } from '../../mol-gl/render-object';
import { MeshBuilder } from '../../mol-geo/geometry/mesh/mesh-builder';
import { addSphere } from '../../mol-geo/geometry/mesh/builder/sphere';
import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
import { ParamDefinition as PD } from '../../mol-util/param-definition';
import { Scene } from '../../mol-gl/scene';
import { Sphere3D } from '../../mol-math/geometry';
import { ColorNames } from '../../mol-util/color/names';
import { sphereVertexCount } from '../../mol-geo/primitive/sphere';
import { ValueCell } from '../../mol-util';
import { Geometry } from '../../mol-geo/geometry/geometry';
export var DebugHelperParams = {
sceneBoundingSpheres: PD.Boolean(false, { description: 'Show full scene bounding spheres.' }),
visibleSceneBoundingSpheres: PD.Boolean(false, { description: 'Show visible scene bounding spheres.' }),
objectBoundingSpheres: PD.Boolean(false, { description: 'Show bounding spheres of visible render objects.' }),
instanceBoundingSpheres: PD.Boolean(false, { description: 'Show bounding spheres of visible instances.' }),
};
var BoundingSphereHelper = /** @class */ (function () {
function BoundingSphereHelper(ctx, parent, props) {
this.objectsData = new Map();
this.instancesData = new Map();
this.scene = Scene.create(ctx);
this.parent = parent;
this._props = __assign(__assign({}, PD.getDefaultValues(DebugHelperParams)), props);
}
BoundingSphereHelper.prototype.update = function () {
var _this = this;
var newSceneData = updateBoundingSphereData(this.scene, this.parent.boundingSphere, this.sceneData, ColorNames.lightgrey, sceneMaterialId);
if (newSceneData)
this.sceneData = newSceneData;
var newVisibleSceneData = updateBoundingSphereData(this.scene, this.parent.boundingSphereVisible, this.visibleSceneData, ColorNames.black, visibleSceneMaterialId);
if (newVisibleSceneData)
this.visibleSceneData = newVisibleSceneData;
this.parent.forEach(function (r, ro) {
var objectData = _this.objectsData.get(ro);
var newObjectData = updateBoundingSphereData(_this.scene, r.values.boundingSphere.ref.value, objectData, ColorNames.tomato, objectMaterialId);
if (newObjectData)
_this.objectsData.set(ro, newObjectData);
var instanceData = _this.instancesData.get(ro);
var newInstanceData = updateBoundingSphereData(_this.scene, r.values.invariantBoundingSphere.ref.value, instanceData, ColorNames.skyblue, instanceMaterialId, {
aTransform: ro.values.aTransform,
matrix: ro.values.matrix,
transform: ro.values.transform,
extraTransform: ro.values.extraTransform,
uInstanceCount: ro.values.uInstanceCount,
instanceCount: ro.values.instanceCount,
aInstance: ro.values.aInstance,
hasReflection: ro.values.hasReflection,
});
if (newInstanceData)
_this.instancesData.set(ro, newInstanceData);
});
this.objectsData.forEach(function (objectData, ro) {
if (!_this.parent.has(ro)) {
_this.scene.remove(objectData.renderObject);
_this.objectsData.delete(ro);
}
});
this.instancesData.forEach(function (instanceData, ro) {
if (!_this.parent.has(ro)) {
_this.scene.remove(instanceData.renderObject);
_this.instancesData.delete(ro);
}
});
this.scene.update(void 0, false);
this.scene.commit();
};
BoundingSphereHelper.prototype.syncVisibility = function () {
var _this = this;
if (this.sceneData) {
this.sceneData.renderObject.state.visible = this._props.sceneBoundingSpheres;
}
if (this.visibleSceneData) {
this.visibleSceneData.renderObject.state.visible = this._props.visibleSceneBoundingSpheres;
}
this.parent.forEach(function (_, ro) {
var objectData = _this.objectsData.get(ro);
if (objectData)
objectData.renderObject.state.visible = ro.state.visible && _this._props.objectBoundingSpheres;
var instanceData = _this.instancesData.get(ro);
if (instanceData)
instanceData.renderObject.state.visible = ro.state.visible && _this._props.instanceBoundingSpheres;
});
};
BoundingSphereHelper.prototype.clear = function () {
this.sceneData = undefined;
this.objectsData.clear();
this.scene.clear();
};
Object.defineProperty(BoundingSphereHelper.prototype, "isEnabled", {
get: function () {
return (this._props.sceneBoundingSpheres || this._props.visibleSceneBoundingSpheres ||
this._props.objectBoundingSpheres || this._props.instanceBoundingSpheres);
},
enumerable: false,
configurable: true
});
Object.defineProperty(BoundingSphereHelper.prototype, "props", {
get: function () { return this._props; },
enumerable: false,
configurable: true
});
BoundingSphereHelper.prototype.setProps = function (props) {
Object.assign(this._props, props);
if (this.isEnabled)
this.update();
};
return BoundingSphereHelper;
}());
export { BoundingSphereHelper };
function updateBoundingSphereData(scene, boundingSphere, data, color, materialId, transform) {
if (!data || !Sphere3D.equals(data.boundingSphere, boundingSphere)) {
var mesh = createBoundingSphereMesh(boundingSphere, data && data.mesh);
var renderObject = data ? data.renderObject : createBoundingSphereRenderObject(mesh, color, materialId, transform);
if (data) {
ValueCell.updateIfChanged(renderObject.values.drawCount, Geometry.getDrawCount(mesh));
}
else {
scene.add(renderObject);
}
return { boundingSphere: Sphere3D.clone(boundingSphere), renderObject: renderObject, mesh: mesh };
}
}
function createBoundingSphereMesh(boundingSphere, mesh) {
var detail = 2;
var vertexCount = sphereVertexCount(detail);
var builderState = MeshBuilder.createState(vertexCount, vertexCount / 2, mesh);
if (boundingSphere.radius) {
addSphere(builderState, boundingSphere.center, boundingSphere.radius, detail);
if (Sphere3D.hasExtrema(boundingSphere)) {
for (var _i = 0, _a = boundingSphere.extrema; _i < _a.length; _i++) {
var e = _a[_i];
addSphere(builderState, e, 1.0, 0);
}
}
}
return MeshBuilder.getMesh(builderState);
}
var sceneMaterialId = getNextMaterialId();
var visibleSceneMaterialId = getNextMaterialId();
var objectMaterialId = getNextMaterialId();
var instanceMaterialId = getNextMaterialId();
function createBoundingSphereRenderObject(mesh, color, materialId, transform) {
var values = Mesh.Utils.createValuesSimple(mesh, { alpha: 0.1, doubleSided: false }, color, 1, transform);
return createRenderObject('mesh', values, { disposed: false, visible: true, alphaFactor: 1, pickable: false, colorOnly: false, opaque: false, writeDepth: false, noClip: false }, materialId);
}
//# sourceMappingURL=bounding-sphere-helper.js.map