set_tex_offset
, set_tex_scale
and set_tex_rotate
are methods that transform the texture coordinates (UV’s) of each vertex of a model. The order in which transformations are combined matters. The default order is: first scale, then rotation and finally translation (offset). The only way to change that order is by using a matrix; simply changing the order of the calls to the corresponding methods has no effect.
One thing that might be confusing about transforming a vertex in texture space is that the texture itself seems to be inversely transformed. For example, if you scale by 2, the texture will appear only half as big.
And if you offset by a positive amount, the texture moves in the negative direction. Also the order of transformations seems reversed.
Here is some code you can play around with to more clearly see how it all works.
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
cm = CardMaker('square')
cm.set_frame(0., 1., 0., 1.)
# the square has default texture coordinates like so:
# (0, 1) ------------- (1, 1)
# | |
# | |
# | |
# | |
# (0, 0) ------------- (1, 0)
square = render.attach_new_node(cm.generate())
square.set_pos(-.5, 3., -.5)
tex = loader.load_texture('maps/lilsmiley.rgba')
ts = TextureStage.get_default()
square.set_texture(ts, tex)
square.set_tex_rotate(ts, 30.) # texture rotates 30 degrees clockwise
square.set_tex_scale(ts, 2., 1.) # texture is halved in X-direction
square.set_tex_offset(ts, .3, 0.) # texture moves .3 to the left
# alternatively, use the following matrix, which reverses the order
# of transformations; this gets rid of the shearing
# mat = Mat3.translate_mat(.3, 0.)
# mat = mat * Mat3.rotate_mat(30.)
# mat = mat * Mat3.scale_mat(2., 1.)
# square.set_tex_transform(ts, TransformState.make_mat3(mat))
app = MyApp()
app.run()
The texture seems skewed because the non-uniform scaling combined with the rotation introduces shearing. This is the result of the default transformation order being seemingly reversed (first rotation, then scale).
Hopefully this will make things a bit more clear.