state.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 { BehaviorSubject } from 'rxjs';
  7. // import { ValueCell } from 'mol-util/value-cell'
  8. // import { Vec3, Mat4 } from 'mol-math/linear-algebra'
  9. import Viewer from 'mol-view/viewer'
  10. // import { createColorTexture } from 'mol-gl/util';
  11. // import Icosahedron from 'mol-geo/primitive/icosahedron'
  12. // import Box from 'mol-geo/primitive/box'
  13. import Spacefill, { SpacefillProps } from 'mol-geo/representation/structure/spacefill'
  14. import Point, { PointProps } from 'mol-geo/representation/structure/point'
  15. import { Run } from 'mol-task'
  16. import { Symmetry } from 'mol-model/structure'
  17. // import mcubes from './utils/mcubes'
  18. import { getStructuresFromPdbId, log } from './utils'
  19. import { StructureRepresentation } from 'mol-geo/representation/structure';
  20. // import Cylinder from 'mol-geo/primitive/cylinder';
  21. export default class State {
  22. viewer: Viewer
  23. pdbId = '4cup'
  24. initialized = new BehaviorSubject<boolean>(false)
  25. loading = new BehaviorSubject<boolean>(false)
  26. async initRenderer (canvas: HTMLCanvasElement, container: HTMLDivElement) {
  27. this.viewer = Viewer.create(canvas, container)
  28. this.initialized.next(true)
  29. this.loadPdbId()
  30. this.viewer.animate()
  31. }
  32. async loadPdbId () {
  33. const { viewer, pdbId } = this
  34. viewer.clear()
  35. if (pdbId.length !== 4) return
  36. this.loading.next(true)
  37. const structures = await getStructuresFromPdbId(pdbId)
  38. const struct = await Run(Symmetry.buildAssembly(structures[0], '1'), log, 100)
  39. const structPointRepr = StructureRepresentation(Point)
  40. const pointProps: PointProps = {
  41. colorTheme: { name: 'uniform', value: 0xFF4411 },
  42. sizeTheme: { name: 'uniform', value: 0.1 }
  43. }
  44. await Run(structPointRepr.create(struct, pointProps), log, 100)
  45. structPointRepr.renderObjects.forEach(viewer.add)
  46. const structSpacefillRepr = StructureRepresentation(Spacefill)
  47. const spacefillProps: SpacefillProps = {
  48. detail: 1,
  49. // colorTheme: { name: 'uniform', value: 0xFF4411 },
  50. // colorTheme: { name: 'instance-id' },
  51. // colorTheme: { name: 'element-symbol' },
  52. colorTheme: { name: 'atom-id' },
  53. }
  54. await Run(structSpacefillRepr.create(struct, spacefillProps), log, 100)
  55. structSpacefillRepr.renderObjects.forEach(viewer.add)
  56. viewer.requestDraw()
  57. console.log(viewer.stats)
  58. this.loading.next(false)
  59. }
  60. }
  61. // async foo () {
  62. // const p1 = Vec3.create(0, 4, 0)
  63. // const p2 = Vec3.create(-3, 0, 0)
  64. // // const position = ValueCell.create(new Float32Array([0, -1, 0, -1, 0, 0, 1, 1, 0]))
  65. // // const normal = ValueCell.create(new Float32Array([0, 0, 0, 0, 0, 0, 0, 0, 0]))
  66. // const transformArray1 = ValueCell.create(new Float32Array(16))
  67. // const transformArray2 = ValueCell.create(new Float32Array(16 * 3))
  68. // const m4 = Mat4.identity()
  69. // Mat4.toArray(m4, transformArray1.ref.value, 0)
  70. // Mat4.toArray(m4, transformArray2.ref.value, 0)
  71. // Mat4.setTranslation(m4, p1)
  72. // Mat4.toArray(m4, transformArray2.ref.value, 16)
  73. // Mat4.setTranslation(m4, p2)
  74. // Mat4.toArray(m4, transformArray2.ref.value, 32)
  75. // const color = ValueCell.create(createColorTexture(3))
  76. // color.ref.value.set([
  77. // 0, 0, 255,
  78. // 0, 255, 0,
  79. // 255, 0, 0
  80. // ])
  81. // // const points = createRenderObject('point', {
  82. // // position,
  83. // // transform: transformArray1
  84. // // })
  85. // // // renderer.add(points)
  86. // // const mesh = createRenderObject('mesh', {
  87. // // position,
  88. // // normal,
  89. // // color,
  90. // // transform: transformArray2
  91. // // })
  92. // // renderer.add(mesh)
  93. // // const cylinder = Cylinder({ height: 3, radiusBottom: 0.5, radiusTop: 0.5 })
  94. // // console.log(cylinder)
  95. // // const cylinderMesh = createRenderObject('mesh', {
  96. // // position: ValueCell.create(cylinder.vertices),
  97. // // normal: ValueCell.create(cylinder.normals),
  98. // // color,
  99. // // transform: transformArray2
  100. // // }, cylinder.indices)
  101. // // renderer.add(cylinderMesh)
  102. // // const sphere = Icosahedron()
  103. // // console.log(sphere)
  104. // // const box = Box()
  105. // // console.log(box)
  106. // // const points2 = createRenderObject('point', {
  107. // // position: ValueCell.create(new Float32Array(box.vertices)),
  108. // // transform: transformArray1
  109. // // })
  110. // // renderer.add(points2)
  111. // // let rr = 0.7;
  112. // // function cubesF(x: number, y: number, z: number) {
  113. // // return x * x + y * y + z * z - rr * rr;
  114. // // }
  115. // // let cubes = await mcubes(cubesF);
  116. // // const makeCubesMesh = () => createRenderObject('mesh', {
  117. // // position: cubes.surface.vertexBuffer,
  118. // // normal: cubes.surface.normalBuffer,
  119. // // color,
  120. // // transform: transformArray2,
  121. // // elements: cubes.surface.indexBuffer,
  122. // // instanceCount: transformArray2.ref.value.length / 16,
  123. // // elementCount: cubes.surface.triangleCount,
  124. // // positionCount: cubes.surface.vertexCount
  125. // // }, {});
  126. // // const mesh2 = makeCubesMesh();
  127. // // renderer.add(mesh2)
  128. // }