geometry.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Mesh } from './mesh/mesh';
  7. import { Points } from './points/points';
  8. import { RenderableState } from 'mol-gl/renderable';
  9. import { ValueCell } from 'mol-util';
  10. import { BaseValues } from 'mol-gl/renderable/schema';
  11. import { Color } from 'mol-util/color';
  12. import { ColorThemeOptions, ColorThemeName } from 'mol-view/theme/color';
  13. import { LocationIterator } from '../util/location-iterator';
  14. import { ColorType } from './color-data';
  15. import { SizeType } from './size-data';
  16. import { Lines } from './lines/lines';
  17. import { paramDefaultValues, RangeParam, BooleanParam, SelectParam, ColorParam, StructureParam, ValueParam } from 'mol-view/parameter'
  18. import { Structure } from 'mol-model/structure';
  19. import { DirectVolume2d, DirectVolume3d } from './direct-volume/direct-volume';
  20. import { Context } from 'mol-gl/webgl/context';
  21. //
  22. export const VisualQualityInfo = {
  23. 'custom': {},
  24. 'auto': {},
  25. 'highest': {},
  26. 'higher': {},
  27. 'high': {},
  28. 'medium': {},
  29. 'low': {},
  30. 'lower': {},
  31. 'lowest': {},
  32. }
  33. export type VisualQuality = keyof typeof VisualQualityInfo
  34. export const VisualQualityNames = Object.keys(VisualQualityInfo)
  35. export const VisualQualityOptions = VisualQualityNames.map(n => [n, n] as [VisualQuality, string])
  36. //
  37. export type GeometryKindType = {
  38. 'mesh': Mesh,
  39. 'points': Points,
  40. 'lines': Lines,
  41. 'direct-volume-2d': DirectVolume2d,
  42. 'direct-volume-3d': DirectVolume3d
  43. }
  44. export type GeometryKind = keyof GeometryKindType
  45. export type Geometry = Helpers.ValueOf<GeometryKindType>
  46. export namespace Geometry {
  47. export function getDrawCount(geometry: Geometry) {
  48. switch (geometry.kind) {
  49. case 'mesh': return geometry.triangleCount * 3
  50. case 'points': return geometry.pointCount
  51. case 'lines': return geometry.lineCount * 2 * 3
  52. case 'direct-volume-2d': return 12 * 3
  53. case 'direct-volume-3d': return 12 * 3
  54. }
  55. }
  56. //
  57. export const Params = {
  58. alpha: RangeParam('Opacity', '', 1, 0, 1, 0.01),
  59. visible: BooleanParam('Visible', '', true),
  60. depthMask: BooleanParam('Depth Mask', '', true),
  61. useFog: BooleanParam('Use Fog', '', false),
  62. quality: SelectParam<VisualQuality>('Quality', '', 'auto', VisualQualityOptions),
  63. colorTheme: SelectParam<ColorThemeName>('Color Theme', '', 'uniform', ColorThemeOptions),
  64. colorValue: ColorParam('Color Value', '', Color(0xCCCCCC)),
  65. structure: StructureParam('Structure', '', Structure.Empty),
  66. webgl: ValueParam('WebGL Context', '', undefined as Context | undefined),
  67. }
  68. export const DefaultProps = paramDefaultValues(Params)
  69. export type Props = typeof DefaultProps
  70. export type Counts = { drawCount: number, groupCount: number, instanceCount: number }
  71. export function createValues(props: Props, counts: Counts) {
  72. return {
  73. uAlpha: ValueCell.create(props.alpha),
  74. uGroupCount: ValueCell.create(counts.groupCount),
  75. drawCount: ValueCell.create(counts.drawCount),
  76. dUseFog: ValueCell.create(props.useFog),
  77. }
  78. }
  79. export function updateValues(values: BaseValues, props: Props) {
  80. ValueCell.updateIfChanged(values.uAlpha, props.alpha)
  81. ValueCell.updateIfChanged(values.dUseFog, props.useFog)
  82. }
  83. }
  84. //
  85. export function createRenderableState(props: Geometry.Props): RenderableState {
  86. return {
  87. visible: props.visible,
  88. depthMask: props.depthMask
  89. }
  90. }
  91. export function updateRenderableState(state: RenderableState, props: Geometry.Props) {
  92. state.visible = props.visible
  93. state.depthMask = props.depthMask
  94. }
  95. //
  96. export function getGranularity(locationIt: LocationIterator, granularity: ColorType | SizeType) {
  97. // Always use 'group' granularity for 'complex' location iterators,
  98. // i.e. for which an instance may include multiple units
  99. return granularity === 'instance' && locationIt.isComplex ? 'group' : granularity
  100. }