point.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 { createRenderObject, RenderObject } from 'mol-gl/scene'
  8. import { createColorTexture } from 'mol-gl/util';
  9. import { Mat4 } from 'mol-math/linear-algebra'
  10. import { OrderedSet } from 'mol-data/int'
  11. import { ChunkedArray } from 'mol-data/util';
  12. import { Unit, ElementGroup } from 'mol-model/structure';
  13. import { RepresentationProps, UnitsRepresentation } from './index';
  14. import { Task } from 'mol-task'
  15. import { VdwRadius } from 'mol-model/structure/model/properties/atomic';
  16. export const DefaultPointProps = {
  17. }
  18. export type PointProps = Partial<typeof DefaultPointProps>
  19. export default function Point(): UnitsRepresentation<PointProps> {
  20. const renderObjects: RenderObject[] = []
  21. const vertices = ChunkedArray.create(Float32Array, 3, 1024, 2048);
  22. const sizes = ChunkedArray.create(Float32Array, 1, 1024, 2048);
  23. return {
  24. renderObjects,
  25. create: (units: ReadonlyArray<Unit>, elementGroup: ElementGroup, props: PointProps = {}) => Task.create('Spacefill', async ctx => {
  26. // const l = Element.Location();
  27. const { x, y, z } = units[0].model.conformation
  28. const { type_symbol } = units[0].model.hierarchy.atoms
  29. const elementCount = OrderedSet.size(elementGroup.elements)
  30. for (let i = 0; i < elementCount; i++) {
  31. const e = OrderedSet.getAt(elementGroup.elements, i)
  32. ChunkedArray.add3(vertices, x[e], y[e], z[e])
  33. ChunkedArray.add(sizes, VdwRadius(type_symbol.value(e)))
  34. if (i % 10 === 0 && ctx.shouldUpdate) {
  35. await ctx.update({ message: 'Point', current: i, max: elementCount });
  36. }
  37. }
  38. const unitsCount = units.length
  39. const transformArray = new Float32Array(unitsCount * 16)
  40. for (let i = 0; i < unitsCount; i++) {
  41. Mat4.toArray(units[i].operator.matrix, transformArray, i * 16)
  42. }
  43. const color = ValueCell.create(createColorTexture(unitsCount))
  44. color.ref.value.set([ 0, 0, 255 ])
  45. const points = createRenderObject('point', {
  46. position: ValueCell.create(ChunkedArray.compact(vertices, true) as Float32Array),
  47. size: ValueCell.create(ChunkedArray.compact(sizes, true) as Float32Array),
  48. color,
  49. transform: ValueCell.create(transformArray),
  50. instanceCount: unitsCount,
  51. positionCount: vertices.elementCount
  52. }, {})
  53. renderObjects.push(points)
  54. }),
  55. update: (props: RepresentationProps) => false
  56. }
  57. }