着色器中的Threejs正常值设置为0

Fla*_*vio 2 shader webgl vertex-shader fragment-shader three.js

我正在尝试使用本教程,但我遇到了两个问题,其中一个问题可以在这里找到.另一个是以下.

为方便起见,这是应该工作的代码,这里是一个jsfiddle.

顶点着色器:

uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
attribute vec3 position;
uniform vec3 normal;
varying vec3 vNormal;

void main() {
    test = 0.5;
    vNormal = normal;
    gl_Position = projectionMatrix *
                modelViewMatrix *
                vec4(position,1.0);
}
Run Code Online (Sandbox Code Playgroud)

片段着色器:不同的mediump vec3 vNormal;

void main() {
    mediump vec3 light = vec3(0.5, 0.2, 1.0);

    // ensure it's normalized
    light = normalize(light);

    // calculate the dot product of
    // the light to the vertex normal
    mediump float dProd = max(0.0, dot(vNormal, light));

    // feed into our frag colour
    gl_FragColor = vec4(dProd, // R
                        dProd, // G
                        dProd, // B
                        1.0);  // A
}
Run Code Online (Sandbox Code Playgroud)

该值normal在顶点着色器或至少值vNormal在片段着色器似乎是0是应该显示黑色停留的球体.只要我gl_FragColor手动更改值,球体就会改变颜色.谁能告诉我为什么这不起作用?

Ant*_*ton 6

在你的顶点着色器中,vec3 normal应该是一个属性(因为每个顶点都有一个法线)而不是一个统一的:

attribute vec3 normal;
Run Code Online (Sandbox Code Playgroud)

是代码的工作版本.