我正在通过此处的示例学习着色器。 https://itp-xstory.github.io/p5js-shaders/#/./docs/examples/basic_gradient_texcoord
代码提到使用 attribute vec2 aTexCoord;
作为从 cpu 发送的 texCoordinates,然后尝试分配一个 varying vec2 vTextCoord
与 fragment 共享。我不确定这些命名是否特定于 p5 着色器,但我觉得是因为当我尝试使用 glslViewer 运行它时,出现以下错误
Error linking shader: WARNING: Output of vertex shader 'v_position' not read by fragment shader
WARNING: Output of vertex shader 'v_texcoord' not read by fragment shader
ERROR: Input of fragment shader 'v_texCoord' not written by vertex shader
然后,我将名称(基于错误)更改为 v_texcoord
用于变化和 attribute vec4 vTexCoord;
而不是 aTexCoord
并且它在查看器中工作。 vTexCoord
这个属性的名称通常和 v_texcoord
标准不同吗?还是基于我的 GL_SL 版本?搜索具有这些名称的文档,但我没有得出明确的答案。以下是我的有效代码...
.vert
#ifdef GL_ES
precision mediump float;
#endif
attribute vec3 aPosition;
// attribute vec2 aTexCoord; // from example does not work
// varying vec2 vTexCoord; // from example does not work
attribute vec4 vTexCoord; // works
// attribute vec4 vPosition;
varying vec2 v_texcoord; // works
void main() {
// copy the texture coordinates
// vTexCoord = aTexCoord;
v_texcoord = vTexCoord.st;
// Copy the position data into a vec4, adding 1.0 as the w parameter
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
gl_Position = positionVec4;
}
.frag
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
// varying vec2 vTexCoord; does not work
varying vec2 v_texcoord;
void main() {
vec2 uv = v_texcoord;
vec4 color = vec4(uv.x, uv.y, uv.x + uv.y, 1.0);
gl_FragColor = color;
}
回答1
GLSL 中的属性或纹理坐标没有标准名称。有一些具有特殊名称的输入和输出,例如 gl_Position
和 gl_FragCoord
,但其他一切都取决于您。重要的是着色器之间的名称匹配(顶点着色器的输出必须与 fragment shader 的输入具有完全相同的名称,包括大写),以及着色器和非着色器代码之间的匹配指定名称。
但是,您使用的框架或引擎可能有自己的标准名称。我不知道p5.js。
我认为您收到的错误消息是因为您的变量在顶点和 fragment shader 之间的名称略有不同。