当前位置:   article > 正文

uniapp使用threejs-miniprogram在微信小程序加载模型

uniapp使用threejs-miniprogram在微信小程序加载模型

1.通过 npm 安装

npm install --save threejs-miniprogram

2.导入小程序版本的 Three.js并创建一个与 canvas 绑定的 three.js

  1. <template>
  2. <canvas type="webgl" id="webgl" style="width: 100vw; height: 100vh"></canvas>
  3. </template>
  1. <script setup>
  2. import { createScopedThreejs } from 'threejs-miniprogram';
  3. import { onReady } from '@dcloudio/uni-app';
  4. let THREE = null;
  5. let canvas = null;
  6. onReady(() => {
  7. init();
  8. });
  9. async function init() {
  10. await createThree();
  11. }
  12. function createThree() {
  13. return new Promise((resolve) => {
  14. uni.createSelectorQuery()
  15. .select('#webgl')
  16. .node()
  17. .exec((res) => {
  18. //canvas做动画时要用
  19. canvas = res[0].node;
  20. // 创建一个与 canvas 绑定的 three.js
  21. THREE = createScopedThreejs(canvas);
  22. resolve();
  23. });
  24. });
  25. }
  26. </script>

 3.创建渲染器

  1. let renderer = null
  2. async function init() {
  3. await createThree();
  4. createRenderer()
  5. }
  6. function createRenderer() {
  7. renderer = new THREE.WebGLRenderer();
  8. renderer.setSize(canvas.width, canvas.height);
  9. }

4.创建场景,创建相机,渲染

  1. let scene = null;
  2. let camera = null;
  3. async function init() {
  4. await createThree();
  5. createRenderer();
  6. createScene();
  7. createCamera();
  8. renderer.render(scene, camera);
  9. }
  10. function createScene() {
  11. scene = new THREE.Scene();
  12. }
  13. function createCamera() {
  14. camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
  15. camera.position.set(400, 400, 800);
  16. camera.lookAt(0, 0, 0);
  17. }

5.注册GLTF加载器,加载模型添加到场景

threejs-miniprogram/example/loaders/gltf-loader.js at master · wechat-miniprogram/threejs-miniprogram · GitHub

下载 gltf-loader.js

注册gltf-loader

  1. import { registerGLTFLoader } from '@/utils/gltfloader.js';
  2. async function init() {
  3. await createThree();
  4. createRenderer();
  5. createScene();
  6. createCamera();
  7. registerGLTFLoader(THREE)
  8. renderer.render(scene, camera);
  9. }

加载模型和光源

  1. async function init() {
  2. await createThree();
  3. createRenderer();
  4. createScene();
  5. createCamera();
  6. createLight();
  7. registerGLTFLoader(THREE);
  8. //glb纹理需要blob,小程序没有
  9. const res = await loadGLTF('http://127.0.0.1:3000/abc.gltf');
  10. scene.add(res);
  11. renderer.render(scene, camera);
  12. }
  13. function loadGLTF(url) {
  14. return new Promise((resolve, reject) => {
  15. new THREE.GLTFLoader().load(url, (gltf) => {
  16. // gltf.scene.scale.set(1, 1, 1);
  17. resolve(gltf.scene);
  18. });
  19. });
  20. }
  21. function createLight() {
  22. const light = new THREE.AmbientLight(0xffffff);
  23. scene.add(light);
  24. }

 6.添加控制器

threejs-miniprogram/example/test-cases/orbit.js at master · wechat-miniprogram/threejs-miniprogram · GitHub

下载 orbit.js,创建控制器

  1. import registerOrbit from '@/utils/controls.js';
  2. let controls = null
  3. async function init() {
  4. await createThree();
  5. createRenderer();
  6. createScene();
  7. createCamera();
  8. createLight();
  9. registerGLTFLoader(THREE);
  10. //glb纹理需要blob,小程序没有
  11. const res = await loadGLTF('http://127.0.0.1:3000/abc.gltf');
  12. scene.add(res);
  13. renderer.render(scene, camera);
  14. createControls()
  15. }
  16. function createControls() {
  17. const orbits = registerOrbit(THREE);
  18. controls = new orbits.OrbitControls(camera, renderer.domElement);
  19. controls.enableDamping = true;
  20. }

动起来

  1. <template>
  2. <canvas type="webgl" id="webgl" style="width: 100vw; height: 100vh" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
  3. </template>
  1. async function init() {
  2. await createThree();
  3. createRenderer();
  4. createScene();
  5. createCamera();
  6. createLight();
  7. registerGLTFLoader(THREE);
  8. //glb纹理需要blob,小程序没有
  9. const res = await loadGLTF('http://127.0.0.1:3000/abc.gltf');
  10. scene.add(res);
  11. renderer.render(scene, camera);
  12. createControls();
  13. animate();
  14. }
  15. function animate() {
  16. //小程序没有window.requestAnimationFrame
  17. canvas.requestAnimationFrame(animate);
  18. renderer.render(scene, camera);
  19. controls.update();
  20. }
  21. function touchMove(e) {
  22. controls.onTouchMove(e);
  23. }
  24. function touchEnd(e) {
  25. controls.onTouchEnd(e);
  26. }
  27. function touchStart(e) {
  28. controls.onTouchStart(e);
  29. }

报错了

原因是微信小程序没有addEventListener

把orbit.js的addEventListener删掉,我们手动触发

  

 完整代码

  1. <template>
  2. <canvas type="webgl" id="webgl" style="width: 100vw; height: 100vh" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
  3. </template>
  4. <script setup>
  5. import { createScopedThreejs } from 'threejs-miniprogram';
  6. import { onReady } from '@dcloudio/uni-app';
  7. import { registerGLTFLoader } from '@/utils/gltfloader.js';
  8. import registerOrbit from '@/utils/controls.js';
  9. let THREE = null;
  10. let canvas = null;
  11. let renderer = null;
  12. let scene = null;
  13. let camera = null;
  14. let controls = null;
  15. onReady(() => {
  16. init();
  17. });
  18. async function init() {
  19. await createThree();
  20. createRenderer();
  21. createScene();
  22. createCamera();
  23. createLight();
  24. registerGLTFLoader(THREE);
  25. //glb纹理需要blob,小程序没有
  26. const res = await loadGLTF('http://127.0.0.1:3000/abc.gltf');
  27. scene.add(res);
  28. renderer.render(scene, camera);
  29. createControls();
  30. console.log(controls);
  31. animate();
  32. }
  33. function createThree() {
  34. return new Promise((resolve) => {
  35. uni.createSelectorQuery()
  36. .select('#webgl')
  37. .node()
  38. .exec((res) => {
  39. //canvas做动画时要用
  40. canvas = res[0].node;
  41. // 创建一个与 canvas 绑定的 three.js
  42. THREE = createScopedThreejs(canvas);
  43. resolve();
  44. });
  45. });
  46. }
  47. function createRenderer() {
  48. renderer = new THREE.WebGLRenderer();
  49. renderer.setSize(canvas.width, canvas.height);
  50. }
  51. function createScene() {
  52. scene = new THREE.Scene();
  53. }
  54. function createCamera() {
  55. camera = new THREE.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
  56. camera.position.set(400, 400, 800);
  57. camera.lookAt(0, 0, 0);
  58. }
  59. function loadGLTF(url) {
  60. return new Promise((resolve) => {
  61. new THREE.GLTFLoader().load(url, (gltf) => {
  62. resolve(gltf.scene);
  63. });
  64. });
  65. }
  66. function createLight() {
  67. const light = new THREE.AmbientLight(0xffffff);
  68. scene.add(light);
  69. }
  70. function createControls() {
  71. const orbits = registerOrbit(THREE);
  72. controls = new orbits.OrbitControls(camera, renderer.domElement);
  73. controls.enableDamping = true;
  74. }
  75. function animate() {
  76. //小程序没有window.requestAnimationFrame
  77. canvas.requestAnimationFrame(animate);
  78. renderer.render(scene, camera);
  79. controls.update();
  80. }
  81. function touchMove(e) {
  82. controls.onTouchMove(e);
  83. }
  84. function touchEnd(e) {
  85. controls.onTouchEnd(e);
  86. }
  87. function touchStart(e) {
  88. controls.onTouchStart(e);
  89. }
  90. </script>
  91. <style></style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/330948?site
推荐阅读
相关标签
  

闽ICP备14008679号