All files / Sources/Rendering/OpenGL/ForwardPass index.js

97.22% Statements 70/72
87.87% Branches 29/33
100% Functions 9/9
97.01% Lines 65/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164                  204x           204x 426x         426x     426x 426x   426x     426x 426x 439x 482x 482x   482x   456x 456x 456x 456x 456x   456x     456x         13x   13x 4x   13x 13x 13x 13x         4x 4x   13x       13x 13x 13x     13x     456x 456x 456x 306x 306x   456x 9x 6x     9x   456x 18x 18x   456x 4x 4x             204x 3x 3x         204x 10x     3879x 204x 27x 204x 204x             1x                       204x     204x   204x               204x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkOpenGLFramebuffer from 'vtk.js/Sources/Rendering/OpenGL/Framebuffer';
import vtkRenderPass from 'vtk.js/Sources/Rendering/SceneGraph/RenderPass';
import vtkOpenGLOrderIndependentTranslucentPass from 'vtk.js/Sources/Rendering/OpenGL/OrderIndependentTranslucentPass';
 
// ----------------------------------------------------------------------------
 
function vtkForwardPass(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkForwardPass');
 
  // this pass implements a forward rendering pipeline
  // if both volumes and opaque geometry are present
  // it will mix the two together by capturing a zbuffer
  // first
  publicAPI.traverse = (viewNode, parent = null) => {
    Iif (model.deleted) {
      return;
    }
 
    // we just render our delegates in order
    model._currentParent = parent;
 
    // build
    publicAPI.setCurrentOperation('buildPass');
    viewNode.traverse(publicAPI);
 
    const numlayers = viewNode.getRenderable().getNumberOfLayers();
 
    // iterate over renderers
    const renderers = viewNode.getRenderable().getRenderersByReference();
    for (let i = 0; i < numlayers; i++) {
      for (let index = 0; index < renderers.length; index++) {
        const ren = renderers[index];
        const renNode = viewNode.getViewNodeFor(ren);
 
        if (ren.getDraw() && ren.getLayer() === i) {
          // check for both opaque and volume actors
          model.opaqueActorCount = 0;
          model.translucentActorCount = 0;
          model.volumeCount = 0;
          model.overlayActorCount = 0;
          publicAPI.setCurrentOperation('queryPass');
 
          renNode.traverse(publicAPI);
 
          // do we need to capture a zbuffer?
          if (
            ((model.opaqueActorCount > 0 || model.translucentActorCount > 0) &&
              model.volumeCount > 0) ||
            model.depthRequested
          ) {
            const size = viewNode.getFramebufferSize();
            // make sure the framebuffer is setup
            if (model.framebuffer === null) {
              model.framebuffer = vtkOpenGLFramebuffer.newInstance();
            }
            model.framebuffer.setOpenGLRenderWindow(viewNode);
            model.framebuffer.saveCurrentBindingsAndBuffers();
            const fbSize = model.framebuffer.getSize();
            if (
              fbSize === null ||
              fbSize[0] !== size[0] ||
              fbSize[1] !== size[1]
            ) {
              model.framebuffer.create(size[0], size[1]);
              model.framebuffer.populateFramebuffer();
            }
            model.framebuffer.bind();
            // opaqueZBufferPass only renders opaque actors
            // zBufferPass renders both translucent and opaque actors
            // we want to be able to pick translucent actors
            publicAPI.setCurrentOperation('zBufferPass');
            renNode.traverse(publicAPI);
            model.framebuffer.restorePreviousBindingsAndBuffers();
 
            // reset now that we have done it
            model.depthRequested = false;
          }
 
          publicAPI.setCurrentOperation('cameraPass');
          renNode.traverse(publicAPI);
          if (model.opaqueActorCount > 0) {
            publicAPI.setCurrentOperation('opaquePass');
            renNode.traverse(publicAPI);
          }
          if (model.translucentActorCount > 0) {
            if (!model.translucentPass) {
              model.translucentPass =
                vtkOpenGLOrderIndependentTranslucentPass.newInstance();
            }
            model.translucentPass.traverse(viewNode, renNode, publicAPI);
          }
          if (model.volumeCount > 0) {
            publicAPI.setCurrentOperation('volumePass');
            renNode.traverse(publicAPI);
          }
          if (model.overlayActorCount > 0) {
            publicAPI.setCurrentOperation('overlayPass');
            renNode.traverse(publicAPI);
          }
        }
      }
    }
  };
 
  publicAPI.getZBufferTexture = () => {
    if (model.framebuffer) {
      return model.framebuffer.getColorTexture();
    }
    return null;
  };
 
  publicAPI.requestDepth = () => {
    model.depthRequested = true;
  };
 
  publicAPI.incrementOpaqueActorCount = () => model.opaqueActorCount++;
  publicAPI.incrementTranslucentActorCount = () =>
    model.translucentActorCount++;
  publicAPI.incrementVolumeCount = () => model.volumeCount++;
  publicAPI.incrementOverlayActorCount = () => model.overlayActorCount++;
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  opaqueActorCount: 0,
  translucentActorCount: 0,
  volumeCount: 0,
  overlayActorCount: 0,
  framebuffer: null,
  depthRequested: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  vtkRenderPass.extend(publicAPI, model, initialValues);
 
  macro.get(publicAPI, model, [
    'framebuffer',
    'opaqueActorCount',
    'translucentActorCount',
    'volumeCount',
  ]);
 
  // Object methods
  vtkForwardPass(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkForwardPass');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };