GLSL 2.0颜色和纹理
嗨,我是GLSL的新手,我有一些问题 我正在尝试创建一对GLSL着色器来使用颜色或纹理,但我一定是做错了什么。 问题是,如果将uUseTexture设置为0(这应该表示颜色),它将不起作用(对象未着色)。我知道着色代码是单独工作的,有没有提示为什么它不能使用if语句 代码如下: //碎片GLSL 2.0颜色和纹理,glsl,webgl,Glsl,Webgl,嗨,我是GLSL的新手,我有一些问题 我正在尝试创建一对GLSL着色器来使用颜色或纹理,但我一定是做错了什么。 问题是,如果将uUseTexture设置为0(这应该表示颜色),它将不起作用(对象未着色)。我知道着色代码是单独工作的,有没有提示为什么它不能使用if语句 代码如下: //碎片 precision mediump float; uniform int uUseTexture; uniform sampler2D uSampler; varying vec4 vColor; vary
precision mediump float;
uniform int uUseTexture;
uniform sampler2D uSampler;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
if(uUseTexture == 1) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
} else {
gl_FragColor = vColor;
}
}
uniform float uUseTexture;
uniform sampler2D uSampler;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
// vTextureCoord is already a vec2, BTW
vec4 texColor = texture2D(uSampler, vTextureCoord) * uUseTexture;
vec4 vertColor = vColor * (1.0 - uUseTexture);
gl_FragColor = texColor + vertColor;
}
//顶点
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
attribute vec2 aTextureCoord;
uniform int uUseTexture;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
if(uUseTexture == 1) {
vTextureCoord = aTextureCoord;
} else {
vColor = aVertexColor;
}
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
// It may actually be faster to just assign both of these anyway
vTextureCoord = aTextureCoord;
vColor = aVertexColor;
}
立即浏览一下您的代码,我不会想到什么,但我想花点时间指出,不需要if语句就可以涵盖这个用例。例如,让我们将
uUseTexture
视为浮点而不是int(您可以在着色器中投射它,但这更有趣):
//顶点
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
attribute vec2 aTextureCoord;
uniform int uUseTexture;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
if(uUseTexture == 1) {
vTextureCoord = aTextureCoord;
} else {
vColor = aVertexColor;
}
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
// It may actually be faster to just assign both of these anyway
vTextureCoord = aTextureCoord;
vColor = aVertexColor;
}
//碎片
precision mediump float;
uniform int uUseTexture;
uniform sampler2D uSampler;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
if(uUseTexture == 1) {
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
} else {
gl_FragColor = vColor;
}
}
uniform float uUseTexture;
uniform sampler2D uSampler;
varying vec4 vColor;
varying vec2 vTextureCoord;
void main(void) {
// vTextureCoord is already a vec2, BTW
vec4 texColor = texture2D(uSampler, vTextureCoord) * uUseTexture;
vec4 vertColor = vColor * (1.0 - uUseTexture);
gl_FragColor = texColor + vertColor;
}
现在,uUseTexture只是作为一个调制器,调节您想要使用的每个颜色源的数量。更灵活的是,可以将其设置为0.5,并获得半纹理/半顶点颜色
可能会让您感到惊讶的是,当您使用这样的if语句时,着色器编译器很有可能在幕后这么做。这样对硬件来说通常效率更高。这个
vec2(vtexturecord.s,vtexturecord.t)
应该是vtexturecord.st
。您应该查看GLSL中的工作原理。您的意思是,如果将if(uUseTexture==1)
替换为if(true)
,相同的着色器代码也可以工作吗?感谢您的快速响应,发现了问题,与着色器无关,结果是我使用项目大小22而不是2来缓冲纹理坐标。。。我需要多睡一会儿:)谢谢你的提示,我会试试看:)