o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
handleClick(event) { const rect = map.getCanvas().getBoundingClientRect(); const mouseX…
- User
handleClick(event) { const rect = map.getCanvas().getBoundingClientRect(); const mouseX = event.clientX - rect.left; const mouseY = event.clientY - rect.top;
if (mouseX >= 0 && mouseX <= rect.width && mouseY >= 0 && mouseY <= rect.height) { this.mouse.x = (mouseX / rect.width) * 2 - 1; this.mouse.y = - (mouseY / rect.height) * 2 + 1; this.mouseClicked = true; } this.raycaster.ray.origin.copy(this.camera.position); this.raycaster.ray.direction.set(this.mouse.x, this.mouse.y, 1).unproject(this.camera).sub(this.camera.position).normalize(); const distance = 1000; // 1 公里 const farthestPoint = this.raycaster.ray.origin.clone().add(this.raycaster.ray.direction.clone().multiplyScalar(distance)); const box = new THREE.Box3().setFromPoints([this.raycaster.ray.origin, farthestPoint]); // 过滤场景中的模型 const intersects = this.scene.children.filter(child => { const boxChild = new THREE.Box3().setFromObject(child); return box.intersectsBox(boxChild); }); console.info(intersects); if (intersects.length > 0) { console.log('Intersects:', intersects); // 输出所有相交的对象 const selectedModel = intersects[0].object; // 获取选中的模型 console.log('Selected model:', selectedModel); // 检查选中的模型是否存在并有材质 if (selectedModel && selectedModel.material) { const highlightColor = 0xffff00; // 高亮颜色,黄色 if (Array.isArray(selectedModel.material)) { selectedModel.material.forEach(material => { material.emissive.set(highlightColor); // 设置发光颜色 material.color.set(highlightColor); // 设置颜色 }); } else { selectedModel.material.emissive.set(highlightColor); // 设置发光颜色 selectedModel.material.color.set(highlightColor); // 设置颜色 } // 可选:添加边框效果 const outlineMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff }); const outlineMesh = new THREE.Mesh(selectedModel.geometry, outlineMaterial); outlineMesh.scale.multiplyScalar(1.05); selectedModel.add(outlineMesh); } else { console.warn('Selected model does not have a material:', selectedModel); } } else { console.warn('No intersections found.'); }}这个是mapbox的代码,我想选中模型并高亮显示,如何修改, intersects 有数据但是 intersects[0].object;没有object对象
- Assistant