-
-
Notifications
You must be signed in to change notification settings - Fork 394
feat(objwriter): add support for vtkOBJWriter #2405
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
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
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,177 @@ | ||
import '@kitware/vtk.js/favicon'; | ||
|
||
// Load the rendering pieces we want to use (for both WebGL and WebGPU) | ||
import '@kitware/vtk.js/Rendering/Profiles/Geometry'; | ||
|
||
import '@kitware/vtk.js/IO/Core/DataAccessHelper/HttpDataAccessHelper'; // HTTP + zip | ||
|
||
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; | ||
import vtkCubeSource from '@kitware/vtk.js/Filters/Sources/CubeSource'; | ||
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray'; | ||
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; | ||
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; | ||
import vtkOBJWriter from '@kitware/vtk.js/IO/Misc/OBJWriter'; | ||
import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture'; | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Standard rendering code setup | ||
// ---------------------------------------------------------------------------- | ||
|
||
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance(); | ||
const renderer = fullScreenRenderer.getRenderer(); | ||
const renderWindow = fullScreenRenderer.getRenderWindow(); | ||
|
||
const resetCamera = renderer.resetCamera; | ||
const render = renderWindow.render; | ||
|
||
// Create a colored texture for each face of the cube | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = 256; | ||
canvas.height = 256; | ||
const ctx = canvas.getContext('2d'); | ||
|
||
// Define colors for each face (order: +X, -X, +Y, -Y, +Z, -Z) | ||
const faceColors = [ | ||
'#FF0000', // +X: Red | ||
'#00FF00', // -X: Green | ||
'#0000FF', // +Y: Blue | ||
'#FFFF00', // -Y: Yellow | ||
'#00FFFF', // +Z: Cyan | ||
'#FF00FF', // -Z: Magenta | ||
]; | ||
|
||
// Draw each face as a square in a 3x2 grid | ||
const faceSize = 128; | ||
for (let i = 0; i < 6; i++) { | ||
const x = (i % 3) * faceSize; | ||
const y = Math.floor(i / 3) * faceSize; | ||
ctx.fillStyle = faceColors[i]; | ||
ctx.fillRect(x, y, faceSize, faceSize); | ||
} | ||
|
||
function callback(source, tex) { | ||
const writer = vtkOBJWriter.newInstance(); | ||
writer.setInputData(source.getOutputData()); | ||
writer.setTexture(tex); | ||
|
||
// const objContent = writer.getOutputData(); | ||
// const mtlContent = writer.getMtl(); | ||
|
||
const zip = writer.exportAsZip(); | ||
zip.then((zipData) => { | ||
const blob = new Blob([zipData], { type: 'application/zip' }); | ||
const url = URL.createObjectURL(blob); | ||
const link = document.createElement('a'); | ||
link.href = url; | ||
link.text = 'Download'; | ||
link.style.position = 'absolute'; | ||
link.style.left = '50%'; | ||
link.style.bottom = '10px'; | ||
link.style.background = 'white'; | ||
link.style.padding = '5px'; | ||
global.writer = writer; | ||
document.body.appendChild(link); | ||
}); | ||
} | ||
|
||
// Create a cube source | ||
const cubeSource = vtkCubeSource.newInstance({ | ||
xLength: 1, | ||
yLength: 1, | ||
zLength: 1, | ||
}); | ||
|
||
// Create vtkTexture from canvas | ||
const texture = vtkTexture.newInstance(); | ||
const image = new Image(); | ||
image.src = canvas.toDataURL(); | ||
image.onload = () => { | ||
texture.setImage(image); | ||
callback(cubeSource, texture); | ||
render(); | ||
}; | ||
|
||
// Add tcoords to the cube for proper texture mapping | ||
const addCubeTCoords = (polyData) => { | ||
const points = polyData.getPoints().getData(); | ||
const numPoints = points.length / 3; | ||
const tcoords = new Float32Array(numPoints * 2); | ||
|
||
// Map each face to a region of the texture | ||
const polys = polyData.getPolys().getData(); | ||
const faces = []; | ||
const f = []; | ||
let i = 0; | ||
while (i < polys.length) { | ||
f.length = 0; // Reset face array | ||
const n = polys[i++]; | ||
for (let j = 0; j < n; j++) { | ||
f.push(polys[i++]); | ||
} | ||
faces.push(f); | ||
} | ||
|
||
// Map each face's points to the corresponding region in the texture | ||
faces.forEach((face, faceIdx) => { | ||
// Compute texture region for this face | ||
const col = faceIdx % 3; | ||
const row = Math.floor(faceIdx / 3); | ||
const u0 = col / 3; | ||
const v0 = row / 2; | ||
const u1 = (col + 1) / 3; | ||
const v1 = (row + 1) / 2; | ||
|
||
// Assign tcoords to each point in the face | ||
face.forEach((ptIdx, j) => { | ||
// For quads, assign corners in order | ||
// For triangles, just map to the region | ||
let u; | ||
let v; | ||
if (face.length === 4) { | ||
// Map corners: 0-bottom left, 1-bottom right, 2-top right, 3-top left | ||
if (j === 0) { | ||
u = u0; | ||
v = v0; | ||
} else if (j === 1) { | ||
u = u1; | ||
v = v0; | ||
} else if (j === 2) { | ||
u = u1; | ||
v = v1; | ||
} else { | ||
u = u0; | ||
v = v1; | ||
} | ||
} else { | ||
// For triangles, just use barycentric mapping | ||
u = u0 + (u1 - u0) * (j % 2); | ||
v = v0 + (v1 - v0) * Math.floor(j / 2); | ||
} | ||
tcoords[ptIdx * 2] = u; | ||
tcoords[ptIdx * 2 + 1] = v; | ||
}); | ||
}); | ||
|
||
const tcoordArray = vtkDataArray.newInstance({ | ||
name: 'TextureCoordinates', | ||
numberOfComponents: 2, | ||
values: tcoords, | ||
}); | ||
polyData.getPointData().setTCoords(tcoordArray); | ||
}; | ||
|
||
const polyData = cubeSource.getOutputData(); | ||
addCubeTCoords(polyData); | ||
|
||
const mapper = vtkMapper.newInstance(); | ||
mapper.setInputData(polyData); | ||
|
||
const actor = vtkActor.newInstance(); | ||
actor.setMapper(mapper); | ||
actor.addTexture(texture); | ||
|
||
renderer.addActor(actor); | ||
resetCamera(); | ||
render(); | ||
|
||
global.fullScreenRenderer = fullScreenRenderer; | ||
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,103 @@ | ||
import vtkPolyData from '../../../Common/DataModel/PolyData'; | ||
import vtkTexture from '../../../Rendering/Core/Texture'; | ||
import { vtkAlgorithm, vtkObject } from '../../../interfaces'; | ||
|
||
/** | ||
* | ||
*/ | ||
export interface IOBJWriterInitialValues { | ||
modelFilename?: string; | ||
materialFilename?: string; | ||
texture?: vtkTexture; | ||
textureFileName?: string; | ||
} | ||
|
||
type vtkOBJWriterBase = vtkObject & vtkAlgorithm; | ||
|
||
export interface vtkOBJWriter extends vtkOBJWriterBase { | ||
/** | ||
* Get the zip file containing the OBJ and MTL files. | ||
*/ | ||
exportAsZip(): Promise<Uint8Array>; | ||
|
||
/** | ||
* Get the MTL file as a string. | ||
*/ | ||
getMtl(): string; | ||
|
||
/** | ||
* | ||
* @param inData | ||
* @param outData | ||
*/ | ||
requestData(inData: any, outData: any): void; | ||
|
||
/** | ||
* Set the material filename. | ||
* @param materialFilename | ||
* @returns {boolean} true if the material file name was set successfully | ||
*/ | ||
setMaterialFilename(materialFilename: string): boolean; | ||
|
||
/** | ||
* Set the model filename. | ||
* @param modelFilename | ||
*/ | ||
setModelFilename(modelFilename: string): boolean; | ||
|
||
/** | ||
* Set the texture instance. | ||
* @param {vtkTexture} texture | ||
* @returns {boolean} true if the texture was set successfully | ||
*/ | ||
setTexture(texture: vtkTexture): boolean; | ||
|
||
/** | ||
* Set the texture file name. | ||
* @param {string} textureFileName | ||
* @returns {boolean} true if the texture file name was set successfully | ||
*/ | ||
setTextureFileName(textureFileName: string): boolean; | ||
} | ||
|
||
/** | ||
* Method used to decorate a given object (publicAPI+model) with vtkOBJWriter characteristics. | ||
* | ||
* @param publicAPI object on which methods will be bounds (public) | ||
* @param model object on which data structure will be bounds (protected) | ||
* @param {IOBJWriterInitialValues} [initialValues] (default: {}) | ||
*/ | ||
export function extend( | ||
publicAPI: object, | ||
model: object, | ||
initialValues?: IOBJWriterInitialValues | ||
): void; | ||
|
||
/** | ||
* Method used to create a new instance of vtkOBJWriter | ||
* @param {IOBJWriterInitialValues} [initialValues] for pre-setting some of its content | ||
*/ | ||
export function newInstance( | ||
initialValues?: IOBJWriterInitialValues | ||
): vtkOBJWriter; | ||
|
||
/** | ||
* | ||
* @param {vktPolyData} polyData | ||
*/ | ||
export function writeOBJ(polyData: vtkPolyData): vtkPolyData; | ||
|
||
/** | ||
* vtkOBJWriter writes wavefront obj (.obj) files in ASCII form. OBJ files | ||
* contain the geometry including lines, triangles and polygons. Normals and | ||
* texture coordinates on points are also written if they exist. | ||
* | ||
* One can specify a texture passing a vtkTexture using `setTexture`. If a texture is | ||
* set, additional .mtl and .png files are generated. | ||
*/ | ||
export declare const vtkOBJWriter: { | ||
newInstance: typeof newInstance; | ||
extend: typeof extend; | ||
writeOBJ: typeof writeOBJ; | ||
}; | ||
export default vtkOBJWriter; |
Oops, something went wrong.
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.