I'd like to do a map in a game that moves with the actor. What is the proper syntax to move uv coordinates around and have the game engine update the result?
uv_offset = [0, 0] def init(c): # Copy UVs to second channel using an identity matrix. c.owner.meshes.transformUV(-1, mathutils.Matrix(), 1, 0)
Thereafter you can transform from that second channel using a custom matrix:
def update(c): # Transform stored original values and write to first channel. uv_offset += SPEED_X uv_offset += SPEED_Y uv_mat = mathutils.Matrix.Translation((uv_offset, uv_offset, 0)) c.owner.meshes.transformUV(-1, uv_mat, 0, 1)
This avoids the need to iterate over the vertices in your script, so it may be faster for large meshes. You can also perform multiple transforms at once, e.g. scale and translate, by multiplying two matrices together.
After another search session, I found the answer here:
For a working blendfile, see the Blenderartists thread, thanks to HG1
from bge import logic # get the controller that executes the script cont = logic.getCurrentController() # get the object that uses the controller own = cont.owner speed_x = 0.002 # speed by which the coordinates are moved speed_y = 0.0 # speed by which the coordinates are moved mesh = own.meshes # access the mesh of the object in question v_array = mesh.getVertexArrayLength(0) # how many verts are there? for v in range(0,v_array): # go through the uv's of every vert vert = mesh.getVertex(0,v) uv = vert.getUV() uv += speed_x # change the uv coordinates. uv = x, uv = y uv += speed_y vert.setUV(uv) # get the game engine to notice the change!!
The name of your script belongs into the controller.