'Constructor' 参数太多 [顶点着色器]

'Constructor' has too many arguments [vertex shader]

我正在使用 WebGL 并在我的 .html 文件中编写顶点着色器,该文件与我的程序的 .js 文件一起使用。这里主要是处理光照。

我收到的错误是:顶点着色器编译失败。错误日志 is:ERROR: 0:29: 'constructor' : too many arguments 错误:0:32:'dot':未找到匹配的重载函数

29和32对应下面代码中的(见注释)

这是我的顶点着色器代码

<script id="vertex-shader" type="x-shader/x-vertex">

attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec3 a_Normal;
attribute vec2 a_Texture;

uniform mat4 u_MvpMatrix;
uniform mat3 u_NormalMatrix;

uniform vec3 uAmbientColor; // all 3 of these passed in .js
uniform vec3 uLightPosition; //
uniform vec3 uLightColor; //

varying vec4 v_Color;
varying vec2 v_Texture;

varying vec3 vLightWeighting; 

void
main()
{

    vec4 eyeLightPos = vec4(0.0, 200.0, 0.0, 1.0); // Line 29***

    vec4 eyePosition = u_MvpMatrix * vec4(a_Position, 1.0); // vertex position in the eye space

    vec3 normal = normalize(u_NormalMatrix * a_Normal);
    float nDotL = max(dot(normal, eyeLightPos), 0.0);  // Line 32***
    v_Color = vec4(a_Color.rgb * nDotL, a_Color.a);

    v_Texture = a_Texture;


    ////*************
    vec3 eyeLightVector = normalize(eyeLightPos.xyz - eyePosition.xyz);

    vec3 eyeViewVector = -normalize(eyePosition.xyz); // eye position is at (0, 0, 0) in the eye space
    vec3 eyeReflectVector = -reflect(eyeLightVector, normal);

    float shininess = 16.0;
    float specular = pow(max(dot(eyeViewVector, eyeReflectVector),0.0), shininess);;
    vLightWeighting = uAmbientColor + uLightColor * nDotL + uLightColor * specular;
}
</script>

为什么会这样?如果您想查看其他内容,请告诉我。

您很可能将 29 标记为错误的行。错误发生在以下两行:

vec4 eyePosition = u_MvpMatrix * vec4(a_Position, 1.0);

问题是,a_Position 已经是 vec4,因此您尝试调用不存在的 vec4(vec4, float) 形式的构造函数。也许您只想传递 a_Position 的前三个轴,在这种情况下,代码将是:

vec4 eyePosition = u_MvpMatrix * vec4(a_Position.xyz, 1.0);

第二个错误是因为您的类型不匹配。在点方法中 normal 是一个 vec3 但 eyeLightPos 是一个 vec4。 dot函数只定义了两个相同类型的参数。

vec4 eyePosition = u_MvpMatrix * vec4(a_Position);

a_Position 已经有 4 个向量,在 eyePosition 中,你乘以 5 个向量

是否删除最后一个轴 vec4(a_Position);vec4(a_Position.xyz, 1.0);