complex-visual.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * Copyright (c) 2018-2021 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 { Visual, VisualContext } from '../visual';
  8. import { Structure, StructureElement } from '../../mol-model/structure';
  9. import { Geometry, GeometryUtils } from '../../mol-geo/geometry/geometry';
  10. import { LocationIterator } from '../../mol-geo/util/location-iterator';
  11. import { Theme } from '../../mol-theme/theme';
  12. import { createIdentityTransform } from '../../mol-geo/geometry/transform-data';
  13. import { createRenderObject, GraphicsRenderObject, RenderObjectValues } from '../../mol-gl/render-object';
  14. import { PickingId } from '../../mol-geo/geometry/picking';
  15. import { Loci, isEveryLoci, EmptyLoci } from '../../mol-model/loci';
  16. import { Interval } from '../../mol-data/int';
  17. import { VisualUpdateState } from '../util';
  18. import { ColorTheme } from '../../mol-theme/color';
  19. import { ValueCell, deepEqual } from '../../mol-util';
  20. import { createSizes, SizeData } from '../../mol-geo/geometry/size-data';
  21. import { createColors } from '../../mol-geo/geometry/color-data';
  22. import { MarkerAction } from '../../mol-util/marker-action';
  23. import { Mat4 } from '../../mol-math/linear-algebra';
  24. import { Overpaint } from '../../mol-theme/overpaint';
  25. import { Transparency } from '../../mol-theme/transparency';
  26. import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
  27. import { Cylinders } from '../../mol-geo/geometry/cylinders/cylinders';
  28. import { Lines } from '../../mol-geo/geometry/lines/lines';
  29. import { Text } from '../../mol-geo/geometry/text/text';
  30. import { SizeTheme } from '../../mol-theme/size';
  31. import { DirectVolume } from '../../mol-geo/geometry/direct-volume/direct-volume';
  32. import { createMarkers } from '../../mol-geo/geometry/marker-data';
  33. import { StructureParams, StructureMeshParams, StructureTextParams, StructureDirectVolumeParams, StructureLinesParams, StructureCylindersParams, StructureTextureMeshParams } from './params';
  34. import { Clipping } from '../../mol-theme/clipping';
  35. import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh';
  36. import { WebGLContext } from '../../mol-gl/webgl/context';
  37. import { isPromiseLike } from '../../mol-util/type-helpers';
  38. import { Substance } from '../../mol-theme/substance';
  39. export interface ComplexVisual<P extends StructureParams> extends Visual<Structure, P> { }
  40. function createComplexRenderObject<G extends Geometry>(structure: Structure, geometry: G, locationIt: LocationIterator, theme: Theme, props: PD.Values<Geometry.Params<G>>, materialId: number) {
  41. const { createValues, createRenderableState } = Geometry.getUtils(geometry);
  42. const transform = createIdentityTransform();
  43. const values = createValues(geometry, transform, locationIt, theme, props);
  44. const state = createRenderableState(props);
  45. return createRenderObject(geometry.kind, values, state, materialId);
  46. }
  47. interface ComplexVisualBuilder<P extends StructureParams, G extends Geometry> {
  48. defaultProps: PD.Values<P>
  49. createGeometry(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G): Promise<G> | G
  50. createLocationIterator(structure: Structure): LocationIterator
  51. getLoci(pickingId: PickingId, structure: Structure, id: number): Loci
  52. eachLocation(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean, isMarking: boolean): boolean,
  53. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure): void
  54. mustRecreate?: (structure: Structure, props: PD.Values<P>) => boolean
  55. processValues?: (values: RenderObjectValues<G['kind']>, geometry: G, props: PD.Values<P>, theme: Theme, webgl?: WebGLContext) => void
  56. dispose?: (geometry: G) => void
  57. }
  58. interface ComplexVisualGeometryBuilder<P extends StructureParams, G extends Geometry> extends ComplexVisualBuilder<P, G> {
  59. geometryUtils: GeometryUtils<G>
  60. }
  61. export function ComplexVisual<G extends Geometry, P extends StructureParams & Geometry.Params<G>>(builder: ComplexVisualGeometryBuilder<P, G>, materialId: number): ComplexVisual<P> {
  62. const { defaultProps, createGeometry, createLocationIterator, getLoci, eachLocation, setUpdateState, mustRecreate, processValues, dispose } = builder;
  63. const { updateValues, updateBoundingSphere, updateRenderableState, createPositionIterator } = builder.geometryUtils;
  64. const updateState = VisualUpdateState.create();
  65. const previousMark: Visual.PreviousMark = { loci: EmptyLoci, action: MarkerAction.None, status: -1 };
  66. let renderObject: GraphicsRenderObject<G['kind']> | undefined;
  67. let newProps: PD.Values<P>;
  68. let newTheme: Theme;
  69. let newStructure: Structure;
  70. let currentProps: PD.Values<P> = Object.assign({}, defaultProps);
  71. let currentTheme: Theme = Theme.createEmpty();
  72. let currentStructure: Structure;
  73. let geometry: G;
  74. let geometryVersion = -1;
  75. let locationIt: LocationIterator;
  76. let positionIt: LocationIterator;
  77. function prepareUpdate(theme: Theme, props: Partial<PD.Values<P>>, structure: Structure) {
  78. if (!structure && !currentStructure) {
  79. throw new Error('missing structure');
  80. }
  81. newProps = Object.assign({}, currentProps, props);
  82. newTheme = theme;
  83. newStructure = structure;
  84. VisualUpdateState.reset(updateState);
  85. if (!renderObject || !currentStructure) {
  86. updateState.createNew = true;
  87. updateState.createGeometry = true;
  88. return;
  89. }
  90. setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  91. if (!Structure.areEquivalent(newStructure, currentStructure)) {
  92. updateState.createGeometry = true;
  93. }
  94. if (!Structure.areHierarchiesEqual(newStructure, currentStructure)) {
  95. updateState.updateTransform = true;
  96. updateState.createGeometry = true;
  97. }
  98. if (!ColorTheme.areEqual(theme.color, currentTheme.color)) {
  99. updateState.updateColor = true;
  100. }
  101. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) {
  102. updateState.createGeometry = true;
  103. }
  104. if (currentStructure.child !== newStructure.child) {
  105. // console.log('new child');
  106. updateState.createGeometry = true;
  107. }
  108. if (updateState.updateSize && !('uSize' in renderObject.values)) {
  109. updateState.createGeometry = true;
  110. }
  111. if (updateState.createGeometry) {
  112. updateState.updateColor = true;
  113. updateState.updateSize = true;
  114. }
  115. }
  116. function update(newGeometry?: G) {
  117. if (updateState.createNew) {
  118. locationIt = createLocationIterator(newStructure);
  119. if (newGeometry) {
  120. renderObject = createComplexRenderObject(newStructure, newGeometry, locationIt, newTheme, newProps, materialId);
  121. positionIt = createPositionIterator(newGeometry, renderObject.values);
  122. } else {
  123. throw new Error('expected geometry to be given');
  124. }
  125. } else {
  126. if (!renderObject) {
  127. throw new Error('expected renderObject to be available');
  128. }
  129. if (updateState.updateTransform) {
  130. // console.log('update transform')
  131. locationIt = createLocationIterator(newStructure);
  132. const { instanceCount, groupCount } = locationIt;
  133. createMarkers(instanceCount * groupCount, renderObject.values);
  134. }
  135. if (updateState.createGeometry) {
  136. if (newGeometry) {
  137. ValueCell.updateIfChanged(renderObject.values.drawCount, Geometry.getDrawCount(newGeometry));
  138. ValueCell.updateIfChanged(renderObject.values.uVertexCount, Geometry.getVertexCount(newGeometry));
  139. } else {
  140. throw new Error('expected geometry to be given');
  141. }
  142. }
  143. if (updateState.updateTransform || updateState.createGeometry) {
  144. updateBoundingSphere(renderObject.values, newGeometry || geometry);
  145. positionIt = createPositionIterator(geometry, renderObject.values);
  146. }
  147. if (updateState.updateSize) {
  148. // not all geometries have size data, so check here
  149. if ('uSize' in renderObject.values) {
  150. createSizes(locationIt, newTheme.size, renderObject.values as SizeData);
  151. }
  152. }
  153. if (updateState.updateColor) {
  154. createColors(locationIt, positionIt, newTheme.color, renderObject.values);
  155. }
  156. updateValues(renderObject.values, newProps);
  157. updateRenderableState(renderObject.state, newProps);
  158. }
  159. currentProps = newProps;
  160. currentTheme = newTheme;
  161. currentStructure = newStructure;
  162. if (newGeometry) {
  163. geometry = newGeometry;
  164. geometryVersion += 1;
  165. }
  166. }
  167. function lociIsSuperset(loci: Loci) {
  168. if (isEveryLoci(loci)) return true;
  169. if (Structure.isLoci(loci) && Structure.areRootsEquivalent(loci.structure, currentStructure)) return true;
  170. if (StructureElement.Loci.is(loci) && Structure.areRootsEquivalent(loci.structure, currentStructure)) {
  171. if (StructureElement.Loci.isWholeStructure(loci)) return true;
  172. }
  173. return false;
  174. }
  175. function lociApply(loci: Loci, apply: (interval: Interval) => boolean, isMarking: boolean) {
  176. if (lociIsSuperset(loci)) {
  177. return apply(Interval.ofBounds(0, locationIt.groupCount * locationIt.instanceCount));
  178. } else {
  179. return eachLocation(loci, currentStructure, apply, isMarking);
  180. }
  181. }
  182. function finalize(ctx: VisualContext) {
  183. if (renderObject) {
  184. processValues?.(renderObject.values, geometry, currentProps, currentTheme, ctx.webgl);
  185. }
  186. }
  187. return {
  188. get groupCount() { return locationIt ? locationIt.count : 0; },
  189. get renderObject() { return locationIt && locationIt.count ? renderObject : undefined; },
  190. get geometryVersion() { return geometryVersion; },
  191. createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  192. prepareUpdate(theme, props, structure || currentStructure);
  193. if (updateState.createGeometry) {
  194. const newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry);
  195. if (isPromiseLike(newGeometry)) {
  196. return newGeometry.then(g => {
  197. update(g);
  198. finalize(ctx);
  199. });
  200. }
  201. update(newGeometry);
  202. } else {
  203. update();
  204. }
  205. finalize(ctx);
  206. },
  207. getLoci(pickingId: PickingId) {
  208. return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci;
  209. },
  210. mark(loci: Loci, action: MarkerAction) {
  211. return Visual.mark(renderObject, loci, action, lociApply, previousMark);
  212. },
  213. setVisibility(visible: boolean) {
  214. Visual.setVisibility(renderObject, visible);
  215. },
  216. setAlphaFactor(alphaFactor: number) {
  217. Visual.setAlphaFactor(renderObject, alphaFactor);
  218. },
  219. setPickable(pickable: boolean) {
  220. Visual.setPickable(renderObject, pickable);
  221. },
  222. setColorOnly(colorOnly: boolean) {
  223. Visual.setColorOnly(renderObject, colorOnly);
  224. },
  225. setTransform(matrix?: Mat4, instanceMatrices?: Float32Array | null) {
  226. Visual.setTransform(renderObject, matrix, instanceMatrices);
  227. },
  228. setOverpaint(overpaint: Overpaint, webgl?: WebGLContext) {
  229. const smoothing = { geometry, props: currentProps, webgl };
  230. Visual.setOverpaint(renderObject, overpaint, lociApply, true, smoothing);
  231. },
  232. setTransparency(transparency: Transparency, webgl?: WebGLContext) {
  233. const smoothing = { geometry, props: currentProps, webgl };
  234. Visual.setTransparency(renderObject, transparency, lociApply, true, smoothing);
  235. },
  236. setSubstance(substance: Substance, webgl?: WebGLContext) {
  237. const smoothing = { geometry, props: currentProps, webgl };
  238. Visual.setSubstance(renderObject, substance, lociApply, true, smoothing);
  239. },
  240. setClipping(clipping: Clipping) {
  241. Visual.setClipping(renderObject, clipping, lociApply, true);
  242. },
  243. destroy() {
  244. dispose?.(geometry);
  245. if (renderObject) {
  246. renderObject.state.disposed = true;
  247. renderObject = undefined;
  248. }
  249. },
  250. mustRecreate
  251. };
  252. }
  253. // mesh
  254. export const ComplexMeshParams = { ...StructureMeshParams, ...StructureParams };
  255. export type ComplexMeshParams = typeof ComplexMeshParams
  256. export interface ComplexMeshVisualBuilder<P extends ComplexMeshParams> extends ComplexVisualBuilder<P, Mesh> { }
  257. export function ComplexMeshVisual<P extends ComplexMeshParams>(builder: ComplexMeshVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  258. return ComplexVisual<Mesh, P>({
  259. ...builder,
  260. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  261. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  262. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  263. },
  264. geometryUtils: Mesh.Utils
  265. }, materialId);
  266. }
  267. // cylinders
  268. export const ComplexCylindersParams = { ...StructureCylindersParams, ...StructureParams };
  269. export type ComplexCylindersParams = typeof ComplexCylindersParams
  270. export interface ComplexCylindersVisualBuilder<P extends ComplexCylindersParams> extends ComplexVisualBuilder<P, Cylinders> { }
  271. export function ComplexCylindersVisual<P extends ComplexCylindersParams>(builder: ComplexCylindersVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  272. return ComplexVisual<Cylinders, P>({
  273. ...builder,
  274. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  275. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  276. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  277. },
  278. geometryUtils: Cylinders.Utils
  279. }, materialId);
  280. }
  281. // lines
  282. export const ComplexLinesParams = { ...StructureLinesParams, ...StructureParams };
  283. export type ComplexLinesParams = typeof ComplexLinesParams
  284. export interface ComplexLinesVisualBuilder<P extends ComplexLinesParams> extends ComplexVisualBuilder<P, Lines> { }
  285. export function ComplexLinesVisual<P extends ComplexLinesParams>(builder: ComplexLinesVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  286. return ComplexVisual<Lines, P>({
  287. ...builder,
  288. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  289. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  290. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  291. },
  292. geometryUtils: Lines.Utils
  293. }, materialId);
  294. }
  295. // text
  296. export const ComplexTextParams = { ...StructureTextParams, ...StructureParams };
  297. export type ComplexTextParams = typeof ComplexTextParams
  298. export interface ComplexTextVisualBuilder<P extends ComplexTextParams> extends ComplexVisualBuilder<P, Text> { }
  299. export function ComplexTextVisual<P extends ComplexTextParams>(builder: ComplexTextVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  300. return ComplexVisual<Text, P>({
  301. ...builder,
  302. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  303. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  304. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  305. if (newProps.background !== currentProps.background) state.createGeometry = true;
  306. if (newProps.backgroundMargin !== currentProps.backgroundMargin) state.createGeometry = true;
  307. if (newProps.tether !== currentProps.tether) state.createGeometry = true;
  308. if (newProps.tetherLength !== currentProps.tetherLength) state.createGeometry = true;
  309. if (newProps.tetherBaseWidth !== currentProps.tetherBaseWidth) state.createGeometry = true;
  310. if (newProps.attachment !== currentProps.attachment) state.createGeometry = true;
  311. if (newProps.fontFamily !== currentProps.fontFamily) state.createGeometry = true;
  312. if (newProps.fontQuality !== currentProps.fontQuality) state.createGeometry = true;
  313. if (newProps.fontStyle !== currentProps.fontStyle) state.createGeometry = true;
  314. if (newProps.fontVariant !== currentProps.fontVariant) state.createGeometry = true;
  315. if (newProps.fontWeight !== currentProps.fontWeight) state.createGeometry = true;
  316. },
  317. geometryUtils: Text.Utils
  318. }, materialId);
  319. }
  320. // direct-volume
  321. export const ComplexDirectVolumeParams = { ...StructureDirectVolumeParams, ...StructureParams };
  322. export type ComplexDirectVolumeParams = typeof ComplexDirectVolumeParams
  323. export interface ComplexDirectVolumeVisualBuilder<P extends ComplexDirectVolumeParams> extends ComplexVisualBuilder<P, DirectVolume> { }
  324. export function ComplexDirectVolumeVisual<P extends ComplexDirectVolumeParams>(builder: ComplexDirectVolumeVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  325. return ComplexVisual<DirectVolume, P>({
  326. ...builder,
  327. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  328. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  329. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  330. },
  331. geometryUtils: DirectVolume.Utils
  332. }, materialId);
  333. }
  334. // texture-mesh
  335. export const ComplexTextureMeshParams = { ...StructureTextureMeshParams, ...StructureParams };
  336. export type ComplexTextureMeshParams = typeof ComplexTextureMeshParams
  337. export interface ComplexTextureMeshVisualBuilder<P extends ComplexTextureMeshParams> extends ComplexVisualBuilder<P, TextureMesh> { }
  338. export function ComplexTextureMeshVisual<P extends ComplexTextureMeshParams>(builder: ComplexTextureMeshVisualBuilder<P>, materialId: number): ComplexVisual<P> {
  339. return ComplexVisual<TextureMesh, P>({
  340. ...builder,
  341. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructure: Structure, currentStructure: Structure) => {
  342. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
  343. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  344. },
  345. geometryUtils: TextureMesh.Utils
  346. }, materialId);
  347. }