spheres.ts 13 KB

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