complex-visual.ts 18 KB

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