|
| 1 | +import 'vtk.js/Sources/favicon'; |
| 2 | + |
| 3 | +// Load the rendering pieces we want to use (for both WebGL and WebGPU) |
| 4 | +import 'vtk.js/Sources/Rendering/Profiles/Volume'; |
| 5 | + |
| 6 | +// Force DataAccessHelper to have access to various data source |
| 7 | +import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; |
| 8 | +import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; |
| 9 | + |
| 10 | +import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction'; |
| 11 | +import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow'; |
| 12 | +import HttpDataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; |
| 13 | +import vtkPiecewiseFunction from 'vtk.js/Sources/Common/DataModel/PiecewiseFunction'; |
| 14 | +import vtkURLExtract from 'vtk.js/Sources/Common/Core/URLExtract'; |
| 15 | +import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume'; |
| 16 | +import vtkVolumeMapper from 'vtk.js/Sources/Rendering/Core/VolumeMapper'; |
| 17 | +import vtkXMLImageDataReader from 'vtk.js/Sources/IO/XML/XMLImageDataReader'; |
| 18 | + |
| 19 | +import './WebXRVolume.module.css'; |
| 20 | + |
| 21 | +// ---------------------------------------------------------------------------- |
| 22 | +// Standard rendering code setup |
| 23 | +// ---------------------------------------------------------------------------- |
| 24 | + |
| 25 | +const background = [0, 0, 0]; |
| 26 | +const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ |
| 27 | + background, |
| 28 | +}); |
| 29 | +const renderer = fullScreenRenderer.getRenderer(); |
| 30 | +const renderWindow = fullScreenRenderer.getRenderWindow(); |
| 31 | + |
| 32 | +// ---------------------------------------------------------------------------- |
| 33 | +// Set up pipeline objects |
| 34 | +// ---------------------------------------------------------------------------- |
| 35 | + |
| 36 | +const vtiReader = vtkXMLImageDataReader.newInstance(); |
| 37 | +const actor = vtkVolume.newInstance(); |
| 38 | +const mapper = vtkVolumeMapper.newInstance(); |
| 39 | +mapper.setInputConnection(vtiReader.getOutputPort()); |
| 40 | +actor.setMapper(mapper); |
| 41 | +renderer.addVolume(actor); |
| 42 | + |
| 43 | +// create color and opacity transfer functions |
| 44 | +const ctfun = vtkColorTransferFunction.newInstance(); |
| 45 | +const ofun = vtkPiecewiseFunction.newInstance(); |
| 46 | + |
| 47 | +// ---------------------------------------------------------------------------- |
| 48 | +// Example code |
| 49 | +// ---------------------------------------------------------------------------- |
| 50 | + |
| 51 | +const { |
| 52 | + fileURL = 'https://data.kitware.com/api/v1/file/59de9dca8d777f31ac641dc2/download', |
| 53 | +} = vtkURLExtract.extractURLParameters(); |
| 54 | + |
| 55 | +HttpDataAccessHelper.fetchBinary(fileURL).then((fileContents) => { |
| 56 | + // Read data |
| 57 | + vtiReader.parseAsArrayBuffer(fileContents); |
| 58 | + const data = vtiReader.getOutputData(0); |
| 59 | + const dataArray = |
| 60 | + data.getPointData().getScalars() || data.getPointData().getArrays()[0]; |
| 61 | + const dataRange = dataArray.getRange(); |
| 62 | + |
| 63 | + // Restyle visual appearance |
| 64 | + const sampleDistance = |
| 65 | + 0.7 * |
| 66 | + Math.sqrt( |
| 67 | + data |
| 68 | + .getSpacing() |
| 69 | + .map((v) => v * v) |
| 70 | + .reduce((a, b) => a + b, 0) |
| 71 | + ); |
| 72 | + mapper.setSampleDistance(sampleDistance); |
| 73 | + |
| 74 | + ctfun.addRGBPoint(dataRange[0], 0.0, 0.3, 0.3); |
| 75 | + ctfun.addRGBPoint(dataRange[1], 1.0, 1.0, 1.0); |
| 76 | + ofun.addPoint(dataRange[0], 0.0); |
| 77 | + ofun.addPoint((dataRange[1] - dataRange[0]) / 4, 0.0); |
| 78 | + ofun.addPoint(dataRange[1], 0.5); |
| 79 | + actor.getProperty().setRGBTransferFunction(0, ctfun); |
| 80 | + actor.getProperty().setScalarOpacity(0, ofun); |
| 81 | + actor.getProperty().setInterpolationTypeToLinear(); |
| 82 | + |
| 83 | + // Set up rendering |
| 84 | + renderer.resetCamera(); |
| 85 | + renderWindow.render(); |
| 86 | + |
| 87 | + // Add button to launch AR (default) or VR scene |
| 88 | + const VR = 1; |
| 89 | + const AR = 2; |
| 90 | + let xrSessionType = 0; |
| 91 | + const xrButton = document.createElement('button'); |
| 92 | + let enterText = 'XR not available!'; |
| 93 | + const exitText = 'Exit XR'; |
| 94 | + xrButton.textContent = enterText; |
| 95 | + if ( |
| 96 | + navigator.xr !== undefined && |
| 97 | + fullScreenRenderer.getApiSpecificRenderWindow().getXrSupported() |
| 98 | + ) { |
| 99 | + navigator.xr.isSessionSupported('immersive-ar').then((arSupported) => { |
| 100 | + if (arSupported) { |
| 101 | + xrSessionType = AR; |
| 102 | + enterText = 'Start AR'; |
| 103 | + xrButton.textContent = enterText; |
| 104 | + } else { |
| 105 | + navigator.xr.isSessionSupported('immersive-vr').then((vrSupported) => { |
| 106 | + if (vrSupported) { |
| 107 | + xrSessionType = VR; |
| 108 | + enterText = 'Start VR'; |
| 109 | + xrButton.textContent = enterText; |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + xrButton.addEventListener('click', () => { |
| 116 | + if (xrButton.textContent === enterText) { |
| 117 | + if (xrSessionType === AR) { |
| 118 | + fullScreenRenderer.setBackground([0, 0, 0, 0]); |
| 119 | + } |
| 120 | + fullScreenRenderer |
| 121 | + .getApiSpecificRenderWindow() |
| 122 | + .startXR(xrSessionType === AR); |
| 123 | + xrButton.textContent = exitText; |
| 124 | + } else { |
| 125 | + fullScreenRenderer.setBackground([...background, 255]); |
| 126 | + fullScreenRenderer |
| 127 | + .getApiSpecificRenderWindow() |
| 128 | + .stopXR(xrSessionType === AR); |
| 129 | + xrButton.textContent = enterText; |
| 130 | + } |
| 131 | + }); |
| 132 | + document.querySelector('.content').appendChild(xrButton); |
| 133 | +}); |
| 134 | + |
| 135 | +// ----------------------------------------------------------- |
| 136 | +// Make some variables global so that you can inspect and |
| 137 | +// modify objects in your browser's developer console: |
| 138 | +// ----------------------------------------------------------- |
| 139 | + |
| 140 | +global.source = vtiReader; |
| 141 | +global.mapper = mapper; |
| 142 | +global.actor = actor; |
| 143 | +global.ctfun = ctfun; |
| 144 | +global.ofun = ofun; |
| 145 | +global.renderer = renderer; |
| 146 | +global.renderWindow = renderWindow; |
0 commit comments