polymer-backbone-sphere.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Copyright (c) 2021-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { VisualContext } from '../../visual';
  8. import { Unit, Structure, ElementIndex, StructureElement } from '../../../mol-model/structure';
  9. import { Theme } from '../../../mol-theme/theme';
  10. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  11. import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
  12. import { Vec3 } from '../../../mol-math/linear-algebra';
  13. import { eachPolymerElement, getPolymerElementLoci, PolymerLocationIterator } from './util/polymer';
  14. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual, UnitsSpheresVisual, UnitsSpheresParams } from '../units-visual';
  15. import { VisualUpdateState } from '../../util';
  16. import { BaseGeometry } from '../../../mol-geo/geometry/base';
  17. import { Sphere3D } from '../../../mol-math/geometry';
  18. import { addSphere } from '../../../mol-geo/geometry/mesh/builder/sphere';
  19. import { sphereVertexCount } from '../../../mol-geo/primitive/sphere';
  20. import { WebGLContext } from '../../../mol-gl/webgl/context';
  21. import { Spheres } from '../../../mol-geo/geometry/spheres/spheres';
  22. import { SpheresBuilder } from '../../../mol-geo/geometry/spheres/spheres-builder';
  23. import { eachPolymerBackboneElement } from './util/polymer/backbone';
  24. import { StructureGroup } from './util/common';
  25. export const PolymerBackboneSphereParams = {
  26. ...UnitsMeshParams,
  27. ...UnitsSpheresParams,
  28. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  29. detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }, BaseGeometry.CustomQualityParamInfo),
  30. tryUseImpostor: PD.Boolean(true),
  31. };
  32. export type PolymerBackboneSphereParams = typeof PolymerBackboneSphereParams
  33. export function PolymerBackboneSphereVisual(materialId: number, structure: Structure, props: PD.Values<PolymerBackboneSphereParams>, webgl?: WebGLContext) {
  34. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth && webgl.extensions.textureFloat
  35. ? PolymerBackboneSphereImpostorVisual(materialId)
  36. : PolymerBackboneSphereMeshVisual(materialId);
  37. }
  38. interface PolymerBackboneSphereProps {
  39. detail: number,
  40. sizeFactor: number,
  41. }
  42. function createPolymerBackboneSphereImpostor(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerBackboneSphereProps, spheres?: Spheres) {
  43. const polymerElementCount = unit.polymerElements.length;
  44. if (!polymerElementCount) return Spheres.createEmpty(spheres);
  45. const builder = SpheresBuilder.create(polymerElementCount, polymerElementCount / 2, spheres);
  46. const pos = unit.conformation.invariantPosition;
  47. const p = Vec3();
  48. const add = (index: ElementIndex, group: number) => {
  49. pos(index, p);
  50. builder.add(p[0], p[1], p[2], group);
  51. };
  52. eachPolymerBackboneElement(unit, add);
  53. const s = builder.getSpheres();
  54. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  55. s.setBoundingSphere(sphere);
  56. return s;
  57. }
  58. export function PolymerBackboneSphereImpostorVisual(materialId: number): UnitsVisual<PolymerBackboneSphereParams> {
  59. return UnitsSpheresVisual<PolymerBackboneSphereParams>({
  60. defaultProps: PD.getDefaultValues(PolymerBackboneSphereParams),
  61. createGeometry: createPolymerBackboneSphereImpostor,
  62. createLocationIterator: PolymerLocationIterator.fromGroup,
  63. getLoci: getPolymerElementLoci,
  64. eachLocation: eachPolymerElement,
  65. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerBackboneSphereParams>, currentProps: PD.Values<PolymerBackboneSphereParams>) => { },
  66. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<PolymerBackboneSphereParams>, webgl?: WebGLContext) => {
  67. return !props.tryUseImpostor || !webgl;
  68. }
  69. }, materialId);
  70. }
  71. function createPolymerBackboneSphereMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PolymerBackboneSphereProps, mesh?: Mesh) {
  72. const polymerElementCount = unit.polymerElements.length;
  73. if (!polymerElementCount) return Mesh.createEmpty(mesh);
  74. const { detail, sizeFactor } = props;
  75. const vertexCount = polymerElementCount * sphereVertexCount(detail);
  76. const builderState = MeshBuilder.createState(vertexCount, vertexCount / 2, mesh);
  77. const pos = unit.conformation.invariantPosition;
  78. const p = Vec3();
  79. const center = StructureElement.Location.create(structure, unit);
  80. const add = (index: ElementIndex, group: number) => {
  81. center.element = index;
  82. pos(center.element, p);
  83. builderState.currentGroup = group;
  84. addSphere(builderState, p, theme.size.size(center) * sizeFactor, detail);
  85. };
  86. eachPolymerBackboneElement(unit, add);
  87. const m = MeshBuilder.getMesh(builderState);
  88. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor);
  89. m.setBoundingSphere(sphere);
  90. return m;
  91. }
  92. export function PolymerBackboneSphereMeshVisual(materialId: number): UnitsVisual<PolymerBackboneSphereParams> {
  93. return UnitsMeshVisual<PolymerBackboneSphereParams>({
  94. defaultProps: PD.getDefaultValues(PolymerBackboneSphereParams),
  95. createGeometry: createPolymerBackboneSphereMesh,
  96. createLocationIterator: PolymerLocationIterator.fromGroup,
  97. getLoci: getPolymerElementLoci,
  98. eachLocation: eachPolymerElement,
  99. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<PolymerBackboneSphereParams>, currentProps: PD.Values<PolymerBackboneSphereParams>) => {
  100. state.createGeometry = (
  101. newProps.sizeFactor !== currentProps.sizeFactor ||
  102. newProps.detail !== currentProps.detail
  103. );
  104. },
  105. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<PolymerBackboneSphereParams>, webgl?: WebGLContext) => {
  106. return props.tryUseImpostor && !!webgl;
  107. }
  108. }, materialId);
  109. }