complex-representation.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  8. import { ComplexVisual, StructureRepresentation, StructureRepresentationStateBuilder, StructureRepresentationState } from './representation';
  9. import { RepresentationContext, RepresentationParamsGetter } from '../representation';
  10. import { Structure, StructureElement, Bond } from '../../mol-model/structure';
  11. import { Subject } from 'rxjs';
  12. import { getNextMaterialId, GraphicsRenderObject } from '../../mol-gl/render-object';
  13. import { Theme } from '../../mol-theme/theme';
  14. import { Task } from '../../mol-task';
  15. import { PickingId } from '../../mol-geo/geometry/picking';
  16. import { EmptyLoci, Loci, isEveryLoci, isDataLoci } from '../../mol-model/loci';
  17. import { MarkerAction, MarkerActions } from '../../mol-util/marker-action';
  18. import { Overpaint } from '../../mol-theme/overpaint';
  19. import { StructureParams } from './params';
  20. import { Clipping } from '../../mol-theme/clipping';
  21. import { Transparency } from '../../mol-theme/transparency';
  22. import { WebGLContext } from '../../mol-gl/webgl/context';
  23. export function ComplexRepresentation<P extends StructureParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: (materialId: number, structure: Structure, props: PD.Values<P>, webgl?: WebGLContext) => ComplexVisual<P>): StructureRepresentation<P> {
  24. let version = 0;
  25. const { webgl } = ctx;
  26. const updated = new Subject<number>();
  27. const materialId = getNextMaterialId();
  28. const renderObjects: GraphicsRenderObject[] = [];
  29. const _state = StructureRepresentationStateBuilder.create();
  30. let visual: ComplexVisual<P> | undefined;
  31. let _structure: Structure;
  32. let _params: P;
  33. let _props: PD.Values<P>;
  34. let _theme = Theme.createEmpty();
  35. function createOrUpdate(props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  36. if (structure && structure !== _structure) {
  37. _params = getParams(ctx, structure);
  38. _structure = structure;
  39. if (!_props) _props = PD.getDefaultValues(_params);
  40. }
  41. _props = Object.assign({}, _props, props);
  42. return Task.create('Creating or updating ComplexRepresentation', async runtime => {
  43. let newVisual = false;
  44. if (!visual) {
  45. visual = visualCtor(materialId, _structure, _props, webgl);
  46. newVisual = true;
  47. } else if (visual.mustRecreate?.(_structure, _props, webgl)) {
  48. visual.destroy();
  49. visual = visualCtor(materialId, _structure, _props, webgl);
  50. newVisual = true;
  51. }
  52. const promise = visual.createOrUpdate({ webgl, runtime }, _theme, _props, structure);
  53. if (promise) await promise;
  54. if (newVisual) setState(_state); // current state for new visual
  55. // update list of renderObjects
  56. renderObjects.length = 0;
  57. if (visual && visual.renderObject) renderObjects.push(visual.renderObject);
  58. // increment version
  59. updated.next(version++);
  60. });
  61. }
  62. function getLoci(pickingId?: PickingId) {
  63. if (pickingId === undefined) return Structure.Loci(_structure);
  64. return visual ? visual.getLoci(pickingId) : EmptyLoci;
  65. }
  66. function mark(loci: Loci, action: MarkerAction) {
  67. if (!_structure) return false;
  68. if (!MarkerActions.is(_state.markerActions, action)) return false;
  69. if (Structure.isLoci(loci) || StructureElement.Loci.is(loci) || Bond.isLoci(loci)) {
  70. if (!Structure.areRootsEquivalent(loci.structure, _structure)) return false;
  71. // Remap `loci` from equivalent structure to the current `_structure`
  72. loci = Loci.remap(loci, _structure);
  73. } else if (!isEveryLoci(loci) && !isDataLoci(loci)) {
  74. return false;
  75. }
  76. if (Loci.isEmpty(loci)) return false;
  77. return visual ? visual.mark(loci, action) : false;
  78. }
  79. function setState(state: Partial<StructureRepresentationState>) {
  80. StructureRepresentationStateBuilder.update(_state, state);
  81. if (state.visible !== undefined && visual) {
  82. // hide visual when _unitTransforms is set and not the identity
  83. visual.setVisibility(state.visible && (_state.unitTransforms === null || _state.unitTransforms.isIdentity));
  84. }
  85. if (state.alphaFactor !== undefined && visual) visual.setAlphaFactor(state.alphaFactor);
  86. if (state.pickable !== undefined && visual) visual.setPickable(state.pickable);
  87. if (state.overpaint !== undefined && visual) {
  88. // Remap loci from equivalent structure to the current structure
  89. const remappedOverpaint = Overpaint.remap(state.overpaint, _structure);
  90. visual.setOverpaint(remappedOverpaint);
  91. }
  92. if (state.transparency !== undefined && visual) {
  93. // Remap loci from equivalent structure to the current structure
  94. const remappedTransparency = Transparency.remap(state.transparency, _structure);
  95. visual.setTransparency(remappedTransparency);
  96. }
  97. if (state.clipping !== undefined && visual) {
  98. // Remap loci from equivalent structure to the current structure
  99. const remappedClipping = Clipping.remap(state.clipping, _structure);
  100. visual.setClipping(remappedClipping);
  101. }
  102. if (state.transform !== undefined && visual) visual.setTransform(state.transform);
  103. if (state.unitTransforms !== undefined && visual) {
  104. // Since ComplexVisuals always renders geometries between units, the application
  105. // of `unitTransforms` does not make sense. When given here and not the identity,
  106. // it is ignored and sets the visual's visibility to `false`.
  107. visual.setVisibility(_state.visible && (state.unitTransforms === null || state.unitTransforms.isIdentity));
  108. }
  109. }
  110. function setTheme(theme: Theme) {
  111. _theme = theme;
  112. }
  113. function destroy() {
  114. if (visual) visual.destroy();
  115. }
  116. return {
  117. label,
  118. get groupCount() {
  119. return visual ? visual.groupCount : 0;
  120. },
  121. get props() { return _props; },
  122. get params() { return _params; },
  123. get state() { return _state; },
  124. get theme() { return _theme; },
  125. renderObjects,
  126. updated,
  127. createOrUpdate,
  128. setState,
  129. setTheme,
  130. getLoci,
  131. mark,
  132. destroy
  133. };
  134. }