【发布时间】:2022-01-01 22:53:28
【问题描述】:
我一直在使用 three.js 制作一个项目,该项目创建一个点材质对象,然后可以与之交互。我正在尝试将图像的像素映射到缓冲区几何图形的顶点,以便将图像显示为一组点(类似点云)。有问题的图像是地球地图(缩小到 106 x 53 像素)
我通过在画布上绘制图像、提取图像数据、从图像数据设置颜色(基于像素坐标)然后设置我的几何图形的颜色属性(在本例中为球体缓冲区)来做到这一点几何学)。我的问题是映射哪里出错了?
这是提取颜色并将它们放置在几何体数组中的代码:
let colors = [];
let color = new THREE.Color();
let positionAttribute = g.attributes.position; //g being geometry
for (let x = 0; x < img.width; x++) {
for (let y = 0; y < img.height; y++) {
let c = earthCanvas.getContext("2d");
let p = c.getImageData(x, y, 1, 1).data;
let hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
color.set(hex);
console.log("set");
colors.push(color.r, color.g, color.b);
}
}
g.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
有什么办法可以让这个看起来像地球一样吗?我只是把像素的坐标放错了吗?
几何本身的代码如下所示:
g = new THREE.SphereBufferGeometry(3,104,52);
count = g.attributes.position.count;
console.log(count);
g.center();
let pointShape = new THREE.TextureLoader().load("./models/particle.png");
m = new THREE.PointsMaterial({
size: pointSize,
map: pointShape,
vertexColors: true,
//blending: THREE.AdditiveBlending,
depthTest: false,
opacity: 1
});
画布的 html 和 JS 看起来像这样:
function drawImageSource(source, canvas) {
img = new Image();
img.addEventListener("load", function () {
// The image can be drawn from any source
canvas
.getContext("2d")
.drawImage(
img,
0,
0,
img.width,
img.height,
0,
0,
canvas.width,
canvas.height
);
});
img.crossOrigin = "Anonymous";
img.setAttribute("src", source);
}
<div id="canvasDiv">
<canvas id="earthCanvas", width="106", height="53" hidden />
</body>
任何帮助将不胜感激,谢谢!
代码沙盒项目的链接在这里: https://codesandbox.io/s/small-hill-mvhgc?file=/src/index.js:5956-6389 (我为混乱的代码道歉,这是一个原型)
【问题讨论】:
标签: javascript three.js point-clouds buffer-geometry