当前位置: 首页 > news >正文

three自带的框选工具SelectionBox、SelectionHelper

🧱 一、SelectionBox 是什么?

SelectionBox 是 Three.js 示例库中的一个工具类(examples/jsm/interactive/SelectionBox.js),
用于通过定义一个**三维空间包围盒(Box3)**来选中视野内的物体。

换句话说:

它根据相机、鼠标拖拽时的起点和终点,在 3D 世界中计算出被框选区域(视锥体内)的所有对象。

 

🌐 典型用法

import { SelectionBox } from 'three/examples/jsm/interactive/SelectionBox.js';const selectionBox = new SelectionBox(camera, scene);
  • camera:用于确定视野。

  • scene:搜索物体的根节点。

  • 拖拽时更新起点/终点,再调用 selectionBox.select() 获取选中的对象列表。

 

📦 常用属性/方法

属性 / 方法 说明
startPoint 拖拽起点(NDC 坐标,即 -1~1)
endPoint 拖拽终点(NDC 坐标)
collection 当前选中的对象数组
select() 执行选择逻辑,返回选中的对象数组
updateFrustum() 更新内部视锥体,用于优化性能

🧩 二、SelectionHelper 是什么?

SelectionHelper 是用于辅助显示的一个小类(examples/jsm/interactive/SelectionHelper.js)。

它的作用是:在屏幕上画出一个矩形框(通过 <div> 叠加),显示鼠标框选范围。

它不参与 3D 计算,只负责 UI 层面的可视化。


🌐 用法示例

import { SelectionHelper } from 'three/examples/jsm/interactive/SelectionHelper.js';const helper = new SelectionHelper(renderer, 'selectBox');
  • renderer:Three.js 渲染器,用来确定附着的 DOM。

  • 第二个参数 'selectBox' 是 CSS 类名(可用于自定义样式)。

SelectionHelper 会自动创建一个 <div class="selectBox">
并根据鼠标拖拽动态调整其位置和大小。

 

🧰 三、完整示例 — 鼠标框选物体

下面是一个完整、可运行的代码例子,展示如何结合两者实现框选高亮:

 
import * as THREE from 'three';
import { SelectionBox } from 'three/examples/jsm/interactive/SelectionBox.js';
import { SelectionHelper } from 'three/examples/jsm/interactive/SelectionHelper.js';// 场景基础配置
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set(0, 2, 5);const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);// 添加地面与立方体
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x0088ff });
for (let i = 0; i < 20; i++) {const cube = new THREE.Mesh(geometry, material.clone());cube.position.set(Math.random() * 8 - 4, 0.5, Math.random() * 8 - 4);scene.add(cube);
}// 初始化 SelectionBox 与 Helper
const selectionBox = new SelectionBox(camera, scene);
const helper = new SelectionHelper(renderer, 'selectBox');let selectedObjects = [];function render() {requestAnimationFrame(render);renderer.render(scene, camera);
}
render();// 鼠标事件
function getNormalizedPosition(event) {return new THREE.Vector2((event.clientX / window.innerWidth) * 2 - 1,-(event.clientY / window.innerHeight) * 2 + 1);
}window.addEventListener('pointerdown', (event) => {for (const obj of selectedObjects) obj.material.color.set(0x0088ff); // 重置颜色selectedObjects = [];selectionBox.startPoint = getNormalizedPosition(event);selectionBox.endPoint = getNormalizedPosition(event);
});window.addEventListener('pointermove', (event) => {if (helper.isDown) {selectionBox.endPoint = getNormalizedPosition(event);selectionBox.select(); // 实时更新选中}
});window.addEventListener('pointerup', (event) => {selectionBox.endPoint = getNormalizedPosition(event);const allSelected = selectionBox.select();selectedObjects = allSelected;for (const obj of selectedObjects) obj.material.color.set(0xff8800);
});

🎨 CSS 样式(用于框选矩形)

.selectBox {border: 1px solid #55aaff;background-color: rgba(75, 160, 255, 0.1);position: fixed;pointer-events: none;z-index: 100;
}

🧠 四、交互逻辑总结

操作 功能
mousedown 记录起点、清空旧选中
mousemove 更新终点,更新 SelectionHelper
mouseup 获取选中对象、更新高亮

⚡ 五、扩展建议

  • 可以在 pointerup 事件中配合 OutlinePass 做选中高亮;

  • 或在场景中为选中对象添加包围框;

  • 若对象很多,建议限制参与检测的层级(例如仅检测 scene.children)。

http://www.hskmm.com/?act=detail&tid=32809

相关文章:

  • 2025年精密磨床/CNC机械加工厂家推荐榜单:涵盖铣床/车床/磨削/多轴/复合加工,铝/不锈钢/钛合金/模具钢/塑料件定制,汽车/医疗/航空航天/机器人零件及模具工装夹具加工
  • 2025 电动缸源头厂家最新推荐榜:剖析国产厂商成本优势与技术实力,附权威选购指南
  • 网络编程实践笔记_4_阿贝云_免费云服务器_简易博客_
  • 10 17
  • 2025年铝单板厂家推荐排行榜,氟碳铝单板,木纹铝单板,冲孔铝单板,外墙铝单板,雕花铝单板,异形铝单板,双曲铝单板公司推荐!
  • 2025 年最新推荐热熔胶源头厂家榜:覆盖书刊装订 / 包装等场景,助企业选高性价比产品
  • 开发日志
  • Gitee 2025:中国开发者生态的崛起与本土化优势
  • C++中的new操作符:new operator、operator new、placement new
  • JavaBean知识总结及范例
  • 2025 年家装管道生产厂家最新推荐排行榜:覆盖云南昆明贵州贵阳四川成都重庆,精选优质 PPR/PVC 管道品牌,解决选购难题
  • 同一设备多账号登录,如何避免消息推送“串门”?
  • 强合规行业DevOps选型:告别工具拼凑,找到真正适配的国产化DevOps方案
  • 大疆无人机RTMP推流至LiveNVR实现web页面实时播放与录像回放,并可以转GB28181协议级联推送给上级监控视频管理平台
  • Character Animator 2025下载安装教程:2D角色动画软件零基础入门,附最新下载安装教程及激活方法
  • 2025年彩钢瓦/镀锌板/折弯件/C型钢/Z型钢/压型瓦/楼承板/次檩条厂家推荐排行榜,专业钢结构安装与定制加工实力解析
  • 2025 年最新金相厂家最新推荐排行榜:涵盖金相磨抛机 / 切割机 / 显微镜 / 抛光机 / 预磨机设备,助力企业精准选择优质品牌
  • 武汉图核科技
  • 完整教程:display ospf peer 概念及题目
  • 2025中国开发者必看:主流代码托管平台本土化能力深度测评
  • 开源数据采集工具 logstash(收集日志)/telegraf(收集指标)
  • 2025年粉末冶金制品厂家推荐排行榜,粉末冶金零件,金属注射成形,结构件,齿轮,轴承公司最新精选
  • 2025 年升降平台车厂家最新推荐口碑排行榜:覆盖多类型产品,聚焦实力厂家,为企业选购提供权威参考剪叉式/手动液压/电动液压升降平台车厂家推荐
  • 供应商图纸协同是什么?主要有哪几个核心原则?
  • 2025 年堆高车厂家最新推荐排行榜:聚焦专利技术、华为等大牌合作案例及国内优质品牌解析手动液压/手动液压/卷筒/油桶堆高车厂家推荐
  • 2025 年最新推荐!编码器源头厂家排行榜:聚焦无磁 / 光学 / 脉冲等多类型产品,精选行业优质企业
  • Excelize 开源基础库发布 2.10.0 版本更新
  • 高效搞定outlook大附件怎么发送的方法与技巧
  • 2025年点胶机厂家权威推荐榜:精密点胶设备、自动化点胶系统、桌面点胶机源头厂家综合实力解析
  • HAP 签名提取:从定位到解析的实操指南