Question

I don't understand how to write the tiling code to repeat an image on a mesh : the material contains the texture, so is it TEXCOORD0 that should be used instead of the "vertex" information?


EDIT: Previous code :

vertex :

varying vec2 v_texCoord0;

void main() {
    vec2 modXZ = vec2 (mod (a_position.x, u_widthImg),
                       mod (a_position.z, u_heightImg));

    v_texCoord0 = modXZ / vec2 (u_widthImg, u_heightImg);

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = u_projTrans * u_worldTrans * vec4(a_position, 1.0);
}

fragment :

uniform sampler2D u_texture0;
varying vec2 v_texCoord0; 

void main() {
    vec4 color1 = texture2D(u_texture0, gl_TexCoord[0].st);
    gl_FragColor = vec4(v_texCoord0.x *color1, 0, v_texCoord0.y*color1, 1.0);
}

It says "too many arguments for constructor vec4". I don't know what this means, maybe it is about gl_TexCoord[0].


EDIT NEW CODE :

vertex :

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_worldTrans;
uniform mat4 u_projTrans;
uniform float u_widthImg;
uniform float u_heightImg;

varying vec4 v_color;
varying vec2 v_texCoord0;

void main() {
    v_color = a_color;
    vec2 modXZ = vec2 (mod (a_position.x, u_widthImg),
                       mod (a_position.z, u_heightImg));

    v_texCoord0 = modXZ / vec2 (u_widthImg, u_heightImg);
    gl_Position = u_projTrans * u_worldTrans * vec4(a_position, 1.0);
}

fragment :

#ifdef GL_ES 
precision mediump float;
#endif

uniform sampler2D u_texture0;

varying vec2 v_texCoord0; 
varying vec4 v_color;

void main() {
    vec4 color = texture2D(u_texture0, v_texCoord0) * v_color;
    gl_FragColor = color;
}
Was it helpful?

Solution

First of all, this is not a GLSL shader.

float2, half4, tex2D (...), : SV_POSITION, etc. are all HLSL / Cg.

What is more, the modulus operator is not going to work in GLSL on a floating-point type.

The % operator may not even work on an integer type if your GPU does not support GL_EXT_gpu_shader4. You could use the built-in mod (...) function, but again your shader is not actually GLSL to begin with.


UPDATE:

You are currently mismatching the dimensionality of mod (...). It will return a floating-point scalar, but you are trying to store that in a vec2.

Use this instead:

void main() {
    vec2 modXZ = vec2 (mod (a_position.x, u_widthImg),
                       mod (a_position.z, u_heightImg));

    v_texCoord0 = modXZ / vec2 (u_widthImg, u_heightImg);

    gl_Position = u_projTrans * u_worldTrans * vec4(a_position, 1.0);
}

As for the last error in your question, that comes from your fragment shader, which you have not shown. I know this because the line number is out-of-synch with the others and the only z swizzle in your shader is for a_position, which is declared as vec3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top