Topic: Need help with a texture scroll shader

Hi, i have this shader and i want texture to scroll x direction, but something is wrong and texture wont move. I'm new to shaders, please help


VERT:
attribute vec3 Vertex;
attribute vec2 TexCoord0;

varying vec2 TexCoord; 

uniform mat4 TextureMatrix[8];
uniform mat4 ModelViewMatrix;
uniform mat4 ProjModelViewMatrix;

void main(void)

    TexCoord = vec2(TextureMatrix[0] * vec4(TexCoord0, 1.0,1.0)); 
   
    gl_Position = ProjModelViewMatrix * vec4(Vertex,1.0); 


FRAG:

uniform sampler2D Texture[8];

uniform float Time; 
varying vec2 TexCoord; 

void main(void) { 
    vec4 texel = texture2D(Texture[0], vec2(TexCoord.x + 100.0, TexCoord.y)); 
    //vec4 texel = texture(Texture[0], vec2(TexCoord.x + Time,0.0)); 

    gl_FragColor = texel;
    //gl_FragColor=vec4(1.0,1.0,1.0,1.0);
}   

I have time implemented in behaviour dll and its set to <time+=0.1>. But for some reason it doesnt scroll the texture. Setting 0.5f instead of time scrolls the texture. This is my dll code:

.......
void MyBehavior::update(void)
{
    MEngine * engine = MEngine::getInstance();

    MRenderingContext *render = engine->getRenderingContext();

    MGame * game = engine->getGame();

    MLevel * level = engine->getLevel();

    MScene * scene = level->getCurrentScene();

    MOEntity* entity = scene->getEntityByName("Entity0");

    MMesh * mesh = entity->getMesh();

    // check if the game is running, removing thif test will enable In-Editor update (like for the lookAt behavior)
    if(! game->isRunning())
        return;

    // get the associated parent object (who is using this behavior)
    MObject3d * parent = getParentObject();

    // lets rotate the parent object around the z axis, using our custom "rotationSpeed" variable
    //parent->addAxisAngleRotation(MVector3(0, 0, 1), m_rotationSpeed);
    if (time > 1.0)
    {
        time = 0.0;
    }
    else
    {
        time += 0.1;
    }

    MMaterial * material = mesh->getMaterial(0);
    if (material)
    {
        MFXRef * FXRef = material->getFXRef();
        if (FXRef)
        {
            unsigned int fxId = FXRef->getFXId();
            render->bindFX(fxId);

            // send uniform variable
            render->sendUniformFloat(fxId, "Time", &time);

            render->bindFX(0);
        }
    }
}
........

Last edited by Skywriter (2014-07-18 14:10:43)

Re: Need help with a texture scroll shader

It looks ok,
did you check if "render->sendUniformFloat" is called ?
also, are you sure the shader is linked to material 0 of Entity0 ?

the line "if(! game->isRunning()) return;" means the code will only run when you play the game (pacman button).

Re: Need help with a texture scroll shader

Hi anael. How can i check it? In documentation it says its void(returns no value). Sorry for being stupid...

I done this calll:

gl_FragColor=vec4(Time,1.0,1.0,1.0);

It had no effect. Seems Time is not being passed. What else i can do?

Last edited by Skywriter (2014-07-18 16:40:36)

Re: Need help with a texture scroll shader

I just meant to check if the function was called, because "if (material)" and "if (FXRef)" needs to be true.
You can just use a printf to be sure you enter the code :

render->sendUniformFloat(fxId, "Time", &time);
printf("send uniform now\n"); // DEBUG

if "send uniform now" doesn't appear in the console, it means you don't enter the code.

Re: Need help with a texture scroll shader

I'm happy to announce that shader code worked fine. Thanks anael for help. This is the final working code:

VERT shader:

//VERT

attribute vec3 Vertex;
attribute vec2 TexCoord0;

varying vec2 TexCoord;  

uniform mat4 TextureMatrix[8];
uniform mat4 ProjModelViewMatrix;

void main(void) 
{  
    TexCoord = vec2(TextureMatrix[0] * vec4(TexCoord0, 1.0,1.0));  
    gl_Position = ProjModelViewMatrix * vec4(Vertex,1.0);  
} 

FRAG shader:

//FRAG shader

uniform float Time;
uniform sampler2D Texture[8];

  
varying vec2 TexCoord;  

void main(void) 
{  
    gl_FragColor = texture(Texture[0], vec2(TexCoord.x+Time,TexCoord.y));
}   

For the ones that will be implementing the code, they will need a code that updates the "Time" variable. C++ or hopefully lua in the future.

Last edited by Skywriter (2014-07-19 12:11:27)