当前位置:   article > 正文

微信小程序集成three.js--VR全景项目源码_微信小程序实景导航vr

微信小程序实景导航vr

1.实例演示

小程序集成Three.js,展示不同贴图材质的运用

2.源码

(1)引入库文件

  1. import * as THREE from '../../libs/three.weapp.js'
  2. import gLTF from '../../jsm/loaders/GLTFLoader'
  3. import {
  4. OrbitControls
  5. } from '../../jsm/controls/OrbitControls'
  6. const app = getApp()

(2)在onLoad声明周期函数中初始化场景

  1. onLoad: function () {
  2. initScene()
  3. },

(3)initScene()函数源码

  1. initScene() {
  2. wx.createSelectorQuery()
  3. .select('#webgl')
  4. .node()
  5. .exec((res) => {
  6. let canvasId = String(res[0].node.id)
  7. const canvas = THREE.global.registerCanvas(canvasId, res[0].node)
  8. this.setData({
  9. canvasId: canvasId
  10. })
  11. //相机
  12. const camera = new THREE.PerspectiveCamera(70, canvas.width / canvas.height, 1, 1000);
  13. //场景
  14. const scene = new THREE.Scene();
  15. scene.background = new THREE.Color(0xffffff);
  16. const renderer = new THREE.WebGLRenderer({
  17. antialias: true
  18. });
  19. //设置相机位置
  20. camera.position.set(0, 0, 5);
  21. //控制器
  22. const controls = new OrbitControls(camera, renderer.domElement);
  23. controls.enableDamping = true;
  24. controls.update();
  25. //添加灯光
  26. const spotLight = new THREE.SpotLight(0xffffff);
  27. spotLight.position.set(-40, 60, -10)
  28. //设置点光源投射阴影
  29. spotLight.castShadow = true;
  30. scene.add(spotLight)
  31. //加入环境光
  32. let ambiColor = "#33322b";
  33. let ambientLight = new THREE.AmbientLight(ambiColor);
  34. scene.add(ambientLight)
  35. //定义图片
  36. let backurl = 'https://mmbiz.qpic.cn/mmbiz_png/DWsjgNA1bNg0SfFX1Od4SLicD89xz919XU0mgSlH2YKCnumENibq57JOhSuVPunNLmtUDXXicibfvwP6MsaEPQ3D8Q/0?wx_fmt=png'
  37. let fronturl = 'https://mmbiz.qpic.cn/mmbiz_png/DWsjgNA1bNg0SfFX1Od4SLicD89xz919XicHLxnM9wvgdrhNzHoK9aibicWZ5ice7NIVZxf9ict6MiaahHJdoCGRiaOhYw/0?wx_fmt=png'
  38. let lefturl = 'https://mmbiz.qpic.cn/mmbiz_png/DWsjgNA1bNg0SfFX1Od4SLicD89xz919XFTA6ZwMg4wp7nibYfMVUflicbQEJAhxicwvqlXLhY300JB9icWUo7USpeg/0?wx_fmt=png'
  39. let righturl = 'https://mmbiz.qpic.cn/mmbiz_png/DWsjgNA1bNg0SfFX1Od4SLicD89xz919XZ1V9ibeFdp9iaw1icWD6rl4YOxQXNaqJyIFShtBKDu5veNNmC1j3kW1tw/0?wx_fmt=png'
  40. let topurl = 'https://mmbiz.qpic.cn/mmbiz_png/DWsjgNA1bNg0SfFX1Od4SLicD89xz919Xia5ot5lPeiaDUHT9UPvJhUZobL9Wl4lEgBrna7zI0GdCwEE7B2sCCExw/0?wx_fmt=png'
  41. let bottomurl = 'https://mmbiz.qpic.cn/mmbiz_png/DWsjgNA1bNg0SfFX1Od4SLicD89xz919X26hbtubv0f43zOr2zqZQ0tUlFJwIONKzRoVqgdcTWnur113McB3XNA/0?wx_fmt=png'
  42. //正方体1
  43. wx.showLoading({
  44. title: '纹理加载中',
  45. })
  46. const geometry = new THREE.BoxBufferGeometry(20, 20, 20);
  47. const textureb = new THREE.TextureLoader().load(backurl)
  48. textureb.minFilter = THREE.LinearFilter
  49. const material1 = new THREE.MeshBasicMaterial({map:textureb})
  50. const texturef = new THREE.TextureLoader().load(fronturl)
  51. texturef.minFilter = THREE.LinearFilter
  52. const material2 = new THREE.MeshBasicMaterial({map:texturef})
  53. const texturel = new THREE.TextureLoader().load(lefturl)
  54. texturel.minFilter = THREE.LinearFilter
  55. const material3 = new THREE.MeshBasicMaterial({map:texturel})
  56. const texturer = new THREE.TextureLoader().load(righturl)
  57. texturer.minFilter = THREE.LinearFilter
  58. const material4 = new THREE.MeshBasicMaterial({map:texturer})
  59. const textureu = new THREE.TextureLoader().load(topurl)
  60. textureu.minFilter = THREE.LinearFilter
  61. const material5 = new THREE.MeshBasicMaterial({map:textureu})
  62. const textured = new THREE.TextureLoader().load(bottomurl)
  63. textured.minFilter = THREE.LinearFilter
  64. const material6 = new THREE.MeshBasicMaterial({map:textured})
  65. wx.hideLoading({
  66. })
  67. //1:左侧 2:前册 3 顶部 4 底部 5前
  68. var boxMaterials = [material4,material3,material5,material6,material2,material1]
  69. const cube = new THREE.Mesh(geometry,boxMaterials)
  70. cube.geometry.scale(1,1,-1)
  71. cube.rotation.y = Math.PI
  72. scene.add(cube)
  73. //设置cube纹理加载器
  74. const cubeTextureLoader = new THREE.CubeTextureLoader()
  75. const envMapTexture = cubeTextureLoader.load([
  76. backurl,
  77. fronturl,
  78. righturl,
  79. lefturl,
  80. topurl,
  81. bottomurl
  82. ])
  83. envMapTexture.minFilter = THREE.LinearFilter
  84. const sphereGeomtry = new THREE.SphereGeometry(1,20,20)
  85. const sphereMaterial = new THREE.MeshStandardMaterial({
  86. metalness:0.7,
  87. roughness:0.1,
  88. envMap:envMapTexture //设置环境贴图
  89. })
  90. const sphere = new THREE.Mesh(sphereGeomtry,sphereMaterial)
  91. scene.add(sphere)
  92. //辅助线
  93. /* const axesHelper = new THREE.AxesHelper(500);
  94. scene.add(axesHelper) */
  95. renderer.setPixelRatio(wx.getSystemInfoSync().pixelRatio);
  96. renderer.setSize(canvas.width, canvas.height);
  97. function render() {
  98. canvas.requestAnimationFrame(render);
  99. //更新控制器
  100. controls.update();
  101. renderer.render(scene, camera);
  102. }
  103. render()
  104. })
  105. },

(4)源码解读

实现VR全景效果的部分,主要实现步骤如下:

<1> 创建一个正方体

<2>加载6个不同面的图片作为正方体的材质

<3>设置正方体属性 cube.geometry.scale(1,1,-1),即完成了正方体面的反转

<4>将camera的位置设置在正方体内部,即实现了从内部观察正方体的全景效果

源码中还添加了一个金属球,来反射周围的环境,这里主要就是在给金属球设置材质时,将材质的envMap属性设置为想要的环境贴图

3.3D模型下载及实例小程序

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

闽ICP备14008679号