spheres.ts 12 KB

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