i used jsc3d viewer import group parts of 3d object in .obj format texture defined. , able assign new textures each separate part. example, have .obj file loaded, 10 differents parts. parts white. have 3 textures in .png format (red, blue , green). want assign texture "red" parts, textures "blue" , "green" others. , want able modify them. how that?
thanks help
let's start obj format: example of simple cube 3 different textures applied opposite faces:
# obj file generated meshlab # vertices: 8 # faces: 12 mtllib ./tex_cube.obj.mtl v -50.00 -50.00 50.00 v -50.00 50.00 50.00 v 50.00 -50.00 50.00 v 50.00 50.00 50.00 v 50.00 -50.00 -50.00 v 50.00 50.00 -50.00 v -50.00 -50.00 -50.00 v -50.00 50.00 -50.00
pay attention path of mtl file, of issues because file not found, , 3d tools don't allow relative paths mtl file, or textures. suggest you, keep side side obj file.
there 12 triangles, because each side of cube has been triangulated. vt (vertex texture) tag defines how textures mapped, in case uv (trivially each face):
vt 1.00 1.00 vt 0.00 1.00 vt 0.00 0.00 vt 1.00 0.00
jsc3d read vt tag, f (faces) tag , usemtl tag build 3d object:
usemtl material_0 f 4/1 2/2 1/3 f 3/4 4/1 1/3 f 8/1 6/2 5/3 f 7/4 8/1 5/3 usemtl material_1 f 6/1 4/2 3/3 f 5/4 6/1 3/3 f 2/1 8/2 7/3 f 1/4 2/1 7/3 usemtl material_2 f 6/1 8/2 2/3 f 4/4 6/1 2/3 f 3/1 1/2 7/3 f 5/4 3/1 7/3
materials remapped corresponding png files in mtl file:
newmtl material_0 ka 0.200000 0.200000 0.200000 kd 1.000000 1.000000 1.000000 ks 1.000000 1.000000 1.000000 map_kd red_tex.png
... , on other 2 textures, green , blue.
this file has been obtained following export settings in meshlab, point out don't need export normals, jsc3d recreate them on fly:
sorry boring explanation of obj test file.
if load cube in jsc3d viewer , check viewer.scene.children[] find 3 meshes, because jsc3d has been grouped 12 triangles of cube 3 separate parts, one each usemtl tag has been found inside obj file.
now, can reference of 1 of parts through pickinfo structure selecting face inside viewer (by clicking mouse or touch), or code.
for example, if need replace blue texture :
function replacebluetexture() { objparts = []; // can replace more 1 part objparts[0] = viewer.scene.children[2]; objloader.setuptexture(objparts, "models/obj/aluminum.png"); }
the result shows (left right after loading, right after texture change):
now, top , bottom faces of cube aluminum-textured.
edit:
the replacebluetexture function assume know mesh shall replaced.
what if have replace material (or texture) of mesh name?
the obj file loaded , parsed first, mtl file loaded after obj file, material names captured , stored jsc3d in mesh object during parsing of obj file.
if @ viewer.scene object, see meshes described above , associated materials , textures:
what need check mesh.mtl (or maybe mesh.material.name - 1 of both shall filled in latest version of jsc3d):
function replacebluetexturebyname() { var scene = viewer.getscene(); var meshes = scene.getchildren(); (var i=0, l=meshes.length; i<l; i++) { var mesh = meshes[i]; var mat = mesh.material; if (mat.name == 'mat_blue_tex') { var objparts = []; objparts[0] = mesh; loader.setuptexture(objparts, "models/obj/aluminum.png"); } } }
btw, sure render mode can show textures:
viewer.setparameter('rendermode', 'texturesmooth');
Comments
Post a Comment