【问题标题】:Threejs normal values in shader are set to 0着色器中的 Threejs 正常值设置为 0
【发布时间】:2015-01-20 00:24:17
【问题描述】:

我试图让这个tutorial 工作,但我遇到了两个问题,其中一个可以找到here。另一个如下。

为方便起见,这是应该工作的代码,这里是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);
}

片段着色器: 变介质p 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
}

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

【问题讨论】:

    标签: three.js webgl shader fragment-shader vertex-shader


    【解决方案1】:

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

    attribute vec3 normal;
    

    Here 是您的代码的工作版本。

    【讨论】:

    • 你是我的英雄!非常感谢!
    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2014-01-14
    • 2014-02-16
    相关资源
    最近更新 更多