Rotate a texture
-
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 !
-
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.
-
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! -
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
-
@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.
-
NewOne and me try to make a script which rotate textures automatically. See this post to understand what I want...
-
@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 -
@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.
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 !
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?
-
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?
Advertisement