point.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. transform: ValueCell<Float32Array>
  17. instanceCount: number
  18. positionCount: number
  19. }
  20. export function create(regl: REGL.Regl, data: Data): Renderable {
  21. console.log(data)
  22. const instanceId = ValueCell.create(fillSerial(new Float32Array(data.instanceCount)))
  23. const command = regl({
  24. ...PointShaders,
  25. attributes: getBuffers({
  26. instanceId: Attribute.create(regl, instanceId, data.instanceCount, { size: 1, divisor: 1 }),
  27. position: Attribute.create(regl, data.position, data.positionCount, { size: 3 }),
  28. ...createTransformAttributes(regl, data.transform, data.positionCount)
  29. }),
  30. count: data.positionCount,
  31. instances: data.instanceCount,
  32. primitive: 'points'
  33. })
  34. return {
  35. draw: () => command(),
  36. }
  37. }
  38. }
  39. export default Point
  40. // namespace Point {
  41. // export type DataType = {
  42. // position: { type: Float32Array, itemSize: 3 }
  43. // }
  44. // export type Data = { [K in keyof DataType]: DataType[K]['type'] }
  45. // export type Attributes = { [K in keyof Data]: Attribute<Data[K]> }
  46. // export function create(regl: REGL.Regl, dataOrCount: Data | number): Renderable<Data> {
  47. // let count: number
  48. // let data: Data
  49. // if (typeof dataOrCount === 'number') {
  50. // count = dataOrCount
  51. // data = {
  52. // position: new Float32Array(count * 3)
  53. // }
  54. // } else {
  55. // count = dataOrCount.position.length / 3
  56. // data = dataOrCount
  57. // }
  58. // const attributes = createAttributes(regl, data)
  59. // const command = regl({
  60. // vert: pointVert,
  61. // frag: pointFrag,
  62. // attributes: getBuffers(attributes),
  63. // count,
  64. // primitive: 'points'
  65. // })
  66. // return {
  67. // draw: () => command(),
  68. // setCount: (newCount: number) => {
  69. // for (const k of Object.keys(data)) {
  70. // attributes[k as keyof Data].setCount(newCount)
  71. // }
  72. // count = newCount
  73. // },
  74. // getCount: () => count,
  75. // attributes
  76. // }
  77. // }
  78. // }