units-visual.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**
  2. * Copyright (c) 2018-2022 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 { Structure, Unit, StructureElement, Bond } from '../../mol-model/structure';
  8. import { RepresentationProps } from '../representation';
  9. import { Visual, VisualContext } from '../visual';
  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 { createUnitsTransform, includesUnitKind, StructureGroup } from './visual/util/common';
  14. import { createRenderObject, GraphicsRenderObject, RenderObjectValues } 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 { createMarkers } from '../../mol-geo/geometry/marker-data';
  21. import { MarkerAction } from '../../mol-util/marker-action';
  22. import { ValueCell, deepEqual } from '../../mol-util';
  23. import { createSizes } from '../../mol-geo/geometry/size-data';
  24. import { createColors } from '../../mol-geo/geometry/color-data';
  25. import { Mat4 } from '../../mol-math/linear-algebra';
  26. import { Overpaint } from '../../mol-theme/overpaint';
  27. import { Transparency } from '../../mol-theme/transparency';
  28. import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
  29. import { SizeTheme } from '../../mol-theme/size';
  30. import { Spheres } from '../../mol-geo/geometry/spheres/spheres';
  31. import { Cylinders } from '../../mol-geo/geometry/cylinders/cylinders';
  32. import { Points } from '../../mol-geo/geometry/points/points';
  33. import { Lines } from '../../mol-geo/geometry/lines/lines';
  34. import { Text } from '../../mol-geo/geometry/text/text';
  35. import { DirectVolume } from '../../mol-geo/geometry/direct-volume/direct-volume';
  36. import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh';
  37. import { SizeValues } from '../../mol-gl/renderable/schema';
  38. import { StructureParams, StructureMeshParams, StructureSpheresParams, StructurePointsParams, StructureLinesParams, StructureTextParams, StructureDirectVolumeParams, StructureTextureMeshParams, StructureCylindersParams } from './params';
  39. import { Clipping } from '../../mol-theme/clipping';
  40. import { WebGLContext } from '../../mol-gl/webgl/context';
  41. import { isPromiseLike } from '../../mol-util/type-helpers';
  42. import { Substance } from '../../mol-theme/substance';
  43. export interface UnitsVisual<P extends RepresentationProps = {}> extends Visual<StructureGroup, P> { }
  44. function createUnitsRenderObject<G extends Geometry>(structureGroup: StructureGroup, geometry: G, locationIt: LocationIterator, theme: Theme, props: PD.Values<StructureParams & Geometry.Params<G>>, materialId: number) {
  45. const { createValues, createRenderableState } = Geometry.getUtils(geometry);
  46. const transform = createUnitsTransform(structureGroup, props.includeParent);
  47. const values = createValues(geometry, transform, locationIt, theme, props);
  48. const state = createRenderableState(props);
  49. return createRenderObject(geometry.kind, values, state, materialId);
  50. }
  51. interface UnitsVisualBuilder<P extends StructureParams, G extends Geometry> {
  52. defaultProps: PD.Values<P>
  53. createGeometry(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G): Promise<G> | G
  54. createLocationIterator(structureGroup: StructureGroup): LocationIterator
  55. getLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number): Loci
  56. eachLocation(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean, isMarking: boolean): boolean
  57. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup): void
  58. mustRecreate?: (structureGroup: StructureGroup, props: PD.Values<P>) => boolean
  59. processValues?: (values: RenderObjectValues<G['kind']>, geometry: G, props: PD.Values<P>, theme: Theme, webgl?: WebGLContext) => void
  60. dispose?: (geometry: G) => void
  61. }
  62. interface UnitsVisualGeometryBuilder<P extends StructureParams, G extends Geometry> extends UnitsVisualBuilder<P, G> {
  63. geometryUtils: GeometryUtils<G>
  64. }
  65. export function UnitsVisual<G extends Geometry, P extends StructureParams & Geometry.Params<G>>(builder: UnitsVisualGeometryBuilder<P, G>, materialId: number): UnitsVisual<P> {
  66. const { defaultProps, createGeometry, createLocationIterator, getLoci, eachLocation, setUpdateState, mustRecreate, processValues, dispose } = builder;
  67. const { createEmpty: createEmptyGeometry, updateValues, updateBoundingSphere, updateRenderableState, createPositionIterator } = builder.geometryUtils;
  68. const updateState = VisualUpdateState.create();
  69. const previousMark: Visual.PreviousMark = { loci: EmptyLoci, action: MarkerAction.None, status: -1 };
  70. let renderObject: GraphicsRenderObject<G['kind']> | undefined;
  71. let newProps: PD.Values<P> = Object.assign({}, defaultProps);
  72. let newTheme: Theme = Theme.createEmpty();
  73. let newStructureGroup: StructureGroup;
  74. let currentProps: PD.Values<P>;
  75. let currentTheme: Theme;
  76. let currentStructureGroup: StructureGroup;
  77. let geometry: G;
  78. let geometryVersion = -1;
  79. let locationIt: LocationIterator;
  80. let positionIt: LocationIterator;
  81. function prepareUpdate(theme: Theme, props: PD.Values<P>, structureGroup: StructureGroup) {
  82. if (!structureGroup && !currentStructureGroup) {
  83. throw new Error('missing structureGroup');
  84. }
  85. newProps = props;
  86. newTheme = theme;
  87. newStructureGroup = structureGroup;
  88. VisualUpdateState.reset(updateState);
  89. if (!renderObject || !currentStructureGroup) {
  90. // console.log('create new');
  91. updateState.createNew = true;
  92. updateState.createGeometry = true;
  93. return;
  94. }
  95. setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  96. if (!Structure.areHierarchiesEqual(currentStructureGroup.structure, newStructureGroup.structure)) {
  97. // console.log('new hierarchy');
  98. updateState.updateTransform = true;
  99. updateState.updateColor = true;
  100. updateState.updateSize = true;
  101. }
  102. if (!ColorTheme.areEqual(newTheme.color, currentTheme.color)) {
  103. // console.log('new colorTheme');
  104. updateState.updateColor = true;
  105. }
  106. if (currentStructureGroup.structure.child !== newStructureGroup.structure.child) {
  107. // console.log('new child');
  108. updateState.createGeometry = true;
  109. }
  110. if (newProps.instanceGranularity !== currentProps.instanceGranularity) {
  111. updateState.updateTransform = true;
  112. }
  113. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) {
  114. // console.log('new unitKinds');
  115. updateState.createGeometry = true;
  116. }
  117. if (newStructureGroup.group.transformHash !== currentStructureGroup.group.transformHash) {
  118. // console.log('new transformHash');
  119. if (newStructureGroup.group.units.length !== currentStructureGroup.group.units.length || updateState.updateColor) {
  120. updateState.updateTransform = true;
  121. } else {
  122. updateState.updateMatrix = true;
  123. }
  124. }
  125. // check if the operator or conformation of unit has changed
  126. const newUnit = newStructureGroup.group.units[0];
  127. const currentUnit = currentStructureGroup.group.units[0];
  128. if (!Unit.areOperatorsEqual(newUnit, currentUnit)) {
  129. // console.log('new operators');
  130. updateState.updateTransform = true;
  131. }
  132. if (!Unit.areConformationsEqual(newUnit, currentUnit)) {
  133. // console.log('new conformation');
  134. updateState.createGeometry = true;
  135. }
  136. if (updateState.updateTransform) {
  137. updateState.updateMatrix = true;
  138. }
  139. if (updateState.updateSize && !('uSize' in renderObject.values)) {
  140. updateState.createGeometry = true;
  141. }
  142. if (updateState.createGeometry || updateState.updateTransform) {
  143. if (currentStructureGroup.structure.hashCode !== newStructureGroup.structure.hashCode) {
  144. // console.log('new hashCode');
  145. updateState.updateColor = true;
  146. updateState.updateSize = true;
  147. }
  148. if (newTheme.color.granularity.startsWith('vertex') ||
  149. renderObject.values.dColorType.ref.value.startsWith('vertex') ||
  150. newTheme.color.granularity.startsWith('volume') ||
  151. renderObject.values.dColorType.ref.value.startsWith('volume')
  152. ) {
  153. updateState.updateColor = true;
  154. }
  155. }
  156. }
  157. function update(newGeometry?: G) {
  158. if (updateState.createNew) {
  159. locationIt = createLocationIterator(newStructureGroup);
  160. if (newGeometry) {
  161. renderObject = createUnitsRenderObject(newStructureGroup, newGeometry, locationIt, newTheme, newProps, materialId);
  162. positionIt = createPositionIterator(newGeometry, renderObject.values);
  163. } else {
  164. throw new Error('expected geometry to be given');
  165. }
  166. } else {
  167. if (!renderObject) {
  168. throw new Error('expected renderObject to be available');
  169. }
  170. if (updateState.updateTransform) {
  171. // console.log('update transform');
  172. locationIt = createLocationIterator(newStructureGroup);
  173. const { instanceCount, groupCount } = locationIt;
  174. if (newProps.instanceGranularity) {
  175. createMarkers(instanceCount, 'instance', renderObject.values);
  176. } else {
  177. createMarkers(instanceCount * groupCount, 'groupInstance', renderObject.values);
  178. }
  179. }
  180. if (updateState.updateMatrix) {
  181. // console.log('update matrix');
  182. createUnitsTransform(newStructureGroup, newProps.includeParent, renderObject.values);
  183. }
  184. if (updateState.createGeometry) {
  185. // console.log('update geometry');
  186. if (newGeometry) {
  187. ValueCell.updateIfChanged(renderObject.values.drawCount, Geometry.getDrawCount(newGeometry));
  188. ValueCell.updateIfChanged(renderObject.values.uVertexCount, Geometry.getVertexCount(newGeometry));
  189. ValueCell.updateIfChanged(renderObject.values.uGroupCount, Geometry.getGroupCount(newGeometry));
  190. } else {
  191. throw new Error('expected geometry to be given');
  192. }
  193. }
  194. if (updateState.updateTransform || updateState.createGeometry) {
  195. updateBoundingSphere(renderObject.values, newGeometry || geometry);
  196. positionIt = createPositionIterator(newGeometry || geometry, renderObject.values);
  197. }
  198. if (updateState.updateSize) {
  199. // not all geometries have size data, so check here
  200. if ('uSize' in renderObject.values) {
  201. // console.log('update size');
  202. createSizes(locationIt, newTheme.size, renderObject.values as SizeValues);
  203. }
  204. }
  205. if (updateState.updateColor) {
  206. // console.log('update color');
  207. createColors(locationIt, positionIt, newTheme.color, renderObject.values);
  208. }
  209. updateValues(renderObject.values, newProps);
  210. updateRenderableState(renderObject.state, newProps);
  211. }
  212. currentProps = newProps;
  213. currentTheme = newTheme;
  214. currentStructureGroup = newStructureGroup;
  215. if (newGeometry) {
  216. geometry = newGeometry;
  217. geometryVersion += 1;
  218. }
  219. }
  220. function _createGeometry(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G) {
  221. return includesUnitKind(props.unitKinds, unit)
  222. ? createGeometry(ctx, unit, structure, theme, props, geometry)
  223. : createEmptyGeometry(geometry);
  224. }
  225. function lociIsSuperset(loci: Loci) {
  226. if (isEveryLoci(loci)) return true;
  227. if (Structure.isLoci(loci) && Structure.areRootsEquivalent(loci.structure, currentStructureGroup.structure)) return true;
  228. if (StructureElement.Loci.is(loci) && Structure.areRootsEquivalent(loci.structure, currentStructureGroup.structure)) {
  229. if (StructureElement.Loci.isWholeStructure(loci)) return true;
  230. }
  231. return false;
  232. }
  233. function eachInstance(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean) {
  234. let changed = false;
  235. if (Bond.isLoci(loci)) {
  236. const { structure, group } = structureGroup;
  237. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  238. for (const b of loci.bonds) {
  239. if (b.aUnit !== b.bUnit) continue;
  240. const unitIdx = group.unitIndexMap.get(b.aUnit.id);
  241. if (unitIdx !== undefined) {
  242. if (apply(Interval.ofSingleton(unitIdx))) changed = true;
  243. }
  244. }
  245. } else if (StructureElement.Loci.is(loci)) {
  246. const { structure, group } = structureGroup;
  247. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  248. for (const e of loci.elements) {
  249. const unitIdx = group.unitIndexMap.get(e.unit.id);
  250. if (unitIdx !== undefined) {
  251. if (apply(Interval.ofSingleton(unitIdx))) changed = true;
  252. }
  253. }
  254. }
  255. return changed;
  256. }
  257. function lociApply(loci: Loci, apply: (interval: Interval) => boolean, isMarking: boolean) {
  258. if (lociIsSuperset(loci)) {
  259. if (currentProps.instanceGranularity) {
  260. return apply(Interval.ofBounds(0, locationIt.instanceCount));
  261. } else {
  262. return apply(Interval.ofBounds(0, locationIt.groupCount * locationIt.instanceCount));
  263. }
  264. } else {
  265. if (currentProps.instanceGranularity) {
  266. return eachInstance(loci, currentStructureGroup, apply);
  267. } else {
  268. return eachLocation(loci, currentStructureGroup, apply, isMarking);
  269. }
  270. }
  271. }
  272. function finalize(ctx: VisualContext) {
  273. if (renderObject) {
  274. processValues?.(renderObject.values, geometry, currentProps, currentTheme, ctx.webgl);
  275. }
  276. }
  277. return {
  278. get groupCount() { return locationIt ? locationIt.count : 0; },
  279. get renderObject() { return locationIt && locationIt.count ? renderObject : undefined; },
  280. get geometryVersion() { return geometryVersion; },
  281. createOrUpdate(ctx: VisualContext, theme: Theme, props: PD.Values<P>, structureGroup?: StructureGroup) {
  282. prepareUpdate(theme, props, structureGroup || currentStructureGroup);
  283. if (updateState.createGeometry) {
  284. const newGeometry = _createGeometry(ctx, newStructureGroup.group.units[0], newStructureGroup.structure, newTheme, newProps, geometry);
  285. if (isPromiseLike(newGeometry)) {
  286. return newGeometry.then(g => {
  287. update(g);
  288. finalize(ctx);
  289. });
  290. }
  291. update(newGeometry);
  292. } else {
  293. update();
  294. }
  295. finalize(ctx);
  296. },
  297. getLoci(pickingId: PickingId) {
  298. return renderObject ? getLoci(pickingId, currentStructureGroup, renderObject.id) : EmptyLoci;
  299. },
  300. mark(loci: Loci, action: MarkerAction) {
  301. let hasInvariantId = true;
  302. if (StructureElement.Loci.is(loci)) {
  303. hasInvariantId = false;
  304. const { invariantId } = currentStructureGroup.group.units[0];
  305. for (const e of loci.elements) {
  306. if (e.unit.invariantId === invariantId) {
  307. hasInvariantId = true;
  308. break;
  309. }
  310. }
  311. }
  312. return hasInvariantId ? Visual.mark(renderObject, loci, action, lociApply, previousMark) : false;
  313. },
  314. setVisibility(visible: boolean) {
  315. Visual.setVisibility(renderObject, visible);
  316. },
  317. setAlphaFactor(alphaFactor: number) {
  318. Visual.setAlphaFactor(renderObject, alphaFactor);
  319. },
  320. setPickable(pickable: boolean) {
  321. Visual.setPickable(renderObject, pickable);
  322. },
  323. setColorOnly(colorOnly: boolean) {
  324. Visual.setColorOnly(renderObject, colorOnly);
  325. },
  326. setTransform(matrix?: Mat4, instanceMatrices?: Float32Array | null) {
  327. Visual.setTransform(renderObject, matrix, instanceMatrices);
  328. },
  329. setOverpaint(overpaint: Overpaint, webgl?: WebGLContext) {
  330. const smoothing = { geometry, props: currentProps, webgl };
  331. Visual.setOverpaint(renderObject, overpaint, lociApply, true, smoothing);
  332. },
  333. setTransparency(transparency: Transparency, webgl?: WebGLContext) {
  334. const smoothing = { geometry, props: currentProps, webgl };
  335. Visual.setTransparency(renderObject, transparency, lociApply, true, smoothing);
  336. },
  337. setSubstance(substance: Substance, webgl?: WebGLContext) {
  338. const smoothing = { geometry, props: currentProps, webgl };
  339. Visual.setSubstance(renderObject, substance, lociApply, true, smoothing);
  340. },
  341. setClipping(clipping: Clipping) {
  342. Visual.setClipping(renderObject, clipping, lociApply, true);
  343. },
  344. destroy() {
  345. dispose?.(geometry);
  346. if (renderObject) {
  347. renderObject.state.disposed = true;
  348. renderObject = undefined;
  349. }
  350. },
  351. mustRecreate
  352. };
  353. }
  354. // mesh
  355. export const UnitsMeshParams = { ...StructureMeshParams, ...StructureParams };
  356. export type UnitsMeshParams = typeof UnitsMeshParams
  357. export interface UnitsMeshVisualBuilder<P extends UnitsMeshParams> extends UnitsVisualBuilder<P, Mesh> { }
  358. export function UnitsMeshVisual<P extends UnitsMeshParams>(builder: UnitsMeshVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  359. return UnitsVisual<Mesh, P>({
  360. ...builder,
  361. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  362. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  363. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  364. },
  365. geometryUtils: Mesh.Utils
  366. }, materialId);
  367. }
  368. // spheres
  369. export const UnitsSpheresParams = { ...StructureSpheresParams, ...StructureParams };
  370. export type UnitsSpheresParams = typeof UnitsSpheresParams
  371. export interface UnitsSpheresVisualBuilder<P extends UnitsSpheresParams> extends UnitsVisualBuilder<P, Spheres> { }
  372. export function UnitsSpheresVisual<P extends UnitsSpheresParams>(builder: UnitsSpheresVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  373. return UnitsVisual<Spheres, P>({
  374. ...builder,
  375. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  376. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  377. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  378. },
  379. geometryUtils: Spheres.Utils
  380. }, materialId);
  381. }
  382. // cylinders
  383. export const UnitsCylindersParams = { ...StructureCylindersParams, ...StructureParams };
  384. export type UnitsCylindersParams = typeof UnitsCylindersParams
  385. export interface UnitsCylindersVisualBuilder<P extends UnitsCylindersParams> extends UnitsVisualBuilder<P, Cylinders> { }
  386. export function UnitsCylindersVisual<P extends UnitsCylindersParams>(builder: UnitsCylindersVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  387. return UnitsVisual<Cylinders, P>({
  388. ...builder,
  389. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  390. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  391. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  392. },
  393. geometryUtils: Cylinders.Utils
  394. }, materialId);
  395. }
  396. // points
  397. export const UnitsPointsParams = { ...StructurePointsParams, ...StructureParams };
  398. export type UnitsPointsParams = typeof UnitsPointsParams
  399. export interface UnitsPointVisualBuilder<P extends UnitsPointsParams> extends UnitsVisualBuilder<P, Points> { }
  400. export function UnitsPointsVisual<P extends UnitsPointsParams>(builder: UnitsPointVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  401. return UnitsVisual<Points, P>({
  402. ...builder,
  403. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  404. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  405. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  406. },
  407. geometryUtils: Points.Utils
  408. }, materialId);
  409. }
  410. // lines
  411. export const UnitsLinesParams = { ...StructureLinesParams, ...StructureParams };
  412. export type UnitsLinesParams = typeof UnitsLinesParams
  413. export interface UnitsLinesVisualBuilder<P extends UnitsLinesParams> extends UnitsVisualBuilder<P, Lines> { }
  414. export function UnitsLinesVisual<P extends UnitsLinesParams>(builder: UnitsLinesVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  415. return UnitsVisual<Lines, P>({
  416. ...builder,
  417. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  418. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  419. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  420. },
  421. geometryUtils: Lines.Utils
  422. }, materialId);
  423. }
  424. // text
  425. export const UnitsTextParams = { ...StructureTextParams, ...StructureParams };
  426. export type UnitsTextParams = typeof UnitsTextParams
  427. export interface UnitsTextVisualBuilder<P extends UnitsTextParams> extends UnitsVisualBuilder<P, Text> { }
  428. export function UnitsTextVisual<P extends UnitsTextParams>(builder: UnitsTextVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  429. return UnitsVisual<Text, P>({
  430. ...builder,
  431. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  432. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  433. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  434. if (newProps.background !== currentProps.background) state.createGeometry = true;
  435. if (newProps.backgroundMargin !== currentProps.backgroundMargin) state.createGeometry = true;
  436. if (newProps.tether !== currentProps.tether) state.createGeometry = true;
  437. if (newProps.tetherLength !== currentProps.tetherLength) state.createGeometry = true;
  438. if (newProps.tetherBaseWidth !== currentProps.tetherBaseWidth) state.createGeometry = true;
  439. if (newProps.attachment !== currentProps.attachment) state.createGeometry = true;
  440. if (newProps.fontFamily !== currentProps.fontFamily) state.createGeometry = true;
  441. if (newProps.fontQuality !== currentProps.fontQuality) state.createGeometry = true;
  442. if (newProps.fontStyle !== currentProps.fontStyle) state.createGeometry = true;
  443. if (newProps.fontVariant !== currentProps.fontVariant) state.createGeometry = true;
  444. if (newProps.fontWeight !== currentProps.fontWeight) state.createGeometry = true;
  445. },
  446. geometryUtils: Text.Utils
  447. }, materialId);
  448. }
  449. // direct-volume
  450. export const UnitsDirectVolumeParams = { ...StructureDirectVolumeParams, ...StructureParams };
  451. export type UnitsDirectVolumeParams = typeof UnitsDirectVolumeParams
  452. export interface UnitsDirectVolumeVisualBuilder<P extends UnitsDirectVolumeParams> extends UnitsVisualBuilder<P, DirectVolume> { }
  453. export function UnitsDirectVolumeVisual<P extends UnitsDirectVolumeParams>(builder: UnitsDirectVolumeVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  454. return UnitsVisual<DirectVolume, P>({
  455. ...builder,
  456. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  457. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  458. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  459. },
  460. geometryUtils: DirectVolume.Utils
  461. }, materialId);
  462. }
  463. // texture-mesh
  464. export const UnitsTextureMeshParams = { ...StructureTextureMeshParams, ...StructureParams };
  465. export type UnitsTextureMeshParams = typeof UnitsTextureMeshParams
  466. export interface UnitsTextureMeshVisualBuilder<P extends UnitsTextureMeshParams> extends UnitsVisualBuilder<P, TextureMesh> { }
  467. export function UnitsTextureMeshVisual<P extends UnitsTextureMeshParams>(builder: UnitsTextureMeshVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  468. return UnitsVisual<TextureMesh, P>({
  469. ...builder,
  470. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  471. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  472. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  473. },
  474. geometryUtils: TextureMesh.Utils
  475. }, materialId);
  476. }