sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Rotate a texture

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 5 Posters 1.6k Views 5 Watching
    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.
    • M Offline
      Matt666
      last edited by

      Hi all !
      I don't understand how to rotate a texture in ruby... I suppose I have to use "UV helper" function, but I don't know how... And "get_front_UVQ" function returns a point... Maybe someone can help me ?

      Thank you !

      Frenglish at its best !
      My scripts

      1 Reply Last reply Reply Quote 0
      • P Offline
        psychomuffin
        last edited by

        Textures have driven me craxy so many times in SU. I really hate how sometimes groups will rotate the texture, or sometimes, the object will rotate, but the text keeps it's orientation, lol. SU does not have a good mean of using textures it seems thus far, I would love a good plugin that can make this kinda stuff easier.

        1 Reply Last reply Reply Quote 0
        • N Offline
          NewOne
          last edited by

          Here is a scipt that rotates a texture on a face by a custom angle.
          For now, the script works just for faces in xy plane (red-green) but it can be a usefull example to understand how to position a texture on a face.
          The UV helper retrieves the vertex coordinates. So, if you have a hexagonal face, you will have 12 points (6 vertexes). But position_material method uses maximum 8 points (4 vertexes)... This is because Open GL treats any surface as a composition of triangles or quadrilaterals (sorry if misswriten). This is why i don't know yet how to make the script working for faces oriented arbitrary (not normal on an axis). I use the entitie's boundingbox as a reference.

          For better understanding of what I've done, see this image http://bayimg.com/bALMEaABJ

          require 'sketchup.rb'
          
          def rotate_texture
          prompts = ["rotation angle "]
          value = [0.0]	#use float for better precission
          result = inputbox prompts, value, "Texture Rotator"
          return if not result
          angle = result
          #------------------------------------------------------------------------------------------------------
          
          ox = Math.cos(angle[0].degrees) #projection coefficient
          oy = Math.sin(angle[0].degrees)	#projection coefficient
          		
          selected = Sketchup.active_model.selection[0]
          
          	if( selected.is_a? Sketchup;;Face ) #check if you selected a face
          		
          		boundingbox = selected.bounds
          		material = selected.material
          		texture = material.texture
          		
          		bw = boundingbox.width
          		proj_x = ox*bw
          		proj_y = oy*bw
          		
          		tex_w = (texture.width).inch
          		ratio = bw/tex_w	#calculate the ratio between image dimension and boundingbox dimension,
          							#to preserve same tiling
          
          		pts = []
          		pts[0] = [0, 0, 0]
          		pts[1] = [proj_x, proj_y, 0]
          
          		coords = [pts[0], [0, 0], pts[1], [ratio, 0]]	
          		selected.position_material(material, coords, true)	#here you put the material using new coordinates
          	else
          	UI.messagebox "You must select a face !"
          	end
          end
          
          #------------------------------------------------------------------------------------------------------
          filename="texture_rotator.rb"
          
          if !file_loaded?(filename)
          	plugins_menu = UI.menu("Plugins")
          	if plugins_menu
          		plugins_menu.add_separator
          		plugins_menu.add_item("Rotate texture by angle") {rotate_texture}
          	end
          	file_loaded(filename)
          end
          

          Hope it's helpfull not hellpful πŸ˜›
          regards!

          1 Reply Last reply Reply Quote 0
          • P Offline
            psychomuffin
            last edited by

            Please excuse my lack of understanding, but how does one take the code you provided, and turn it into a plugin? Does it need compiling or anything? I really don't know anything about ruby... sadly.

            Thanks

            1 Reply Last reply Reply Quote 0
            • N Offline
              NewOne
              last edited by

              @psychomuffin said:

              Please excuse my lack of understanding, but how does one take the code you provided, and turn it into a plugin? Does it need compiling or anything? I really don't know anything about ruby... sadly.

              Thanks

              Copy all that text and paste it in a notepad file. Rename that file with " texture_rotator.rb " (see the last lines of the code that add a entry in Plugins menu) and put your newly created file in the Plugins folder of SU.
              Carefull with copying to not miss something.
              Enjoy!

              Sketchup have built-in ruby interpreter, so what you need is just the code. Ruby is an interpreted language (like Fortran if you want), not a compiled one like C.

              Be aware that this script rotates the texture on the face, not on the component (or group). So you must apply the material directly on face.

              1 Reply Last reply Reply Quote 0
              • M Offline
                Matt666
                last edited by

                NewOne and me try to make a script which rotate textures automatically. See this post to understand what I want...

                Frenglish at its best !
                My scripts

                1 Reply Last reply Reply Quote 0
                • T Offline
                  tomasz
                  last edited by

                  @newone said:

                  The UV helper retrieves the vertex coordinates.

                  Hi NewOne,

                  I am glad to see someone here who can cast some light into my dungeon of texture coordinates. πŸ˜„
                  You have mentioned about UVHelper but you are not using it in the script. I would like to learn how to use it efficiently also with nested faces and will contribute to this thread as much as I will be able to.

                  Thanks
                  Tomasz

                  Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

                  1 Reply Last reply Reply Quote 0
                  • N Offline
                    NewOne
                    last edited by

                    @unknownuser said:

                    You have mentioned about UVHelper but you are not using it in the script. I would like to learn how to use it efficiently also with nested faces and will contribute to this thread as much as I will be able to.

                    You are right! I didn't used UV helper and you have the answer why in those posts. As I seen since now, for SU, the priority is the tiling of texture on surface, although you enter the size of the tile in texture editor. So, that's why we need to know the overall dimensions of the face. If the face is normal-to-axis is simple: you use face.bounds. But for a randomly oriented face we must calculate the vertex distance.
                    I have an ideea: it came on-the-fly πŸ˜„... to use UV helper to retrieve the position of all vertexes of the face. Then calulate the projection from boundingbox center to farthest vertex in two perpendicular directions.

                    center = face.bounds.center
                    

                    Then, having this, is easy to know the overall dimensions of the face and the ratio (ratio = fasce_dimension / texture_dimension) to preserve same tiling when rotating. If you don't do this, the texture will shrink. texture shrinks if don't preserve the ratio
                    After that, one of the vertexes is rotated around the normal to face with desired angle. And that's how the texture can be rotated on randomly oriented face !
                    this is a strange shape face, to make things messy ;)
                    Or, maybe it's a easier way to do this... I mean to work on triangulated or quadrilateral faces, but there Open GL must be used, I think.

                    Some suggestions from The Big Ones? πŸ’š

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      I'm working on mirroring the texture and UV mapping of front faces to the backside. Anyone got any idea on how to do that?

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

                      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