-
-
Notifications
You must be signed in to change notification settings - Fork 400
docs(XR): add WebXR volume rendering example #2449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| body { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| button { | ||
| position: absolute; | ||
| left: 10px; | ||
| top: 10px; | ||
| width: 200px; | ||
| z-index: 2; | ||
| cursor: pointer; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import 'vtk.js/Sources/favicon'; | ||
|
|
||
| // Load the rendering pieces we want to use (for both WebGL and WebGPU) | ||
| import 'vtk.js/Sources/Rendering/Profiles/Volume'; | ||
|
|
||
| // Force DataAccessHelper to have access to various data source | ||
| import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; | ||
| import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; | ||
|
|
||
| import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction'; | ||
| import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow'; | ||
| import HttpDataAccessHelper from 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; | ||
| import vtkPiecewiseFunction from 'vtk.js/Sources/Common/DataModel/PiecewiseFunction'; | ||
| import vtkURLExtract from 'vtk.js/Sources/Common/Core/URLExtract'; | ||
| import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume'; | ||
| import vtkVolumeMapper from 'vtk.js/Sources/Rendering/Core/VolumeMapper'; | ||
| import vtkXMLImageDataReader from 'vtk.js/Sources/IO/XML/XMLImageDataReader'; | ||
|
|
||
| import './WebXRVolume.module.css'; | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| // Standard rendering code setup | ||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| const background = [0, 0, 0]; | ||
| const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ | ||
| background, | ||
| }); | ||
| const renderer = fullScreenRenderer.getRenderer(); | ||
| const renderWindow = fullScreenRenderer.getRenderWindow(); | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| // Set up pipeline objects | ||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| const vtiReader = vtkXMLImageDataReader.newInstance(); | ||
| const actor = vtkVolume.newInstance(); | ||
| const mapper = vtkVolumeMapper.newInstance(); | ||
| mapper.setInputConnection(vtiReader.getOutputPort()); | ||
| actor.setMapper(mapper); | ||
| renderer.addVolume(actor); | ||
|
|
||
| // create color and opacity transfer functions | ||
| const ctfun = vtkColorTransferFunction.newInstance(); | ||
| const ofun = vtkPiecewiseFunction.newInstance(); | ||
|
|
||
| // ---------------------------------------------------------------------------- | ||
| // Example code | ||
| // ---------------------------------------------------------------------------- | ||
|
|
||
| const { | ||
| fileURL = 'https://data.kitware.com/api/v1/file/59de9dca8d777f31ac641dc2/download', | ||
| } = vtkURLExtract.extractURLParameters(); | ||
|
|
||
| HttpDataAccessHelper.fetchBinary(fileURL).then((fileContents) => { | ||
| // Read data | ||
| vtiReader.parseAsArrayBuffer(fileContents); | ||
| const data = vtiReader.getOutputData(0); | ||
| const dataArray = | ||
| data.getPointData().getScalars() || data.getPointData().getArrays()[0]; | ||
| const dataRange = dataArray.getRange(); | ||
|
|
||
| // Restyle visual appearance | ||
| const sampleDistance = | ||
| 0.7 * | ||
| Math.sqrt( | ||
| data | ||
| .getSpacing() | ||
| .map((v) => v * v) | ||
| .reduce((a, b) => a + b, 0) | ||
| ); | ||
| mapper.setSampleDistance(sampleDistance); | ||
|
|
||
| ctfun.addRGBPoint(dataRange[0], 0.0, 0.3, 0.3); | ||
| ctfun.addRGBPoint(dataRange[1], 1.0, 1.0, 1.0); | ||
| ofun.addPoint(dataRange[0], 0.0); | ||
| ofun.addPoint((dataRange[1] - dataRange[0]) / 4, 0.0); | ||
| ofun.addPoint(dataRange[1], 0.5); | ||
| actor.getProperty().setRGBTransferFunction(0, ctfun); | ||
| actor.getProperty().setScalarOpacity(0, ofun); | ||
| actor.getProperty().setInterpolationTypeToLinear(); | ||
|
|
||
| // Set up rendering | ||
| renderer.resetCamera(); | ||
| renderWindow.render(); | ||
|
|
||
| // Add button to launch AR (default) or VR scene | ||
| const VR = 1; | ||
| const AR = 2; | ||
| let xrSessionType = 0; | ||
| const xrButton = document.createElement('button'); | ||
| let enterText = 'XR not available!'; | ||
| const exitText = 'Exit XR'; | ||
| xrButton.textContent = enterText; | ||
| if ( | ||
| navigator.xr !== undefined && | ||
| fullScreenRenderer.getApiSpecificRenderWindow().getXrSupported() | ||
| ) { | ||
| navigator.xr.isSessionSupported('immersive-ar').then((arSupported) => { | ||
| if (arSupported) { | ||
| xrSessionType = AR; | ||
| enterText = 'Start AR'; | ||
| xrButton.textContent = enterText; | ||
| } else { | ||
| navigator.xr.isSessionSupported('immersive-vr').then((vrSupported) => { | ||
| if (vrSupported) { | ||
| xrSessionType = VR; | ||
| enterText = 'Start VR'; | ||
| xrButton.textContent = enterText; | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| xrButton.addEventListener('click', () => { | ||
| if (xrButton.textContent === enterText) { | ||
| if (xrSessionType === AR) { | ||
| fullScreenRenderer.setBackground([0, 0, 0, 0]); | ||
| } | ||
| fullScreenRenderer | ||
| .getApiSpecificRenderWindow() | ||
| .startXR(xrSessionType === AR); | ||
| xrButton.textContent = exitText; | ||
| } else { | ||
| fullScreenRenderer.setBackground([...background, 255]); | ||
| fullScreenRenderer | ||
| .getApiSpecificRenderWindow() | ||
| .stopXR(xrSessionType === AR); | ||
| xrButton.textContent = enterText; | ||
| } | ||
| }); | ||
| document.querySelector('.content').appendChild(xrButton); | ||
| }); | ||
|
|
||
| // ----------------------------------------------------------- | ||
| // Make some variables global so that you can inspect and | ||
| // modify objects in your browser's developer console: | ||
| // ----------------------------------------------------------- | ||
|
|
||
| global.source = vtiReader; | ||
| global.mapper = mapper; | ||
| global.actor = actor; | ||
| global.ctfun = ctfun; | ||
| global.ofun = ofun; | ||
| global.renderer = renderer; | ||
| global.renderWindow = renderWindow; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
|
|
||
| ### WebXR Volume Viewer | ||
|
|
||
| vtk.js supports volume rendering in virtual and augmented reality environments for WebXR compatible systems. | ||
|
|
||
| This example loads and renders a .vti volume file found [here](https://data.kitware.com/api/v1/file/59de9dca8d777f31ac641dc2/download). Press the "Enter VR" or "Enter AR" button to launch an XR rendering on a WebXR-compatible system. To specify other .vti files, provide the `?fileURL=<link>` URL parameter. | ||
|
|
||
| Volume rendering can be processor-intensive. Smaller volumes may give better performance on mobile devices. | ||
|
|
||
| - [binary-head.vti](https://kitware.github.io/vtk-js/examples/WebXRVolume/WebXRVolume.html?fileURL=https://data.kitware.com/api/v1/file/59de9dca8d777f31ac641dc2/download) 15.6 MB | ||
| - [binary-head-2.vti](https://kitware.github.io/vtk-js/examples/WebXRVolume/WebXRVolume.html?fileURL=https://data.kitware.com/api/v1/file/629921a64acac99f429a45a7/download) 361 kB | ||
| - [tiny-image.vti](https://kitware.github.io/vtk-js/examples/WebXRVolume/WebXRVolume.html?fileURL=https://data.kitware.com/api/v1/file/624320e74acac99f42254a25/download) 1.6 kB | ||
|
|
||
| ### See Also | ||
|
|
||
| [Full list of WebXR Examples](https://kitware.github.io/vtk-js/docs/develop_webxr.html) | ||
| [VolumeViewer Example](https://kitware.github.io/vtk-js/examples/VolumeViewer.html) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.