All files / Sources/Rendering/WebGPU/Camera index.js

4.05% Statements 3/74
0% Branches 0/9
0% Functions 0/5
4.05% Lines 3/74

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 165 166 167 168 169 170 171 172 173 174 175 176 177                                                                                                                                                                                                                                                                          1x                                                                     1x             1x  
import { mat4 } from 'gl-matrix';
 
import macro from 'vtk.js/Sources/macros';
import vtkViewNode from 'vtk.js/Sources/Rendering/SceneGraph/ViewNode';
 
import { registerOverride } from 'vtk.js/Sources/Rendering/WebGPU/ViewNodeFactory';
 
// ----------------------------------------------------------------------------
// vtkWebGPUCamera methods
// ----------------------------------------------------------------------------
 
function vtkWebGPUCamera(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkWebGPUCamera');
 
  publicAPI.getProjectionMatrix = (outMat, aspect, cRange, windowCenter) => {
    mat4.identity(outMat);
    if (model.renderable.getParallelProjection()) {
      // set up a rectangular parallelipiped
      const parallelScale = model.renderable.getParallelScale();
      const width = parallelScale * aspect;
      const height = parallelScale;
 
      const xmin = (windowCenter[0] - 1.0) * width;
      const xmax = (windowCenter[0] + 1.0) * width;
      const ymin = (windowCenter[1] - 1.0) * height;
      const ymax = (windowCenter[1] + 1.0) * height;
 
      const xr = 1.0 / (xmax - xmin);
      const yr = 1.0 / (ymax - ymin);
      outMat[0] = 2.0 * xr;
      outMat[5] = 2.0 * yr;
      outMat[10] = 1.0 / (cRange[1] - cRange[0]);
      outMat[12] = (xmax + xmin) * xr;
      outMat[13] = (ymax + ymin) * yr;
      outMat[14] = cRange[1] / (cRange[1] - cRange[0]);
    } else {
      const tmp = Math.tan((Math.PI * model.renderable.getViewAngle()) / 360.0);
      let width;
      let height;
      if (model.renderable.getUseHorizontalViewAngle() === true) {
        width = cRange[0] * tmp;
        height = (cRange[0] * tmp) / aspect;
      } else {
        width = cRange[0] * tmp * aspect;
        height = cRange[0] * tmp;
      }
 
      const xmin = (windowCenter[0] - 1.0) * width;
      const xmax = (windowCenter[0] + 1.0) * width;
      const ymin = (windowCenter[1] - 1.0) * height;
      const ymax = (windowCenter[1] + 1.0) * height;
 
      outMat[0] = (2.0 * cRange[0]) / (xmax - xmin);
      outMat[5] = (2.0 * cRange[0]) / (ymax - ymin);
      outMat[12] = (xmin + xmax) / (xmax - xmin);
      outMat[13] = (ymin + ymax) / (ymax - ymin);
      outMat[10] = 0.0;
      outMat[14] = cRange[0];
      outMat[11] = -1.0;
      outMat[15] = 0.0;
    }
  };
 
  publicAPI.convertToOpenGLDepth = (val) => {
    if (model.renderable.getParallelProjection()) {
      return 1.0 - val;
    }
    const cRange = model.renderable.getClippingRangeByReference();
    let zval = -cRange[0] / val;
    zval =
      (cRange[0] + cRange[1]) / (cRange[1] - cRange[0]) +
      (2.0 * cRange[0] * cRange[1]) / (zval * (cRange[1] - cRange[0]));
    return 0.5 * zval + 0.5;
  };
 
  publicAPI.getKeyMatrices = (webGPURenderer) => {
    // has the camera changed?
    const ren = webGPURenderer.getRenderable();
    const webGPURenderWindow = webGPURenderer.getParent();
    if (
      Math.max(
        webGPURenderWindow.getMTime(),
        publicAPI.getMTime(),
        ren.getMTime(),
        model.renderable.getMTime(),
        webGPURenderer.getStabilizedTime()
      ) > model.keyMatrixTime.getMTime()
    ) {
      const wcvc = model.renderable.getViewMatrix();
 
      mat4.copy(model.keyMatrices.normalMatrix, wcvc);
      // zero out translation
      model.keyMatrices.normalMatrix[3] = 0.0;
      model.keyMatrices.normalMatrix[7] = 0.0;
      model.keyMatrices.normalMatrix[11] = 0.0;
      mat4.invert(
        model.keyMatrices.normalMatrix,
        model.keyMatrices.normalMatrix
      );
      mat4.transpose(model.keyMatrices.wcvc, wcvc);
 
      const center = webGPURenderer.getStabilizedCenterByReference();
      mat4.translate(model.keyMatrices.scvc, model.keyMatrices.wcvc, center);
 
      const aspectRatio = webGPURenderer.getAspectRatio();
 
      const cRange = model.renderable.getClippingRangeByReference();
      publicAPI.getProjectionMatrix(
        model.keyMatrices.vcpc,
        aspectRatio,
        cRange,
        model.renderable.getWindowCenterByReference()
      );
 
      mat4.multiply(
        model.keyMatrices.scpc,
        model.keyMatrices.vcpc,
        model.keyMatrices.scvc
      );
 
      mat4.invert(model.keyMatrices.pcsc, model.keyMatrices.scpc);
 
      model.keyMatrixTime.modified();
    }
    return model.keyMatrices;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  keyMatrixTime: null,
  keyMatrices: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkViewNode.extend(publicAPI, model, initialValues);
 
  model.keyMatrixTime = {};
  macro.obj(model.keyMatrixTime);
 
  // values always get set by the get method
  model.keyMatrices = {
    normalMatrix: new Float64Array(16),
    vcpc: new Float64Array(16),
    pcsc: new Float64Array(16),
    wcvc: new Float64Array(16),
    scpc: new Float64Array(16),
    scvc: new Float64Array(16),
  };
 
  // Build VTK API
  macro.setGet(publicAPI, model, ['keyMatrixTime']);
 
  // Object methods
  vtkWebGPUCamera(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };
 
// Register ourself to WebGPU backend if imported
registerOverride('vtkCamera', newInstance);