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

uniapp中使用threejs加载几何体

我的建议是使用这个库
https://github.com/deepkolos/three-platformize
为什么?我试了uniapp推荐的和threejs-miniprogram这个小程序官方库,都加载不出来我的obj模型。所有我推荐不要用obj模型最好,挺多都支持GLTF模型的,但是我不能改。

安装,使用pnpm比较快

pnpm install three-platformize

以下是完整代码

<template><view class="content"><canvas type="webgl" id="webgl" style="width: 100vw; height: 100vh;" @touchstart="touchStart"@touchmove="touchMove" @touchend="touchEnd" /></view>
</template><script>import {WechatPlatform} from 'three-platformize/src/WechatPlatform';import * as THREE from 'three-platformize';//轨道控制器import {OrbitControls} from 'three-platformize/examples/jsm/controls/OrbitControls'export default {data() {return {platform: ''};},mounted() {uni.createSelectorQuery().in(this).select('#webgl').fields({node: true}).exec(res => {console.log('res', res[0].node);const canvas = res[0].node;console.log('canvas', canvas);const platform = new WechatPlatform(canvas); // webgl canvasconsole.log('1111', platform);platform.enableDeviceOrientation('game'); // 开启DeviceOrientationTHREE.PLATFORM.set(platform);this.platform = platform;var scene = new THREE.Scene();var camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);camera.position.set(0, 0, 10);scene.add(camera);const geometry = new THREE.BoxGeometry(1, 1, 1);const materials = new THREE.MeshBasicMaterial();const cube = new THREE.Mesh(geometry, materials);scene.add(cube);const light = new THREE.AmbientLight(0xffffff);scene.add(light);//注意,这里必须要添加一个{ canvas: canvas },不然会报createElementNS错误const renderer = new THREE.WebGLRenderer({canvas: canvas});renderer.setSize(canvas.width, canvas.height);const controls = new OrbitControls(camera, renderer.domElement);function animate() {//这里不再是requestAnimationFrame而是canvas.requestAnimationFramecanvas.requestAnimationFrame(animate);renderer.render(scene, camera);}animate();});},methods: {touchStart(e) {this.platform.dispatchTouchEvent(e);},touchMove(e) {this.platform.dispatchTouchEvent(e);},touchEnd(e) {this.platform.dispatchTouchEvent(e);}}}
</script><style></style>

http://www.lryc.cn/news/388738.html

相关文章:

  • 【SQL注入】 数据库基础
  • 文件操作~
  • 身边的故事(十二):阿文的故事:消失
  • 智能扫地机器人程序中出现的问题可以参考的解决方案
  • 如何借用物联网快速实现高标准农田信息化
  • 计算机网络基础入门
  • uniApp vue2 vue3配置代理
  • 运维锅总详解RocketMQ
  • 【Linux】使用ntp同步时间
  • 【FedMut】Generalized Federated Learning via Stochastic Mutation
  • 在线教育项目(一):如何防止一个账号多个地方登陆
  • 旋转变压器软件解码simulink仿真
  • LeetCode 1321, 209, 102
  • vant ( weapp ) - - - - - van-tabs组件选中下划线初始位置异常
  • 007 栈(lua)
  • SQL中Order by详解
  • 【git】存在git LFS文件时如何处理
  • 面向阿克曼移动机器人(自行车模型)的LQR(最优二次型调节器)路径跟踪方法
  • 【运维】在 Docker 容器中指定 UTF-8 编码:方法与技巧
  • primetime中cell和net的OCV
  • FlinkX学习
  • 新书速览|解密AI绘画与修图: Stable Diffusion+Photoshop
  • 1111111111111
  • 云原生概念
  • NoSQL之Redis高可用与优化
  • MySQL 常见存储引擎详解(一)
  • Leetcode 股票买卖
  • 小白学习手册:轻松理解MQ消息队列
  • electron线上更新
  • 谈谈检测浏览器类型