point.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/value-cell'
  7. import { Renderable, BaseProps } from '../renderable'
  8. import { getBaseValues, getBaseDefs, getBaseDefines } from './util'
  9. import { PointShaderCode, addShaderDefines } from '../shader-code'
  10. import { ColorData } from 'mol-geo/util/color-data';
  11. import { SizeData } from 'mol-geo/util/size-data';
  12. import { Context } from '../webgl/context';
  13. import { createRenderItem, RenderItemState, RenderItemProps } from '../webgl/render-item';
  14. type Point = 'point'
  15. namespace Point {
  16. export type Props = {
  17. position: ValueCell<Float32Array>
  18. id: ValueCell<Float32Array>
  19. size: SizeData
  20. color: ColorData
  21. transform: ValueCell<Float32Array>
  22. instanceCount: number
  23. elementCount: number
  24. positionCount: number,
  25. usePointSizeAttenuation?: boolean
  26. } & BaseProps
  27. function getDefs(props: Props) {
  28. const defines = getBaseDefines(props)
  29. if (props.usePointSizeAttenuation) defines.POINT_SIZE_ATTENUATION = ''
  30. const defs: RenderItemProps = {
  31. ...getBaseDefs(props),
  32. shaderCode: addShaderDefines(defines, PointShaderCode),
  33. drawMode: 'points'
  34. }
  35. return defs
  36. }
  37. function getVals(props: Props) {
  38. const vals: RenderItemState = {
  39. ...getBaseValues(props),
  40. drawCount: ValueCell.create(props.positionCount),
  41. instanceCount: ValueCell.create(props.instanceCount)
  42. }
  43. return vals
  44. }
  45. function getRenderItem(ctx: Context, props: Props) {
  46. const defs = getDefs(props)
  47. const vals = getVals(props)
  48. return createRenderItem(ctx, defs, vals)
  49. }
  50. export function create<T = Props>(ctx: Context, props: Props): Renderable<Props> {
  51. // const defs = getDefs(props)
  52. let renderItem = getRenderItem(ctx, props)
  53. // let curProps = props
  54. return {
  55. draw: () => {
  56. renderItem.draw()
  57. },
  58. name: 'point',
  59. get program () { return renderItem.program },
  60. update: (newProps: Props) => {
  61. console.log('Updating point renderable')
  62. renderItem.destroy()
  63. renderItem = getRenderItem(ctx, { ...props, ...newProps })
  64. },
  65. dispose: () => {
  66. renderItem.destroy()
  67. }
  68. }
  69. }
  70. }
  71. export default Point