我正在使用Three.js
,我有一个ParticleSystem
每个粒子可能有不同的透明度和颜色的地方。
代码:
var shaderMaterial = new THREE.ShaderMaterial({
uniforms: customUniforms,
attributes: customAttributes,
vertexShader: document.getElementById('vertexshader').textContent,
fragmentShader: document.getElementById('fragmentshader').textContent,
transparent: true,
alphaTest: 0.5
});
particles = new THREE.ParticleSystem(geometry, shaderMaterial);
particles.dynamic = true;
顶点着色器:
attribute float size;
attribute vec3 color;
varying vec3 vColor;
void main() {
vColor = color;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
//gl_PointSize = size;
gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
gl_Position = projectionMatrix * mvPosition;
}
片段着色器:
uniform sampler2D texture;
uniform float alpha;
varying vec3 vColor;
void main() {
gl_FragColor = vec4(vColor, 100);
vec4 textureColor = texture2D( texture, gl_PointCoord );
gl_FragColor = gl_FragColor * textureColor;
gl_FragColor.a = alpha;
}
纹理是一个圆形,但是当我设置 alpha 时,像这样:gl_FragColor.a = alpha
,我的纹理变成黑色正方形中的圆形,alpha 级别还可以,但我不需要正方形,如果我不设置 alpha,只需要圆形,正方形不出现。那么如何为提供的纹理正确设置 alpha 呢?