|
| 1 | +import '@kitware/vtk.js/favicon'; |
| 2 | + |
| 3 | +// Load the rendering pieces we want to use (for both WebGL and WebGPU) |
| 4 | +import '@kitware/vtk.js/Rendering/Profiles/Geometry'; |
| 5 | + |
| 6 | +import '@kitware/vtk.js/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + zip |
| 7 | + |
| 8 | +import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray'; |
| 9 | +import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; |
| 10 | +import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; |
| 11 | +import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; |
| 12 | +import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture'; |
| 13 | +import vtkOBJWriter from '@kitware/vtk.js/IO/Misc/OBJWriter'; |
| 14 | +import vtkCubeSource from '@kitware/vtk.js/Filters/Sources/CubeSource'; |
| 15 | + |
| 16 | +// ---------------------------------------------------------------------------- |
| 17 | +// Standard rendering code setup |
| 18 | +// ---------------------------------------------------------------------------- |
| 19 | + |
| 20 | +const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance(); |
| 21 | +const renderer = fullScreenRenderer.getRenderer(); |
| 22 | +const renderWindow = fullScreenRenderer.getRenderWindow(); |
| 23 | + |
| 24 | +const resetCamera = renderer.resetCamera; |
| 25 | +const render = renderWindow.render; |
| 26 | + |
| 27 | +// Create a colored texture for each face of the cube |
| 28 | +const canvas = document.createElement('canvas'); |
| 29 | +canvas.width = 256; |
| 30 | +canvas.height = 256; |
| 31 | +document.body.appendChild(canvas); |
| 32 | +const ctx = canvas.getContext('2d'); |
| 33 | + |
| 34 | +// Define colors for each face (order: +X, -X, +Y, -Y, +Z, -Z) |
| 35 | +const faceColors = [ |
| 36 | + '#FF0000', // +X: Red |
| 37 | + '#00FF00', // -X: Green |
| 38 | + '#0000FF', // +Y: Blue |
| 39 | + '#FFFF00', // -Y: Yellow |
| 40 | + '#00FFFF', // +Z: Cyan |
| 41 | + '#FF00FF', // -Z: Magenta |
| 42 | +]; |
| 43 | + |
| 44 | +// Draw each face as a square in a 3x2 grid |
| 45 | +const faceSize = 128; |
| 46 | +for (let i = 0; i < 6; i++) { |
| 47 | + const x = (i % 3) * faceSize; |
| 48 | + const y = Math.floor(i / 3) * faceSize; |
| 49 | + ctx.fillStyle = faceColors[i]; |
| 50 | + ctx.fillRect(x, y, faceSize, faceSize); |
| 51 | +} |
| 52 | + |
| 53 | +function callback(source, tex) { |
| 54 | + const writer = vtkOBJWriter.newInstance(); |
| 55 | + writer.setInputData(source.getOutputData()); |
| 56 | + writer.setTexture(tex); |
| 57 | + |
| 58 | + // const objContent = writer.getOutputData(); |
| 59 | + // const mtlContent = writer.getMtl(); |
| 60 | + |
| 61 | + const zip = writer.exportAsZip(); |
| 62 | + zip.then((zipData) => { |
| 63 | + const blob = new Blob([zipData], { type: 'application/zip' }); |
| 64 | + const url = URL.createObjectURL(blob); |
| 65 | + const link = document.createElement('a'); |
| 66 | + link.href = url; |
| 67 | + link.text = 'Download'; |
| 68 | + link.style.position = 'absolute'; |
| 69 | + link.style.left = '50%'; |
| 70 | + link.style.bottom = '10px'; |
| 71 | + link.style.background = 'white'; |
| 72 | + link.style.padding = '5px'; |
| 73 | + global.writer = writer; |
| 74 | + document.body.appendChild(link); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +// Create a cube source |
| 79 | +const cubeSource = vtkCubeSource.newInstance({ |
| 80 | + xLength: 1, |
| 81 | + yLength: 1, |
| 82 | + zLength: 1, |
| 83 | +}); |
| 84 | + |
| 85 | +// Create vtkTexture from canvas |
| 86 | +const texture = vtkTexture.newInstance(); |
| 87 | +const image = new Image(); |
| 88 | +image.src = canvas.toDataURL(); |
| 89 | +image.onload = () => { |
| 90 | + texture.setImage(image); |
| 91 | + callback(cubeSource, texture); |
| 92 | + render(); |
| 93 | +}; |
| 94 | + |
| 95 | +// Add tcoords to the cube for proper texture mapping |
| 96 | +const addCubeTCoords = (polyData) => { |
| 97 | + const points = polyData.getPoints().getData(); |
| 98 | + const numPoints = points.length / 3; |
| 99 | + const tcoords = new Float32Array(numPoints * 2); |
| 100 | + |
| 101 | + // Map each face to a region of the texture |
| 102 | + const polys = polyData.getPolys().getData(); |
| 103 | + const faces = []; |
| 104 | + let i = 0; |
| 105 | + while (i < polys.length) { |
| 106 | + const n = polys[i++]; |
| 107 | + const face = []; |
| 108 | + for (let j = 0; j < n; j++) { |
| 109 | + face.push(polys[i++]); |
| 110 | + } |
| 111 | + faces.push(face); |
| 112 | + } |
| 113 | + |
| 114 | + // Map each face's points to the corresponding region in the texture |
| 115 | + faces.forEach((face, faceIdx) => { |
| 116 | + // Compute texture region for this face |
| 117 | + const col = faceIdx % 3; |
| 118 | + const row = Math.floor(faceIdx / 3); |
| 119 | + const u0 = col / 3; |
| 120 | + const v0 = row / 2; |
| 121 | + const u1 = (col + 1) / 3; |
| 122 | + const v1 = (row + 1) / 2; |
| 123 | + |
| 124 | + // Assign tcoords to each point in the face |
| 125 | + face.forEach((ptIdx, j) => { |
| 126 | + // For quads, assign corners in order |
| 127 | + // For triangles, just map to the region |
| 128 | + let u; |
| 129 | + let v; |
| 130 | + if (face.length === 4) { |
| 131 | + // Map corners: 0-bottom left, 1-bottom right, 2-top right, 3-top left |
| 132 | + if (j === 0) { |
| 133 | + u = u0; |
| 134 | + v = v0; |
| 135 | + } else if (j === 1) { |
| 136 | + u = u1; |
| 137 | + v = v0; |
| 138 | + } else if (j === 2) { |
| 139 | + u = u1; |
| 140 | + v = v1; |
| 141 | + } else { |
| 142 | + u = u0; |
| 143 | + v = v1; |
| 144 | + } |
| 145 | + } else { |
| 146 | + // For triangles, just use barycentric mapping |
| 147 | + u = u0 + (u1 - u0) * (j % 2); |
| 148 | + v = v0 + (v1 - v0) * Math.floor(j / 2); |
| 149 | + } |
| 150 | + tcoords[ptIdx * 2] = u; |
| 151 | + tcoords[ptIdx * 2 + 1] = v; |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + const tcoordArray = vtkDataArray.newInstance({ |
| 156 | + name: 'TextureCoordinates', |
| 157 | + numberOfComponents: 2, |
| 158 | + values: tcoords, |
| 159 | + }); |
| 160 | + polyData.getPointData().setTCoords(tcoordArray); |
| 161 | +}; |
| 162 | + |
| 163 | +const polyData = cubeSource.getOutputData(); |
| 164 | +addCubeTCoords(polyData); |
| 165 | + |
| 166 | +const mapper = vtkMapper.newInstance(); |
| 167 | +mapper.setInputData(polyData); |
| 168 | + |
| 169 | +const actor = vtkActor.newInstance(); |
| 170 | +actor.setMapper(mapper); |
| 171 | +actor.addTexture(texture); |
| 172 | + |
| 173 | +renderer.addActor(actor); |
| 174 | +resetCamera(); |
| 175 | +render(); |
| 176 | + |
| 177 | +global.fullScreenRenderer = fullScreenRenderer; |
0 commit comments