obj-to-colored-triangles
v0.0.35
Published
convert a textured 3d mesh [Wavefront .obj] into a list of colored triangles. eg, color the mesh with per-face or per-vertex colors that approximate the original texture
Downloads
4
Readme
obj-to-colored-triangles
utility to convert a textured 3d mesh [Wavefront .obj] into a list of colored triangles.
eg, this colors the mesh with per-face or per-vertex colors that approximate the original texture.
note:
this tool is for Node.js only, not browsers.
this tool does not support quads -- if your model contains quads, you must first triangulate it with a tool like assimp
assimp export "model.obj" "model_triangulated_optimized.obj" -tri -om
Installation
npm i obj-to-colored-triangles
Usage
var otct = require('obj-to-colored-triangles');
var TEX = './Ficus-Carica-Texture-8k.jpg'; //texture file
var OBJ = './Ficus-Carica.OBJ'; //obj file including UV coords etc
var smoothColors = false; //if true, uses average of vertex colors. if false, takes color at center of each triangle.
var res = otct.obj2ColoredTrianglesSync(TEX, OBJ, smoothColors)[0]; //multi-mesh OBJ's may contain more than 1 result
//res = {
// tris, //triangles, each one [[x,y,z],[x,y,z],[x,y,z]]
// cells, //indexed faces, each one [a,b,c]
// positions, //vertices, each one [x,y,z]
// cells_uvs, //uv coords, each one [x,y]
// cellFlatColors, //color per triangle, each one like [255,255,255]
// cellVertsColors //color per triangle per vertex, each row like [[255,255,255],[255,255,255],[255,255,255]]
// }
var cellFlatColors = res.cellFlatColors;
//var cellVertsColors = res.cellVertsColors;
//get simple reduced JSON representation, rescaled to be contained in bounding box size 10:
var boundsMaxDimension = 10;
var jsonObj = otct.getJsonModel(res2, boundsMaxDimension)
//getJsonModel(coloredTrianglesResultObj, rescaleSize = 10, FLOAT_DIGITS=3, doIncludeFlatColors = true, doIncludeVertColors=false)
//returns {meshIndexed, flatColors?, vertColors?}
//where meshIndexed = {cells, positions}
//obj2ColoredTrianglesMtl(objFile, mtlFile)
//for models whose colours come exclusively from .mtl color diffuse per-face
// returns
// {
// cells,
// positions,
// tris,
// cellFlatColors
// }
^ original mesh with texture
^ mesh built from colored triangles, smoothColors = false;
^ mesh built from colored triangles, smoothColors = true;