[英]Set transparency of buffer geometry vertex colors rather than color in threejs
I have a particle cloud I'm making in threejs with buffer geometry, and I am connecting those particles via THREE.LineSegments with a THREE.LineBasicMaterial:我有一个粒子云,我在 threejs 中使用缓冲区几何形状制作,我通过 THREE.LineSegments 将这些粒子与 THREE.LineBasicMaterial 连接起来:
As you can (kind of) see, some of the lines are black or gray - I want to make it so the lines are transparent shades of white.正如你可以(有点)看到的那样,有些线条是黑色或灰色的——我想把它做成透明的白色阴影。
Here is, I believe, the relevant lines of code:我相信这是相关的代码行:
var material = new THREE.LineBasicMaterial({
vertexColors: THREE.VertexColors,
blending: THREE.AdditiveBlending,
color: 0xffffff,
transparent: true,
});
var colors = new Float32Array(segments * 3);
geometry.addAttribute(
"color",
new THREE.BufferAttribute(colors, 3).setDynamic(true)
); //will allow us to set the color of the lines between particles in buffer geometry
linesMesh = new THREE.LineSegments(geometry, material);
...
animate(){
for (var i = 0; i < particleCount; i++) { //loop through particles to make line connections
for (var j = i + 1; j < particleCount; j++) { //check collision
var dist = Math.sqrt(dx * dx + dy * dy + dz * dz); //getting particle positions to neighbors
if (dist < effectController.minDistance) { //make a connection
var alpha = 1.0 - dist / effectController.minDistance;
colors[colorpos++] = alpha;
}
}
}
}
The default three.js shader implements vertex colors as a vec3
(RGB), so there is no transparency component.默认的 three.js 着色器将顶点 colors 实现为vec3
(RGB),因此没有透明度组件。
Search for USE_COLOR
in https://github.com/mrdoob/three.js/blob/r118/src/renderers/webgl/WebGLProgram.js (r118) to see this definition.在https://github.com/mrdoob/three.js/blob/r118/src/renderers/webgl/WebGLProgram.js (r118) 中搜索USE_COLOR
以查看此定义。
You could write your own shader to accept a color attribute as a vec4
instead, adding a transparency component to each color.您可以编写自己的着色器来接受颜色属性作为vec4
,为每种颜色添加一个透明度组件。 You will need to look further into how a line-type material uses colors, and how it blends vertex colors along a line.您需要进一步了解线型材质如何使用 colors,以及它如何沿线混合顶点 colors。 Or, because it's your shader, make it do the blending however you want.或者,因为它是您的着色器,所以让它按照您的意愿进行混合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.