element-sphere.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { UnitsMeshParams, UnitsSpheresParams, UnitsVisual, UnitsSpheresVisual, UnitsMeshVisual, StructureGroup } from '../units-visual';
  9. import { WebGLContext } from '../../../mol-gl/webgl/context';
  10. import { createElementSphereImpostor, ElementIterator, getElementLoci, eachElement, createElementSphereMesh } from './util/element';
  11. import { VisualUpdateState } from '../../util';
  12. import { BaseGeometry } from '../../../mol-geo/geometry/base';
  13. import { Structure } from '../../../mol-model/structure';
  14. export const ElementSphereParams = {
  15. ...UnitsMeshParams,
  16. ...UnitsSpheresParams,
  17. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  18. detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }, BaseGeometry.CustomQualityParamInfo),
  19. ignoreHydrogens: PD.Boolean(false),
  20. traceOnly: PD.Boolean(false),
  21. tryUseImpostor: PD.Boolean(true),
  22. };
  23. export type ElementSphereParams = typeof ElementSphereParams
  24. export function ElementSphereVisual(materialId: number, structure: Structure, props: PD.Values<ElementSphereParams>, webgl?: WebGLContext) {
  25. return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
  26. ? ElementSphereImpostorVisual(materialId)
  27. : ElementSphereMeshVisual(materialId);
  28. }
  29. export function ElementSphereImpostorVisual(materialId: number): UnitsVisual<ElementSphereParams> {
  30. return UnitsSpheresVisual<ElementSphereParams>({
  31. defaultProps: PD.getDefaultValues(ElementSphereParams),
  32. createGeometry: createElementSphereImpostor,
  33. createLocationIterator: ElementIterator.fromGroup,
  34. getLoci: getElementLoci,
  35. eachLocation: eachElement,
  36. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<ElementSphereParams>, currentProps: PD.Values<ElementSphereParams>) => {
  37. state.createGeometry = (
  38. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  39. newProps.traceOnly !== currentProps.traceOnly
  40. );
  41. },
  42. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<ElementSphereParams>, webgl?: WebGLContext) => {
  43. return !props.tryUseImpostor || !webgl;
  44. }
  45. }, materialId);
  46. }
  47. export function ElementSphereMeshVisual(materialId: number): UnitsVisual<ElementSphereParams> {
  48. return UnitsMeshVisual<ElementSphereParams>({
  49. defaultProps: PD.getDefaultValues(ElementSphereParams),
  50. createGeometry: createElementSphereMesh,
  51. createLocationIterator: ElementIterator.fromGroup,
  52. getLoci: getElementLoci,
  53. eachLocation: eachElement,
  54. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<ElementSphereParams>, currentProps: PD.Values<ElementSphereParams>) => {
  55. state.createGeometry = (
  56. newProps.sizeFactor !== currentProps.sizeFactor ||
  57. newProps.detail !== currentProps.detail ||
  58. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  59. newProps.traceOnly !== currentProps.traceOnly
  60. );
  61. },
  62. mustRecreate: (structureGroup: StructureGroup, props: PD.Values<ElementSphereParams>, webgl?: WebGLContext) => {
  63. return props.tryUseImpostor && !!webgl;
  64. }
  65. }, materialId);
  66. }