设置缓冲区几何顶点颜色的透明度,而不是Threejs中的颜色

时间:2020-07-15 17:22:30

标签: javascript three.js buffer-geometry

我在用缓冲几何结构的Threejs中制作一个粒子云,并且通过THREE.LineSegments和THREE.LineBasicMaterial连接这些粒子:

enter image description here

如您所见,某些线条是黑色或灰色的-我想制作成这样,所以线条是透明的白色阴影。

我相信,这是相关的代码行:

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;
        
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

简单答案

默认的three.js着色器将顶点颜色实现为vec3(RGB),因此没有透明度组件。

https://github.com/mrdoob/three.js/blob/r118/src/renderers/webgl/WebGLProgram.js(r118)中搜索USE_COLOR,以查看此定义。

高级答案

可以编写自己的着色器,以接受颜色属性作为vec4,而是为每种颜色添加透明度组件。您将需要进一步研究线型材料如何使用颜色,以及如何沿线混合顶点颜色。或者,因为它是您的着色器,所以可以根据需要进行混合。