[Plugin] triangulateFaces.rb v1.2 20101120
-
@honoluludesktop said:
Hi TIG, Any way to convert this kind of mesh into its component triangles?
I wrote an example script a few weeks ago to do something like this... http://forums.sketchucation.com/viewtopic.php?p=186068#p186068
-
TIG, Thanks for the link, it works. Now I can make small edits to the (mesh?).
-
@tig said:
Result: it triangulates a face... front Texture UVs are kept.
hey TIG,
if i run the script on this sphere, the UVs go haywire.
am i misunderstanding or misusing the script?
thanks[edit] ok, wait.. i just read the rest of the thread and i guess this hasn't been sorted out ?
sorry about the size.. i guess i could of used a smaller texture
-
@unknownuser said:
@tig said:
Result: it triangulates a face... front Texture UVs are kept.
hey TIG,
if i run the script on this sphere, the UVs go haywire.
am i misunderstanding or misusing the script?
thanks[edit] ok, wait.. i just read the rest of the thread and i guess this hasn't been sorted out ?
It's broken... the UV mapping was CJP's but it still needs fixing...
-
@tig said:
It's broken... the UV mapping was CJP's but it still needs fixing...
ah, ok..
too bad[fwiw - i can manually triangulate the sphere using and everything turns out ok.. i guess this script is doing something other than just drawing lines to cut quads in half?]
-
@unknownuser said:
@tig said:
It's broken... the UV mapping was CJP's but it still needs fixing...
ah, ok..
too bad[fwiw - i can manually triangulate the sphere using and everything turns out ok.. i guess this script is doing something other than just drawing lines to cut quads in half?]
It triangulates the quads and tries to fix the UV mapping - obviously very badly !!!! CJP help!
-
See this new script just for selected quad faces - it keeps UV mapping etc...
http://forums.sketchucation.com/viewtopic.php?p=201025#p201025 -
@tig said:
Known Issues: Back Materials are kept, but these Texture UVs cannot be re-used;
there's a Ruby Console warning message if this problem is found...
Solution: use the front faces only for textures IF you might be triangulating later.Why is it that you can't preserve the UVs of the backfaces?
-
We tried [CJP+me] but we couldn't get it to work - any ideas on fixing the whole issue of triangulation and keep UV mapping to front and back faces would be appreciated...
-
hm... looking at the code:
mesh=face.mesh(1)
This will give you a PolygonMesh with only the FrontUV.mesh=face.mesh(3)
will give you front and back.
but you need to change this as well:uvs=mesh.uvs(true)
true will give you set of front UVs, false will give you back UVs if you specified that in the .mesh method.I'm not quite sure I understand the purpose of this:
outmesh = Geom::PolygonMesh.new faces.each{|f|outmesh.add_polygon(verts[f[0].abs-1],verts[f[1].abs-1],verts[f[2].abs-1])}
Why create a new PolygonMesh? Rearranging the polygons? -
This was CJP's code...
I've messed on with your ideas but can only get the front OR back UVs to work... how do I get both ? Here's the re-jigged code so far...def triangulateFace(face) mesh=face.mesh(3) verts=mesh.points ents=Sketchup.active_model.active_entities mat=face.material bac=face.back_material face.erase! grp=ents.add_group grp.entities.add_faces_from_mesh(mesh) grp.entities.each{|e| if e.class==Sketchup;;Edge e.smooth=false e.soft=false elsif e.class==Sketchup;;Face uva=verts.index(e.vertices[0].position) uvb=verts.index(e.vertices[1].position) uvc=verts.index(e.vertices[2].position) if mat uvsF=mesh.uvs(true) e.position_material(mat, [verts[uva],uvsF[uva], verts[uvb],uvsF[uvb], verts[uvc],uvsF[uvc]], true) end#if if bac uvsB=mesh.uvs(false) e.position_material(bac,[verts[uva],uvsB[uva], verts[uvb],uvsB[uvb], verts[uvc],uvsB[uvc]], false) end#if end#if } grp.explode end#def
-
That bit of code seem to work.
-
@thomthom said:
That bit of code seem to work.
It does work for the back_material but it doesn't map the front material's uvs...
Try running it on a face with a hole in it and and materials front and back - both adjusted at angles/shear etc with Texture tool... Both materials are put back onto the now triangulated faces but the front material will be at its default settings...I can't see how to do it
-
Holes and sheared textures seems fine.
But, when I distort them, so the texture doesn't fit to a parallelogram, then it fails.
And that's excatly the conversation I've been having with Whaat a couple of days ago. That in order to sample textures which are distorted you need four points. Which leads to the question of PolygonMeshes: what do you do then, because if you just sample the UV from each point in the polygon you only get tree samples.I wonder if
PolygonMesh.uv_at(u,v)
can be used though. The manual is not clear on this method. The description and example don't add up. -
See what you mean - I had a distorted front face that failed...
if you have 4 ubs's how do we check ? -
I have some thoughts...
The original face's mesh has a number of points - 3 or more.
A texture usually has 3 uvs 'points'.
A distorted texture has 4 uvs 'points'.
How do these uvs 'points' relate to the mesh points ? If you give them the equivalent 'index' then they'll work until index=3...
A face made from the mesh always has 3 points, these relate to the mesh points in sets of 3 - but how do these tie back to the texture uvs 'points' ? The fourth one index=3 fails ???
How do we make an array to place the material that has this fourth pair of a 3D point and a uvs 'point' ? It must reflect the 'extra point' somehow ??I shall sleep on it and see if the little-grey-cells come up with anything...
-
For my UV plugins I have used the UVHelper to sample the points. What I did then was sample four points of the face's place - completely ignoring the face's vertices.
Maybe, for each face that's turned into a PolygonMesh, sample four points of the front and back. Then once converted into PolygonMesh, apply the sampled UV points to each of the polygon faces. That should work. All the created polygon faces are still on the old face's plane.
-
More ideas... let me sleep... zzzzzzzzzzzzz x
-
def triangulateFace(face) ### if already a triangle leave alone... if face.vertices[3] mesh=face.mesh(3) ents=face.parent.entities mat=face.material bac=face.back_material ### p0=face.vertices[0].position p1=face.vertices[1].position p2=face.vertices[2].position p3=face.vertices[3].position ### get uvs tw=Sketchup.create_texture_writer uv_helperF=face.get_UVHelper(true,false,tw) uv_helperB=face.get_UVHelper(false,true,tw) uf0=uv_helperF.get_front_UVQ(p0) uf1=uv_helperF.get_front_UVQ(p1) uf2=uv_helperF.get_front_UVQ(p2) uf3=uv_helperF.get_front_UVQ(p3) ub0=uv_helperB.get_back_UVQ(p0) ub1=uv_helperB.get_back_UVQ(p1) ub2=uv_helperB.get_back_UVQ(p2) ub3=uv_helperB.get_back_UVQ(p3) ### grp=ents.add_group grp.entities.add_faces_from_mesh(mesh) grp.entities.each{|e| if e.class==Sketchup;;Edge e.smooth=false e.soft=false elsif e.class==Sketchup;;Face if mat e.position_material(mat,[p0,uf0, p1,uf1, p2,uf2, p3,uf3], true) end#if if bac e.position_material(bac,[p0,ub0, p1,ub1, p2,ub2, p3,ub3], false) end#if end#if } face.erase! grp.explode end#if end#def
Seems to me that this should work... but it doesn't - Thomthom, any ideas
-
def triangulateFace(face) ### if already a triangle leave alone... if face.vertices[3] mesh=face.mesh(3) ents=face.parent.entities mat=face.material bac=face.back_material ### # (TT) # Instead sampling vertices we sample points of the face's plane. # This is because we need four points, and some times a face only has tree vertices. # And we also get the wrong result if any of the first four vertices are co-linear. samples = [] samples << face.vertices[0].position # 0,0 | Origin samples << samples[0].offset(face.normal.axes.x) # 1,0 | Offset Origin in X samples << samples[0].offset(face.normal.axes.y) # 0,1 | Offset Origin in Y samples << samples[1].offset(face.normal.axes.y) # 1,1 | Offset X in Y ### get uvs tw=Sketchup.create_texture_writer # (TT) uv_helper=face.get_UVHelper(true,true,tw) # (TT) Only need one UV Helper xyz = [] uv_f = [] uv_b = [] samples.each { |position| # XYZ 3D coordinates xyz << position # UV 2D coordinates # Front uvq = uv_helper.get_front_UVQ(position) uv_f << flattenUVQ(uvq) # Back uvq = uv_helper.get_back_UVQ(position) uv_b << flattenUVQ(uvq) } ### grp=ents.add_group grp.entities.fill_from_mesh(mesh, true, 0) # (TT) Adds the mesh without soft/smooth edges. (and faster) grp.entities.each{|e| next unless e.class==Sketchup;;Face if mat # (TT) # Position texture. pts = [] (0..3).each { |i| pts << xyz[i] pts << uv_f[i] } e.position_material(mat,pts, true) end#if if bac # (TT) # Position texture. pts = [] (0..3).each { |i| pts << xyz[i] pts << uv_b[i] } e.position_material(bac,pts, false) end#if } face.erase! grp.explode end#if end#def # (TT) # Get UV coordinates from UVQ matrix. def flattenUVQ(uvq) return Geom;;Point3d.new(uvq.x / uvq.z, uvq.y / uvq.z, 1.0) end
Advertisement