在 threejs 中设置缓冲区几何顶点颜色的透明度而不是颜色
Set transparency of buffer geometry vertex colors rather than color in threejs
我有一个粒子云,我在 threejs 中用缓冲区几何形状制作,我通过 THREE.LineSegments 和 THREE.LineBasicMaterial:
连接这些粒子
正如您(有点)看到的那样,有些线条是黑色或灰色的 - 我想将其制作成透明的白色阴影。
我相信这是相关的代码行:
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;
}
}
}
}
简单回答
默认 three.js 着色器将顶点颜色实现为 vec3
(RGB),因此没有透明度组件。
在 https://github.com/mrdoob/three.js/blob/r118/src/renderers/webgl/WebGLProgram.js (r118) 中搜索 USE_COLOR
以查看此定义。
高级答案
您 可以 编写自己的着色器来接受颜色属性作为 vec4
,为每种颜色添加透明度组件。您将需要进一步研究线型 material 如何使用颜色,以及它如何沿线混合顶点颜色。或者,因为它是您的着色器,所以让它按照您想要的方式进行混合。
我有一个粒子云,我在 threejs 中用缓冲区几何形状制作,我通过 THREE.LineSegments 和 THREE.LineBasicMaterial:
连接这些粒子正如您(有点)看到的那样,有些线条是黑色或灰色的 - 我想将其制作成透明的白色阴影。
我相信这是相关的代码行:
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;
}
}
}
}
简单回答
默认 three.js 着色器将顶点颜色实现为 vec3
(RGB),因此没有透明度组件。
在 https://github.com/mrdoob/three.js/blob/r118/src/renderers/webgl/WebGLProgram.js (r118) 中搜索 USE_COLOR
以查看此定义。
高级答案
您 可以 编写自己的着色器来接受颜色属性作为 vec4
,为每种颜色添加透明度组件。您将需要进一步研究线型 material 如何使用颜色,以及它如何沿线混合顶点颜色。或者,因为它是您的着色器,所以让它按照您想要的方式进行混合。