index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. var sceneReadyBehavior = require('../../behavior-scene/scene-ready');
  2. Page({
  3. behaviors:[sceneReadyBehavior],
  4. moveTimes: 0,
  5. data: {
  6. // 内置
  7. height: 600,
  8. heightScale: 0.85,
  9. showBackBtn: true,
  10. // 页面
  11. useScan: true,
  12. useGLTF: false,
  13. useVideo1: false,
  14. useVideo2: false,
  15. markerList: [],
  16. // scan 相关
  17. showMarkerWrap: false,
  18. markerLeft: -50,
  19. markerTop: 50,
  20. markerWidth: 0,
  21. markerHeight: 0,
  22. // 全局状态
  23. dataReady: false,
  24. // Debug
  25. debugMsg: 'Defalut Words',
  26. },
  27. onLoad( ) {
  28. this.refreshData();
  29. },
  30. resetData() {
  31. this.setData({
  32. dataReady: false,
  33. showMarkerWrap: false,
  34. markerLeft: -50,
  35. markerTop: 50,
  36. markerWidth: 0,
  37. markerHeight: 0,
  38. })
  39. },
  40. refreshData() {
  41. this.resetData();
  42. // 模拟用的数据集合,可以跟进需要切换为后端接口
  43. const mockDataList = [];
  44. // 识别框
  45. if (this.data.useScan) {
  46. mockDataList.push({
  47. name: '微信球',
  48. markerImg: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/wxball.jpg',
  49. type: 'scan',
  50. src: '',
  51. });
  52. }
  53. // glTF模型
  54. if (this.data.useGLTF) {
  55. mockDataList.push({
  56. name: '扫描画',
  57. markerImg: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/portalImage.jpg',
  58. type: 'gltf',
  59. src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/fiesta_tea/scene.gltf',
  60. });
  61. }
  62. // 视频
  63. if (this.data.useVideo1) {
  64. mockDataList.push({
  65. name: '2Dmarker',
  66. markerImg: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/marker/2dmarker-test.jpg',
  67. type: 'video',
  68. src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/videos/cat.mp4',
  69. });
  70. }
  71. if (this.data.useVideo2) {
  72. mockDataList.push({
  73. name: '虎年企鹅',
  74. markerImg: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/marker/osdmarker-test.jpg',
  75. type: 'video',
  76. src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/videos/paris.mp4',
  77. });
  78. }
  79. // 需要使用的marker
  80. const markerList = []
  81. let scanIndex = 0
  82. let videoIndex = 0
  83. let gltfIndex = 0
  84. for (let i = 0; i < mockDataList.length; i++) {
  85. const mockItem = mockDataList[i];
  86. switch (mockItem.type) {
  87. case 'scan': // scan
  88. const scanId = 'scan' + scanIndex
  89. markerList.push({
  90. id: scanId,
  91. name: mockItem.name,
  92. renderType: mockItem.type,
  93. markerImage: mockItem.markerImg,
  94. src: mockItem.src,
  95. });
  96. scanIndex++;
  97. break;
  98. case 'video': // video
  99. const videoId = 'video' + videoIndex
  100. markerList.push({
  101. id: videoId,
  102. name: mockItem.name,
  103. renderType: mockItem.type,
  104. markerImage: mockItem.markerImg,
  105. src: mockItem.src,
  106. });
  107. videoIndex++;
  108. break;
  109. case 'gltf': // gltf
  110. const gltfId = 'gltf' + gltfIndex
  111. markerList.push({
  112. id: gltfId,
  113. name: mockItem.name,
  114. renderType: mockItem.type,
  115. markerImage: mockItem.markerImg,
  116. src: mockItem.src,
  117. });
  118. gltfIndex++;
  119. break;
  120. }
  121. }
  122. console.log('markerList', markerList);
  123. this.setData({
  124. dataReady: true,
  125. markerList: markerList
  126. });
  127. this.setData({
  128. debugMsg: 'markerList:' + markerList.length
  129. })
  130. },
  131. tapScan() {
  132. this.setData({
  133. useScan: !this.data.useScan
  134. });
  135. this.refreshData();
  136. },
  137. tapGLTF() {
  138. this.setData({
  139. useGLTF: !this.data.useGLTF
  140. });
  141. this.refreshData();
  142. },
  143. tapVideo1() {
  144. this.setData({
  145. useVideo1: !this.data.useVideo1
  146. });
  147. this.refreshData();
  148. },
  149. tapVideo2() {
  150. this.setData({
  151. useVideo2: !this.data.useVideo2
  152. });
  153. this.refreshData();
  154. },
  155. handleTrackerChange(cur) {
  156. const item = cur.detail;
  157. this.setData({
  158. debugMsg: 'handleTrackerChange:' + item.name
  159. })
  160. },
  161. handleTrackerMove(cur) {
  162. const detail = cur.detail
  163. const trackerInfo = detail.trackerInfo
  164. this.moveTimes++
  165. if (detail.type === 'scan') {
  166. if (detail.active) {
  167. this.setData({
  168. showMarkerWrap: true,
  169. markerLeft: Math.floor((trackerInfo.x) * 100),
  170. markerTop: Math.floor((trackerInfo.y) * 100) * this.data.heightScale,
  171. markerWidth: Math.floor(trackerInfo.halfWidth * 2 * this.data.width),
  172. markerHeight: Math.floor(trackerInfo.halfWidth * 2 * this.data.width / trackerInfo.widthDivideHeight),
  173. // debugMsg: 'pos:' + trackerInfo.x + '\n' + trackerInfo.y + '\n halfWidth:' + trackerInfo.halfWidth + '\nmoveTimes:' + this.moveTimes
  174. })
  175. } else {
  176. this.setData({
  177. showMarkerWrap: false,
  178. })
  179. }
  180. }
  181. },
  182. });