text.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. import { createEmptyOverpaint } from '../overpaint-data';
  27. type TextAttachment = (
  28. 'bottom-left' | 'bottom-center' | 'bottom-right' |
  29. 'middle-left' | 'middle-center' | 'middle-right' |
  30. 'top-left' | 'top-center' | 'top-right'
  31. )
  32. /** Text */
  33. export interface Text {
  34. readonly kind: 'text',
  35. /** Number of characters in the text */
  36. readonly charCount: number,
  37. /** Font Atlas */
  38. readonly fontTexture: ValueCell<TextureImage<Uint8Array>>,
  39. /** Center buffer as array of xyz values wrapped in a value cell */
  40. readonly centerBuffer: ValueCell<Float32Array>,
  41. /** Mapping buffer as array of xy values wrapped in a value cell */
  42. readonly mappingBuffer: ValueCell<Float32Array>,
  43. /** Depth buffer as array of z values wrapped in a value cell */
  44. readonly depthBuffer: ValueCell<Float32Array>,
  45. /** Index buffer as array of center index triplets wrapped in a value cell */
  46. readonly indexBuffer: ValueCell<Uint32Array>,
  47. /** Group buffer as array of group ids for each vertex wrapped in a value cell */
  48. readonly groupBuffer: ValueCell<Float32Array>,
  49. /** Texture coordinates buffer as array of uv values wrapped in a value cell */
  50. readonly tcoordBuffer: ValueCell<Float32Array>,
  51. }
  52. export namespace Text {
  53. export function createEmpty(text?: Text): Text {
  54. const ft = text ? text.fontTexture.ref.value : createTextureImage(0, 1)
  55. const cb = text ? text.centerBuffer.ref.value : new Float32Array(0)
  56. const mb = text ? text.mappingBuffer.ref.value : new Float32Array(0)
  57. const db = text ? text.depthBuffer.ref.value : new Float32Array(0)
  58. const ib = text ? text.indexBuffer.ref.value : new Uint32Array(0)
  59. const gb = text ? text.groupBuffer.ref.value : new Float32Array(0)
  60. const tb = text ? text.tcoordBuffer.ref.value : new Float32Array(0)
  61. return {
  62. kind: 'text',
  63. charCount: 0,
  64. fontTexture: text ? ValueCell.update(text.fontTexture, ft) : ValueCell.create(ft),
  65. centerBuffer: text ? ValueCell.update(text.centerBuffer, cb) : ValueCell.create(cb),
  66. mappingBuffer: text ? ValueCell.update(text.mappingBuffer, mb) : ValueCell.create(mb),
  67. depthBuffer: text ? ValueCell.update(text.depthBuffer, db) : ValueCell.create(db),
  68. indexBuffer: text ? ValueCell.update(text.indexBuffer, ib) : ValueCell.create(ib),
  69. groupBuffer: text ? ValueCell.update(text.groupBuffer, gb) : ValueCell.create(gb),
  70. tcoordBuffer: text ? ValueCell.update(text.tcoordBuffer, tb) : ValueCell.create(tb)
  71. }
  72. }
  73. export const Params = {
  74. ...BaseGeometry.Params,
  75. ...FontAtlasParams,
  76. sizeFactor: PD.Numeric(1, { min: 0, max: 10, step: 0.1 }),
  77. borderWidth: PD.Numeric(0, { min: 0, max: 0.5, step: 0.01 }),
  78. borderColor: PD.Color(ColorNames.grey),
  79. offsetX: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }),
  80. offsetY: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }),
  81. offsetZ: PD.Numeric(0, { min: 0, max: 10, step: 0.1 }),
  82. background: PD.Boolean(false),
  83. backgroundMargin: PD.Numeric(0.2, { min: 0, max: 1, step: 0.01 }),
  84. backgroundColor: PD.Color(ColorNames.grey),
  85. backgroundOpacity: PD.Numeric(1, { min: 0, max: 1, step: 0.01 }),
  86. tether: PD.Boolean(false),
  87. tetherLength: PD.Numeric(1, { min: 0, max: 5, step: 0.1 }),
  88. tetherBaseWidth: PD.Numeric(0.3, { min: 0, max: 1, step: 0.01 }),
  89. attachment: PD.Select('middle-center', [
  90. ['bottom-left', 'bottom-left'], ['bottom-center', 'bottom-center'], ['bottom-right', 'bottom-right'],
  91. ['middle-left', 'middle-left'], ['middle-center', 'middle-center'], ['middle-right', 'middle-right'],
  92. ['top-left', 'top-left'], ['top-center', 'top-center'], ['top-right', 'top-right'],
  93. ] as [TextAttachment, string][]),
  94. }
  95. export type Params = typeof Params
  96. export const Utils: GeometryUtils<Text, Params> = {
  97. Params,
  98. createEmpty,
  99. createValues,
  100. createValuesSimple,
  101. updateValues,
  102. updateBoundingSphere,
  103. createRenderableState,
  104. updateRenderableState,
  105. }
  106. function createValues(text: Text, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): TextValues {
  107. const { instanceCount, groupCount } = locationIt
  108. if (instanceCount !== transform.instanceCount.ref.value) {
  109. throw new Error('instanceCount values in TransformData and LocationIterator differ')
  110. }
  111. const color = createColors(locationIt, theme.color)
  112. const size = createSizes(locationIt, theme.size)
  113. const marker = createMarkers(instanceCount * groupCount)
  114. const overpaint = createEmptyOverpaint()
  115. const counts = { drawCount: text.charCount * 2 * 3, groupCount, instanceCount }
  116. const padding = getPadding(text.mappingBuffer.ref.value, text.depthBuffer.ref.value, text.charCount, getMaxSize(size))
  117. const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
  118. text.centerBuffer.ref.value, text.charCount * 4,
  119. transform.aTransform.ref.value, instanceCount, padding
  120. )
  121. return {
  122. aPosition: text.centerBuffer,
  123. aMapping: text.mappingBuffer,
  124. aDepth: text.depthBuffer,
  125. aGroup: text.groupBuffer,
  126. elements: text.indexBuffer,
  127. boundingSphere: ValueCell.create(boundingSphere),
  128. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  129. ...color,
  130. ...size,
  131. ...marker,
  132. ...overpaint,
  133. ...transform,
  134. aTexCoord: text.tcoordBuffer,
  135. tFont: text.fontTexture,
  136. padding: ValueCell.create(padding),
  137. ...BaseGeometry.createValues(props, counts),
  138. uSizeFactor: ValueCell.create(props.sizeFactor),
  139. uBorderWidth: ValueCell.create(clamp(props.borderWidth / 2, 0, 0.5)),
  140. uBorderColor: ValueCell.create(Color.toArrayNormalized(props.borderColor, Vec3.zero(), 0)),
  141. uOffsetX: ValueCell.create(props.offsetX),
  142. uOffsetY: ValueCell.create(props.offsetY),
  143. uOffsetZ: ValueCell.create(props.offsetZ),
  144. uBackgroundColor: ValueCell.create(Color.toArrayNormalized(props.backgroundColor, Vec3.zero(), 0)),
  145. uBackgroundOpacity: ValueCell.create(props.backgroundOpacity),
  146. }
  147. }
  148. function createValuesSimple(text: Text, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  149. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform)
  150. const p = { ...PD.getDefaultValues(Params), props }
  151. return createValues(text, s.transform, s.locationIterator, s.theme, p)
  152. }
  153. function updateValues(values: TextValues, props: PD.Values<Params>) {
  154. BaseGeometry.updateValues(values, props)
  155. ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor)
  156. ValueCell.updateIfChanged(values.uBorderWidth, props.borderWidth)
  157. if (Color.fromNormalizedArray(values.uBorderColor.ref.value, 0) !== props.borderColor) {
  158. Color.toArrayNormalized(props.borderColor, values.uBorderColor.ref.value, 0)
  159. ValueCell.update(values.uBorderColor, values.uBorderColor.ref.value)
  160. }
  161. ValueCell.updateIfChanged(values.uOffsetX, props.offsetX)
  162. ValueCell.updateIfChanged(values.uOffsetY, props.offsetY)
  163. ValueCell.updateIfChanged(values.uOffsetZ, props.offsetZ)
  164. if (Color.fromNormalizedArray(values.uBackgroundColor.ref.value, 0) !== props.backgroundColor) {
  165. Color.toArrayNormalized(props.backgroundColor, values.uBackgroundColor.ref.value, 0)
  166. ValueCell.update(values.uBackgroundColor, values.uBackgroundColor.ref.value)
  167. }
  168. ValueCell.updateIfChanged(values.uBackgroundOpacity, props.backgroundOpacity)
  169. }
  170. function updateBoundingSphere(values: TextValues, text: Text) {
  171. const padding = getPadding(values.aMapping.ref.value, values.aDepth.ref.value, text.charCount, getMaxSize(values))
  172. const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
  173. values.aPosition.ref.value, text.charCount * 4,
  174. values.aTransform.ref.value, values.instanceCount.ref.value, padding
  175. )
  176. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  177. ValueCell.update(values.boundingSphere, boundingSphere)
  178. }
  179. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  180. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere)
  181. }
  182. ValueCell.update(values.padding, padding)
  183. }
  184. function createRenderableState(props: PD.Values<Params>): RenderableState {
  185. const state = BaseGeometry.createRenderableState(props)
  186. updateRenderableState(state, props)
  187. return state
  188. }
  189. function updateRenderableState(state: RenderableState, props: PD.Values<Params>) {
  190. BaseGeometry.updateRenderableState(state, props)
  191. state.opaque = false
  192. }
  193. }
  194. function getPadding(mappings: Float32Array, depths: Float32Array, charCount: number, maxSize: number) {
  195. let maxOffset = 0
  196. let maxDepth = 0
  197. for (let i = 0, il = charCount * 4; i < il; ++i) {
  198. const i2 = 2 * i
  199. const ox = Math.abs(mappings[i2])
  200. if (ox > maxOffset) maxOffset = ox
  201. const oy = Math.abs(mappings[i2 + 1])
  202. if (oy > maxOffset) maxOffset = oy
  203. const d = Math.abs(depths[i])
  204. if (d > maxDepth) maxDepth = d
  205. }
  206. // console.log(maxDepth + maxSize, maxDepth, maxSize, maxSize + maxSize * maxOffset, depths)
  207. return Math.max(maxDepth, maxSize + maxSize * maxOffset)
  208. // return maxSize + maxSize * maxOffset + maxDepth
  209. }