cylinders.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ValueCell } from '../../../mol-util';
  7. import { Mat4, Vec3, Vec4 } from '../../../mol-math/linear-algebra';
  8. import { transformPositionArray, GroupMapping, createGroupMapping } from '../../util';
  9. import { GeometryUtils } from '../geometry';
  10. import { createColors } from '../color-data';
  11. import { createMarkers } from '../marker-data';
  12. import { createSizes, getMaxSize } from '../size-data';
  13. import { TransformData } from '../transform-data';
  14. import { LocationIterator, PositionLocation } from '../../util/location-iterator';
  15. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  16. import { calculateInvariantBoundingSphere, calculateTransformBoundingSphere } from '../../../mol-gl/renderable/util';
  17. import { Sphere3D } from '../../../mol-math/geometry';
  18. import { Theme } from '../../../mol-theme/theme';
  19. import { Color } from '../../../mol-util/color';
  20. import { BaseGeometry } from '../base';
  21. import { createEmptyOverpaint } from '../overpaint-data';
  22. import { createEmptyTransparency } from '../transparency-data';
  23. import { hashFnv32a } from '../../../mol-data/util';
  24. import { createEmptyClipping } from '../clipping-data';
  25. import { CylindersValues } from '../../../mol-gl/renderable/cylinders';
  26. import { RenderableState } from '../../../mol-gl/renderable';
  27. import { createEmptySubstance } from '../substance-data';
  28. export interface Cylinders {
  29. readonly kind: 'cylinders',
  30. /** Number of cylinders */
  31. cylinderCount: number,
  32. /** Mapping buffer as array of uvw values wrapped in a value cell */
  33. readonly mappingBuffer: ValueCell<Float32Array>,
  34. /** Index buffer as array of vertex index triplets wrapped in a value cell */
  35. readonly indexBuffer: ValueCell<Uint32Array>,
  36. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  37. readonly groupBuffer: ValueCell<Float32Array>,
  38. /** Cylinder start buffer as array of xyz values wrapped in a value cell */
  39. readonly startBuffer: ValueCell<Float32Array>,
  40. /** Cylinder end buffer as array of xyz values wrapped in a value cell */
  41. readonly endBuffer: ValueCell<Float32Array>,
  42. /** Cylinder scale buffer as array of scaling factors wrapped in a value cell */
  43. readonly scaleBuffer: ValueCell<Float32Array>,
  44. /** Cylinder cap buffer as array of cap flags wrapped in a value cell */
  45. readonly capBuffer: ValueCell<Float32Array>,
  46. /** Bounding sphere of the cylinders */
  47. readonly boundingSphere: Sphere3D
  48. /** Maps group ids to cylinder indices */
  49. readonly groupMapping: GroupMapping
  50. setBoundingSphere(boundingSphere: Sphere3D): void
  51. }
  52. export namespace Cylinders {
  53. export function create(mappings: Float32Array, indices: Uint32Array, groups: Float32Array, starts: Float32Array, ends: Float32Array, scales: Float32Array, caps: Float32Array, cylinderCount: number, cylinders?: Cylinders): Cylinders {
  54. return cylinders ?
  55. update(mappings, indices, groups, starts, ends, scales, caps, cylinderCount, cylinders) :
  56. fromArrays(mappings, indices, groups, starts, ends, scales, caps, cylinderCount);
  57. }
  58. export function createEmpty(cylinders?: Cylinders): Cylinders {
  59. const mb = cylinders ? cylinders.mappingBuffer.ref.value : new Float32Array(0);
  60. const ib = cylinders ? cylinders.indexBuffer.ref.value : new Uint32Array(0);
  61. const gb = cylinders ? cylinders.groupBuffer.ref.value : new Float32Array(0);
  62. const sb = cylinders ? cylinders.startBuffer.ref.value : new Float32Array(0);
  63. const eb = cylinders ? cylinders.endBuffer.ref.value : new Float32Array(0);
  64. const ab = cylinders ? cylinders.scaleBuffer.ref.value : new Float32Array(0);
  65. const cb = cylinders ? cylinders.capBuffer.ref.value : new Float32Array(0);
  66. return create(mb, ib, gb, sb, eb, ab, cb, 0, cylinders);
  67. }
  68. function hashCode(cylinders: Cylinders) {
  69. return hashFnv32a([
  70. cylinders.cylinderCount, cylinders.mappingBuffer.ref.version, cylinders.indexBuffer.ref.version,
  71. cylinders.groupBuffer.ref.version, cylinders.startBuffer.ref.version, cylinders.endBuffer.ref.version, cylinders.scaleBuffer.ref.version, cylinders.capBuffer.ref.version
  72. ]);
  73. }
  74. function fromArrays(mappings: Float32Array, indices: Uint32Array, groups: Float32Array, starts: Float32Array, ends: Float32Array, scales: Float32Array, caps: Float32Array, cylinderCount: number): Cylinders {
  75. const boundingSphere = Sphere3D();
  76. let groupMapping: GroupMapping;
  77. let currentHash = -1;
  78. let currentGroup = -1;
  79. const cylinders = {
  80. kind: 'cylinders' as const,
  81. cylinderCount,
  82. mappingBuffer: ValueCell.create(mappings),
  83. indexBuffer: ValueCell.create(indices),
  84. groupBuffer: ValueCell.create(groups),
  85. startBuffer: ValueCell.create(starts),
  86. endBuffer: ValueCell.create(ends),
  87. scaleBuffer: ValueCell.create(scales),
  88. capBuffer: ValueCell.create(caps),
  89. get boundingSphere() {
  90. const newHash = hashCode(cylinders);
  91. if (newHash !== currentHash) {
  92. const s = calculateInvariantBoundingSphere(cylinders.startBuffer.ref.value, cylinders.cylinderCount * 6, 6);
  93. const e = calculateInvariantBoundingSphere(cylinders.endBuffer.ref.value, cylinders.cylinderCount * 6, 6);
  94. Sphere3D.expandBySphere(boundingSphere, s, e);
  95. currentHash = newHash;
  96. }
  97. return boundingSphere;
  98. },
  99. get groupMapping() {
  100. if (cylinders.groupBuffer.ref.version !== currentGroup) {
  101. groupMapping = createGroupMapping(cylinders.groupBuffer.ref.value, cylinders.cylinderCount, 6);
  102. currentGroup = cylinders.groupBuffer.ref.version;
  103. }
  104. return groupMapping;
  105. },
  106. setBoundingSphere(sphere: Sphere3D) {
  107. Sphere3D.copy(boundingSphere, sphere);
  108. currentHash = hashCode(cylinders);
  109. }
  110. };
  111. return cylinders;
  112. }
  113. function update(mappings: Float32Array, indices: Uint32Array, groups: Float32Array, starts: Float32Array, ends: Float32Array, scales: Float32Array, caps: Float32Array, cylinderCount: number, cylinders: Cylinders) {
  114. if (cylinderCount > cylinders.cylinderCount) {
  115. ValueCell.update(cylinders.mappingBuffer, mappings);
  116. ValueCell.update(cylinders.indexBuffer, indices);
  117. }
  118. cylinders.cylinderCount = cylinderCount;
  119. ValueCell.update(cylinders.groupBuffer, groups);
  120. ValueCell.update(cylinders.startBuffer, starts);
  121. ValueCell.update(cylinders.endBuffer, ends);
  122. ValueCell.update(cylinders.scaleBuffer, scales);
  123. ValueCell.update(cylinders.capBuffer, caps);
  124. return cylinders;
  125. }
  126. export function transform(cylinders: Cylinders, t: Mat4) {
  127. const start = cylinders.startBuffer.ref.value;
  128. transformPositionArray(t, start, 0, cylinders.cylinderCount * 6);
  129. ValueCell.update(cylinders.startBuffer, start);
  130. const end = cylinders.endBuffer.ref.value;
  131. transformPositionArray(t, end, 0, cylinders.cylinderCount * 6);
  132. ValueCell.update(cylinders.endBuffer, end);
  133. }
  134. //
  135. export const Params = {
  136. ...BaseGeometry.Params,
  137. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  138. sizeAspectRatio: PD.Numeric(1, { min: 0, max: 3, step: 0.01 }),
  139. doubleSided: PD.Boolean(false, BaseGeometry.CustomQualityParamInfo),
  140. ignoreLight: PD.Boolean(false, BaseGeometry.ShadingCategory),
  141. xrayShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  142. transparentBackfaces: PD.Select('off', PD.arrayToOptions(['off', 'on', 'opaque']), BaseGeometry.ShadingCategory),
  143. bumpFrequency: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }, BaseGeometry.ShadingCategory),
  144. bumpAmplitude: PD.Numeric(1, { min: 0, max: 5, step: 0.1 }, BaseGeometry.ShadingCategory),
  145. };
  146. export type Params = typeof Params
  147. export const Utils: GeometryUtils<Cylinders, Params> = {
  148. Params,
  149. createEmpty,
  150. createValues,
  151. createValuesSimple,
  152. updateValues,
  153. updateBoundingSphere,
  154. createRenderableState,
  155. updateRenderableState,
  156. createPositionIterator
  157. };
  158. function createPositionIterator(cylinders: Cylinders, transform: TransformData): LocationIterator {
  159. const groupCount = cylinders.cylinderCount * 6;
  160. const instanceCount = transform.instanceCount.ref.value;
  161. const location = PositionLocation();
  162. const p = location.position;
  163. const s = cylinders.startBuffer.ref.value;
  164. const e = cylinders.endBuffer.ref.value;
  165. const m = transform.aTransform.ref.value;
  166. const getLocation = (groupIndex: number, instanceIndex: number) => {
  167. const v = groupIndex % 6 === 0 ? s : e;
  168. if (instanceIndex < 0) {
  169. Vec3.fromArray(p, v, groupIndex * 3);
  170. } else {
  171. Vec3.transformMat4Offset(p, v, m, 0, groupIndex * 3, instanceIndex * 16);
  172. }
  173. return location;
  174. };
  175. return LocationIterator(groupCount, instanceCount, 2, getLocation);
  176. }
  177. function createValues(cylinders: Cylinders, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): CylindersValues {
  178. const { instanceCount, groupCount } = locationIt;
  179. const positionIt = createPositionIterator(cylinders, transform);
  180. const color = createColors(locationIt, positionIt, theme.color);
  181. const size = createSizes(locationIt, theme.size);
  182. const marker = createMarkers(instanceCount * groupCount);
  183. const overpaint = createEmptyOverpaint();
  184. const transparency = createEmptyTransparency();
  185. const material = createEmptySubstance();
  186. const clipping = createEmptyClipping();
  187. const counts = { drawCount: cylinders.cylinderCount * 4 * 3, vertexCount: cylinders.cylinderCount * 6, groupCount, instanceCount };
  188. const padding = getMaxSize(size) * props.sizeFactor;
  189. const invariantBoundingSphere = Sphere3D.clone(cylinders.boundingSphere);
  190. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  191. return {
  192. dGeometryType: ValueCell.create('cylinders'),
  193. aMapping: cylinders.mappingBuffer,
  194. aGroup: cylinders.groupBuffer,
  195. aStart: cylinders.startBuffer,
  196. aEnd: cylinders.endBuffer,
  197. aScale: cylinders.scaleBuffer,
  198. aCap: cylinders.capBuffer,
  199. elements: cylinders.indexBuffer,
  200. boundingSphere: ValueCell.create(boundingSphere),
  201. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  202. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  203. ...color,
  204. ...size,
  205. ...marker,
  206. ...overpaint,
  207. ...transparency,
  208. ...material,
  209. ...clipping,
  210. ...transform,
  211. padding: ValueCell.create(padding),
  212. ...BaseGeometry.createValues(props, counts),
  213. uSizeFactor: ValueCell.create(props.sizeFactor * props.sizeAspectRatio),
  214. uDoubleSided: ValueCell.create(props.doubleSided),
  215. dIgnoreLight: ValueCell.create(props.ignoreLight),
  216. dXrayShaded: ValueCell.create(props.xrayShaded),
  217. dTransparentBackfaces: ValueCell.create(props.transparentBackfaces),
  218. uBumpFrequency: ValueCell.create(props.bumpFrequency),
  219. uBumpAmplitude: ValueCell.create(props.bumpAmplitude),
  220. };
  221. }
  222. function createValuesSimple(cylinders: Cylinders, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  223. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  224. const p = { ...PD.getDefaultValues(Params), ...props };
  225. return createValues(cylinders, s.transform, s.locationIterator, s.theme, p);
  226. }
  227. function updateValues(values: CylindersValues, props: PD.Values<Params>) {
  228. BaseGeometry.updateValues(values, props);
  229. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor * props.sizeAspectRatio);
  230. ValueCell.updateIfChanged(values.uDoubleSided, props.doubleSided);
  231. ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight);
  232. ValueCell.updateIfChanged(values.dXrayShaded, props.xrayShaded);
  233. ValueCell.updateIfChanged(values.dTransparentBackfaces, props.transparentBackfaces);
  234. ValueCell.updateIfChanged(values.uBumpFrequency, props.bumpFrequency);
  235. ValueCell.updateIfChanged(values.uBumpAmplitude, props.bumpAmplitude);
  236. }
  237. function updateBoundingSphere(values: CylindersValues, cylinders: Cylinders) {
  238. const invariantBoundingSphere = Sphere3D.clone(cylinders.boundingSphere);
  239. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  240. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  241. ValueCell.update(values.boundingSphere, boundingSphere);
  242. }
  243. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  244. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  245. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  246. }
  247. }
  248. function createRenderableState(props: PD.Values<Params>): RenderableState {
  249. const state = BaseGeometry.createRenderableState(props);
  250. updateRenderableState(state, props);
  251. return state;
  252. }
  253. function updateRenderableState(state: RenderableState, props: PD.Values<Params>) {
  254. BaseGeometry.updateRenderableState(state, props);
  255. state.opaque = state.opaque && !props.xrayShaded;
  256. state.writeDepth = state.opaque;
  257. }
  258. }