Perpendicular falloff material

时间:2018-06-04 16:48:21

标签: three.js

I want to make a falloff semi-transparent shader, opaque when normals are perpendicular to camera direction and transparent when normals face towards the camera. Here is the code I use so far :

vec3 vertexNormal = normalize( normalMatrix * normal );
vec3 viewDir = vec3( 0.0, 0.0, 1.0 );
float dotProd = dot(vertexNormal, viewDir);
alpha = abs ( 1.0 - dotProd ); 

It works but when the objects are not located in the center of the camera view, the falloff isn't consistent anymore, farther side have a larger falloff :

Falloff larger towards edge of camera view
Falloff larger towards edge of camera view

Is there a way to get consistent falloff thickness all over the camera view (all sphere would be distorded by perspective but the falloff contour would be the same everywhere) ?

Thanks in advance!

1 个答案:

答案 0 :(得分:0)

除非您使用的是正交相机,否则您的查看目录不正确。

尝试

vec4 vp = modelViewMatrix * vec4( position, 1.);
vec3 viewdir = - normalize(vp.xyz);