image.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 { WebGLContext } from '../../mol-gl/webgl/context';
  7. import { RenderTarget } from '../../mol-gl/webgl/render-target';
  8. import { Renderer } from '../../mol-gl/renderer';
  9. import { Scene } from '../../mol-gl/scene';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { DrawPass } from './draw';
  12. import { PostprocessingParams } from './postprocessing';
  13. import { MultiSamplePass, MultiSampleParams, MultiSampleHelper } from './multi-sample';
  14. import { Camera } from '../camera';
  15. import { Viewport } from '../camera/util';
  16. import { PixelData } from '../../mol-util/image';
  17. import { Helper } from '../helper/helper';
  18. import { CameraHelper, CameraHelperParams } from '../helper/camera-helper';
  19. export const ImageParams = {
  20. transparentBackground: PD.Boolean(false),
  21. multiSample: PD.Group(MultiSampleParams),
  22. postprocessing: PD.Group(PostprocessingParams),
  23. cameraHelper: PD.Group(CameraHelperParams),
  24. };
  25. export type ImageProps = PD.Values<typeof ImageParams>
  26. export class ImagePass {
  27. private _width = 0
  28. private _height = 0
  29. private _camera = new Camera()
  30. readonly props: ImageProps
  31. private _colorTarget: RenderTarget
  32. get colorTarget() { return this._colorTarget; }
  33. private readonly drawPass: DrawPass
  34. private readonly multiSamplePass: MultiSamplePass
  35. private readonly multiSampleHelper: MultiSampleHelper
  36. private readonly helper: Helper
  37. get width() { return this._width; }
  38. get height() { return this._height; }
  39. constructor(private webgl: WebGLContext, private renderer: Renderer, private scene: Scene, private camera: Camera, helper: Helper, enableWboit: boolean, props: Partial<ImageProps>) {
  40. this.props = { ...PD.getDefaultValues(ImageParams), ...props };
  41. this.drawPass = new DrawPass(webgl, 128, 128, enableWboit);
  42. this.multiSamplePass = new MultiSamplePass(webgl, this.drawPass);
  43. this.multiSampleHelper = new MultiSampleHelper(this.multiSamplePass);
  44. this.helper = {
  45. camera: new CameraHelper(webgl, this.props.cameraHelper),
  46. debug: helper.debug,
  47. handle: helper.handle,
  48. };
  49. this.setSize(1024, 768);
  50. }
  51. setSize(width: number, height: number) {
  52. if (width === this._width && height === this._height) return;
  53. this._width = width;
  54. this._height = height;
  55. this.drawPass.setSize(width, height);
  56. this.multiSamplePass.syncSize();
  57. }
  58. setProps(props: Partial<ImageProps> = {}) {
  59. Object.assign(this.props, props);
  60. if (props.cameraHelper) this.helper.camera.setProps(props.cameraHelper);
  61. }
  62. render() {
  63. Camera.copySnapshot(this._camera.state, this.camera.state);
  64. Viewport.set(this._camera.viewport, 0, 0, this._width, this._height);
  65. this._camera.update();
  66. if (MultiSamplePass.isEnabled(this.props.multiSample)) {
  67. this.multiSampleHelper.render(this.renderer, this._camera, this.scene, this.helper, false, this.props.transparentBackground, this.props);
  68. this._colorTarget = this.multiSamplePass.colorTarget;
  69. } else {
  70. this.drawPass.render(this.renderer, this._camera, this.scene, this.helper, false, this.props.transparentBackground, this.props.postprocessing);
  71. this._colorTarget = this.drawPass.getColorTarget(this.props.postprocessing);
  72. }
  73. }
  74. getImageData(width: number, height: number, viewport?: Viewport) {
  75. this.setSize(width, height);
  76. this.render();
  77. this.colorTarget.bind();
  78. const w = viewport?.width ?? width, h = viewport?.height ?? height;
  79. const array = new Uint8Array(w * h * 4);
  80. if (!viewport) {
  81. this.webgl.readPixels(0, 0, w, h, array);
  82. } else {
  83. this.webgl.readPixels(viewport.x, height - viewport.y - viewport.height, w, h, array);
  84. }
  85. PixelData.flipY({ array, width: w, height: h });
  86. return new ImageData(new Uint8ClampedArray(array), w, h);
  87. }
  88. }