Android Shader Syntax Error

Erayd

Member
I'm getting a syntax error at line 20 in my fragment shader and I'm unclear as to what is wrong. Line 20 is the one that is: color2 = vec3(texture2D(colorMap, vec2(1.0, i/mapHeight))

The general goal of this shader is to compare the colors of a sprite in an object with the colors in a vertical 2 line pallet, on the left side would be the sprites colors and the right side next to it would be the color I want to transition to over a period of time, a blink basically.

Fragment Shader:
Code:
varying vec4 v_vColour;

uniform float time;
uniform sampler2D colorMap;
uniform float mapHeight;
varying vec2 v_vTexcoord_A;
varying vec2 v_vTexcoord_B;

void main()
{
    vec4 color1 = texture2D(gm_BaseTexture, v_vTexcoord_A);
    vec3 color2 = vec3(0.0);
 
    for(float i = 0.0; i < mapHeight; i++;){
        if(vec3(color1) == vec3(texture2D(colorMap, vec2(0.0, i/mapHeight)))){
            color2 = vec3(texture2D(colorMap, vec2(1.0, i/mapHeight)));
        }
    }
 
    gl_FragColor.rgb = mix(color1.rgb, color2.rgb, time);
    gl_FragColor.a = color1.a;
}
Vertex Shader:
Code:
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec4 v_vColour;

//UVs
uniform vec4 uv_a;
uniform vec4 uv_b;
varying vec2 v_vTexcoord_A;
varying vec2 v_vTexcoord_B;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
 
    v_vColour = in_Colour;
    v_vTexcoord_A = in_TextureCoord;
    //v_vTexcoord_B = uv_b.xy + (in_TextureCoord - uv_a.xy) * uv_b.zw / uv_a.zw;
}
Object Draw Event:
Code:
///Draw self
shader_set(testShader)

//Set Pallet
var palletSampler = shader_get_sampler_index(testShader, "colorMap");
var palletSprite = sprite_get_texture(my_pal_sprite, 0)
texture_set_stage(palletSampler, palletSprite);
shader_set_uniform_f(shader_get_uniform(testShader, "mapHeight"), my_pallet_sprite.sprite_height);

//Set Time
shader_set_uniform_f(shader_get_uniform(testShader, 'time'), timing);

//Set Locations
var uv_a = sprite_get_uvs(sprite_index, 0);
var uv_b = sprite_get_uvs(my_pal_sprite, 0);
shader_set_uniform_f(shader_get_uniform(testShader, "uv_a"), uv_a[0], uv_a[1], uv_a[2] - uv_a[0], uv_a[3] - uv_a[1]);
shader_set_uniform_f(shader_get_uniform(testShader, "uv_b"), uv_b[0], uv_b[1], uv_b[2] - uv_b[0], uv_b[3] - uv_b[1]);
draw_self();
shader_reset();
 
Last edited:

jo-thijs

Member
I'm getting a syntax error at line 20 in my fragment shader and I'm unclear as to what is wrong. Line 20 is the one that is: color2 = vec3(texture2D(colorMap, vec2(1.0, i/mapHeight))

The general goal of this shader is to compare the colors of a sprite in an object with the colors in a vertical 2 line pallet, on the left side would be the sprites colors and the right side next to it would be the color I want to transition to over a period of time, a blink basically.

Fragment Shader:
Code:
uniform sampler2D colorMap;
uniform float mapHeight;
varying vec2 v_vTexcoord_A;
varying vec2 v_vTexcoord_B;

void main()
{
    vec4 color1 = texture2D(gm_BaseTexture, v_vTexcoord_A);
    vec3 color2 = vec3(0.0);
 
    for(float i = 0.0; i < mapHeight; i++;){
        if(vec3(color1) == vec3(texture2D(colorMap, vec2(0.0, i/mapHeight)))){
            color2 = vec3(texture2D(colorMap, vec2(1.0, i/mapHeight))
        }
    }
 
    gl_FragColor.rgb = mix(color1.rgb, color2.rgb, time);
    gl_FragColor.a = color1.a;
}
Vertex Shader:
Code:
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec4 v_vColour;

//UVs
uniform vec4 uv_a;
uniform vec4 uv_b;
varying vec2 v_vTexcoord_A;
varying vec2 v_vTexcoord_B;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
 
    v_vColour = in_Colour;
    v_vTexcoord_A = in_TextureCoord;
    //v_vTexcoord_B = uv_b.xy + (in_TextureCoord - uv_a.xy) * uv_b.zw / uv_a.zw;
}
Object Draw Event:
Code:
///Draw self
shader_set(testShader)

//Set Pallet
var palletSampler = shader_get_sampler_index(testShader, "colorMap");
var palletSprite = sprite_get_texture(my_pal_sprite, 0)
texture_set_stage(palletSampler, palletSprite);
shader_set_uniform_f(shader_get_uniform(testShader, "mapHeight"), my_pallet_sprite.sprite_height);

//Set Time
shader_set_uniform_f(shader_get_uniform(testShader, 'time'), timing);

//Set Locations
var uv_a = sprite_get_uvs(sprite_index, 0);
var uv_b = sprite_get_uvs(my_pal_sprite, 0);
shader_set_uniform_f(shader_get_uniform(testShader, "uv_a"), uv_a[0], uv_a[1], uv_a[2] - uv_a[0], uv_a[3] - uv_a[1]);
shader_set_uniform_f(shader_get_uniform(testShader, "uv_b"), uv_b[0], uv_b[1], uv_b[2] - uv_b[0], uv_b[3] - uv_b[1]);
draw_self();
shader_reset();
Missing semicolon ;
These are necessary for shaders.

EDIT:
And missing closing parentheses too.
 

Erayd

Member
I added the semicolon, but where am I missing the closing parentheses? Time is a uniform I'm populating using the timing variable in my object, its also a float that adds or subtracts 0.01 every step.

Edit: I found the missing parentheses but I'm still getting a syntax error on that line.
 

jo-thijs

Member
I added the semicolon, but where am I missing the closing parentheses? Time is a uniform I'm populating using the timing variable in my object, its also a float that adds or subtracts 0.01 every step.
You've got 3 opening parentheses and 2 closing ones.
You should just add one ) at the end of the line.

In the code you've provided us, there are no signs of time being defined as a uniform.
 

Erayd

Member
I see that now, I accidentally copied the code wrong and missed the top two lines, I've updated the OP with the changes and the added parentheses and semicolon, still an error on that line though.

EDIT: I found it. There was an added semicolon in the for statement after the i++. I got rid of it, must have added it on accident, thanks everyone!
 

Xor

@XorDev
Try this:
Code:
uniform float time;
uniform float mapHeight;
uniform sampler2D colorMap;

varying vec4 v_vColour;
varying vec2 v_vTexcoord_A;
varying vec2 v_vTexcoord_B;

void main()
{
    vec4 color1 = texture2D(gm_BaseTexture, v_vTexcoord_A);
    vec3 color2 = vec3(0.0);
 
    for(float i = 0.0; i < mapHeight; i++;){
        if(color1.rgb == texture2D(colorMap, vec2(0.0, i/mapHeight)).rgb){
            color2 = vec3(texture2D(colorMap, vec2(1.0, i/mapHeight)));
        }
    }
 
    gl_FragColor.rgb = mix(color1.rgb, color2.rgb, time);
    gl_FragColor.a = color1.a;
}
 
Top