point.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 REGL = require('regl');
  7. import { ValueCell } from 'mol-util/value-cell'
  8. import { Renderable } from '../renderable'
  9. import { getBuffers, createTransformAttributes, fillSerial } from './util'
  10. import Attribute from '../attribute';
  11. import { PointShaders } from '../shaders'
  12. type Point = 'point'
  13. namespace Point {
  14. export type Data = {
  15. position: ValueCell<Float32Array>
  16. size?: ValueCell<Float32Array>
  17. transform: ValueCell<Float32Array>
  18. instanceCount: number
  19. positionCount: number
  20. }
  21. export function create(regl: REGL.Regl, data: Data): Renderable {
  22. const instanceId = ValueCell.create(fillSerial(new Float32Array(data.instanceCount)))
  23. const attributes = getBuffers({
  24. instanceId: Attribute.create(regl, instanceId, data.instanceCount, { size: 1, divisor: 1 }),
  25. position: Attribute.create(regl, data.position, data.positionCount, { size: 3 }),
  26. ...createTransformAttributes(regl, data.transform, data.positionCount)
  27. })
  28. if (data.size) {
  29. attributes.size = Attribute.create(regl, data.size, data.positionCount, { size: 1 }).buffer
  30. }
  31. const command = regl({
  32. ...PointShaders,
  33. attributes,
  34. count: data.positionCount,
  35. instances: data.instanceCount,
  36. primitive: 'points'
  37. })
  38. return {
  39. draw: () => command(),
  40. get stats() {
  41. return command.stats
  42. },
  43. name: 'point'
  44. }
  45. }
  46. }
  47. export default Point
  48. // namespace Point {
  49. // export type DataType = {
  50. // position: { type: Float32Array, itemSize: 3 }
  51. // }
  52. // export type Data = { [K in keyof DataType]: DataType[K]['type'] }
  53. // export type Attributes = { [K in keyof Data]: Attribute<Data[K]> }
  54. // export function create(regl: REGL.Regl, dataOrCount: Data | number): Renderable<Data> {
  55. // let count: number
  56. // let data: Data
  57. // if (typeof dataOrCount === 'number') {
  58. // count = dataOrCount
  59. // data = {
  60. // position: new Float32Array(count * 3)
  61. // }
  62. // } else {
  63. // count = dataOrCount.position.length / 3
  64. // data = dataOrCount
  65. // }
  66. // const attributes = createAttributes(regl, data)
  67. // const command = regl({
  68. // vert: pointVert,
  69. // frag: pointFrag,
  70. // attributes: getBuffers(attributes),
  71. // count,
  72. // primitive: 'points'
  73. // })
  74. // return {
  75. // draw: () => command(),
  76. // setCount: (newCount: number) => {
  77. // for (const k of Object.keys(data)) {
  78. // attributes[k as keyof Data].setCount(newCount)
  79. // }
  80. // count = newCount
  81. // },
  82. // getCount: () => count,
  83. // attributes
  84. // }
  85. // }
  86. // }