I wrote the following code to be able to load the VTK file, but the page is white and the geometry is not visible.
Also I get this message in the console:“VTK container ref is not defined.”
import React, { useEffect, useRef, useState } from 'react';
import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow';
import vtkHttpDataSetReader from 'vtk.js/Sources/IO/Core/HttpDataSetReader';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';
function VTKViewer() {
const vtkContainerRef = useRef(null);
const [isMounted, setIsMounted] = useState(false);
useEffect(() => {
if (!vtkContainerRef.current) {
console.warn('VTK container ref is not defined.');
return;
}
setIsMounted(true);
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({
rootContainer: vtkContainerRef.current,
containerStyle: { height: '100%', width: '100%' },
});
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
const actor = vtkActor.newInstance();
const mapper = vtkMapper.newInstance();
actor.setMapper(mapper);
const reader = vtkHttpDataSetReader.newInstance({ fetchGzip: true });
mapper.setInputConnection(reader.getOutputPort());
reader.setUrl('/home/sample/test.vtk').then(() => {
renderer.addActor(actor);
renderer.resetCamera();
renderWindow.render();
});
return () => {
fullScreenRenderer.delete();
};
}, []);
return isMounted ? <div ref={vtkContainerRef} style={{ width: '100%', height: '100%' }} /> : null;
}
export default VTKViewer;