|
| 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/Geometry'; |
| 5 | + |
| 6 | +import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor'; |
| 7 | +import vtkCalculator from 'vtk.js/Sources/Filters/General/Calculator'; |
| 8 | +import vtkConeSource from 'vtk.js/Sources/Filters/Sources/ConeSource'; |
| 9 | +import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow'; |
| 10 | +import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper'; |
| 11 | +import { AttributeTypes } from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants'; |
| 12 | +import { FieldDataTypes } from 'vtk.js/Sources/Common/DataModel/DataSet/Constants'; |
| 13 | + |
| 14 | +// Force DataAccessHelper to have access to various data source |
| 15 | +import 'vtk.js/Sources/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; |
| 16 | +import 'vtk.js/Sources/IO/Core/DataAccessHelper/HttpDataAccessHelper'; |
| 17 | +import 'vtk.js/Sources/IO/Core/DataAccessHelper/JSZipDataAccessHelper'; |
| 18 | + |
| 19 | +import controlPanel from './controller.html'; |
| 20 | + |
| 21 | +// ---------------------------------------------------------------------------- |
| 22 | +// Standard rendering code setup |
| 23 | +// ---------------------------------------------------------------------------- |
| 24 | + |
| 25 | +const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ |
| 26 | + background: [0, 0, 0, 255], |
| 27 | +}); |
| 28 | +const renderer = fullScreenRenderer.getRenderer(); |
| 29 | +const renderWindow = fullScreenRenderer.getRenderWindow(); |
| 30 | + |
| 31 | +// ---------------------------------------------------------------------------- |
| 32 | +// Example code |
| 33 | +// ---------------------------------------------------------------------------- |
| 34 | +// create a filter on the fly, sort of cool, this is a random scalars |
| 35 | +// filter we create inline, for a simple cone you would not need |
| 36 | +// this |
| 37 | +// ---------------------------------------------------------------------------- |
| 38 | + |
| 39 | +const coneSource = vtkConeSource.newInstance({ height: 100, radius: 50 }); |
| 40 | +const filter = vtkCalculator.newInstance(); |
| 41 | + |
| 42 | +filter.setInputConnection(coneSource.getOutputPort()); |
| 43 | + |
| 44 | +filter.setFormula({ |
| 45 | + getArrays: (inputDataSets) => ({ |
| 46 | + input: [], |
| 47 | + output: [ |
| 48 | + { |
| 49 | + location: FieldDataTypes.CELL, |
| 50 | + name: 'Random', |
| 51 | + dataType: 'Float32Array', |
| 52 | + attribute: AttributeTypes.SCALARS, |
| 53 | + }, |
| 54 | + ], |
| 55 | + }), |
| 56 | + evaluate: (arraysIn, arraysOut) => { |
| 57 | + const [scalars] = arraysOut.map((d) => d.getData()); |
| 58 | + for (let i = 0; i < scalars.length; i++) { |
| 59 | + scalars[i] = Math.random(); |
| 60 | + } |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | +const mapper = vtkMapper.newInstance(); |
| 65 | +mapper.setInputConnection(filter.getOutputPort()); |
| 66 | + |
| 67 | +const actor = vtkActor.newInstance(); |
| 68 | +actor.setMapper(mapper); |
| 69 | +actor.setPosition(0.0, 0.0, -20.0); |
| 70 | + |
| 71 | +renderer.addActor(actor); |
| 72 | +renderer.resetCamera(); |
| 73 | +renderWindow.render(); |
| 74 | + |
| 75 | +// ----------------------------------------------------------- |
| 76 | +// UI control handling |
| 77 | +// ----------------------------------------------------------- |
| 78 | + |
| 79 | +fullScreenRenderer.addController(controlPanel); |
| 80 | +const arbutton = document.querySelector('.arbutton'); |
| 81 | + |
| 82 | +const SESSION_IS_AR = true; |
| 83 | +arbutton.addEventListener('click', (e) => { |
| 84 | + if (arbutton.textContent === 'Start AR') { |
| 85 | + fullScreenRenderer.setBackground([0, 0, 0, 0]); |
| 86 | + fullScreenRenderer.getApiSpecificRenderWindow().startXR(SESSION_IS_AR); |
| 87 | + arbutton.textContent = 'Exit AR'; |
| 88 | + } else { |
| 89 | + fullScreenRenderer.setBackground([0, 0, 0, 255]); |
| 90 | + fullScreenRenderer.getApiSpecificRenderWindow().stopXR(SESSION_IS_AR); |
| 91 | + arbutton.textContent = 'Start AR'; |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +// ----------------------------------------------------------- |
| 96 | +// Make some variables global so that you can inspect and |
| 97 | +// modify objects in your browser's developer console: |
| 98 | +// ----------------------------------------------------------- |
| 99 | + |
| 100 | +global.source = coneSource; |
| 101 | +global.mapper = mapper; |
| 102 | +global.actor = actor; |
| 103 | +global.renderer = renderer; |
| 104 | +global.renderWindow = renderWindow; |
0 commit comments