complex-visual.ts 19 KB

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