• 微信小程序 加载 fbx 模型


            three.js在微信小程序端加载3D动画fbx模型的代码。 得到了fbx模型的http链接,使其加载在小程序端,都是适配小程序版本的代码 其中three_new.js是小程序版的3D加载库文件 FBXLoader_new是小程序版本的fbx模型的加载器 inflate.min.js是小程序版的压缩解压库 model.js里面的export函数可以直接调用,把fbx模型的http链接还有相关的dom节点传进去就可以加载啦~

    微信小程序实现从外部加载3d模型
    1.3d模型的几种格式?
    2.怎样加载3d模型?
    3.总结(贴了自己写的整个项目github地址)

    1.模型的格式
    小程序支持从外部加载3d模型的几种格式有:
    a.obj格式
    b.gltf格式
    c.fbx格式
    就列举这几种

    2.加载3d模型
    用 gltf 格式的模型
    gltf 3d模式格式有两种:
    gltf是一个基于json的文本文件,如纹理贴图和二进制网格数据;
    glb是二进制版本,通常较小且独立,但不易编辑。

    (1)首先文件
    下载适配微信小程序的 three.js 地址:https://github.com/wechat-miniprogram/threejs-miniprogram

    (2)导入文件
    需要导入three.js 和 gltf-loader.js (加载模型)
    从下载好的压缩包里复制目录 threejs-miniprogram-master\example\miniprogram_npm\threejs-miniprogram 的 [index.js] 文件
    以及 threejs-miniprogram-master\example\loaders 里的 [gltf-loader.js] 文件

    1. <view style="height: 50px"></view>
    2. <canvas type="webgl" id="webgl" style="width: 100%; height: 450px;"></canvas>
    3. <canvas type="webgl" id="webgl" style="width: 100%; height: 300px;"></canvas>

     

    1. import { createScopedThreejs } from 'threejs-miniprogram';
    2. import { registerGLTFLoader } from '../loaders/gltf-loader';
    3. const app = getApp();
    4. var camera, scene, renderer, model;
    5. var requestAnimationFrame; // 动画回调函数
    6. Page({
    7. data: {},
    8. onLoad: function () {
    9. let that = this;
    10. var query = wx.createSelectorQuery();
    11. query.select('#webgl').node().exec((res) => {
    12. var canvas = res[0].node;
    13. // 设置背景透明
    14. var gl = canvas.getContext('webgl', {
    15. alpha: true
    16. });
    17. if (canvas != undefined) {
    18. // canvas.width 和canvas.style都需要设置,否则无法显示渲染
    19. canvas.width = canvas._width;
    20. canvas.height = canvas._height;
    21. requestAnimationFrame = canvas.requestAnimationFrame;
    22. that.init(canvas);
    23. }
    24. });
    25. },
    26. init: function(canvas){
    27. let that = this;
    28. const THREE = createScopedThreejs(canvas)
    29. registerGLTFLoader(THREE)
    30. //设置相机
    31. camera = new THREE.PerspectiveCamera(45, canvas.width / canvas.height, 0.25, 100);
    32. camera.position.set(- 5, 3, 10);
    33. camera.lookAt(new THREE.Vector3(0, 2, 0));
    34. scene = new THREE.Scene();
    35. //设置光线
    36. var light = new THREE.HemisphereLight(0xffffff, 0x444444);
    37. light.position.set(0, 20, 0);
    38. scene.add(light);
    39. light = new THREE.DirectionalLight(0xffffff);
    40. light.position.set(0, 20, 10);
    41. scene.add(light);
    42. //加载外部模型
    43. var loader = new THREE.GLTFLoader();
    44. loader.load('https://threejs.org/examples/models/gltf/RobotExpressive/RobotExpressive.glb', function (gltf) {
    45. model = gltf.scene;
    46. scene.add(model);
    47. }, undefined, function (e) {
    48. console.error(e);
    49. });
    50. renderer = new THREE.WebGLRenderer({ antialias: true });
    51. renderer.setSize(canvas.width, canvas.height);
    52. //调用动画
    53. that.animate();
    54. },
    55. animate:function(){
    56. let that = this;
    57. requestAnimationFrame(that.animate);
    58. renderer.render(scene, camera);
    59. }
    60. })

  • 相关阅读:
    索引-mysql详解(三)
    基于Java+vue前后端分离安全教育平台设计实现(源码+lw+部署文档+讲解等)
    相机噪声评估
    使用SpringBoot集成Mybatis的详细步骤
    竞赛选题 深度学习+python+opencv实现动物识别 - 图像识别
    版本特性 | Cloudpods v3.9重点功能介绍
    MAC如何重装系统(怒冲30大洋,才拿到的教程~,收藏点赞兄弟们)
    小白入门深度学习 | 6-5:Inception-v1(2014年)详解
    深入浅出图神经网络【阅读笔记】
    智能运维实战:银行业务流程及单笔交易追踪
  • 原文地址:https://blog.csdn.net/vcit102/article/details/127576460