changing textures in game

Hi, I need to have a character in my game change texture when you press a key is that possible?

thanks in advance

Hi,

Let’s say you have an image consisting of four tiles and on each tile a different texture. You could use a python script to move the UV coordinates depending on what tile you want to show.

I’ve made some exercises. The second blend illustrates how you can scroll through your tiles. Of course, if you only need two switch between two textures you won’t be needing as much. Following script will do. Speaks for itself that if your image texture consists of two tiles next to each other you will have to scroll the UV-coordinates on the X-axis, so you change the first parameter of cont.owner.meshes[0].getVertex(0, v).getUV().

def scroll(cont):
            
    own = cont.owner
    mesh = own.meshes[0]
    array = mesh.getVertexArrayLength(0)

    for v in range(0, array):
        vert = mesh.getVertex(0, v) 
        uv = vert.getUV()
        <b>uv[0] += 0.5</b>
        vert.setUV(uv)

PS: make sure you won’t get two pulses every time you hit a key, otherwise uv[0] will return to it’s initial position. You could change the value to 0.25 or just use this code in the beginning:

    sen = cont.sensors[0]
    
    if not sen.positive:
        return

    [...]

EDIT: the code was messy so I’ve replaced the blends, my apologies
Edit2: updated ChangingTextures0.blend (old method)
Edit3: updated so it works with all meshes, not only planes

Attachments

ChangingTextures0.blend (81.5 KB)

Hi, thanks for the code!! it works great! the only thing is what I want is for the character to change to the texture of the platform that he is standing on, how can I have it change to that texture instead of skipping through?

There’s a python module called bge.Texture built into blender, it allows you to dynamically modify the textures in a material, swapping them should be easy with it.

here’s the python api for it.
http://www.blender.org/documentation/blender_python_api_2_63_17/bge.texture.html

A quick search of the forums for “bge.texture” or “video texture” should turn up a thread with examples of how it works, I remember seeing one.

Thanks a lot, Mattline1. This is perfect when using separate images. I’ve added an example zip with blend and png if anyone is interested. Use Up and Down Arrows to swap between textures.

EDIT: again I made a mistake (on line 9). :o Sorry for who ever was downloading the zip. It’s changed now.

Attachments

ChangingTextures2.zip (77.9 KB)

Ok, I tried the python api script and the changingTextures2.zip one but when I try to duplicate it, it just does not seem to work… here is one of my test files

http://www.pasteall.org/blend/15897

To get the reference pointer (ID) of the internal texture, the image.name must be preceded with “IM”. So if your image.name is “Cracks”, you should write “IMCracks”.

Attachments

ChangingTextures3.zip (100 KB)

IT WORKED!! finally, thanks for all your code and posts :slight_smile:

I’m happy to, also because I’m learning a lot myself with making such exercises. :wink:

Hi all, this post is very useful, but it tried and has a inconvenient. If for example there are many objects in the scene with the same texture, everyone will be changed!. There one trick to apply just to selected object or especific material?

No, I don’t believe there is such a trick. I believe you have to set up the meshes beforehand if you want to have the objects not share materials. Alternatively, you might be able to set up a node material to use several other materials, mixing them in according to vertex color. That way, each mesh can have its own vertex color values, and so have its own material (one of several specified). However, it might be a better idea to ask on the forums about the specific thing you’re trying to achieve, rather than simply if there’s a way to change materials. It might be that you don’t need to change materials at all.

Hi folks,

I stumbled into this topic again and wanted to update some of the files regarding scrolling textures. I rewrote the script so now you also can select the texture randomly. Also it’s now possible to scroll through rectangle shaped images. To summarize, at startup the coordinates of all ‘cells’ will be stored to a list. The order of the list corresponds to the order viewed in the color grid, so from bottom-left to up-right. Depending on what method you use you then select one of the coordinates in the list and apply it to the current uv coordinates. There are three examples in the script: ‘scoll’, ‘select1’, ‘select2’. If you would only use one of them just delete the other modules (because you won’t be needing them).

The script is very light because most of the calculation is already done after initializing. I hope it will be useful for you. Though the method using the bge.texture may be the easiest way, this can be an alternative for when you want to pick textures out of one image.

Edit: Corrected code in module ‘init’ (there was some unnecessary storing of properties).
Edit2: Rewrote it so it works with all meshes, not only planes
Edit3: Updated so it works when there’s more than one material in the mesh

Attachments

ChangingTextures18.blend (94.6 KB)

Hi folks,

Last update on uv-scrolling. I rewrote the script so it now works with all meshes, not only planes.

Edit: The script didn’t run always on meshes with more than one material. Fixed by adding an identifier to the material name. Identifier can be changed in top of the code.