points.ts 4.2 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 { ValueCell } from 'mol-util'
  7. import { Mat4 } from 'mol-math/linear-algebra'
  8. import { transformPositionArray/* , transformDirectionArray, getNormalMatrix */ } from '../../util';
  9. import { Geometry } from '../geometry';
  10. import { PointsValues } from 'mol-gl/renderable';
  11. import { RuntimeContext } from 'mol-task';
  12. import { createColors } from '../color-data';
  13. import { createMarkers } from '../marker-data';
  14. import { createSizes } from '../size-data';
  15. import { TransformData } from '../transform-data';
  16. import { LocationIterator } from '../../util/location-iterator';
  17. import { SizeThemeName, SizeThemeOptions } from 'mol-canvas3d/theme/size';
  18. import { BooleanParam, NumberParam, SelectParam, paramDefaultValues } from 'mol-util/parameter';
  19. /** Point cloud */
  20. export interface Points {
  21. readonly kind: 'points',
  22. /** Number of vertices in the point cloud */
  23. pointCount: number,
  24. /** Vertex buffer as array of xyz values wrapped in a value cell */
  25. readonly centerBuffer: ValueCell<Float32Array>,
  26. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  27. readonly groupBuffer: ValueCell<Float32Array>,
  28. }
  29. export namespace Points {
  30. export function createEmpty(points?: Points): Points {
  31. const cb = points ? points.centerBuffer.ref.value : new Float32Array(0)
  32. const gb = points ? points.groupBuffer.ref.value : new Float32Array(0)
  33. return {
  34. kind: 'points',
  35. pointCount: 0,
  36. centerBuffer: points ? ValueCell.update(points.centerBuffer, cb) : ValueCell.create(cb),
  37. groupBuffer: points ? ValueCell.update(points.groupBuffer, gb) : ValueCell.create(gb),
  38. }
  39. }
  40. export function transformImmediate(points: Points, t: Mat4) {
  41. transformRangeImmediate(points, t, 0, points.pointCount)
  42. }
  43. export function transformRangeImmediate(points: Points, t: Mat4, offset: number, count: number) {
  44. const c = points.centerBuffer.ref.value
  45. transformPositionArray(t, c, offset, count)
  46. ValueCell.update(points.centerBuffer, c);
  47. }
  48. //
  49. export const Params = {
  50. ...Geometry.Params,
  51. pointSizeAttenuation: BooleanParam('Point Size Attenuation', '', false),
  52. pointFilledCircle: BooleanParam('Point Filled Circle', '', false),
  53. pointEdgeBleach: NumberParam('Point Edge Bleach', '', 0.2, 0, 1, 0.05),
  54. sizeTheme: SelectParam<SizeThemeName>('Size Theme', '', 'uniform', SizeThemeOptions),
  55. sizeValue: NumberParam('Size Value', '', 1, 0, 20, 0.1),
  56. sizeFactor: NumberParam('Size Factor', '', 1, 0, 10, 0.1),
  57. }
  58. export const DefaultProps = paramDefaultValues(Params)
  59. export type Props = typeof DefaultProps
  60. export async function createValues(ctx: RuntimeContext, points: Points, transform: TransformData, locationIt: LocationIterator, props: Props): Promise<PointsValues> {
  61. const { instanceCount, groupCount } = locationIt
  62. const color = await createColors(ctx, locationIt, props)
  63. const size = await createSizes(ctx, locationIt, props)
  64. const marker = createMarkers(instanceCount * groupCount)
  65. const counts = { drawCount: points.pointCount, groupCount, instanceCount }
  66. return {
  67. aPosition: points.centerBuffer,
  68. aGroup: points.groupBuffer,
  69. ...color,
  70. ...size,
  71. ...marker,
  72. ...transform,
  73. ...Geometry.createValues(props, counts),
  74. dPointSizeAttenuation: ValueCell.create(props.pointSizeAttenuation),
  75. dPointFilledCircle: ValueCell.create(props.pointFilledCircle),
  76. uPointEdgeBleach: ValueCell.create(props.pointEdgeBleach),
  77. }
  78. }
  79. export function updateValues(values: PointsValues, props: Props) {
  80. Geometry.updateValues(values, props)
  81. ValueCell.updateIfChanged(values.dPointSizeAttenuation, props.pointSizeAttenuation)
  82. ValueCell.updateIfChanged(values.dPointFilledCircle, props.pointFilledCircle)
  83. ValueCell.updateIfChanged(values.uPointEdgeBleach, props.pointEdgeBleach)
  84. }
  85. }