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

vue2使用three.js实现一个旋转球体

vue页面中 

 <div ref="container"></div>

data声明 

scene: null,       camera: null,       renderer: null,       controls: null,      rotationType: 'sphere',       rotationTimer: null,backgroundImageUrl: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/plugin/cake/screen-bg.png',sphereGroup: null,defautNum: 200,

 渲染方法

initThree() {this.clearScene();// 创建场景this.scene = new THREE.Scene();// 设置场景背景为透明this.scene.background = null;// 创建相机this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);this.camera.position.z = 10;// 创建渲染器this.renderer = new THREE.WebGLRenderer({ alpha: true });this.renderer.setSize(window.innerWidth, window.innerHeight);this.renderer.setClearColor(0x000000, 0); // 设置清除颜色为透明const canvas = this.renderer.domElement;canvas.style.zIndex = 1; // 设置画布的 z-indexthis.$refs.container.appendChild(canvas);// 创建控制器this.controls = new OrbitControls(this.camera, canvas);// 创建一个用于存放球体元素的组this.sphereGroup = new THREE.Group();this.scene.add(this.sphereGroup);},animate() {this.isAnimate = true;requestAnimationFrame(this.animate);if (this.rotationType === 'sphere') {if (this.sphereGroup) {this.sphereGroup.rotation.y += 0.01;}}this.controls.update();this.renderer.render(this.scene, this.camera);},createInitialSphere() {const radius = 5;if(this.signList.length > 0){if (this.signList.length >= this.defautNum) {// 超出200个截取组成this.peopleList = this.signList.slice(0, this.defautNum);} else {// 未超出200个随机截取组成for (let i = 0; i < this.defautNum; i++) {const randomIndex = Math.floor(Math.random() * this.signList.length);this.peopleList.push(this.signList[randomIndex]);}}}else{for (let i = 0; i < 200; i++) {this.peopleList.push({uid: null,nickname: '默认用户',avatar: 'https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/liveCardBj.png'});}}for (let i = 0; i < this.defautNum; i++) {const participant = this.peopleList[i];const loader = new THREE.TextureLoader();loader.load(participant.avatar, (texture) => {const material = new THREE.MeshBasicMaterial({ map: texture });const geometry = new THREE.PlaneGeometry(1, 1);const mesh = new THREE.Mesh(geometry, material);const group = new THREE.Group();group.add(mesh);participant.group = group;const phi = Math.acos(-1 + (2 * i) / this.defautNum);const theta = Math.sqrt(this.defautNum * Math.PI) * phi;const x = radius * Math.cos(theta) * Math.sin(phi);const y = radius * Math.sin(theta) * Math.sin(phi);const z = radius * Math.cos(phi);group.position.set(x, y, z);group.lookAt(0, 0, 0);this.sphereGroup.add(group);});}},

渲染有一个问题 重新触发会导致球体运动加速;再这里要加个判断如果有触发动画的话;再次渲染不需要加载动画方法

 that.initThree();if (!that.isAnimate){that.animate();}that.createInitialSphere();

http://www.xdnf.cn/news/529705.html

相关文章:

  • AI自媒体封面生成系统
  • c++字符串常用语法特性查询示例文档(二)
  • HarmonyOS开发样式布局
  • web常见的攻击方式
  • UniApp 实现的文件预览与查看功能#三方框架 #Uniapp
  • 阻塞队列:线程安全与生产者消费者模型解析
  • nginx 流量控制
  • map与set封装
  • Web安全基础
  • 十三、面向对象底层逻辑-Dubbo序列化Serialization接口
  • MacBook连接不上星巴克Wi-Fi的解决方法
  • 《Effective Python》第三章 循环和迭代器——在遍历参数时保持防御性
  • 江协科技EXTI外部中断hal库实现
  • 需求频繁变更?AI 驱动的自动化解决方案实践
  • 企业销售管理痛点解析与数字化解决方案
  • Unity 如何使用Timeline预览、播放特效
  • 第十六届蓝桥杯复盘
  • C#中的ThreadStart委托
  • 软件架构风格系列(7):闭环控制架构
  • 基于不透光法的柴油机排放精准监测
  • Android13 以太网(YT8531)
  • 【JavaScript】用 Proxy 拦截对象属性
  • Xshell实战:远程连接VMware CentOS7虚拟机与高效运维指南——从零配置到自动化操作,解锁Xshell的核心价值
  • Bootstrap 5 容器与网格系统详解
  • 项目删除了,为什么vscode中的git还是存在未提交记录,应该怎么删除掉
  • vue3个生命周期解析,及setup
  • 遨游科普:三防平板是什么?有什么作用?
  • 线光谱共焦传感器:复杂材质检测
  • MCU 温度采样理论(-ADC Temperature sensor)
  • 用户账号及权限管理:企业安全的基石与艺术