Pau*_*aul 1 shader tiling glsl vertex-shader fragment-shader
我不明白如何编写平铺代码以在网格上重复图像:材质包含纹理,所以TEXCOORD0应该使用它而不是“顶点”信息吗?
编辑:以前的代码:
顶点:
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);
}
Run Code Online (Sandbox Code Playgroud)
片段:
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);
}
Run Code Online (Sandbox Code Playgroud)
它说“构造函数 vec4 的参数太多”。我不知道这是什么意思,也许是关于gl_TexCoord[0].
编辑新代码:
顶点:
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);
}
Run Code Online (Sandbox Code Playgroud)
片段:
#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;
}
Run Code Online (Sandbox Code Playgroud)
float2、half4、tex2D (...)、: SV_POSITION等都是 HLSL / Cg。
该%运营商可能甚至没有在整数类型的工作,如果你的显卡不支持GL_EXT_gpu_shader4。您可以使用内置mod (...)函数,但同样,您的着色器实际上并不是一开始就使用 GLSL。
您当前与 的维度不匹配mod (...)。它将返回一个浮点标量,但您正试图将其存储在vec2.
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);
}
Run Code Online (Sandbox Code Playgroud)
至于您问题中的最后一个错误,它来自您尚未显示的片段着色器。我知道这是因为行号与其他行号不同步,并且z着色器中唯一的混淆是 for a_position,它被声明为vec3.