【发布时间】:2021-08-04 08:36:24
【问题描述】:
我正在使用threejs 来创建和渲染点云。我想做的是根据每个点的 Z 坐标给每个点一个特定的颜色。这种颜色应该从具有最小 Z 坐标的某种颜色(例如具有最小 Z 坐标的绿色)映射到具有最大 Z 值的蓝色、黄色和红色。按照这种渐变方法为每个点赋予特定颜色的最简单方法是什么?
我的脚本很长,所以我把它简化为只有这几行代码:
function drawCloud(){
for(i = 0; i < totalPoints * 3; i = i + 3){
//Setting the new Z value of each point
//Each third value is the Z value, therefore using i+2
points.geometry.attributes.position.array[i+2] = theNewZValueForThisPoint;
//Now setting the color of each point
// i, i+1 AND i+2 is the R,G, B value of each point
//here each point is set to a green color
points.geometry.attributes.color.array[i] = 0;
points.geometry.attributes.color.array[i+1] = 1;
points.geometry.attributes.color.array[i+2] = 0;
}
}
function animate() {
requestAnimationFrame( animate );
drawCloud();
render();
}
function render() {
renderer.render( scene, camera );
}
我已经创建了一种分割方法,其中每个点在一个范围内都有一个固定的颜色。像这样的:
function getColor() {
if(ZValue > 0 && ZValue < 100){
color = green;
}
if(ZValue > 100 && ZValue < 200){
color = blue;
}
}
这不是我想要的,因为会有一个区域的颜色发生了巨大的变化。我希望它有一个更渐变的方法,随着 Z 值的增加而缓慢变化。
请记住,此代码已在很大程度上进行了简化,以使其保持简单并仅显示基本思想。任何关于其他改进的建议也将不胜感激。
【问题讨论】:
标签: javascript three.js