sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    [Code] realign UVs, or: convert projective to affine

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 5 Posters 1.3k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      Aerilius
      last edited by Aerilius

      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.
      projective2affine.gif
      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
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Looks very interesting. Though, could you explain "projective to affine" in this context? I'd like to understand this.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • A Offline
          Aerilius
          last edited by

          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)

          1 Reply Last reply Reply Quote 0
          • C Offline
            chris341
            last edited by

            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!

            Rich O BrienR TIGT 2 Replies Last reply Reply Quote 1
            • Rich O BrienR Offline
              Rich O Brien Moderator @chris341
              last edited by

              @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?

              Download the free D'oh Book for SketchUp πŸ“–

              C 1 Reply Last reply Reply Quote 0
              • C Offline
                chris341 @Rich O Brien
                last edited by

                @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.

                Rich O BrienR 1 Reply Last reply Reply Quote 1
                • Rich O BrienR Offline
                  Rich O Brien Moderator @chris341
                  last edited by

                  @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.

                  Download the free D'oh Book for SketchUp πŸ“–

                  1 Reply Last reply Reply Quote 0
                  • C Offline
                    chris341
                    last edited by

                    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.

                    KLEX1.skp

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator @chris341
                      last edited by

                      @chris341

                      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

                      C 1 Reply Last reply Reply Quote 0
                      • C Offline
                        chris341 @TIG
                        last edited by

                        @TIG said in [Code] realign UVs, or: convert projective to affine:

                        @chris341

                        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

                        Haha 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.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement