complex-visual.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  7. import { StructureMeshParams, StructureDirectVolumeParams, StructureTextParams, StructureParams } from './representation';
  8. import { Visual, VisualContext } from '../visual';
  9. import { Structure, StructureElement } from '../../mol-model/structure';
  10. import { Geometry, GeometryUtils } from '../../mol-geo/geometry/geometry';
  11. import { LocationIterator } from '../../mol-geo/util/location-iterator';
  12. import { Theme } from '../../mol-theme/theme';
  13. import { createIdentityTransform } from '../../mol-geo/geometry/transform-data';
  14. import { createRenderObject, RenderObjectValues, GraphicsRenderObject } from '../../mol-gl/render-object';
  15. import { PickingId } from '../../mol-geo/geometry/picking';
  16. import { Loci, isEveryLoci, EmptyLoci } from '../../mol-model/loci';
  17. import { Interval } from '../../mol-data/int';
  18. import { VisualUpdateState } from '../util';
  19. import { ColorTheme } from '../../mol-theme/color';
  20. import { ValueCell, deepEqual } from '../../mol-util';
  21. import { createSizes, SizeData } from '../../mol-geo/geometry/size-data';
  22. import { createColors } from '../../mol-geo/geometry/color-data';
  23. import { MarkerAction } from '../../mol-util/marker-action';
  24. import { Mat4 } from '../../mol-math/linear-algebra';
  25. import { Overpaint } from '../../mol-theme/overpaint';
  26. import { Transparency } from '../../mol-theme/transparency';
  27. import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
  28. import { Text } from '../../mol-geo/geometry/text/text';
  29. import { SizeTheme } from '../../mol-theme/size';
  30. import { DirectVolume } from '../../mol-geo/geometry/direct-volume/direct-volume';
  31. import { createMarkers } from '../../mol-geo/geometry/marker-data';
  32. export interface ComplexVisual<P extends StructureParams> extends Visual<Structure, P> { }
  33. function createComplexRenderObject<G extends Geometry>(structure: Structure, geometry: G, locationIt: LocationIterator, theme: Theme, props: PD.Values<Geometry.Params<G>>, materialId: number) {
  34. const { createValues, createRenderableState } = Geometry.getUtils(geometry)
  35. const transform = createIdentityTransform()
  36. const values = createValues(geometry, transform, locationIt, theme, props)
  37. const state = createRenderableState(props)
  38. return createRenderObject(geometry.kind, values, state, materialId)
  39. }
  40. interface ComplexVisualBuilder<P extends StructureParams, G extends Geometry> {
  41. defaultProps: PD.Values<P>
  42. createGeometry(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G): Promise<G> | G
  43. createLocationIterator(structure: Structure): LocationIterator
  44. getLoci(pickingId: PickingId, structure: Structure, id: number): Loci
  45. eachLocation(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean): boolean,
  46. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure): void
  47. }
  48. interface ComplexVisualGeometryBuilder<P extends StructureParams, G extends Geometry> extends ComplexVisualBuilder<P, G> {
  49. geometryUtils: GeometryUtils<G>
  50. }
  51. export function ComplexVisual<G extends Geometry, P extends StructureParams & Geometry.Params<G>>(builder: ComplexVisualGeometryBuilder<P, G>, materialId: number): ComplexVisual<P> {
  52. const { defaultProps, createGeometry, createLocationIterator, getLoci, eachLocation, setUpdateState } = builder
  53. const { updateValues, updateBoundingSphere, updateRenderableState } = builder.geometryUtils
  54. const updateState = VisualUpdateState.create()
  55. let renderObject: GraphicsRenderObject<G['kind']> | undefined
  56. let newProps: PD.Values<P>
  57. let newTheme: Theme
  58. let newStructure: Structure
  59. let currentProps: PD.Values<P> = Object.assign({}, defaultProps)
  60. let currentTheme: Theme = Theme.createEmpty()
  61. let currentStructure: Structure
  62. let geometry: G
  63. let locationIt: LocationIterator
  64. function prepareUpdate(theme: Theme, props: Partial<PD.Values<P>>, structure: Structure) {
  65. if (!structure && !currentStructure) {
  66. throw new Error('missing structure')
  67. }
  68. newProps = Object.assign({}, currentProps, props)
  69. newTheme = theme
  70. newStructure = structure
  71. VisualUpdateState.reset(updateState)
  72. if (!renderObject) {
  73. updateState.createNew = true
  74. } else if (!currentStructure || !Structure.areEquivalent(newStructure, currentStructure)) {
  75. updateState.createNew = true
  76. }
  77. if (updateState.createNew) {
  78. updateState.createGeometry = true
  79. return
  80. }
  81. setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure)
  82. if (Structure.conformationHash(newStructure) !== Structure.conformationHash(currentStructure)) {
  83. updateState.updateTransform = true;
  84. updateState.createGeometry = true
  85. }
  86. if (!ColorTheme.areEqual(theme.color, currentTheme.color)) {
  87. updateState.updateColor = true
  88. }
  89. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) {
  90. updateState.createGeometry = true
  91. }
  92. if (updateState.createGeometry) {
  93. updateState.updateColor = true
  94. }
  95. }
  96. function update(newGeometry?: G) {
  97. if (updateState.createNew) {
  98. locationIt = createLocationIterator(newStructure)
  99. if (newGeometry) {
  100. renderObject = createComplexRenderObject(newStructure, newGeometry, locationIt, newTheme, newProps, materialId)
  101. } else {
  102. throw new Error('expected geometry to be given')
  103. }
  104. } else {
  105. if (!renderObject) {
  106. throw new Error('expected renderObject to be available')
  107. }
  108. if (updateState.updateTransform) {
  109. // console.log('update transform')
  110. locationIt = createLocationIterator(newStructure)
  111. const { instanceCount, groupCount } = locationIt
  112. createMarkers(instanceCount * groupCount, renderObject.values)
  113. }
  114. if (updateState.createGeometry) {
  115. if (newGeometry) {
  116. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(newGeometry))
  117. updateBoundingSphere(renderObject.values as RenderObjectValues<G['kind']>, newGeometry)
  118. } else {
  119. throw new Error('expected geometry to be given')
  120. }
  121. }
  122. if (updateState.updateSize) {
  123. // not all geometries have size data, so check here
  124. if ('uSize' in renderObject.values) {
  125. createSizes(locationIt, newTheme.size, renderObject.values as SizeData)
  126. }
  127. }
  128. if (updateState.updateColor) {
  129. createColors(locationIt, newTheme.color, renderObject.values)
  130. }
  131. updateValues(renderObject.values as RenderObjectValues<G['kind']>, newProps)
  132. updateRenderableState(renderObject.state, newProps)
  133. }
  134. currentProps = newProps
  135. currentTheme = newTheme
  136. currentStructure = newStructure
  137. if (newGeometry) geometry = newGeometry
  138. }
  139. function lociIsSuperset(loci: Loci) {
  140. if (isEveryLoci(loci)) return true
  141. if (Structure.isLoci(loci) && Structure.areRootsEquivalent(loci.structure, currentStructure)) return true
  142. if (StructureElement.Loci.is(loci) && Structure.areRootsEquivalent(loci.structure, currentStructure)) {
  143. if (StructureElement.Loci.isWholeStructure(loci)) return true
  144. }
  145. return false
  146. }
  147. function lociApply(loci: Loci, apply: (interval: Interval) => boolean) {
  148. if (lociIsSuperset(loci)) {
  149. return apply(Interval.ofBounds(0, locationIt.groupCount * locationIt.instanceCount))
  150. } else {
  151. return eachLocation(loci, currentStructure, apply)
  152. }
  153. }
  154. return {
  155. get groupCount() { return locationIt ? locationIt.count : 0 },
  156. get renderObject () { return locationIt && locationIt.count ? renderObject : undefined },
  157. createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  158. prepareUpdate(theme, props, structure || currentStructure)
  159. if (updateState.createGeometry) {
  160. const newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry)
  161. return newGeometry instanceof Promise ? newGeometry.then(update) : update(newGeometry)
  162. } else {
  163. update()
  164. }
  165. },
  166. getLoci(pickingId: PickingId) {
  167. return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci
  168. },
  169. mark(loci: Loci, action: MarkerAction) {
  170. return Visual.mark(renderObject, loci, action, lociApply)
  171. },
  172. setVisibility(visible: boolean) {
  173. Visual.setVisibility(renderObject, visible)
  174. },
  175. setAlphaFactor(alphaFactor: number) {
  176. Visual.setAlphaFactor(renderObject, alphaFactor)
  177. },
  178. setPickable(pickable: boolean) {
  179. Visual.setPickable(renderObject, pickable)
  180. },
  181. setTransform(matrix?: Mat4, instanceMatrices?: Float32Array | null) {
  182. Visual.setTransform(renderObject, matrix, instanceMatrices)
  183. },
  184. setOverpaint(overpaint: Overpaint) {
  185. Visual.setOverpaint(renderObject, overpaint, lociApply, true)
  186. },
  187. setTransparency(transparency: Transparency) {
  188. Visual.setTransparency(renderObject, transparency, lociApply, true)
  189. },
  190. destroy() {
  191. // TODO
  192. renderObject = undefined
  193. }
  194. }
  195. }
  196. // mesh
  197. export const ComplexMeshParams = { ...StructureMeshParams, ...StructureParams }
  198. export type ComplexMeshParams = typeof ComplexMeshParams
  199. export interface ComplexMeshVisualBuilder<P extends ComplexMeshParams> extends ComplexVisualBuilder<P, Mesh> { }
  200. export function ComplexMeshVisual<P extends ComplexMeshParams>(builder: ComplexMeshVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  201. return ComplexVisual<Mesh, P>({
  202. ...builder,
  203. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  204. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure)
  205. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true
  206. },
  207. geometryUtils: Mesh.Utils
  208. }, materialId)
  209. }
  210. // text
  211. export const ComplexTextParams = { ...StructureTextParams, ...StructureParams }
  212. export type ComplexTextParams = typeof ComplexTextParams
  213. export interface ComplexTextVisualBuilder<P extends ComplexTextParams> extends ComplexVisualBuilder<P, Text> { }
  214. export function ComplexTextVisual<P extends ComplexTextParams>(builder: ComplexTextVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  215. return ComplexVisual<Text, P>({
  216. ...builder,
  217. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  218. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure)
  219. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true
  220. if (newProps.background !== currentProps.background) state.createGeometry = true
  221. if (newProps.backgroundMargin !== currentProps.backgroundMargin) state.createGeometry = true
  222. if (newProps.tether !== currentProps.tether) state.createGeometry = true
  223. if (newProps.tetherLength !== currentProps.tetherLength) state.createGeometry = true
  224. if (newProps.tetherBaseWidth !== currentProps.tetherBaseWidth) state.createGeometry = true
  225. if (newProps.attachment !== currentProps.attachment) state.createGeometry = true
  226. },
  227. geometryUtils: Text.Utils
  228. }, materialId)
  229. }
  230. // direct-volume
  231. export const ComplexDirectVolumeParams = { ...StructureDirectVolumeParams, ...StructureParams }
  232. export type ComplexDirectVolumeParams = typeof ComplexDirectVolumeParams
  233. export interface ComplexDirectVolumeVisualBuilder<P extends ComplexDirectVolumeParams> extends ComplexVisualBuilder<P, DirectVolume> { }
  234. export function ComplexDirectVolumeVisual<P extends ComplexDirectVolumeParams>(builder: ComplexDirectVolumeVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  235. return ComplexVisual<DirectVolume, P>({
  236. ...builder,
  237. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  238. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure)
  239. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
  240. },
  241. geometryUtils: DirectVolume.Utils
  242. }, materialId)
  243. }