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 canvas
					console.log('1111', platform);
					platform.enableDeviceOrientation('game'); // 开启DeviceOrientation
					THREE.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.requestAnimationFrame
						canvas.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>
相关推荐
timeguys5 小时前
【前端】[vue3] [uni-app]使用 vantUI 框架
前端·uni-app
Aiden Targaryen8 小时前
Windows/MacOS WebStorm/IDEA 中开发 Uni-App 配置
java·uni-app·webstorm
想要飞翔的pig9 小时前
uniapp+vue3页面滚动加载数据
前端·vue.js·uni-app
会功夫的李白9 小时前
uniapp自动构建pages.json的vite插件
前端·uni-app·vite
TE-茶叶蛋9 小时前
Uniapp、Flutter 和 React Native 全面对比
flutter·react native·uni-app
特立独行的猫a13 小时前
uni-app 开发HarmonyOS的鸿蒙影视项目分享:从实战案例到开源后台
uni-app·开源·harmonyos·鸿蒙·影视
七七小报16 小时前
uniapp-商城-61-后台 新增商品(添加商品到数据库)
uni-app
hbcui198416 小时前
uni-app x正式支持鸿蒙原生应用开发
uni-app·harmonyos·uni-app x
lqj_本人16 小时前
鸿蒙OS&UniApp制作支持多图上传的图片选择器:打造高性能移动端上传体验#三方框架 #Uniapp
华为·uni-app·harmonyos
好好的哦18 小时前
uni-app小程序登录后…
小程序·uni-app