position 是一个用户命名的变量。 position 对 WebGL 没有任何意义。它可能被称为foo 或bar 或whatever。它对 WebGL 没有意义,就像变量 xyz 在 JavaScript 中没有意义一样
JavaScript
var xyz = 123; // this has no meaning to JavaScript, only to the programmer
var position = 789; // this has no meaning to JavaScript either.
WebGL GLSL
attribute vec3 xyz; // this has no meaning to WebGL
attribute vec3 position; // this has no meaning to WebGL either
projectionMatrix 也是如此。它是程序员制作的变量。 WebGL 不在乎名称是什么。如果您正在使用某个库(例如 three.js),它可能会为变量组成一些名称,但这些变量和选择的名称是库的一部分,而不是 WebGL 的一部分。日本程序员可能会使用 haichi 和 shaeigyouretu 之类的名称,而不是 position 和 projectionMatrix。
以gl_ 开头的变量是特殊的全局变量。在WebGL Quick Reference Card 上有一个列表。 所有其他变量均由程序员编写。
内置输入、输出和常量 [7]
着色器程序使用特殊变量与管道的固定功能部分进行通信。输出特殊变量可以在写入后读回。输入特殊变量是只读的。所有特殊变量都具有全局范围。
顶点着色器特殊变量 [7.1]
Outputs:
Variable |Description | Units or coordinate system
---------------------------+----------------------+------------------
highp vec4 gl_Position; |transformed vertex |clip coordinates
|position |
---------------------------+----------------------+------------------
mediump float gl_PointSize;|transformed point size|pixels
|(point rasterization |
|only) |
---------------------------+----------------------+------------------
片段着色器特殊变量 [7.2]
片段着色器可以写入gl_FragColor 或gl_FragData[] 的一个或多个元素,但不能同时写入。 gl_FragData 数组的大小由内置常量 gl_MaxDrawBuffers 给出。
Inputs:
Variable |Description | Units or coordinate system
---------------------------+----------------------+------------------
mediump vec4 gl_FragCoord; |fragment position | window coordinates
| within frame buffer |
---------------------------+----------------------+------------------
bool gl_FrontFacing; |fragment belongs to a | Boolean
|front-facing primitive|
---------------------------+----------------------+------------------
mediump vec2 gl_PointCoord;|fragment position | 0.0 to 1.0 for
|within a point (point | each component
|rasterization only) |
---------------------------+----------------------+------------------
Outputs:
Variable |Description | Units or coordinate system
---------------------------+----------------------+------------------
mediump vec4 gl_FragColor; |fragment color | RGBA color
---------------------------+----------------------+------------------
mediump vec4 gl_FragData[n]|fragment color for | RGBA color
|color attachment n |
---------------------------+----------------------+------------------
具有最小值的内置常量 [7.4]
Built-in Constant | Minimum value
--------------------------------------------------+------------------
const mediump int gl_MaxVertexAttribs | 8
--------------------------------------------------+------------------
const mediump int gl_MaxVertexUniformVectors | 128
--------------------------------------------------+------------------
const mediump int gl_MaxVaryingVectors 8 |
--------------------------------------------------+------------------
const mediump int gl_MaxVertexTextureImageUnits | 0
--------------------------------------------------+------------------
const mediump int gl_MaxCombinedTextureImageUnits | 8
--------------------------------------------------+------------------
const mediump int gl_MaxTextureImageUnits | 8
--------------------------------------------------+------------------
const mediump int gl_MaxFragmentUniformVectors | 16
--------------------------------------------------+------------------
const mediump int gl_MaxDrawBuffers | 1
--------------------------------------------------+------------------
内置统一状态 [7.5]
在窗口坐标中指定深度范围。如果一个实现确实
在片段语言中不支持 highp 精度,并且 state 被列为 highp,那么该 state 在片段中将仅作为 mediump 可用
语言。
struct gl_DepthRangeParameters {
highp float near; // n
highp float far; // f
highp float diff; // f - n
};
uniform gl_DepthRangeParameters gl_DepthRange;
作为 var 和 position 一样,作为 vec3,这也是程序员的决定。成为vec4 或float 或任何你想要的都一样好。
虽然着色器可以是您想要的任何东西,并且可以使用您想要的任何变量名称,但最常见的顶点着色器可能类似于
attribute vec4 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * position;
}
一些程序员将vec3 用于position,但无论如何他们都必须将其强制转换为vec4
attribute vec3 position;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1);
}
而且由于 vec4 属性的 w 值默认为 1.0,因此没有理由手动进行。
You might find these articles helpful in explaining WebGL