index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var sceneReadyBehavior = require('../behavior-scene/scene-ready');
  2. var handleDecodedXML = require('../behavior-scene/util').handleDecodedXML;
  3. var xmlCode = `<xr-scene ar-system="modes:Marker" id="xr-scene" bind:ready="handleReady" bind:arReady="handleARReady" bind:log="handleLog">
  4. <xr-assets bind:progress="handleAssetsProgress" bind:loaded="handleAssetsLoaded">
  5. <xr-asset-material asset-id="ar-anchor" effect="standrand" uniforms="u_baseColorFactor:0 1 0 1"></xr-asset-material>
  6. </xr-assets>
  7. <xr-node>
  8. <xr-node node-id="id2" scale="2 2 2" position="0 0 0"></xr-node>
  9. <xr-ar-tracker mode="Marker" src="{{markerImg}}">
  10. <xr-mesh geometry="cylinder" material="ar-anchor" />
  11. </xr-ar-tracker>
  12. <xr-camera
  13. id="camera" node-id="camera" position="0.8 2.2 -5" clear-color="0.925 0.925 0.925 1"
  14. target="id2" background="ar" is-ar-camera
  15. camera-orbit-control
  16. ></xr-camera>
  17. </xr-node>
  18. <xr-node node-id="lights">
  19. <xr-light type="ambient" color="1 1 1" intensity="1" />
  20. <xr-light type="directional" rotation="180 0 0" color="1 1 1" intensity="3" />
  21. </xr-node>
  22. </xr-scene>
  23. `;
  24. Page({
  25. behaviors: [sceneReadyBehavior],
  26. data: {
  27. xmlCode: '<div class="codeWrap">' + handleDecodedXML(xmlCode) + '</div>',
  28. markerImg: 'https://ossxiaoan.4dage.com/hq-eduction-vr/hq-bag.jpg',
  29. showLoading: true,
  30. loadingText: '正在初始化相机…',
  31. loadingProgress: 0,
  32. _wxLoadingText: ''
  33. },
  34. onUnload() {
  35. if (wx.hideLoading) wx.hideLoading();
  36. },
  37. handleLoadState(e) {
  38. const detail = (e && e.detail) || {};
  39. const arReady = !!detail.arReady;
  40. const assetsLoaded = !!detail.assetsLoaded;
  41. const item1Loaded = !!detail.item1Loaded;
  42. const item2Loaded = !!detail.item2Loaded;
  43. const p = typeof detail.assetsProgress === 'number' ? detail.assetsProgress : 0;
  44. let loadingText = this.data.loadingText;
  45. if (!arReady) loadingText = '';
  46. else if (!assetsLoaded) loadingText = '';
  47. else if (!item1Loaded || !item2Loaded) loadingText = '';
  48. else loadingText = '';
  49. const loadingProgress = Math.max(0, Math.min(100, Math.round(p)));
  50. const showLoading = !(arReady && assetsLoaded && item1Loaded && item2Loaded);
  51. this.setData({ showLoading, loadingText, loadingProgress });
  52. if (showLoading) {
  53. if (wx.showLoading && loadingText !== this.data._wxLoadingText) {
  54. wx.showLoading({ title: loadingText, mask: true });
  55. this.setData({ _wxLoadingText: loadingText });
  56. }
  57. } else {
  58. if (wx.hideLoading) wx.hideLoading();
  59. if (this.data._wxLoadingText) this.setData({ _wxLoadingText: '' });
  60. }
  61. },
  62. handleChangeMarkerImg: function() {
  63. wx.chooseMedia({
  64. count: 1,
  65. sizeType: ['compressed'],
  66. mediaType: ['image'],
  67. sourceType: ['album'],
  68. success: res => {
  69. const fp = res.tempFiles[0].tempFilePath;
  70. this.setData({markerImg: fp});
  71. },
  72. fail: err => {
  73. console.error('[xr-demo]chooseImage failed', err);
  74. }
  75. });
  76. }
  77. });