text.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 { ParamDefinition as PD } from 'mol-util/param-definition';
  7. import { ValueCell } from 'mol-util';
  8. import { GeometryUtils } from '../geometry';
  9. import { LocationIterator } from 'mol-geo/util/location-iterator';
  10. import { TransformData } from '../transform-data';
  11. import { Theme } from 'mol-theme/theme';
  12. import { createColors } from '../color-data';
  13. import { createSizes, getMaxSize } from '../size-data';
  14. import { createMarkers } from '../marker-data';
  15. import { ColorNames } from 'mol-util/color/tables';
  16. import { Sphere3D } from 'mol-math/geometry';
  17. import { calculateBoundingSphere, TextureImage, createTextureImage } from 'mol-gl/renderable/util';
  18. import { TextValues } from 'mol-gl/renderable/text';
  19. import { Color } from 'mol-util/color';
  20. import { Vec3 } from 'mol-math/linear-algebra';
  21. import { FontAtlasParams } from './font-atlas';
  22. import { RenderableState } from 'mol-gl/renderable';
  23. import { clamp } from 'mol-math/interpolate';
  24. import { createRenderObject as _createRenderObject } from 'mol-gl/render-object';
  25. import { BaseGeometry } from '../base';
  26. type TextAttachment = 'bottom-left' | 'bottom-center' | 'bottom-right' | 'middle-left' | 'middle-center' | 'middle-right' | 'top-left' | 'top-center' | 'top-right'
  27. /** Text */
  28. export interface Text {
  29. readonly kind: 'text',
  30. /** Number of characters in the text */
  31. readonly charCount: number,
  32. /** Font Atlas */
  33. readonly fontTexture: ValueCell<TextureImage<Uint8Array>>,
  34. /** Center buffer as array of xyz values wrapped in a value cell */
  35. readonly centerBuffer: ValueCell<Float32Array>,
  36. /** Mapping buffer as array of xy values wrapped in a value cell */
  37. readonly mappingBuffer: ValueCell<Float32Array>,
  38. /** Index buffer as array of center index triplets wrapped in a value cell */
  39. readonly indexBuffer: ValueCell<Uint32Array>,
  40. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  41. readonly groupBuffer: ValueCell<Float32Array>,
  42. /** Texture coordinates buffer as array of uv values wrapped in a value cell */
  43. readonly tcoordBuffer: ValueCell<Float32Array>,
  44. }
  45. export namespace Text {
  46. export function createEmpty(text?: Text): Text {
  47. const cb = text ? text.centerBuffer.ref.value : new Float32Array(0)
  48. const mb = text ? text.mappingBuffer.ref.value : new Float32Array(0)
  49. const ib = text ? text.indexBuffer.ref.value : new Uint32Array(0)
  50. const gb = text ? text.groupBuffer.ref.value : new Float32Array(0)
  51. const tb = text ? text.tcoordBuffer.ref.value : new Float32Array(0)
  52. const ft = text ? text.fontTexture.ref.value : createTextureImage(0, 1)
  53. return {
  54. kind: 'text',
  55. charCount: 0,
  56. fontTexture: text ? ValueCell.update(text.fontTexture, ft) : ValueCell.create(ft),
  57. centerBuffer: text ? ValueCell.update(text.centerBuffer, cb) : ValueCell.create(cb),
  58. mappingBuffer: text ? ValueCell.update(text.mappingBuffer, mb) : ValueCell.create(mb),
  59. indexBuffer: text ? ValueCell.update(text.indexBuffer, ib) : ValueCell.create(ib),
  60. groupBuffer: text ? ValueCell.update(text.groupBuffer, gb) : ValueCell.create(gb),
  61. tcoordBuffer: text ? ValueCell.update(text.tcoordBuffer, tb) : ValueCell.create(tb)
  62. }
  63. }
  64. export const Params = {
  65. ...BaseGeometry.Params,
  66. ...FontAtlasParams,
  67. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  68. borderWidth: PD.Numeric(0, { min: 0, max: 0.5, step: 0.01 }),
  69. borderColor: PD.Color(ColorNames.grey),
  70. offsetX: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }),
  71. offsetY: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }),
  72. offsetZ: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }),
  73. background: PD.Boolean(false),
  74. backgroundMargin: PD.Numeric(0.2, { min: 0, max: 1, step: 0.01 }),
  75. backgroundColor: PD.Color(ColorNames.grey),
  76. backgroundOpacity: PD.Numeric(1, { min: 0, max: 1, step: 0.01 }),
  77. attachment: PD.Select('normal', [['bottom-left', 'bottom-left'], ['bottom-center', 'bottom-center'], ['bottom-right', 'bottom-right'], ['middle-left', 'middle-left'], ['top-left', 'top-left'], ['top-center', 'top-center'], ['top-right', 'top-right']] as [TextAttachment, string][]),
  78. }
  79. export type Params = typeof Params
  80. export const Utils: GeometryUtils<Text, Params> = {
  81. Params,
  82. createEmpty,
  83. createValues,
  84. createValuesSimple,
  85. updateValues,
  86. updateBoundingSphere,
  87. createRenderableState,
  88. updateRenderableState,
  89. // createRenderObject
  90. }
  91. function createValues(text: Text, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): TextValues {
  92. const { instanceCount, groupCount } = locationIt
  93. if (instanceCount !== transform.instanceCount.ref.value) {
  94. throw new Error('instanceCount values in TransformData and LocationIterator differ')
  95. }
  96. const color = createColors(locationIt, theme.color)
  97. const size = createSizes(locationIt, theme.size)
  98. const marker = createMarkers(instanceCount * groupCount)
  99. const counts = { drawCount: text.charCount * 2 * 3, groupCount, instanceCount }
  100. const padding = getMaxSize(size)
  101. const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
  102. text.centerBuffer.ref.value, text.charCount * 4,
  103. transform.aTransform.ref.value, instanceCount, padding
  104. )
  105. return {
  106. aPosition: text.centerBuffer,
  107. aMapping: text.mappingBuffer,
  108. aGroup: text.groupBuffer,
  109. elements: text.indexBuffer,
  110. boundingSphere: ValueCell.create(boundingSphere),
  111. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  112. ...color,
  113. ...size,
  114. ...marker,
  115. ...transform,
  116. aTexCoord: text.tcoordBuffer,
  117. tFont: text.fontTexture,
  118. padding: ValueCell.create(padding),
  119. ...BaseGeometry.createValues(props, counts),
  120. uSizeFactor: ValueCell.create(props.sizeFactor),
  121. uBorderWidth: ValueCell.create(clamp(props.borderWidth / 2, 0, 0.5)),
  122. uBorderColor: ValueCell.create(Color.toArrayNormalized(props.borderColor, Vec3.zero(), 0)),
  123. uOffsetX: ValueCell.create(props.offsetX),
  124. uOffsetY: ValueCell.create(props.offsetY),
  125. uOffsetZ: ValueCell.create(props.offsetZ),
  126. uBackgroundColor: ValueCell.create(Color.toArrayNormalized(props.backgroundColor, Vec3.zero(), 0)),
  127. uBackgroundOpacity: ValueCell.create(props.backgroundOpacity),
  128. }
  129. }
  130. function createValuesSimple(text: Text, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  131. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform)
  132. const p = { ...PD.getDefaultValues(Params), props }
  133. return createValues(text, s.transform, s.locationIterator, s.theme, p)
  134. }
  135. function updateValues(values: TextValues, props: PD.Values<Params>) {
  136. BaseGeometry.updateValues(values, props)
  137. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor)
  138. }
  139. function updateBoundingSphere(values: TextValues, text: Text) {
  140. const padding = getMaxSize(values)
  141. const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
  142. values.aPosition.ref.value, text.charCount * 4,
  143. values.aTransform.ref.value, values.instanceCount.ref.value, padding
  144. )
  145. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  146. ValueCell.update(values.boundingSphere, boundingSphere)
  147. }
  148. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  149. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere)
  150. }
  151. }
  152. function createRenderableState(props: PD.Values<Params>): RenderableState {
  153. const state = BaseGeometry.createRenderableState(props)
  154. updateRenderableState(state, props)
  155. return state
  156. }
  157. function updateRenderableState(state: RenderableState, props: PD.Values<Params>) {
  158. BaseGeometry.updateRenderableState(state, props)
  159. state.opaque = false
  160. }
  161. // function createRenderObject(values: TextValues, state: RenderableState) {
  162. // return _createRenderObject('text', values, state)
  163. // }
  164. }