Advertisement
Guest User

Untitled

a guest
Jul 17th, 2020
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useRef, useEffect, useState} from 'react'
  2.  
  3. import GenericRenderWindow from 'vtk.js/Sources/Rendering/Misc/GenericRenderWindow'
  4.  
  5. import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
  6. import vtkCubeSource from 'vtk.js/Sources/Filters/Sources/CubeSource';
  7. import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
  8.  
  9. export default () => {
  10.     const container = useRef(null)
  11.  
  12.     const [pipelines, setPipelines] = useState([])
  13.  
  14.     const genericRenderWindow = useRef()
  15.     const renderer = useRef()
  16.     const renderWindow = useRef()
  17.  
  18.     const [width, setWidth] = useState(window.innerWidth * 0.8)
  19.     const [height, setHeight] = useState(window.innerHeight - 200)
  20.  
  21.     useEffect(()=> {
  22.         genericRenderWindow.current = GenericRenderWindow.newInstance({
  23.             background: [0.32, 0.34, 0.43],
  24.             listenWindowResize: true,
  25.             rootContainer: container.current,
  26.         })
  27.         renderer.current = genericRenderWindow.current.getRenderer();
  28.         renderWindow.current = genericRenderWindow.current.getRenderWindow();
  29.         genericRenderWindow.current.setContainer(container.current)
  30.         genericRenderWindow.current.resize()
  31.  
  32.         window.addEventListener('resize', updateSize)
  33.     },[])
  34.  
  35.     useEffect(() => {
  36.         renderer.current.removeAllActors()
  37.         pipelines.forEach(p => {
  38.             renderer.current.addActor(p.actor)
  39.         });
  40.         renderer.current.getActors() // <- Strange behaviour here
  41.         renderer.current.resetCamera();
  42.         renderer.current.resetCameraClippingRange();
  43.         renderWindow.current.render();
  44.     },[pipelines])
  45.  
  46.     const updateSize = () => {
  47.         setWidth(window.innerWidth * 0.8)
  48.         setHeight(window.innerHeight - 200)
  49.         genericRenderWindow.current.resize()
  50.     }
  51.  
  52.     function createCubePipeline() {
  53.         const cubeSource = vtkCubeSource.newInstance();
  54.         const actor = vtkActor.newInstance();
  55.         const mapper = vtkMapper.newInstance();
  56.  
  57.         mapper.setInputConnection(cubeSource.getOutputPort());
  58.         actor.getProperty().setColor(1, 0, 0);
  59.         actor.setMapper(mapper);
  60.        
  61.         return { cubeSource, mapper, actor };
  62.     }
  63.  
  64.     const addPipeline = pipeline => {
  65.         setPipelines(prev => ([...prev, pipeline]))
  66.     }
  67.  
  68.     const clearPipelines = () => {
  69.         setPipelines([])
  70.     }
  71.  
  72.     return (
  73.         <>
  74.             <div style={{width: width, height: height - 20}} ref={container}/>
  75.             <button onClick={() => addPipeline(createCubePipeline())}>Press</button>
  76.             <button onClick={clearPipelines}>Clear</button>
  77.         </>
  78.     )
  79. }
Advertisement
RAW Paste Data Copied
Advertisement