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

相关文章

Java Stream流与使用操作指南

《JavaStream流与使用操作指南》Stream不是数据结构,而是一种高级的数据处理工具,允许你以声明式的方式处理数据集合,类似于SQL语句操作数据库,本文给大家介绍JavaStream流与使用... 目录一、什么是stream流二、创建stream流1.单列集合创建stream流2.双列集合创建str

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。