direct-volume.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { RuntimeContext } from 'mol-task'
  7. import { ValueCell } from 'mol-util'
  8. import { Sphere3D } from 'mol-math/geometry'
  9. import { paramDefaultValues, RangeParam, BooleanParam, SelectParam, TextParam } from 'mol-view/parameter';
  10. import { DirectVolume2dValues } from 'mol-gl/renderable/direct-volume';
  11. import { TextureImage } from 'mol-gl/renderable/util';
  12. import { Vec3, Vec2, Mat4 } from 'mol-math/linear-algebra';
  13. import { Box } from '../../primitive/box';
  14. import { getControlPointsFromString, createTransferFunctionTexture } from './transfer-function';
  15. export interface DirectVolume {
  16. readonly kind: 'direct-volume',
  17. readonly gridDimension: ValueCell<Vec3>,
  18. readonly gridTexture: ValueCell<TextureImage<any>>,
  19. readonly gridTextureDim: ValueCell<Vec2>,
  20. readonly bboxSize: ValueCell<Vec3>
  21. readonly bboxMin: ValueCell<Vec3>
  22. readonly bboxMax: ValueCell<Vec3>
  23. readonly transform: ValueCell<Mat4>
  24. /** Bounding sphere of the volume */
  25. boundingSphere?: Sphere3D
  26. }
  27. const VolumeBox = Box()
  28. const RenderModeOptions = [['isosurface', 'Isosurface'], ['volume', 'Volume']] as [string, string][]
  29. export namespace DirectVolume {
  30. export function createEmpty(directVolume?: DirectVolume): DirectVolume {
  31. // TODO
  32. return {
  33. } as DirectVolume
  34. }
  35. //
  36. export const Params = {
  37. alpha: RangeParam('Opacity', '', 1, 0, 1, 0.01),
  38. visible: BooleanParam('Visible', '', true),
  39. depthMask: BooleanParam('Depth Mask', '', true),
  40. useFog: BooleanParam('Use Fog', '', false),
  41. isoValueAbsolute: RangeParam('Iso Value Absolute', '', 0.22, -1, 1, 0.01),
  42. isoValueRelative: RangeParam('Iso Value Relative', '', 2, -10, 10, 0.1),
  43. renderMode: SelectParam('Render Mode', '', 'volume', RenderModeOptions),
  44. controlPoints: TextParam('Control Points', '', '0.19:0.1, 0.2:0.5, 0.21:0.1, 0.4:0.3'),
  45. }
  46. export const DefaultProps = paramDefaultValues(Params)
  47. export type Props = typeof DefaultProps
  48. export async function createValues(ctx: RuntimeContext, directVolume: DirectVolume, props: Props): Promise<DirectVolume2dValues> {
  49. const { bboxSize, bboxMin, bboxMax, gridDimension, gridTexture, gridTextureDim, transform } = directVolume
  50. const controlPoints = getControlPointsFromString(props.controlPoints)
  51. const transferTex = createTransferFunctionTexture(controlPoints)
  52. return {
  53. drawCount: ValueCell.create(VolumeBox.indices.length),
  54. instanceCount: ValueCell.create(1),
  55. aPosition: ValueCell.create(VolumeBox.vertices as Float32Array),
  56. elements: ValueCell.create(VolumeBox.indices as Uint32Array),
  57. uAlpha: ValueCell.create(props.alpha),
  58. dUseFog: ValueCell.create(props.useFog),
  59. uIsoValue: ValueCell.create(props.isoValueAbsolute),
  60. uBboxMin: bboxMin,
  61. uBboxMax: bboxMax,
  62. uBboxSize: bboxSize,
  63. uTransform: transform,
  64. uGridDim: gridDimension,
  65. uGridTexDim: gridTextureDim,
  66. tGridTex: gridTexture,
  67. dRenderMode: ValueCell.create(props.renderMode),
  68. tTransferTex: transferTex,
  69. }
  70. }
  71. export function updateValues(values: DirectVolume2dValues, props: Props) {
  72. console.log('DirectVolumeValues', props, values)
  73. ValueCell.updateIfChanged(values.uIsoValue, props.isoValueAbsolute)
  74. ValueCell.updateIfChanged(values.uAlpha, props.alpha)
  75. ValueCell.updateIfChanged(values.dUseFog, props.useFog)
  76. ValueCell.updateIfChanged(values.dRenderMode, props.renderMode)
  77. const controlPoints = getControlPointsFromString(props.controlPoints)
  78. createTransferFunctionTexture(controlPoints, values.tTransferTex)
  79. }
  80. }