molecular-surface-mesh.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Copyright (c) 2019-2021 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 { UnitsMeshParams, UnitsVisual, UnitsMeshVisual } from '../units-visual';
  8. import { MolecularSurfaceCalculationParams } from '../../../mol-math/geometry/molecular-surface';
  9. import { VisualContext } from '../../visual';
  10. import { Unit, Structure } from '../../../mol-model/structure';
  11. import { Theme } from '../../../mol-theme/theme';
  12. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  13. import { computeUnitMolecularSurface, MolecularSurfaceProps } from './util/molecular-surface';
  14. import { computeMarchingCubesMesh } from '../../../mol-geo/util/marching-cubes/algorithm';
  15. import { ElementIterator, getElementLoci, eachElement } from './util/element';
  16. import { VisualUpdateState } from '../../util';
  17. import { CommonSurfaceParams, getUnitExtraRadius } from './util/common';
  18. import { Sphere3D } from '../../../mol-math/geometry';
  19. import { MeshValues } from '../../../mol-gl/renderable/mesh';
  20. import { Texture } from '../../../mol-gl/webgl/texture';
  21. import { WebGLContext } from '../../../mol-gl/webgl/context';
  22. import { applyMeshColorSmoothing, ColorSmoothingParams, getColorSmoothingProps } from './util/color';
  23. export const MolecularSurfaceMeshParams = {
  24. ...UnitsMeshParams,
  25. ...MolecularSurfaceCalculationParams,
  26. ...CommonSurfaceParams,
  27. ...ColorSmoothingParams,
  28. };
  29. export type MolecularSurfaceMeshParams = typeof MolecularSurfaceMeshParams
  30. type MolecularSurfaceMeta = {
  31. resolution?: number
  32. colorTexture?: Texture
  33. }
  34. //
  35. async function createMolecularSurfaceMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: MolecularSurfaceProps, mesh?: Mesh): Promise<Mesh> {
  36. const { transform, field, idField, resolution } = await computeUnitMolecularSurface(structure, unit, props).runInContext(ctx.runtime);
  37. const params = {
  38. isoLevel: props.probeRadius,
  39. scalarField: field,
  40. idField
  41. };
  42. const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime);
  43. Mesh.transform(surface, transform);
  44. if (ctx.webgl && !ctx.webgl.isWebGL2) Mesh.uniformTriangleGroup(surface);
  45. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, props.probeRadius + getUnitExtraRadius(unit));
  46. surface.setBoundingSphere(sphere);
  47. (surface.meta.resolution as MolecularSurfaceMeta['resolution']) = resolution;
  48. return surface;
  49. }
  50. export function MolecularSurfaceMeshVisual(materialId: number): UnitsVisual<MolecularSurfaceMeshParams> {
  51. return UnitsMeshVisual<MolecularSurfaceMeshParams>({
  52. defaultProps: PD.getDefaultValues(MolecularSurfaceMeshParams),
  53. createGeometry: createMolecularSurfaceMesh,
  54. createLocationIterator: ElementIterator.fromGroup,
  55. getLoci: getElementLoci,
  56. eachLocation: eachElement,
  57. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<MolecularSurfaceMeshParams>, currentProps: PD.Values<MolecularSurfaceMeshParams>) => {
  58. if (newProps.resolution !== currentProps.resolution) state.createGeometry = true;
  59. if (newProps.probeRadius !== currentProps.probeRadius) state.createGeometry = true;
  60. if (newProps.probePositions !== currentProps.probePositions) state.createGeometry = true;
  61. if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true;
  62. if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true;
  63. if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true;
  64. if (newProps.smoothColors.name !== currentProps.smoothColors.name) {
  65. state.updateColor = true;
  66. } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') {
  67. if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true;
  68. if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true;
  69. }
  70. },
  71. processValues: (values: MeshValues, geometry: Mesh, props: PD.Values<MolecularSurfaceMeshParams>, theme: Theme, webgl?: WebGLContext) => {
  72. const { resolution, colorTexture } = geometry.meta as MolecularSurfaceMeta;
  73. const csp = getColorSmoothingProps(props, theme, resolution, webgl);
  74. if (csp) {
  75. applyMeshColorSmoothing(values, csp.resolution, csp.stride, csp.webgl, colorTexture);
  76. (geometry.meta.colorTexture as MolecularSurfaceMeta['colorTexture']) = values.tColorGrid.ref.value;
  77. }
  78. },
  79. dispose: (geometry: Mesh) => {
  80. (geometry.meta as MolecularSurfaceMeta).colorTexture?.destroy();
  81. }
  82. }, materialId);
  83. }