geometry.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 { ColorThemeProps } 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. export type GeometryKindType = { 'mesh': Mesh, 'points': Points, 'lines': Lines }
  18. export type GeometryKind = keyof GeometryKindType
  19. export type Geometry = Helpers.ValueOf<GeometryKindType>
  20. export namespace Geometry {
  21. export function getDrawCount(geometry: Geometry) {
  22. switch (geometry.kind) {
  23. case 'mesh': return geometry.triangleCount * 3
  24. case 'points': return geometry.pointCount
  25. case 'lines': return geometry.lineCount * 2 * 3
  26. }
  27. }
  28. //
  29. export const DefaultProps = {
  30. alpha: 1,
  31. visible: true,
  32. depthMask: true,
  33. useFog: false,
  34. quality: 'auto' as VisualQuality,
  35. colorTheme: { name: 'uniform', value: Color(0x22EE11) } as ColorThemeProps,
  36. }
  37. export type Props = typeof DefaultProps
  38. export type Counts = { drawCount: number, groupCount: number, instanceCount: number }
  39. export function createValues(props: Props, counts: Counts) {
  40. return {
  41. uAlpha: ValueCell.create(props.alpha),
  42. uGroupCount: ValueCell.create(counts.groupCount),
  43. drawCount: ValueCell.create(counts.drawCount),
  44. dUseFog: ValueCell.create(props.useFog),
  45. }
  46. }
  47. export function updateValues(values: BaseValues, props: Props) {
  48. ValueCell.updateIfChanged(values.uAlpha, props.alpha)
  49. ValueCell.updateIfChanged(values.dUseFog, props.useFog)
  50. }
  51. }
  52. //
  53. export function createRenderableState(props: Geometry.Props): RenderableState {
  54. return {
  55. visible: props.visible,
  56. depthMask: props.depthMask
  57. }
  58. }
  59. export function updateRenderableState(state: RenderableState, props: Geometry.Props) {
  60. state.visible = props.visible
  61. state.depthMask = props.depthMask
  62. }
  63. //
  64. export function getGranularity(locationIt: LocationIterator, granularity: ColorType | SizeType) {
  65. // Always use 'group' granularity for 'complex' location iterators,
  66. // i.e. for which an instance may include multiple units
  67. return granularity === 'instance' && locationIt.isComplex ? 'group' : granularity
  68. }
  69. //
  70. export const VisualQualityInfo = {
  71. 'custom': {},
  72. 'auto': {},
  73. 'highest': {},
  74. 'higher': {},
  75. 'high': {},
  76. 'medium': {},
  77. 'low': {},
  78. 'lower': {},
  79. 'lowest': {},
  80. }
  81. export type VisualQuality = keyof typeof VisualQualityInfo
  82. export const VisualQualityNames = Object.keys(VisualQualityInfo)