javascript - three.js 中线条的颜色
问题描述
我有这样的代码:
const materialLinearInterpolation = new THREE.LineBasicMaterial({ color: 0x0000c9, linewidth: 1 })
const pointsLinearInterpolation = []
for (var i = 0; i < this.pointsCoordinatesLinearInterpolation.length; i++) {
pointsLinearInterpolation.push(
new THREE.Vector3(
this.pointsCoordinatesLinearInterpolation[i].x,
this.pointsCoordinatesLinearInterpolation[i].y,
this.pointsCoordinatesLinearInterpolation[i].z
)
)
}
const geometryLinearInterpolation = new THREE.BufferGeometry().setFromPoints(pointsLinearInterpolation)
this.lineLinearInterpolation = new THREE.Line(geometryLinearInterpolation, materialLinearInterpolation)
this.scene.add(this.lineLinearInterpolation)
我需要为线条使用多种颜色,这样的配置可以吗?如果不可能,我怎样才能绘制几条不同颜色的连接线
解决方案
如果您希望每条线段有不同的颜色,则必须使用THREE.LineSegment
和顶点颜色,如下面的示例中所示:
let camera, scene, renderer;
init();
render();
function init() {
camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 10);
camera.position.z = 3;
scene = new THREE.Scene();
const geometry = new THREE.BufferGeometry().setFromPoints([new THREE.Vector3(1, 0, 0), new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 0, 0), new THREE.Vector3(-1, 0, 0)]);
const colors = [
255, 255, 0, 255, 255, 0,
0, 255, 255, 0, 255, 255
];
geometry.setAttribute('color', new THREE.Uint8BufferAttribute(colors, 3, true));
const material = new THREE.LineBasicMaterial({
vertexColors: true
});
const lines = new THREE.LineSegments(geometry, material);
scene.add(lines);
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
}
function render() {
renderer.render(scene, camera);
}
body {
margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/three@0.130.1/build/three.min.js"></script>
相反,使用THREE.Line
会在线条部分之间产生通常不需要的颜色渐变。
推荐阅读
- javascript - 网站教程中的 Next.js 路由错误
- laravel - 如何按条件从 laravel eloquent 集合中获取项目?
- ios - iOS 13 UIBarButtonItem 在深色模式下的颜色
- r - 你如何在数据框中只保留成对的行?
- javascript - 使用 360° 视频创建一个球体,使用 A-Frame 实现了threejs
- c - STM32F4 的 USB CDC 设置和库问题
- android - 如何从云功能更新 Firestore 文档中的时间戳字段?
- angular - Angular 7如何使用自定义指令属性获取所有子元素
- python-3.x - 带有错误的非线性模型:将对象评估为数值
- javascript - 不可变地更新对象列表中的对象列表