【问题标题】:How to color only the faces where the normals are perpendicular to the camera如何仅对法线垂直于相机的面着色
【发布时间】:2019-07-17 00:23:09
【问题描述】:

我正在尝试计算一个着色器,该着色器需要在法线垂直于相机的面上变暗(点积为 0)。那么基本上我如何获得这个点积? 如何解决以下问题?

uniform float time;
uniform vec3 eye_dir;

varying float darkening;

void main(){

  float product=dot(normalize(eye_dir),normalize(normal.xyz));

  darkening=product;

  gl_Position=
  projectionMatrix*
  modelViewMatrix*
  vec4(position,1.);
}
// in THREE.js
this.camera.getWorldDirection(this.eyeDir);
...
cell.material.uniforms.eye_dir = new Uniform(this.eyeDir);

【问题讨论】:

    标签: three.js glsl


    【解决方案1】:

    要做你想做的事情,你必须计算从片段到相机的向量。最简单的方法是在视图空间(相机空间)中进行,因为在视图空间中相机的位置是 (0, 0, 0)。
    position 通过modelViewMatrix 从模型空间转换到视图空间,将normal 通过normalMatrix 从模型空间转换到视图空间。见WebGLProgram

    由于向量指向相同方向时点积的结果为1.0,因此变暗为1.0 - abs(dotproduct)

    varying float darkening;
    
    void main(){
    
        vec4 view_pos = modelViewMatrix * vec4(position, 1.0);
    
        vec3 view_dir = normalize(-view_pos.xyz); // vec3(0.0) - view_pos;
        vec3 view_nv  = normalize(normalMatrix * normal.xyz);
    
        float NdotV   = dot(view_dir, view_nv);
        darkening     = 1.0 - abs(NdotV);
    
        gl_Position   = projectionMatrix * view_pos;
    }
    

    注意,eye_dirnormalDot product 根本没有任何意义,因为 eye_dir 是世界空间中的向量,normal 是模型(对象)空间中的向量。

    【讨论】:

    • 感谢您的详细解释。我的障碍是视图空间与世界空间的想法。很难想象。
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2014-04-14
    • 2017-12-15
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多