three.js Gltf模型转换为three.points点云模型
背景需求
- 对gltf格式的点云模型进行测距,调用three.js提供的raycaster射线无法穿透选中;
- 通过官网示例发现有相应实现点云点捕捉的代码,其格式为three.Points,因而存在需求将gltf点云模型转换成three.points模型;
代码实现
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
const loader = new GLTFLoader();
loader.load(gltfurl,
(gltf) => {
let mesh = gltf.scene;
const geometry = gltf.scene.children[0].geometry;
const material = gltf.scene.children[0].material;
const particles = new THREE.Points(geometry, material);
particles.position.x = x;
particles.position.y = y;
particles.position.z = z;
this.scene.add(particles);
},
undefined,
function (error) {
console.error(error);
}
);