texture-mesh.ts 7.0 KB

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