我正在使用此脚本渲染光剑(在Blender中), 然而它才能正常工作,直到我开始旋转光剑:此时刀片发疯了。 请有人建议我更好的方法来编写这段代码吗? 这看起来(对我而言)是实现这一目标的最佳方式,但肯定会有更聪明的方法。
VertexShader = """
varying vec3 varyingNormalDirection;
varying vec3 varyingViewDirection;
void main()
{
varyingNormalDirection = normalize(gl_NormalMatrix * gl_Normal);
varyingViewDirection = normalize(gl_ModelViewMatrix * gl_Vertex);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
"""
FragmentShader = """
varying vec3 varyingNormalDirection;
varying vec3 varyingViewDirection;
void main()
{
// I project the two vectors on the plane y = 0
vec2 normalDirection = normalize(varyingNormalDirection.xz);
vec2 viewDirection = normalize(varyingViewDirection.xz);
const vec4 color = vec4(1.0,1.0,1.0,0.3);
// opacity equals infinity when view and normal have an angle of 0,
// equals 0.001 when they have an angle of 90
// I also multiply for the dot product between view and normal
// which is 1 at center, 0 at edge, to increase the falloff
float dotProduct = abs(dot(viewDirection, normalDirection));
float opacity = min(1.0, dotProduct * 0.01 / (1.0 - dotProduct) );
// with opacity I'm also varying the color, from green to white
gl_FragColor = vec4(1.*opacity,1.0,1.*opacity,opacity*2.);
}
"""