Tank Tracks UV Scroll

Hello everyone! This is my first post. I am making a game where you can move a tank with WSAD, aim with mouse, etc.But I’ve got a problem: the tank tracks. What I’ve done is I have modeled a mesh that looks like a tank track, and then put a track texture on it. I found on the Internet that you can make the texture move along the mesh, which is called UV Scrolling, I believe? I found this website too: https://whatjaysaid.wordpress.com/2015/03/09/bge-scrolling-textures-with-glsl/ But this script just makes the texture go round and round at a uniform speed. There is a variable called “rate”, but in the GLSL code it qualifies it as uniform, with no change. I don’t want that. I would like that when you press ‘w’ the image scrolls forward and when you press ‘s’ the image scrolls backward. Anybody know how?

VertexShader = """
    void main()
    {
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        gl_TexCoord[0] = gl_MultiTexCoord0;
    }
"""
  
FragmentShader = """
uniform float timer;
uniform float rate;
uniform sampler2D texture;
 
    void main()
    {   
        vec4 colour = texture2D(texture, vec2(gl_TexCoord[0].x, gl_TexCoord[0].y + (timer * rate)));
        gl_FragColor = colour;
    }
"""
 
 
import bge 
own = bge.logic.getCurrentController().owner
 
try:
    own['shader'].setUniform1f('timer', own['timer'])
except KeyError: 
    mesh = own.meshes[0]
    for mat in mesh.materials:
        shader = mat.getShader()
        own['shader'] = shader
        if shader != None:
            if not shader.isValid():
                shader.setSource(VertexShader, FragmentShader, 1)
                shader.setSampler('texture', 0) 
                shader.setUniform1f('rate', own['rate'])


Logic Brick setup:

Always (True Pulse) – Python (UV Scroll Script) Properties: timer [timer mode]
rate [float mode] (speed of scrolling, can’t change during game)

You don’t need to use a custom GLSL shader for this, you can manipulate the UV map just from python. The script I use is:


#######################################
#  UV SCROLL SCRIPT                   #
#  by Pepius                          #
#  Updated by sdfgeoff                #
#######################################
# To Use                              #
#######################################
# Add This To An Always Sensor        #
# Add A Float Property Called "speed" #
# Add A Int Property Called "dir"     #
#######################################

import bge

cont = bge.logic.getCurrentController() 
obj = cont.owner
mesh = obj.meshes[0]
array = mesh.getVertexArrayLength(0)

#The recommended max. speed is 0.10 and the min. is 0.009
speed = obj["speed"]

# 0 is horizontal, 1 is vertical
dir = obj["dir"]

for v in range(0,array):
    vert = mesh.getVertex(0,v) 
    uv = vert.getUV() 
    uv[dir] = uv[dir]+speed
    vert.setUV(uv)

You can set the speed by changing the game property ‘speed’

There are better scripts than this now, but I like this one because of it’s simplicity.

Thanks a lot! I will try it. I remember using a script similar to this one, but when I set it up it didn’t work. Hopefully this one does!

Somehow it doesn’t do anything. Is it just valid for planes? I’ll check again…
Meanwhile, here’s the blend file:PzIII.blend (1.69 MB)

OK, I’m stupid. I forgot to change the ‘dir’ property to integer