texture-mesh.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Copyright (c) 2019-2021 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 { Sphere3D } from '../../../mol-math/geometry';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { LocationIterator } from '../../../mol-geo/util/location-iterator';
  10. import { TransformData } from '../transform-data';
  11. import { createColors } from '../color-data';
  12. import { createMarkers } from '../marker-data';
  13. import { GeometryUtils } from '../geometry';
  14. import { Theme } from '../../../mol-theme/theme';
  15. import { Color } from '../../../mol-util/color';
  16. import { BaseGeometry } from '../base';
  17. import { createEmptyOverpaint } from '../overpaint-data';
  18. import { createEmptyTransparency } from '../transparency-data';
  19. import { TextureMeshValues } from '../../../mol-gl/renderable/texture-mesh';
  20. import { calculateTransformBoundingSphere } from '../../../mol-gl/renderable/util';
  21. import { createNullTexture, Texture } from '../../../mol-gl/webgl/texture';
  22. import { Vec2, Vec4 } from '../../../mol-math/linear-algebra';
  23. import { createEmptyClipping } from '../clipping-data';
  24. import { NullLocation } from '../../../mol-model/location';
  25. import { createEmptySubstance } from '../substance-data';
  26. export interface TextureMesh {
  27. readonly kind: 'texture-mesh',
  28. /** Number of vertices in the texture-mesh */
  29. vertexCount: number,
  30. /** Number of groups in the texture-mesh */
  31. groupCount: number,
  32. readonly geoTextureDim: ValueCell<Vec2>,
  33. readonly vertexTexture: ValueCell<Texture>,
  34. readonly groupTexture: ValueCell<Texture>,
  35. readonly normalTexture: ValueCell<Texture>,
  36. readonly doubleBuffer: TextureMesh.DoubleBuffer
  37. readonly boundingSphere: Sphere3D
  38. readonly meta: { [k: string]: unknown }
  39. }
  40. export namespace TextureMesh {
  41. export class DoubleBuffer {
  42. private index = 0;
  43. private textures: ({ vertex: Texture, group: Texture, normal: Texture } | undefined)[] = [];
  44. get() {
  45. return this.textures[this.index];
  46. }
  47. set(vertex: Texture, group: Texture, normal: Texture) {
  48. this.textures[this.index] = Object.assign(this.textures[this.index] || {}, {
  49. vertex, group, normal
  50. });
  51. this.index = (this.index + 1) % 2;
  52. }
  53. destroy() {
  54. for (const buffer of this.textures) {
  55. buffer!.vertex.destroy();
  56. buffer!.group.destroy();
  57. buffer!.normal.destroy();
  58. }
  59. }
  60. }
  61. export function create(vertexCount: number, groupCount: number, vertexTexture: Texture, groupTexture: Texture, normalTexture: Texture, boundingSphere: Sphere3D, textureMesh?: TextureMesh): TextureMesh {
  62. const width = vertexTexture.getWidth();
  63. const height = vertexTexture.getHeight();
  64. if (textureMesh) {
  65. textureMesh.vertexCount = vertexCount;
  66. textureMesh.groupCount = groupCount;
  67. ValueCell.update(textureMesh.geoTextureDim, Vec2.set(textureMesh.geoTextureDim.ref.value, width, height));
  68. ValueCell.update(textureMesh.vertexTexture, vertexTexture);
  69. ValueCell.update(textureMesh.groupTexture, groupTexture);
  70. ValueCell.update(textureMesh.normalTexture, normalTexture);
  71. textureMesh.doubleBuffer.set(vertexTexture, groupTexture, normalTexture);
  72. Sphere3D.copy(textureMesh.boundingSphere, boundingSphere);
  73. return textureMesh;
  74. } else {
  75. return {
  76. kind: 'texture-mesh',
  77. vertexCount,
  78. groupCount,
  79. geoTextureDim: ValueCell.create(Vec2.create(width, height)),
  80. vertexTexture: ValueCell.create(vertexTexture),
  81. groupTexture: ValueCell.create(groupTexture),
  82. normalTexture: ValueCell.create(normalTexture),
  83. doubleBuffer: new DoubleBuffer(),
  84. boundingSphere: Sphere3D.clone(boundingSphere),
  85. meta: {}
  86. };
  87. }
  88. }
  89. export function createEmpty(textureMesh?: TextureMesh): TextureMesh {
  90. const vt = textureMesh ? textureMesh.vertexTexture.ref.value : createNullTexture();
  91. const gt = textureMesh ? textureMesh.groupTexture.ref.value : createNullTexture();
  92. const nt = textureMesh ? textureMesh.normalTexture.ref.value : createNullTexture();
  93. const bs = textureMesh ? textureMesh.boundingSphere : Sphere3D();
  94. return create(0, 0, vt, gt, nt, bs, textureMesh);
  95. }
  96. export const Params = {
  97. ...BaseGeometry.Params,
  98. doubleSided: PD.Boolean(false, BaseGeometry.CustomQualityParamInfo),
  99. flipSided: PD.Boolean(false, BaseGeometry.ShadingCategory),
  100. flatShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  101. ignoreLight: PD.Boolean(false, BaseGeometry.ShadingCategory),
  102. xrayShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  103. bumpFrequency: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }, BaseGeometry.ShadingCategory),
  104. bumpAmplitude: PD.Numeric(1, { min: 0, max: 5, step: 0.1 }, BaseGeometry.ShadingCategory),
  105. };
  106. export type Params = typeof Params
  107. export const Utils: GeometryUtils<TextureMesh, Params> = {
  108. Params,
  109. createEmpty,
  110. createValues,
  111. createValuesSimple,
  112. updateValues,
  113. updateBoundingSphere,
  114. createRenderableState: BaseGeometry.createRenderableState,
  115. updateRenderableState: BaseGeometry.updateRenderableState,
  116. createPositionIterator: () => LocationIterator(1, 1, 1, () => NullLocation)
  117. };
  118. function createValues(textureMesh: TextureMesh, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): TextureMeshValues {
  119. const { instanceCount, groupCount } = locationIt;
  120. const positionIt = Utils.createPositionIterator(textureMesh, transform);
  121. const color = createColors(locationIt, positionIt, theme.color);
  122. const marker = createMarkers(instanceCount * groupCount);
  123. const overpaint = createEmptyOverpaint();
  124. const transparency = createEmptyTransparency();
  125. const substance = createEmptySubstance();
  126. const clipping = createEmptyClipping();
  127. const counts = { drawCount: textureMesh.vertexCount, vertexCount: textureMesh.vertexCount, groupCount, instanceCount };
  128. const invariantBoundingSphere = Sphere3D.clone(textureMesh.boundingSphere);
  129. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  130. return {
  131. uGeoTexDim: textureMesh.geoTextureDim,
  132. tPosition: textureMesh.vertexTexture,
  133. tGroup: textureMesh.groupTexture,
  134. tNormal: textureMesh.normalTexture,
  135. boundingSphere: ValueCell.create(boundingSphere),
  136. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  137. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  138. ...color,
  139. ...marker,
  140. ...overpaint,
  141. ...transparency,
  142. ...substance,
  143. ...clipping,
  144. ...transform,
  145. ...BaseGeometry.createValues(props, counts),
  146. uDoubleSided: ValueCell.create(props.doubleSided),
  147. dFlatShaded: ValueCell.create(props.flatShaded),
  148. dFlipSided: ValueCell.create(props.flipSided),
  149. dIgnoreLight: ValueCell.create(props.ignoreLight),
  150. dXrayShaded: ValueCell.create(props.xrayShaded),
  151. uBumpFrequency: ValueCell.create(props.bumpFrequency),
  152. uBumpAmplitude: ValueCell.create(props.bumpAmplitude),
  153. dGeoTexture: ValueCell.create(true),
  154. meta: ValueCell.create(textureMesh.meta),
  155. };
  156. }
  157. function createValuesSimple(textureMesh: TextureMesh, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  158. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  159. const p = { ...PD.getDefaultValues(Params), ...props };
  160. return createValues(textureMesh, s.transform, s.locationIterator, s.theme, p);
  161. }
  162. function updateValues(values: TextureMeshValues, props: PD.Values<Params>) {
  163. BaseGeometry.updateValues(values, props);
  164. ValueCell.updateIfChanged(values.uDoubleSided, props.doubleSided);
  165. ValueCell.updateIfChanged(values.dFlatShaded, props.flatShaded);
  166. ValueCell.updateIfChanged(values.dFlipSided, props.flipSided);
  167. ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight);
  168. ValueCell.updateIfChanged(values.dXrayShaded, props.xrayShaded);
  169. ValueCell.updateIfChanged(values.uBumpFrequency, props.bumpFrequency);
  170. ValueCell.updateIfChanged(values.uBumpAmplitude, props.bumpAmplitude);
  171. }
  172. function updateBoundingSphere(values: TextureMeshValues, textureMesh: TextureMesh) {
  173. const invariantBoundingSphere = Sphere3D.clone(textureMesh.boundingSphere);
  174. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  175. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  176. ValueCell.update(values.boundingSphere, boundingSphere);
  177. }
  178. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  179. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  180. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  181. }
  182. }
  183. }