element-sphere.ts 3.3 KB

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