在微信小程序中怎么使用three.js

前端开发   发布日期:2023年07月25日   浏览次数:981

本篇内容主要讲解“在微信小程序中怎么使用three.js”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“在微信小程序中怎么使用three.js”吧!

1.首先引入下载材料,最后有完整源码

默认你很熟悉小程序开发

直接搜索three.weapp.js官网下载,具体那里下载的我也忘记了

  1. import * as THREE from '../../libs/three.weapp.js'
  2. import { OrbitControls } from '../../libs/OrbitControls.js'
  3. import GLTF from '../../libs/GLTFLoader.js'

2.将three.js和canvas绑定

在小程序的生命周期中onLoad使用

  1. bindThree() {
  2. wx.createSelectorQuery()
  3. .select('#c')
  4. .node()
  5. .exec((res) => {
  6. let canvasId = res[0].node._canvasId
  7. canvas = THREE.global.registerCanvas(canvasId, res[0].node)
  8. this.setData({ canvasId })
  9. this.init()
  10. })
  11. },

3.初始化场景这些

  1. // 场景,相机,渲染器,控制器
  2. let scene, camera, renderer, controls, canvas,
  3. // 初始化场景
  4. initScene() {
  5. scene = new THREE.Scene()
  6. // scene.background = new THREE.Color(0xffffff)
  7. //灯光 黄光环境
  8. scene.add(new THREE.AmbientLight(0xffffff))
  9. scene.background = new THREE.Color(0xaa000000)
  10. },
  11. // 初始化相机
  12. initCamera() {
  13. camera = new THREE.PerspectiveCamera(
  14. 75,
  15. canvas.width / canvas.height,
  16. 0.1,
  17. 4000
  18. )
  19. camera.position.set(0, 70, 1200)
  20. // camera.lookAt(0, 0, 0)
  21. },
  22. // 初始化渲染器
  23. initRenderer() {
  24. renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
  25. renderer.setSize(
  26. wx.getSystemInfoSync().windowWidth,
  27. wx.getSystemInfoSync().windowHeight
  28. )
  29. },
  30. // 初始化控制器
  31. initControls() {
  32. controls = new OrbitControls(camera, renderer.domElement)
  33. controls.enableDamping = true // 设置阻尼,需要在 update 调用
  34. },

4.到这一步 你已经完成了初始化了接下来就是对gltf模型的渲染了

渲染模型的代码如下:

  1. // 添加测试模型
  2. addGuangzhouta() {
  3. loader.load(this.data.testUrl, (gltf) => {
  4. // 位置更正
  5. gltf.scene.position.set(200, 580, -700)
  6. gltf.scene.scale.set(2, 2, 2)
  7. scene.add(gltf.scene)
  8. })
  9. },

5. 话不多少,可能还有一些人看的云里雾里的,直接上源码

  1. import * as THREE from '../../libs/three.weapp.js'
  2. import { OrbitControls } from '../../libs/OrbitControls.js'
  3. import GLTF from '../../libs/GLTFLoader.js'
  4. // 场景,相机,渲染器,控制器
  5. let scene, camera, renderer, controls, canvas, textureGuangzhouta
  6. // gltf加载器
  7. let GLTFloader = GLTF(THREE)
  8. const loader = new GLTFloader()
  9. Page({
  10. data: {
  11. canvasId: null,
  12. testUrl: 'https://threejsfundamentals.org/threejs/resources/models/cartoon_lowpoly_small_city_free_pack/scene.gltf'
  13. },
  14. // 将微信dom和three.js绑定
  15. bindThree() {
  16. wx.createSelectorQuery()
  17. .select('#c')
  18. .node()
  19. .exec((res) => {
  20. let canvasId = res[0].node._canvasId
  21. canvas = THREE.global.registerCanvas(canvasId, res[0].node)
  22. this.setData({ canvasId })
  23. this.init()
  24. })
  25. },
  26. // 初始化场景
  27. initScene() {
  28. scene = new THREE.Scene()
  29. // scene.background = new THREE.Color(0xffffff)
  30. //灯光 黄光环境
  31. scene.add(new THREE.AmbientLight(0xffffff))
  32. scene.background = new THREE.Color(0xaa000000)
  33. },
  34. // 初始化相机
  35. initCamera() {
  36. camera = new THREE.PerspectiveCamera(
  37. 75,
  38. canvas.width / canvas.height,
  39. 0.1,
  40. 4000
  41. )
  42. camera.position.set(0, 700, 1700)
  43. // camera.lookAt(0, 0, 0)
  44. },
  45. // 初始化渲染器
  46. initRenderer() {
  47. renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true })
  48. renderer.setSize(
  49. wx.getSystemInfoSync().windowWidth,
  50. wx.getSystemInfoSync().windowHeight
  51. )
  52. },
  53. // 初始化控制器
  54. initControls() {
  55. controls = new OrbitControls(camera, renderer.domElement)
  56. controls.enableDamping = true // 设置阻尼,需要在 update 调用
  57. },
  58. // 添加测试模型
  59. addGuangzhouta() {
  60. loader.load(this.data.testUrl, (gltf) => {
  61. // 位置更正
  62. gltf.scene.position.set(200, 580, -700)
  63. gltf.scene.scale.set(2, 2, 2)
  64. scene.add(gltf.scene)
  65. })
  66. },
  67. // 动画帧函数
  68. animate() {
  69. canvas.requestAnimationFrame(this.animate)
  70. if (textureGuangzhouta) {
  71. textureGuangzhouta.offset.y -= 0.009
  72. // console.log(textureGuangzhouta.offset.y)
  73. if (textureGuangzhouta.offset.y < -0.5) {
  74. textureGuangzhouta.offset.y = 0
  75. }
  76. }
  77. controls.update()
  78. renderer.render(scene, camera)
  79. },
  80. // 初始化函数
  81. init() {
  82. this.initScene()
  83. this.initCamera()
  84. this.initRenderer()
  85. this.initControls()
  86. this.addGuangzhouta()
  87. this.onWindowResize()
  88. this.animate()
  89. },
  90. // 页面自适应
  91. onWindowResize() {
  92. console.log(canvas.width)
  93. // camera.aspect = window.innerWidth / window.innerHeight
  94. camera.aspect =
  95. wx.getSystemInfoSync().windowWidth / wx.getSystemInfoSync().windowHeight
  96. camera.updateProjectionMatrix()
  97. renderer.setSize(canvas.width, canvas.height)
  98. },
  99. // 生命周期函数:小程序初始化完成时触发,全局只触发一次
  100. onLoad: function () {
  101. this.bindThree()
  102. },
  103. onUnload: function () {
  104. //清理global中的canvas对象
  105. THREE.global.clearCanvas()
  106. THREE.global.unregisterCanvas(this.data.canvasId)
  107. },
  108. })

6.注意事项

到这一步之后你就可以自己写模型丢里面了,gltf加载器和pc的用法是一样的,目前我这里就提供一个测试用的模型吧

目前代码里没有提供滑动的方法,也没有提供适配任意gltf模型的办法,需要注意的是小程序中没办法加载本地模型,只能通过https的方式加载线上的模型。

以上就是在微信小程序中怎么使用three.js的详细内容,更多关于在微信小程序中怎么使用three.js的资料请关注九品源码其它相关文章!