texture-mesh.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 { 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. export interface TextureMesh {
  26. readonly kind: 'texture-mesh',
  27. /** Number of vertices in the texture-mesh */
  28. vertexCount: number,
  29. /** Number of groups in the texture-mesh */
  30. groupCount: number,
  31. readonly geoTextureDim: ValueCell<Vec2>,
  32. readonly vertexTexture: ValueCell<Texture>,
  33. readonly groupTexture: ValueCell<Texture>,
  34. readonly normalTexture: ValueCell<Texture>,
  35. readonly doubleBuffer: TextureMesh.DoubleBuffer
  36. readonly boundingSphere: Sphere3D
  37. meta?: unknown
  38. }
  39. export namespace TextureMesh {
  40. export class DoubleBuffer {
  41. private index = 0;
  42. private textures: ({ vertex: Texture, group: Texture, normal: Texture } | undefined)[] = []
  43. get() {
  44. return this.textures[this.index];
  45. }
  46. set(vertex: Texture, group: Texture, normal: Texture) {
  47. this.textures[this.index] = Object.assign(this.textures[this.index] || {}, {
  48. vertex, group, normal
  49. });
  50. this.index = (this.index + 1) % 2;
  51. }
  52. destroy() {
  53. for (const buffer of this.textures) {
  54. buffer!.vertex.destroy();
  55. buffer!.group.destroy();
  56. buffer!.normal.destroy();
  57. }
  58. }
  59. }
  60. export function create(vertexCount: number, groupCount: number, vertexTexture: Texture, groupTexture: Texture, normalTexture: Texture, boundingSphere: Sphere3D, textureMesh?: TextureMesh): TextureMesh {
  61. const width = vertexTexture.getWidth();
  62. const height = vertexTexture.getHeight();
  63. if (textureMesh) {
  64. textureMesh.vertexCount = vertexCount;
  65. textureMesh.groupCount = groupCount;
  66. ValueCell.update(textureMesh.geoTextureDim, Vec2.set(textureMesh.geoTextureDim.ref.value, width, height));
  67. ValueCell.update(textureMesh.vertexTexture, vertexTexture);
  68. ValueCell.update(textureMesh.groupTexture, groupTexture);
  69. ValueCell.update(textureMesh.normalTexture, normalTexture);
  70. textureMesh.doubleBuffer.set(vertexTexture, groupTexture, normalTexture);
  71. Sphere3D.copy(textureMesh.boundingSphere, boundingSphere);
  72. return textureMesh;
  73. } else {
  74. return {
  75. kind: 'texture-mesh',
  76. vertexCount,
  77. groupCount,
  78. geoTextureDim: ValueCell.create(Vec2.create(width, height)),
  79. vertexTexture: ValueCell.create(vertexTexture),
  80. groupTexture: ValueCell.create(groupTexture),
  81. normalTexture: ValueCell.create(normalTexture),
  82. doubleBuffer: new DoubleBuffer(),
  83. boundingSphere: Sphere3D.clone(boundingSphere),
  84. };
  85. }
  86. }
  87. export function createEmpty(textureMesh?: TextureMesh): TextureMesh {
  88. return {} as TextureMesh; // TODO
  89. }
  90. export const Params = {
  91. ...BaseGeometry.Params,
  92. doubleSided: PD.Boolean(false, BaseGeometry.CustomQualityParamInfo),
  93. flipSided: PD.Boolean(false, BaseGeometry.ShadingCategory),
  94. flatShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  95. ignoreLight: PD.Boolean(false, BaseGeometry.ShadingCategory),
  96. xrayShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  97. };
  98. export type Params = typeof Params
  99. export const Utils: GeometryUtils<TextureMesh, Params> = {
  100. Params,
  101. createEmpty,
  102. createValues,
  103. createValuesSimple,
  104. updateValues,
  105. updateBoundingSphere,
  106. createRenderableState: BaseGeometry.createRenderableState,
  107. updateRenderableState: BaseGeometry.updateRenderableState,
  108. createPositionIterator: () => LocationIterator(1, 1, 1, () => NullLocation)
  109. };
  110. function createValues(textureMesh: TextureMesh, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): TextureMeshValues {
  111. const { instanceCount, groupCount } = locationIt;
  112. const positionIt = Utils.createPositionIterator(textureMesh, transform);
  113. const color = createColors(locationIt, positionIt, theme.color);
  114. const marker = createMarkers(instanceCount * groupCount);
  115. const overpaint = createEmptyOverpaint();
  116. const transparency = createEmptyTransparency();
  117. const clipping = createEmptyClipping();
  118. const counts = { drawCount: textureMesh.vertexCount, vertexCount: textureMesh.vertexCount, groupCount, instanceCount };
  119. const invariantBoundingSphere = Sphere3D.clone(textureMesh.boundingSphere);
  120. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  121. return {
  122. uGeoTexDim: textureMesh.geoTextureDim,
  123. tPosition: textureMesh.vertexTexture,
  124. tGroup: textureMesh.groupTexture,
  125. tNormal: textureMesh.normalTexture,
  126. boundingSphere: ValueCell.create(boundingSphere),
  127. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  128. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  129. ...color,
  130. ...marker,
  131. ...overpaint,
  132. ...transparency,
  133. ...clipping,
  134. ...transform,
  135. ...BaseGeometry.createValues(props, counts),
  136. dDoubleSided: ValueCell.create(props.doubleSided),
  137. dFlatShaded: ValueCell.create(props.flatShaded),
  138. dFlipSided: ValueCell.create(props.flipSided),
  139. dIgnoreLight: ValueCell.create(props.ignoreLight),
  140. dXrayShaded: ValueCell.create(props.xrayShaded),
  141. dGeoTexture: ValueCell.create(true),
  142. };
  143. }
  144. function createValuesSimple(textureMesh: TextureMesh, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  145. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  146. const p = { ...PD.getDefaultValues(Params), ...props };
  147. return createValues(textureMesh, s.transform, s.locationIterator, s.theme, p);
  148. }
  149. function updateValues(values: TextureMeshValues, props: PD.Values<Params>) {
  150. BaseGeometry.updateValues(values, props);
  151. ValueCell.updateIfChanged(values.dDoubleSided, props.doubleSided);
  152. ValueCell.updateIfChanged(values.dFlatShaded, props.flatShaded);
  153. ValueCell.updateIfChanged(values.dFlipSided, props.flipSided);
  154. ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight);
  155. ValueCell.updateIfChanged(values.dXrayShaded, props.xrayShaded);
  156. }
  157. function updateBoundingSphere(values: TextureMeshValues, textureMesh: TextureMesh) {
  158. const invariantBoundingSphere = Sphere3D.clone(textureMesh.boundingSphere);
  159. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  160. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  161. ValueCell.update(values.boundingSphere, boundingSphere);
  162. }
  163. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  164. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  165. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  166. }
  167. }
  168. }