[Code] realign UVs, or: convert projective to affine
-
When we want to work with textured organic meshes (like me in my current project), we want that the texture flows continuously over the surface. However it can happen that they become misaligned by editing etc.
I wrote this method and surprisingly figured out that the resulting mesh not only has an affine distortion but is still always continuous/seamless – even if it resulted from a projective distortion. That's something I've wanted for long for cases that don't work for projected textures.
Of course the conversion is not lossless, the more subdivided the mesh is, the less noticeable is the difference.def realign_uvs(entities=Sketchup.active_model.selection.to_a) model = Sketchup.active_model if Sketchup.version.to_i >= 7 model.start_operation("Repair misaligned UV coordinates", true) else model.start_operation("Repair misaligned UV coordinates") end @tw = Sketchup.create_texture_writer side = true # frontside hash = {} # vertex => [uvs] faces = entities.find_all{|e| e.is_a?(Sketchup;;Face) && (mat = (side)? e.material ; e.back_material) && mat.materialType>0} # 1) Let's collect all uvs at each vertex faces.each{|f| uv_helper = f.get_UVHelper(true, true, @tw) f.vertices.each{|v| p = v.position uv = (side)? uv_helper.get_front_UVQ(p) ; uv_helper.get_back_UVQ(p) uv.x /= uv.z; uv.y /= uv.z; uv.z = 1 hash[v] = [] if !hash[v] hash[v] << uv } } # 2) Find the uv with the least aberration. Alternative; Calculate the average of the uvs at a vertex. hash.each{|v, array| #average_uv = array.inject(Geom;;Point3d.new([0,0,0])){|sum, uv| sum+uv.to_a}.to_a.collect{|c| c/array.length.to_f} #hash[v] = average_uv best_uv = array.min{|uv_a,uv_b| dist_a = array.inject(0){|dist, uv| d = uv_a.distance(uv); [d,dist].min} # sum of distances from uv_a dist_b = array.inject(0){|dist, uv| d = uv_b.distance(uv); [d,dist].min} # sum of distances from uv_b dist_b <=> dist_a} hash[v] = best_uv } # 3) Apply the uvs for each face faces.each{|f| pt_array = [] vs = f.vertices[0..3] vs.each{|v| pt_array << v.position pt_array << hash[v] } f.position_material(((side)? f.material ; f.back_material), pt_array, side) } model.commit_operation end
-
Looks very interesting. Though, could you explain "projective to affine" in this context? I'd like to understand this.
-
Projective: like projected textures (all four texture pins modified) (this it what the TextureWriter makes unique when exporting)
Affine: skewed or scaled or stretched like a parallelogram, preserves parallel lines and equispaced points along lines.
(I find this document very helpful http://www.cs.cmu.edu/~ph/texfund/texfund.pdf p.13) -
Hello,
Yes, necro-ing a very old thread, I understand.
I am wanting to either use this script in Sketchup 2020 or find a tool that will do this for me. I have a model that I must export to .dae format that is exporting dozens of skewed materials. This material is essentially a painted line on a roadway that I have used ThruPaint to map onto lines following unique paths.
Trying to use this script in SU2020 throws multiple syntax errors. Is there anything that I can do here?
Thanks!
-
@chris341 said in [Code] realign UVs, or: convert projective to affine:
exporting dozens of skewed materials
Did you try triangulating the SketchUp mesh prior to export?
-
@Rich-O-Brien said in [Code] realign UVs, or: convert projective to affine:
@chris341 said in [Code] realign UVs, or: convert projective to affine:
exporting dozens of skewed materials
Did you try triangulating the SketchUp mesh prior to export?
Yes, I have both the 'triangulate faces' option selected in COLLADA export, as well as having tried to triangulate the faces manually using SketchUV and FredoTools. All produce the same results.
-
@chris341 If you feel like sharing the file, or a portion that fails, we could take a look.
If you allow the SketchUp exporter to triangulate its prone to generate lots of little textures. Normally triangulating the mesh then exporting selection works.
-
I tried to disable the option to triangulate faces. It made no difference whether my faces were triangulated manually or not.
I have attached just the portion of the file that is giving me problems.
-
It is a very old thread !
I've fixed the basic syntax errors and added modules etc.
It works in v2024 [should be OK in v2020] - but I'm unsure of its efficacy...
Aerilius_UV_realign_uvs.rb -
@TIG said in [Code] realign UVs, or: convert projective to affine:
It is a very old thread !
I've fixed the basic syntax errors and added modules etc.
It works in v2024 [should be OK in v2020] - but I'm unsure of its efficacy...
Aerilius_UV_realign_uvs.rbHaha yes!! This seems to work, at least at first glance. Just one texture exported. Thank you so much! I will report back if I have any other issues with it.
Advertisement