这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

前言

我的需求是使用uniapp写微信小程序,在小程序中使用threeJs就行了,目前暂不考虑兼容app什么的。
1.引入小程序版的threejs库实现
2.使用webview实现(推荐)

重点

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

使用three-platformize加载obj模型的案例:

核心代码:

html:
<canvas type="webgl" id="webgl" style="width: 100vw; height: 100vh;"
@touchstart="touchStart"
@touchmove="touchMove"
@touchend="touchEnd"
/>
script:
<script>
import * as THREE from 'three-platformize';
import { WechatPlatform } from 'three-platformize/src/WechatPlatform';
import { OBJLoader } from 'three-platformize/examples/jsm/loaders/OBJLoader';
import { GLTFLoader } from 'three-platformize/examples/jsm/loaders/GLTFLoader';
import {OrbitControls} from 'three-platformize/examples/jsm/controls/OrbitControls';
export default {
data() {
return {
canvas:null,
camera:null,
scene:null,
renderer:null,
model:null,
controls:null,
loopIndex:null
}
},
onLoad() { },
methods: {
async init() {
const { canvas }= await this.getCanvas();
this.canvas = canvas;
const platform = new WechatPlatform(canvas); // webgl canvas
platform.enableDeviceOrientation('game'); // 开启DeviceOrientation
THREE.PLATFORM.set(platform);
this.platform = platform;
this.renderModel();
},
//获取画布
async getCanvas(delay = 200) {
return new Promise((resolve, reject) => {
const t = setTimeout(() => {
clearTimeout(t);
uni.createSelectorQuery().in(this)
.select('#webgl')
.fields({ node: true })
.exec((res) => {
console.log('res',res)
if (res && res[0] && res[0].node) {
const canvas = res[0].node;
resolve({ canvas });
} else {
reject("获取canvas失败");
}
});
}, delay);
});
},
renderModel () {
this.camera = new THREE.PerspectiveCamera(45, this.canvas.width / this.canvas.height, 0.25, 100);
this.camera.position.set(- 5, 3, 10);
this.camera.lookAt(new THREE.Vector3(0, 2, 0));
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0xe0e0e0);
this.scene.fog = new THREE.Fog(0xe0e0e0, 20, 100);
this.clock = new THREE.Clock();
// lights
var light = new THREE.HemisphereLight(0xffffff, 0x444444);
light.position.set(0, 20, 0);
this.scene.add(light);
// 改变外壳颜色
var AmbientLight = new THREE.AmbientLight(0x815800); // 环境光
this.scene.add(AmbientLight);
// 平行光
light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 20, 10);
this.scene.add(light);
// // ground
// var mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(2000, 2000), new THREE.MeshPhongMaterial({ color: 0x999999, depthWrite: false }));
// mesh.rotation.x = - Math.PI / 2;
// this.scene.add(mesh);
// var grid = new THREE.GridHelper(200, 40, 0x000000, 0x000000);
// grid.material.opacity = 0.6;
// grid.material.transparent = true;
// this.scene.add(grid);
// model
var loader = new OBJLoader();
loader.load('http://localhost:8888/obj3/file.obj', (obj) => {
console.log("obj+=")
console.log(obj)
// console.log(this.model)
obj.position.set(0, -2, 0);//模型摆放的位置
obj.scale.set(0.2, 0.2, 0.2);
// this.model = obj;
this.scene.add(obj);
}, undefined, function (e) {
console.log("模型加载错误")
console.error(e);
});
// var loader = new GLTFLoader();
// loader.load('https://dtmall-tel.alicdn.com/edgeComputingConfig/upload_models/1591673169101/RobotExpressive.glb', (gltf) => {
// this.model = gltf.scene;
// this.scene.add(this.model);
// }, undefined, function (e) {
// console.error(e);
// });
// var geometry = new THREE.BoxGeometry( 5, 5, 5 );
// var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
// var mesh = new THREE.Mesh( geometry, material );
// this.scene.add(mesh); this.renderer = new THREE.WebGLRenderer({antialias: true });
this.renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio);
this.renderer.setSize(this.canvas.width, this.canvas.height);
//this.renderer.outputEncoding = true;
this.renderer.gammaFactor = 2.2; this.controls = new OrbitControls(this.camera, this.renderer.domElement );
this.camera.position.set( 5, 5, 10 );
this.animate();
},
animate() {
this.loopIndex = this.canvas.requestAnimationFrame(this.animate);
this.renderer.render(this.scene, this.camera);
this.controls.update();
}, touchStart(e) {
this.platform.dispatchTouchEvent(e);
},
touchMove(e) {
this.platform.dispatchTouchEvent(e);
},
touchEnd(e) {
this.platform.dispatchTouchEvent(e);
}
},
mounted() {
this.$nextTick(()=> {
this.init();
});
}
}
</script>

上面的案例中使用了两个模型,一个obj模型,obj模型的地址是自己写的本地服务器地址,需要自己配置,GLTF模型地址是网络地址,可以把注释解开查看。

注意点

1.加载外部模型与threeJs官网api是一致的
2.使用此方法加载外部模型,可能在真机调试时遇到模型不展示,或者微信闪退的情况(原因未知)

webview实现引入threejs库

效果图

实现:
1.使用vue实现threejs导入obj模型(pc端可完美实现),
2.在webview中引入相应的模型展示地址,

完结,就这两步即可,但是我是动态加载,所以在加载模型的时候,你需要在小程序端传值给pc端,让pc端加载相应的模型,这里就只需要用get在地址栏中传参就行了。

以下两个方法可能会用到:
1.模型大小自适应

setScaleToFitSize (obj) {
const boxHelper = new THREE.BoxHelper(obj);
boxHelper.geometry.computeBoundingBox();
const box = boxHelper.geometry.boundingBox;
const maxDiameter = Math.max((box.max.x - box.min.x), (box.max.y - box.min.y), (box.max.z - box.min.z));
const scaleValue = camera.position.z / maxDiameter;
obj.position.set(0, -10, 0);//模型摆放的位置
obj.scale.set(scaleValue, scaleValue, scaleValue);
scene.add(obj);
},

2.禁止小程序展示webview时候向下拉动:
mounted中添加:

document.body.addEventListener('touchmove', function (e) {
e.preventDefault();
}, { passive: false });

本文转载于:

https://blog.csdn.net/hzqzzz/article/details/126428029

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

记录--uniapp微信小程序引入threeJs并导入模型的更多相关文章

  1. uniapp 微信小程序 引入 环信聊天

    最近项目需要实现一个聊天的功能,群聊或者单聊,用到环信,根据官网实现一下相关的配置吧 第一:下载环信demo  地址:https://github.com/easemob/webim-uniapp-d ...

  2. uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤

    uni-app开发微信小程序引入UI组件库(Vant-weapp)步骤 这里以vant-weapp为例 uni-app官方文档介绍引入组件的方法 1. 新建相关目录 根目录下创建 wxcomponen ...

  3. 微信小程序引入md5.js

    今天给大家安利一下微信小程序引入md5.js的方法,不多说 md5.js在下面 直接复制到项目的utils/md5.js即可 /* * A JavaScript implementation of t ...

  4. 微信小程序引入ECharts组件

    首先打开ECharts网页 https://echarts.apache.org/zh/tutorial.html#%E5%9C%A8%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8 ...

  5. uniapp 微信小程序 配置分享朋友和朋友圈

    uniapp 微信小程序 配置分享朋友和朋友圈 首先在小程序中配置微信分享,和微信朋友圈, onShareAppMessage, onShareTimeline 这两个API 和 onLoad 同级目 ...

  6. uni-app微信小程序开发之引入腾讯视频小程序播放插件

    登录微信小程序管理后台添加腾讯视频播放插件: 正式开始使用腾讯视频小程序插件之前需先在微信公众平台 -> 第三方设置 -> 插件管理处添加插件,如下图所示: 在uni-app中引入插件代码 ...

  7. 【重点突破】—— UniApp微信小程序开发教程学习Three

    一.实战 HBuilderX:在微信小程序中运行页面,需要设置->安全 开启微信小程序服务端口,HBuilder工具->设置->配置程序路径 网络请求.模板语法.打开页面.页面传参 ...

  8. 【重点突破】—— UniApp 微信小程序开发官网学习Two

    一.使用Vue.js注意事项 Vue.js在uni-app中使用的差异: 新增:uni-app除了支持Vue实例的生命周期,还支持应用启动.页面显示等生命周期 受限:发布到H5时支持所有vue的语法, ...

  9. 微信小程序之threejs全景

    最近在开发小程序,身心疲惫,原因是功能和app相同,我裂开了. 各种封装组件,各种写页面,不过有个好处是以前写的h5拿来改一下标签,基本上还是ok的,就剩下最后几个功能,其中就有一个VR全景功能. 移 ...

  10. uni-app微信小程序登录授权

    微信小程序授权是非常简单和常用的功能,但为了方便,还是在此记录一下要点: 首先是需要用到一个授权按钮来触发获取用户信息授权: 关键在于 open-type 为 getUserInfo , 然后有个@g ...

随机推荐

  1. Python Rich:美化终端显示效果

    Rich库的功能就像它的名字一样,使Python编程更加丰富(rich),它帮助开发者在控制台(命令行)输出中创建丰富.多彩和具有格式化的文本. 本篇总结了如何使用Rich库让我们的命令行工具更加美观 ...

  2. oracle 使用comment语句添加表注释

    使用oracle comment语句可以给表.字段.视图等对象添加备注信息. 大致语法为: comment on TABLE table_name IS '备注内容'; 权限要求: 默认情况下用户只能 ...

  3. 【CVE-2024-21626】容器逃逸漏洞修复

    哈喽大家好,我是咸鱼. 好久不见,最近有一个很火的 CVE--runc 容器逃逸漏洞.年前的时候我们已经在测试环境进行了相关操作打算年后线上进行修复. 因为今天咸鱼才开工,所以文章也就拖到了现在 漏洞 ...

  4. c2工具sliver的python客户端无法修改grpc超时时间的解决办法

    业务需要,调用了很多implants来执行对应系统上的命令, 但是无论怎么指定interactive.py中execute方法参数, 命令执行超时时间总是30. 后面通过扩展execute方法增加一个 ...

  5. C++ 多线程的错误和如何避免(9)

    有时候使用 std::atomic 比使用 mutexes 更高效 问题分析:使用多线程更新一些简单数据时,比如 int 型,bool 型等等,可以使用 std::atomic,这比 mutex 来得 ...

  6. SpringBoot的自动装配原理及应用

    什么是SpringBoot自动装配 所谓的"SpringBoot自动装配"就是指:通过注解和一些简单的配置就能将某些组件载入Spring容器环境中,便于使用. 比如,很多sprin ...

  7. File.delete()和Files.delete(Path path)的区别

    文件删除时可以选择File.delete()和Files.delete(Path path),这两个方法到底有什么区别呢? //删除暂存的pdfFile file =new File(pdfFilen ...

  8. docker清理已停止的容器

    docker rm -v $(docker ps -aq -f status=exited) 可以将该命令写成shell脚本或者alias.-v参数表示同时清理数据卷

  9. DataGear 制作基于 three.js 的 3D 数据可视化看板

    DataGear专业版 1.0.0 已发布,欢迎试用! http://datagear.tech/pro/ DataGear 支持采用原生的HTML.JavaScript.CSS制作数据可视化看板,也 ...

  10. DataGear数据可视化分析平台介绍

    DataGear 是一款开源免费的数据可视化分析平台,自由制作任何您想要的数据看板,支持接入SQL.CSV.Excel.HTTP接口.JSON等多种数据源. 系统特点: 友好的数据源接入 支持运行时接 ...