point.ts 3.8 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/value-cell'
  7. import { createPointRenderObject, RenderObject } from 'mol-gl/scene'
  8. import { Mat4 } from 'mol-math/linear-algebra'
  9. import { OrderedSet } from 'mol-data/int'
  10. import { ChunkedArray } from 'mol-data/util';
  11. import { Unit, ElementGroup } from 'mol-model/structure';
  12. import { RepresentationProps, UnitsRepresentation } from './index';
  13. import { Task } from 'mol-task'
  14. import { VdwRadius } from 'mol-model/structure/model/properties/atomic';
  15. import { createUniformColor, createInstanceColor } from '../../color/data';
  16. import { fillSerial } from 'mol-gl/renderable/util';
  17. import { ColorScale } from '../../color/scale';
  18. import { createUniformSize } from '../../size/data';
  19. import { vdwSizeData } from '../../size/structure/vdw';
  20. import VertexMap from '../../shape/vertex-map';
  21. export const DefaultPointProps = {
  22. }
  23. export type PointProps = Partial<typeof DefaultPointProps>
  24. export default function Point(): UnitsRepresentation<PointProps> {
  25. const renderObjects: RenderObject[] = []
  26. const vertices = ChunkedArray.create(Float32Array, 3, 1024, 2048);
  27. const sizes = ChunkedArray.create(Float32Array, 1, 1024, 2048);
  28. return {
  29. renderObjects,
  30. create: (units: ReadonlyArray<Unit>, elementGroup: ElementGroup, props: PointProps = {}) => Task.create('Spacefill', async ctx => {
  31. // const l = Element.Location();
  32. const { x, y, z } = units[0].model.conformation
  33. const { type_symbol } = units[0].model.hierarchy.atoms
  34. const elementCount = OrderedSet.size(elementGroup.elements)
  35. for (let i = 0; i < elementCount; i++) {
  36. const e = OrderedSet.getAt(elementGroup.elements, i)
  37. ChunkedArray.add3(vertices, x[e], y[e], z[e])
  38. ChunkedArray.add(sizes, VdwRadius(type_symbol.value(e)))
  39. if (i % 10000 === 0 && ctx.shouldUpdate) {
  40. await ctx.update({ message: 'Point', current: i, max: elementCount });
  41. }
  42. }
  43. const unitCount = units.length
  44. const transformArray = new Float32Array(unitCount * 16)
  45. for (let i = 0; i < unitCount; i++) {
  46. Mat4.toArray(units[i].operator.matrix, transformArray, i * 16)
  47. }
  48. // const color = createUniformColor({ value: 0xFF4411 })
  49. const scale = ColorScale.create({ domain: [ 0, unitCount - 1 ] })
  50. const color = createInstanceColor({
  51. colorFn: scale.color,
  52. unitCount
  53. })
  54. // const size = createUniformSize({ value: 1 })
  55. const size = vdwSizeData({
  56. units,
  57. elementGroup,
  58. vertexMap: VertexMap.create(
  59. elementCount,
  60. elementCount + 1,
  61. fillSerial(new Uint32Array(elementCount)),
  62. fillSerial(new Uint32Array(elementCount + 1))
  63. )
  64. })
  65. console.log(size)
  66. const points = createPointRenderObject({
  67. objectId: 0,
  68. position: ValueCell.create(ChunkedArray.compact(vertices, true) as Float32Array),
  69. id: ValueCell.create(fillSerial(new Float32Array(unitCount))),
  70. size,
  71. color,
  72. transform: ValueCell.create(transformArray),
  73. instanceCount: unitCount,
  74. elementCount,
  75. positionCount: vertices.elementCount,
  76. usePointSizeAttenuation: true
  77. })
  78. renderObjects.push(points)
  79. }),
  80. update: (props: RepresentationProps) => false
  81. }
  82. }