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>
相关推荐
nihui1236 小时前
Uniapp 实现顶部标签页切换功能?
javascript·vue.js·uni-app
尚学教辅学习资料13 小时前
基于SSM+uniapp的鲜花销售小程序+LW示例参考
小程序·uni-app·java毕设·鲜花销售
web_Hsir15 小时前
vue + uniapp + 高德地图实现微信小程序地图polyline、marker展示
vue.js·微信小程序·uni-app
2401_8854055115 小时前
智能硬件定位技术发展趋势
物联网·微信小程序·uni-app·智慧城市·智能硬件·宠物·智能手表
尚学教辅学习资料15 小时前
基于SpringBoot+vue+uniapp的投票小程序+LW示例参考
vue.js·spring boot·小程序·uni-app·java毕设·投票系统
fakaifa16 小时前
云贝餐饮连锁V3独立版全开源+vue源码
微信小程序·小程序·uni-app·开源·php·源码下载
寰宇软件1 天前
PHP图书借阅小程序
小程序·uni-app·vue·php
尚学教辅学习资料2 天前
基于SSM+uniapp的数学辅导小程序+LW示例参考
小程序·uni-app·java毕设·学习平台·数学辅导
FinelyYang2 天前
uniapp webview嵌入外部h5网页后的消息通知
小程序·uni-app
飘逸飘逸2 天前
以若依移动端版为基础,实现uniapp的flowable流程管理
uni-app·ruoyi