spheres.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ValueCell } from '../../../mol-util';
  7. import { GeometryUtils } from '../geometry';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { TransformData } from '../transform-data';
  10. import { LocationIterator } from '../../../mol-geo/util/location-iterator';
  11. import { Theme } from '../../../mol-theme/theme';
  12. import { SpheresValues } from '../../../mol-gl/renderable/spheres';
  13. import { createColors } from '../color-data';
  14. import { createMarkers } from '../marker-data';
  15. import { calculateBoundingSphere } from '../../../mol-gl/renderable/util';
  16. import { Sphere3D } from '../../../mol-math/geometry';
  17. import { createSizes, getMaxSize } from '../size-data';
  18. import { Color } from '../../../mol-util/color';
  19. import { BaseGeometry } from '../base';
  20. import { createEmptyOverpaint } from '../overpaint-data';
  21. import { createEmptyTransparency } from '../transparency-data';
  22. /** Spheres */
  23. export interface Spheres {
  24. readonly kind: 'spheres',
  25. /** Number of spheres */
  26. sphereCount: number,
  27. /** Center buffer as array of xyz values wrapped in a value cell */
  28. readonly centerBuffer: ValueCell<Float32Array>,
  29. /** Mapping buffer as array of xy values wrapped in a value cell */
  30. readonly mappingBuffer: ValueCell<Float32Array>,
  31. /** Index buffer as array of center index triplets wrapped in a value cell */
  32. readonly indexBuffer: ValueCell<Uint32Array>,
  33. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  34. readonly groupBuffer: ValueCell<Float32Array>,
  35. }
  36. export namespace Spheres {
  37. export function createEmpty(spheres?: Spheres): Spheres {
  38. const cb = spheres ? spheres.centerBuffer.ref.value : new Float32Array(0)
  39. const mb = spheres ? spheres.mappingBuffer.ref.value : new Float32Array(0)
  40. const ib = spheres ? spheres.indexBuffer.ref.value : new Uint32Array(0)
  41. const gb = spheres ? spheres.groupBuffer.ref.value : new Float32Array(0)
  42. return {
  43. kind: 'spheres',
  44. sphereCount: 0,
  45. centerBuffer: spheres ? ValueCell.update(spheres.centerBuffer, cb) : ValueCell.create(cb),
  46. mappingBuffer: spheres ? ValueCell.update(spheres.mappingBuffer, mb) : ValueCell.create(mb),
  47. indexBuffer: spheres ? ValueCell.update(spheres.indexBuffer, ib) : ValueCell.create(ib),
  48. groupBuffer: spheres ? ValueCell.update(spheres.groupBuffer, gb) : ValueCell.create(gb)
  49. }
  50. }
  51. export const Params = {
  52. ...BaseGeometry.Params,
  53. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  54. doubleSided: PD.Boolean(false),
  55. ignoreLight: PD.Boolean(false),
  56. }
  57. export type Params = typeof Params
  58. export const Utils: GeometryUtils<Spheres, Params> = {
  59. Params,
  60. createEmpty,
  61. createValues,
  62. createValuesSimple,
  63. updateValues,
  64. updateBoundingSphere,
  65. createRenderableState: BaseGeometry.createRenderableState,
  66. updateRenderableState: BaseGeometry.updateRenderableState
  67. }
  68. function createValues(spheres: Spheres, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): SpheresValues {
  69. const { instanceCount, groupCount } = locationIt
  70. if (instanceCount !== transform.instanceCount.ref.value) {
  71. throw new Error('instanceCount values in TransformData and LocationIterator differ')
  72. }
  73. const color = createColors(locationIt, theme.color)
  74. const size = createSizes(locationIt, theme.size)
  75. const marker = createMarkers(instanceCount * groupCount)
  76. const overpaint = createEmptyOverpaint()
  77. const transparency = createEmptyTransparency()
  78. const counts = { drawCount: spheres.sphereCount * 2 * 3, groupCount, instanceCount }
  79. const padding = getMaxSize(size)
  80. const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
  81. spheres.centerBuffer.ref.value, spheres.sphereCount * 4,
  82. transform.aTransform.ref.value, instanceCount, padding, 4
  83. )
  84. return {
  85. aPosition: spheres.centerBuffer,
  86. aMapping: spheres.mappingBuffer,
  87. aGroup: spheres.groupBuffer,
  88. elements: spheres.indexBuffer,
  89. boundingSphere: ValueCell.create(boundingSphere),
  90. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  91. ...color,
  92. ...size,
  93. ...marker,
  94. ...overpaint,
  95. ...transparency,
  96. ...transform,
  97. padding: ValueCell.create(padding),
  98. ...BaseGeometry.createValues(props, counts),
  99. uSizeFactor: ValueCell.create(props.sizeFactor),
  100. dDoubleSided: ValueCell.create(props.doubleSided),
  101. dIgnoreLight: ValueCell.create(props.ignoreLight),
  102. }
  103. }
  104. function createValuesSimple(spheres: Spheres, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  105. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform)
  106. const p = { ...PD.getDefaultValues(Params), ...props }
  107. return createValues(spheres, s.transform, s.locationIterator, s.theme, p)
  108. }
  109. function updateValues(values: SpheresValues, props: PD.Values<Params>) {
  110. BaseGeometry.updateValues(values, props)
  111. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor)
  112. ValueCell.updateIfChanged(values.dDoubleSided, props.doubleSided)
  113. ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight)
  114. }
  115. function updateBoundingSphere(values: SpheresValues, spheres: Spheres) {
  116. const padding = getMaxSize(values)
  117. const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
  118. values.aPosition.ref.value, spheres.sphereCount * 4,
  119. values.aTransform.ref.value, values.instanceCount.ref.value, padding, 4
  120. )
  121. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  122. ValueCell.update(values.boundingSphere, boundingSphere)
  123. }
  124. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  125. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere)
  126. }
  127. ValueCell.update(values.padding, padding)
  128. }
  129. }