threejs 实现鼠标大面积选取场景内3d模型,SelectionBox API 案例使用

本文主要是介绍threejs 实现鼠标大面积选取场景内3d模型,SelectionBox API 案例使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

SelectionBox API 案例使用

这个函数创建了一个 3D 场景,包括一个相机、光源、多个立方体以及一个 WebGL 渲染器,并在页面上渲染这个场景


function init() {// 创建一个容器 div 元素并将其添加到页面的 body 中container = document.createElement('div');document.body.appendChild(container);// 创建透视相机,并设置位置camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 500);camera.position.z = 50;// 创建场景,并设置背景颜色scene = new THREE.Scene();scene.background = new THREE.Color(0xf0f0f0);// 添加环境光scene.add(new THREE.AmbientLight(0xaaaaaa));// 创建聚光灯,并设置位置、角度和阴影属性const light = new THREE.SpotLight(0xffffff, 10000);light.position.set(0, 25, 50);light.angle = Math.PI / 5;light.castShadow = true;light.shadow.camera.near = 10;light.shadow.camera.far = 100;light.shadow.mapSize.width = 1024;light.shadow.mapSize.height = 1024;scene.add(light);// 创建立方体几何体const geometry = new THREE.BoxGeometry();// 创建 200 个立方体对象,并设置其位置、旋转、缩放以及阴影属性,并将其添加到场景中for (let i = 0; i < 200; i++) {const object = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff }));object.position.x = Math.random() * 80 - 40;object.position.y = Math.random() * 45 - 25;object.position.z = Math.random() * 45 - 25;object.rotation.x = Math.random() * 2 * Math.PI;object.rotation.y = Math.random() * 2 * Math.PI;object.rotation.z = Math.random() * 2 * Math.PI;object.scale.x = Math.random() * 2 + 1;object.scale.y = Math.random() * 2 + 1;object.scale.z = Math.random() * 2 + 1;object.castShadow = true;object.receiveShadow = true;scene.add(object);}// 创建 WebGL 渲染器,并设置像素比例、大小和阴影属性renderer = new THREE.WebGLRenderer({ antialias: true });renderer.setPixelRatio(window.devicePixelRatio);renderer.setSize(window.innerWidth, window.innerHeight);renderer.shadowMap.enabled = true;renderer.shadowMap.type = THREE.PCFShadowMap;// 将渲染器的 DOM 元素添加到容器中container.appendChild(renderer.domElement);// 创建性能监视器,并将其 DOM 元素添加到容器中stats = new Stats();container.appendChild(stats.dom);// 监听窗口大小变化事件,并调用 onWindowResize 函数window.addEventListener('resize', onWindowResize);
}

当窗口大小改变时调整相机的纵横比和渲染器的大小

// 当窗口大小改变时调整相机的纵横比和渲染器的大小
function onWindowResize() {camera.aspect = window.innerWidth / window.innerHeight;camera.updateProjectionMatrix();renderer.setSize(window.innerWidth, window.innerHeight);
}

// 动画函数,用于更新场景并渲染帧

function animate() {requestAnimationFrame(animate);render();stats.update(); // 更新性能监视器
}

// 渲染函数,用于渲染场景

function render() {renderer.render(scene, camera);
}

// 创建一个选择框对象和一个选择辅助器对象

const selectionBox = new SelectionBox(camera, scene);
const helper = new SelectionHelper(renderer, 'selectBox');

// 监听鼠标按下事件

document.addEventListener('pointerdown', function(event) {// 将所有被选择的物体的发光效果重置为黑色for (const item of selectionBox.collection) {item.material.emissive.set(0x000000);}// 设置选择框的起始点坐标selectionBox.startPoint.set((event.clientX / window.innerWidth) * 2 - 1,-(event.clientY / window.innerHeight) * 2 + 1,0.5);
});

// 监听鼠标移动事件

document.addEventListener('pointermove', function(event) {// 如果选择辅助器处于按下状态if (helper.isDown) {// 将所有被选择的物体的发光效果重置为黑色for (let i = 0; i < selectionBox.collection.length; i++) {selectionBox.collection[i].material.emissive.set(0x000000);}// 设置选择框的结束点坐标selectionBox.endPoint.set((event.clientX / window.innerWidth) * 2 - 1,-(event.clientY / window.innerHeight) * 2 + 1,0.5);// 执行选择框的选择操作,并获取所有被选择的物体const allSelected = selectionBox.select();// 将所有被选择的物体的发光效果设置为白色for (let i = 0; i < allSelected.length; i++) {allSelected[i].material.emissive.set(0xffffff);}}
});

// 监听鼠标松开事件

document.addEventListener('pointerup', function(event) {// 设置选择框的结束点坐标selectionBox.endPoint.set((event.clientX / window.innerWidth) * 2 - 1,-(event.clientY / window.innerHeight) * 2 + 1,0.5);// 执行选择框的选择操作,并获取所有被选择的物体const allSelected = selectionBox.select();// 将所有被选择的物体的发光效果设置为白色for (let i = 0; i < allSelected.length; i++) {allSelected[i].material.emissive.set(0xffffff);}
});

全部源码


<!DOCTYPE html>
<html lang="en"><head><title>three.js webgl - box selection</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"><link type="text/css" rel="stylesheet" href="main.css"><style>body {background-color: #f0f0f0;color: #000;touch-action: none;}a {color: #08e;}.selectBox {border: 1px solid #55aaff;background-color: rgba(75, 160, 255, 0.3);position: fixed;}</style></head><body><div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - box selection</div><script type="importmap">{"imports": {"three": "../build/three.module.js","three/addons/": "./jsm/"}}</script><script type="module">import * as THREE from 'three';import Stats from 'three/addons/libs/stats.module.js';import { SelectionBox } from 'three/addons/interactive/SelectionBox.js';import { SelectionHelper } from 'three/addons/interactive/SelectionHelper.js';let container, stats;let camera, scene, renderer;init();animate();function init() {container = document.createElement( 'div' );document.body.appendChild( container );camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 500 );camera.position.z = 50;scene = new THREE.Scene();scene.background = new THREE.Color( 0xf0f0f0 );scene.add( new THREE.AmbientLight( 0xaaaaaa ) );const light = new THREE.SpotLight( 0xffffff, 10000 );light.position.set( 0, 25, 50 );light.angle = Math.PI / 5;light.castShadow = true;light.shadow.camera.near = 10;light.shadow.camera.far = 100;light.shadow.mapSize.width = 1024;light.shadow.mapSize.height = 1024;scene.add( light );const geometry = new THREE.BoxGeometry();for ( let i = 0; i < 200; i ++ ) {const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );object.position.x = Math.random() * 80 - 40;object.position.y = Math.random() * 45 - 25;object.position.z = Math.random() * 45 - 25;object.rotation.x = Math.random() * 2 * Math.PI;object.rotation.y = Math.random() * 2 * Math.PI;object.rotation.z = Math.random() * 2 * Math.PI;object.scale.x = Math.random() * 2 + 1;object.scale.y = Math.random() * 2 + 1;object.scale.z = Math.random() * 2 + 1;object.castShadow = true;object.receiveShadow = true;scene.add( object );}renderer = new THREE.WebGLRenderer( { antialias: true } );renderer.setPixelRatio( window.devicePixelRatio );renderer.setSize( window.innerWidth, window.innerHeight );renderer.shadowMap.enabled = true;renderer.shadowMap.type = THREE.PCFShadowMap;container.appendChild( renderer.domElement );stats = new Stats();container.appendChild( stats.dom );window.addEventListener( 'resize', onWindowResize );}function onWindowResize() {camera.aspect = window.innerWidth / window.innerHeight;camera.updateProjectionMatrix();renderer.setSize( window.innerWidth, window.innerHeight );}//function animate() {requestAnimationFrame( animate );render();stats.update();}function render() {renderer.render( scene, camera );}const selectionBox = new SelectionBox( camera, scene );const helper = new SelectionHelper( renderer, 'selectBox' );document.addEventListener( 'pointerdown', function ( event ) {for ( const item of selectionBox.collection ) {item.material.emissive.set( 0x000000 );}selectionBox.startPoint.set(( event.clientX / window.innerWidth ) * 2 - 1,- ( event.clientY / window.innerHeight ) * 2 + 1,0.5 );} );document.addEventListener( 'pointermove', function ( event ) {if ( helper.isDown ) {for ( let i = 0; i < selectionBox.collection.length; i ++ ) {selectionBox.collection[ i ].material.emissive.set( 0x000000 );}selectionBox.endPoint.set(( event.clientX / window.innerWidth ) * 2 - 1,- ( event.clientY / window.innerHeight ) * 2 + 1,0.5 );const allSelected = selectionBox.select();for ( let i = 0; i < allSelected.length; i ++ ) {allSelected[ i ].material.emissive.set( 0xffffff );}}} );document.addEventListener( 'pointerup', function ( event ) {selectionBox.endPoint.set(( event.clientX / window.innerWidth ) * 2 - 1,- ( event.clientY / window.innerHeight ) * 2 + 1,0.5 );const allSelected = selectionBox.select();for ( let i = 0; i < allSelected.length; i ++ ) {allSelected[ i ].material.emissive.set( 0xffffff );}} );</script></body>
</html>

本内容来源于小豆包,想要更多内容请跳转小豆包 》

这篇关于threejs 实现鼠标大面积选取场景内3d模型,SelectionBox API 案例使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/826033

相关文章

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u