texture-mesh.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright (c) 2019-2020 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 { fillSerial } from '../../../mol-util/array';
  24. import { createEmptyClipping } from '../clipping-data';
  25. import { NullLocation } from '../../../mol-model/location';
  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. /** texture has vertex positions in XYZ and group id in W */
  34. readonly vertexGroupTexture: ValueCell<Texture>,
  35. readonly normalTexture: ValueCell<Texture>,
  36. readonly boundingSphere: Sphere3D
  37. }
  38. export namespace TextureMesh {
  39. export function create(vertexCount: number, groupCount: number, vertexGroupTexture: Texture, normalTexture: Texture, boundingSphere: Sphere3D, textureMesh?: TextureMesh): TextureMesh {
  40. const width = vertexGroupTexture.getWidth();
  41. const height = vertexGroupTexture.getHeight();
  42. if (textureMesh) {
  43. textureMesh.vertexCount = vertexCount;
  44. textureMesh.groupCount = groupCount;
  45. ValueCell.update(textureMesh.geoTextureDim, Vec2.set(textureMesh.geoTextureDim.ref.value, width, height));
  46. ValueCell.update(textureMesh.vertexGroupTexture, vertexGroupTexture);
  47. ValueCell.update(textureMesh.normalTexture, normalTexture);
  48. Sphere3D.copy(textureMesh.boundingSphere, boundingSphere);
  49. return textureMesh;
  50. } else {
  51. return {
  52. kind: 'texture-mesh',
  53. vertexCount,
  54. groupCount,
  55. geoTextureDim: ValueCell.create(Vec2.create(width, height)),
  56. vertexGroupTexture: ValueCell.create(vertexGroupTexture),
  57. normalTexture: ValueCell.create(normalTexture),
  58. boundingSphere: Sphere3D.clone(boundingSphere),
  59. };
  60. }
  61. }
  62. export function createEmpty(textureMesh?: TextureMesh): TextureMesh {
  63. return {} as TextureMesh; // TODO
  64. }
  65. export const Params = {
  66. ...BaseGeometry.Params,
  67. doubleSided: PD.Boolean(false, BaseGeometry.CustomQualityParamInfo),
  68. flipSided: PD.Boolean(false, BaseGeometry.ShadingCategory),
  69. flatShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  70. ignoreLight: PD.Boolean(false, BaseGeometry.ShadingCategory),
  71. xrayShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  72. };
  73. export type Params = typeof Params
  74. export const Utils: GeometryUtils<TextureMesh, Params> = {
  75. Params,
  76. createEmpty,
  77. createValues,
  78. createValuesSimple,
  79. updateValues,
  80. updateBoundingSphere,
  81. createRenderableState: BaseGeometry.createRenderableState,
  82. updateRenderableState: BaseGeometry.updateRenderableState,
  83. createPositionIterator: () => LocationIterator(1, 1, 1, () => NullLocation)
  84. };
  85. function createValues(textureMesh: TextureMesh, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): TextureMeshValues {
  86. const { instanceCount, groupCount } = locationIt;
  87. const positionIt = Utils.createPositionIterator(textureMesh, transform);
  88. const color = createColors(locationIt, positionIt, theme.color);
  89. const marker = createMarkers(instanceCount * groupCount);
  90. const overpaint = createEmptyOverpaint();
  91. const transparency = createEmptyTransparency();
  92. const clipping = createEmptyClipping();
  93. const counts = { drawCount: textureMesh.vertexCount, vertexCount: textureMesh.vertexCount, groupCount, instanceCount };
  94. const invariantBoundingSphere = Sphere3D.clone(textureMesh.boundingSphere);
  95. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  96. return {
  97. uGeoTexDim: textureMesh.geoTextureDim,
  98. tPositionGroup: textureMesh.vertexGroupTexture,
  99. tNormal: textureMesh.normalTexture,
  100. // aGroup is used as a vertex index here and the group id is retirieved from tPositionGroup
  101. aGroup: ValueCell.create(fillSerial(new Float32Array(textureMesh.vertexCount))),
  102. boundingSphere: ValueCell.create(boundingSphere),
  103. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  104. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  105. ...color,
  106. ...marker,
  107. ...overpaint,
  108. ...transparency,
  109. ...clipping,
  110. ...transform,
  111. ...BaseGeometry.createValues(props, counts),
  112. dDoubleSided: ValueCell.create(props.doubleSided),
  113. dFlatShaded: ValueCell.create(props.flatShaded),
  114. dFlipSided: ValueCell.create(props.flipSided),
  115. dIgnoreLight: ValueCell.create(props.ignoreLight),
  116. dXrayShaded: ValueCell.create(props.xrayShaded),
  117. dGeoTexture: ValueCell.create(true),
  118. };
  119. }
  120. function createValuesSimple(textureMesh: TextureMesh, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  121. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  122. const p = { ...PD.getDefaultValues(Params), ...props };
  123. return createValues(textureMesh, s.transform, s.locationIterator, s.theme, p);
  124. }
  125. function updateValues(values: TextureMeshValues, props: PD.Values<Params>) {
  126. BaseGeometry.updateValues(values, props);
  127. ValueCell.updateIfChanged(values.dDoubleSided, props.doubleSided);
  128. ValueCell.updateIfChanged(values.dFlatShaded, props.flatShaded);
  129. ValueCell.updateIfChanged(values.dFlipSided, props.flipSided);
  130. ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight);
  131. ValueCell.updateIfChanged(values.dXrayShaded, props.xrayShaded);
  132. if (values.drawCount.ref.value > values.aGroup.ref.value.length) {
  133. // console.log('updating vertex ids in aGroup to handle larger drawCount')
  134. ValueCell.update(values.aGroup, fillSerial(new Float32Array(values.drawCount.ref.value)));
  135. }
  136. }
  137. function updateBoundingSphere(values: TextureMeshValues, textureMesh: TextureMesh) {
  138. const invariantBoundingSphere = Sphere3D.clone(textureMesh.boundingSphere);
  139. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  140. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  141. ValueCell.update(values.boundingSphere, boundingSphere);
  142. }
  143. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  144. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  145. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  146. }
  147. }
  148. }