visual.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { RuntimeContext } from '../mol-task';
  7. import { GraphicsRenderObject } from '../mol-gl/render-object';
  8. import { PickingId } from '../mol-geo/geometry/picking';
  9. import { Loci, isEmptyLoci, isEveryLoci, EveryLoci } from '../mol-model/loci';
  10. import { MarkerAction, applyMarkerAction, getMarkerInfo, setMarkerValue, getPartialMarkerAverage, MarkerActions, MarkerInfo } from '../mol-util/marker-action';
  11. import { ParamDefinition as PD } from '../mol-util/param-definition';
  12. import { WebGLContext } from '../mol-gl/webgl/context';
  13. import { Theme } from '../mol-theme/theme';
  14. import { Mat4 } from '../mol-math/linear-algebra';
  15. import { updateTransformData, fillIdentityTransform } from '../mol-geo/geometry/transform-data';
  16. import { calculateTransformBoundingSphere } from '../mol-gl/renderable/util';
  17. import { ValueCell } from '../mol-util';
  18. import { Overpaint } from '../mol-theme/overpaint';
  19. import { createOverpaint, clearOverpaint, applyOverpaintColor } from '../mol-geo/geometry/overpaint-data';
  20. import { Interval } from '../mol-data/int';
  21. import { Transparency } from '../mol-theme/transparency';
  22. import { createTransparency, clearTransparency, applyTransparencyValue, getTransparencyAverage } from '../mol-geo/geometry/transparency-data';
  23. import { Clipping } from '../mol-theme/clipping';
  24. import { createClipping, applyClippingGroups, clearClipping } from '../mol-geo/geometry/clipping-data';
  25. import { getMarkersAverage } from '../mol-geo/geometry/marker-data';
  26. import { Texture } from '../mol-gl/webgl/texture';
  27. import { Geometry } from '../mol-geo/geometry/geometry';
  28. import { getColorSmoothingProps, hasColorSmoothingProp } from '../mol-geo/geometry/base';
  29. import { applyMeshOverpaintSmoothing, applyMeshSubstanceSmoothing, applyMeshTransparencySmoothing } from '../mol-geo/geometry/mesh/color-smoothing';
  30. import { applyTextureMeshOverpaintSmoothing, applyTextureMeshSubstanceSmoothing, applyTextureMeshTransparencySmoothing } from '../mol-geo/geometry/texture-mesh/color-smoothing';
  31. import { Substance } from '../mol-theme/substance';
  32. import { applySubstanceMaterial, clearSubstance, createSubstance } from '../mol-geo/geometry/substance-data';
  33. import { LocationCallback } from './util';
  34. export interface VisualContext {
  35. readonly runtime: RuntimeContext
  36. readonly webgl?: WebGLContext
  37. }
  38. export { Visual };
  39. interface Visual<D, P extends PD.Params> {
  40. /** Number of addressable groups in all instances of the visual */
  41. readonly groupCount: number
  42. readonly renderObject: GraphicsRenderObject | undefined
  43. readonly geometryVersion: number
  44. createOrUpdate: (ctx: VisualContext, theme: Theme, props: PD.Values<P>, data?: D) => Promise<void> | void
  45. getLoci: (pickingId: PickingId) => Loci
  46. eachLocation: (cb: LocationCallback) => void
  47. mark: (loci: Loci, action: MarkerAction) => boolean
  48. setVisibility: (visible: boolean) => void
  49. setAlphaFactor: (alphaFactor: number) => void
  50. setPickable: (pickable: boolean) => void
  51. setColorOnly: (colorOnly: boolean) => void
  52. setTransform: (matrix?: Mat4, instanceMatrices?: Float32Array | null) => void
  53. setOverpaint: (overpaint: Overpaint, webgl?: WebGLContext) => void
  54. setTransparency: (transparency: Transparency, webgl?: WebGLContext) => void
  55. setSubstance: (substance: Substance, webgl?: WebGLContext) => void
  56. setClipping: (clipping: Clipping) => void
  57. setThemeStrength: (strength: { overpaint: number, transparency: number, substance: number }) => void
  58. destroy: () => void
  59. mustRecreate?: (data: D, props: PD.Values<P>, webgl?: WebGLContext) => boolean
  60. }
  61. namespace Visual {
  62. export type LociApply = (loci: Loci, apply: (interval: Interval) => boolean, isMarking: boolean) => boolean
  63. export function setVisibility(renderObject: GraphicsRenderObject | undefined, visible: boolean) {
  64. if (renderObject) renderObject.state.visible = visible;
  65. }
  66. export function setAlphaFactor(renderObject: GraphicsRenderObject | undefined, alphaFactor: number) {
  67. if (renderObject) renderObject.state.alphaFactor = alphaFactor;
  68. }
  69. export function setPickable(renderObject: GraphicsRenderObject | undefined, pickable: boolean) {
  70. if (renderObject) renderObject.state.pickable = pickable;
  71. }
  72. export function setColorOnly(renderObject: GraphicsRenderObject | undefined, colorOnly: boolean) {
  73. if (renderObject) renderObject.state.colorOnly = colorOnly;
  74. }
  75. export type PreviousMark = { loci: Loci, action: MarkerAction, status: MarkerInfo['status'] }
  76. export function mark(renderObject: GraphicsRenderObject | undefined, loci: Loci, action: MarkerAction, lociApply: LociApply, previous?: PreviousMark) {
  77. if (!renderObject || isEmptyLoci(loci)) return false;
  78. const { tMarker, uMarker, markerAverage, markerStatus, uGroupCount, instanceCount, instanceGranularity: instanceGranularity } = renderObject.values;
  79. const count = instanceGranularity.ref.value
  80. ? instanceCount.ref.value
  81. : uGroupCount.ref.value * instanceCount.ref.value;
  82. const { array } = tMarker.ref.value;
  83. const currentStatus = markerStatus.ref.value as MarkerInfo['status'];
  84. if (!isEveryLoci(loci)) {
  85. // assume that all interval are non-overlapping
  86. let intervalSize = 0;
  87. lociApply(loci, interval => {
  88. intervalSize += Interval.size(interval);
  89. return true;
  90. }, true);
  91. if (intervalSize === 0) return false;
  92. if (intervalSize === count) loci = EveryLoci;
  93. }
  94. let changed = false;
  95. let average = -1;
  96. let status: MarkerInfo['status'] = -1;
  97. if (isEveryLoci(loci)) {
  98. const info = getMarkerInfo(action, currentStatus);
  99. if (info.status !== -1) {
  100. changed = currentStatus !== info.status;
  101. if (changed) setMarkerValue(array, info.status, count);
  102. } else {
  103. changed = applyMarkerAction(array, Interval.ofLength(count), action);
  104. }
  105. average = info.average;
  106. status = info.status;
  107. } else {
  108. changed = lociApply(loci, interval => applyMarkerAction(array, interval, action), true);
  109. if (changed) {
  110. average = getPartialMarkerAverage(action, currentStatus);
  111. if (previous && previous.status !== -1 && average === -1 &&
  112. MarkerActions.isReverse(previous.action, action) &&
  113. Loci.areEqual(loci, previous.loci)
  114. ) {
  115. status = previous.status;
  116. average = status === 0 ? 0 : 0.5;
  117. }
  118. }
  119. }
  120. if (changed) {
  121. if (average === -1) {
  122. average = getMarkersAverage(array, count);
  123. if (average === 0) status = 0;
  124. }
  125. if (previous) {
  126. previous.action = action;
  127. previous.loci = loci;
  128. previous.status = currentStatus;
  129. }
  130. ValueCell.updateIfChanged(uMarker, status);
  131. if (status === -1) ValueCell.update(tMarker, tMarker.ref.value);
  132. ValueCell.updateIfChanged(markerAverage, average);
  133. ValueCell.updateIfChanged(markerStatus, status);
  134. }
  135. return changed;
  136. }
  137. type SurfaceMeta = {
  138. resolution?: number
  139. overpaintTexture?: Texture
  140. transparencyTexture?: Texture
  141. substanceTexture?: Texture
  142. }
  143. type SmoothingContext = {
  144. geometry: Geometry,
  145. props: PD.Values<any>,
  146. webgl?: WebGLContext
  147. }
  148. export function setOverpaint(renderObject: GraphicsRenderObject | undefined, overpaint: Overpaint, lociApply: LociApply, clear: boolean, smoothing?: SmoothingContext) {
  149. if (!renderObject) return;
  150. const { tOverpaint, dOverpaintType, dOverpaint, uGroupCount, instanceCount, instanceGranularity: instanceGranularity } = renderObject.values;
  151. const count = instanceGranularity.ref.value
  152. ? instanceCount.ref.value
  153. : uGroupCount.ref.value * instanceCount.ref.value;
  154. // ensure texture has right size and type
  155. const type = instanceGranularity.ref.value ? 'instance' : 'groupInstance';
  156. createOverpaint(overpaint.layers.length ? count : 0, type, renderObject.values);
  157. const { array } = tOverpaint.ref.value;
  158. // clear all if requested
  159. if (clear) clearOverpaint(array, 0, count);
  160. for (let i = 0, il = overpaint.layers.length; i < il; ++i) {
  161. const { loci, color, clear } = overpaint.layers[i];
  162. const apply = (interval: Interval) => {
  163. const start = Interval.start(interval);
  164. const end = Interval.end(interval);
  165. return clear
  166. ? clearOverpaint(array, start, end)
  167. : applyOverpaintColor(array, start, end, color);
  168. };
  169. lociApply(loci, apply, false);
  170. }
  171. ValueCell.update(tOverpaint, tOverpaint.ref.value);
  172. ValueCell.updateIfChanged(dOverpaintType, type);
  173. ValueCell.updateIfChanged(dOverpaint, overpaint.layers.length > 0);
  174. if (overpaint.layers.length === 0) return;
  175. if (type === 'instance') return;
  176. if (smoothing && hasColorSmoothingProp(smoothing.props)) {
  177. const { geometry, props, webgl } = smoothing;
  178. if (geometry.kind === 'mesh') {
  179. const { resolution, overpaintTexture } = geometry.meta as SurfaceMeta;
  180. const csp = getColorSmoothingProps(props.smoothColors, true, resolution);
  181. if (csp) {
  182. applyMeshOverpaintSmoothing(renderObject.values as any, csp.resolution, csp.stride, webgl, overpaintTexture);
  183. (geometry.meta as SurfaceMeta).overpaintTexture = renderObject.values.tOverpaintGrid.ref.value;
  184. }
  185. } else if (webgl && geometry.kind === 'texture-mesh') {
  186. const { resolution, overpaintTexture } = geometry.meta as SurfaceMeta;
  187. const csp = getColorSmoothingProps(props.smoothColors, true, resolution);
  188. if (csp) {
  189. applyTextureMeshOverpaintSmoothing(renderObject.values as any, csp.resolution, csp.stride, webgl, overpaintTexture);
  190. (geometry.meta as SurfaceMeta).overpaintTexture = renderObject.values.tOverpaintGrid.ref.value;
  191. }
  192. }
  193. }
  194. }
  195. export function setTransparency(renderObject: GraphicsRenderObject | undefined, transparency: Transparency, lociApply: LociApply, clear: boolean, smoothing?: SmoothingContext) {
  196. if (!renderObject) return;
  197. const { tTransparency, dTransparencyType, transparencyAverage, dTransparency, uGroupCount, instanceCount, instanceGranularity: instanceGranularity } = renderObject.values;
  198. const count = instanceGranularity.ref.value
  199. ? instanceCount.ref.value
  200. : uGroupCount.ref.value * instanceCount.ref.value;
  201. // ensure texture has right size and type
  202. const type = instanceGranularity.ref.value ? 'instance' : 'groupInstance';
  203. createTransparency(transparency.layers.length ? count : 0, type, renderObject.values);
  204. const { array } = tTransparency.ref.value;
  205. // clear if requested
  206. if (clear) clearTransparency(array, 0, count);
  207. for (let i = 0, il = transparency.layers.length; i < il; ++i) {
  208. const { loci, value } = transparency.layers[i];
  209. const apply = (interval: Interval) => {
  210. const start = Interval.start(interval);
  211. const end = Interval.end(interval);
  212. return applyTransparencyValue(array, start, end, value);
  213. };
  214. lociApply(loci, apply, false);
  215. }
  216. ValueCell.update(tTransparency, tTransparency.ref.value);
  217. ValueCell.updateIfChanged(transparencyAverage, getTransparencyAverage(array, count));
  218. ValueCell.updateIfChanged(dTransparencyType, type);
  219. ValueCell.updateIfChanged(dTransparency, transparency.layers.length > 0);
  220. if (transparency.layers.length === 0) return;
  221. if (type === 'instance') return;
  222. if (smoothing && hasColorSmoothingProp(smoothing.props)) {
  223. const { geometry, props, webgl } = smoothing;
  224. if (geometry.kind === 'mesh') {
  225. const { resolution, transparencyTexture } = geometry.meta as SurfaceMeta;
  226. const csp = getColorSmoothingProps(props.smoothColors, true, resolution);
  227. if (csp) {
  228. applyMeshTransparencySmoothing(renderObject.values as any, csp.resolution, csp.stride, webgl, transparencyTexture);
  229. (geometry.meta as SurfaceMeta).transparencyTexture = renderObject.values.tTransparencyGrid.ref.value;
  230. }
  231. } else if (webgl && geometry.kind === 'texture-mesh') {
  232. const { resolution, transparencyTexture } = geometry.meta as SurfaceMeta;
  233. const csp = getColorSmoothingProps(props.smoothColors, true, resolution);
  234. if (csp) {
  235. applyTextureMeshTransparencySmoothing(renderObject.values as any, csp.resolution, csp.stride, webgl, transparencyTexture);
  236. (geometry.meta as SurfaceMeta).transparencyTexture = renderObject.values.tTransparencyGrid.ref.value;
  237. }
  238. }
  239. }
  240. }
  241. export function setSubstance(renderObject: GraphicsRenderObject | undefined, substance: Substance, lociApply: LociApply, clear: boolean, smoothing?: SmoothingContext) {
  242. if (!renderObject) return;
  243. const { tSubstance, dSubstanceType, dSubstance, uGroupCount, instanceCount, instanceGranularity: instanceGranularity } = renderObject.values;
  244. const count = instanceGranularity.ref.value
  245. ? instanceCount.ref.value
  246. : uGroupCount.ref.value * instanceCount.ref.value;
  247. // ensure texture has right size and type
  248. const type = instanceGranularity.ref.value ? 'instance' : 'groupInstance';
  249. createSubstance(substance.layers.length ? count : 0, type, renderObject.values);
  250. const { array } = tSubstance.ref.value;
  251. // clear all if requested
  252. if (clear) clearSubstance(array, 0, count);
  253. for (let i = 0, il = substance.layers.length; i < il; ++i) {
  254. const { loci, material, clear } = substance.layers[i];
  255. const apply = (interval: Interval) => {
  256. const start = Interval.start(interval);
  257. const end = Interval.end(interval);
  258. return clear
  259. ? clearSubstance(array, start, end)
  260. : applySubstanceMaterial(array, start, end, material);
  261. };
  262. lociApply(loci, apply, false);
  263. }
  264. ValueCell.update(tSubstance, tSubstance.ref.value);
  265. ValueCell.updateIfChanged(dSubstanceType, type);
  266. ValueCell.updateIfChanged(dSubstance, substance.layers.length > 0);
  267. if (substance.layers.length === 0) return;
  268. if (type === 'instance') return;
  269. if (smoothing && hasColorSmoothingProp(smoothing.props)) {
  270. const { geometry, props, webgl } = smoothing;
  271. if (geometry.kind === 'mesh') {
  272. const { resolution, substanceTexture } = geometry.meta as SurfaceMeta;
  273. const csp = getColorSmoothingProps(props.smoothColors, true, resolution);
  274. if (csp) {
  275. applyMeshSubstanceSmoothing(renderObject.values as any, csp.resolution, csp.stride, webgl, substanceTexture);
  276. (geometry.meta as SurfaceMeta).substanceTexture = renderObject.values.tSubstanceGrid.ref.value;
  277. }
  278. } else if (webgl && geometry.kind === 'texture-mesh') {
  279. const { resolution, substanceTexture } = geometry.meta as SurfaceMeta;
  280. const csp = getColorSmoothingProps(props.smoothColors, true, resolution);
  281. if (csp) {
  282. applyTextureMeshSubstanceSmoothing(renderObject.values as any, csp.resolution, csp.stride, webgl, substanceTexture);
  283. (geometry.meta as SurfaceMeta).substanceTexture = renderObject.values.tSubstanceGrid.ref.value;
  284. }
  285. }
  286. }
  287. }
  288. export function setClipping(renderObject: GraphicsRenderObject | undefined, clipping: Clipping, lociApply: LociApply, clear: boolean) {
  289. if (!renderObject) return;
  290. const { tClipping, dClippingType, dClipping, uGroupCount, instanceCount, instanceGranularity: instanceGranularity } = renderObject.values;
  291. const count = instanceGranularity.ref.value
  292. ? instanceCount.ref.value
  293. : uGroupCount.ref.value * instanceCount.ref.value;
  294. const { layers } = clipping;
  295. // ensure texture has right size and type
  296. const type = instanceGranularity.ref.value ? 'instance' : 'groupInstance';
  297. createClipping(layers.length ? count : 0, type, renderObject.values);
  298. const { array } = tClipping.ref.value;
  299. // clear if requested
  300. if (clear) clearClipping(array, 0, count);
  301. for (let i = 0, il = clipping.layers.length; i < il; ++i) {
  302. const { loci, groups } = clipping.layers[i];
  303. const apply = (interval: Interval) => {
  304. const start = Interval.start(interval);
  305. const end = Interval.end(interval);
  306. return applyClippingGroups(array, start, end, groups);
  307. };
  308. lociApply(loci, apply, false);
  309. }
  310. ValueCell.update(tClipping, tClipping.ref.value);
  311. ValueCell.updateIfChanged(dClippingType, type);
  312. ValueCell.updateIfChanged(dClipping, clipping.layers.length > 0);
  313. }
  314. export function setThemeStrength(renderObject: GraphicsRenderObject | undefined, strength: { overpaint: number, transparency: number, substance: number }) {
  315. if (renderObject) {
  316. ValueCell.updateIfChanged(renderObject.values.uOverpaintStrength, strength.overpaint);
  317. ValueCell.updateIfChanged(renderObject.values.uTransparencyStrength, strength.transparency);
  318. ValueCell.updateIfChanged(renderObject.values.uSubstanceStrength, strength.substance);
  319. }
  320. }
  321. export function setTransform(renderObject: GraphicsRenderObject | undefined, transform?: Mat4, instanceTransforms?: Float32Array | null) {
  322. if (!renderObject || (!transform && !instanceTransforms)) return;
  323. const { values } = renderObject;
  324. if (transform) {
  325. Mat4.copy(values.matrix.ref.value, transform);
  326. ValueCell.update(values.matrix, values.matrix.ref.value);
  327. }
  328. if (instanceTransforms) {
  329. values.extraTransform.ref.value.set(instanceTransforms);
  330. ValueCell.update(values.extraTransform, values.extraTransform.ref.value);
  331. } else if (instanceTransforms === null) {
  332. fillIdentityTransform(values.extraTransform.ref.value, values.instanceCount.ref.value);
  333. ValueCell.update(values.extraTransform, values.extraTransform.ref.value);
  334. }
  335. updateTransformData(values);
  336. const boundingSphere = calculateTransformBoundingSphere(values.invariantBoundingSphere.ref.value, values.aTransform.ref.value, values.instanceCount.ref.value, 0);
  337. ValueCell.update(values.boundingSphere, boundingSphere);
  338. }
  339. }