index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. Component({
  2. behaviors: [require('../../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. },
  6. data: {
  7. loaded: false,
  8. loading: false,
  9. gltfLoaded: false,
  10. videoLoaded: false,
  11. imageLoaded: false,
  12. gltfIdList: [],
  13. videoIdList: [],
  14. imageIdList: [],
  15. },
  16. properties: {
  17. gltfListRaw: {
  18. type: Array,
  19. value: [],
  20. },
  21. videoListRaw: {
  22. type: Array,
  23. value: [],
  24. },
  25. imageListRaw: {
  26. type: Array,
  27. value: [],
  28. },
  29. },
  30. observers: {
  31. gltfListRaw(newVal) {
  32. this.releaseGLTF();
  33. this.loadGLTF(newVal);
  34. },
  35. videoListRaw(newVal) {
  36. this.releaseVideo();
  37. this.loadVideo(newVal);
  38. },
  39. imageListRaw(newVal) {
  40. this.releaseImage();
  41. this.loadImage(newVal);
  42. },
  43. },
  44. lifetimes: {},
  45. methods: {
  46. handleReady({detail}) {
  47. const xrScene = this.scene = detail.value;
  48. console.log('xr-scene', xrScene);
  49. // 绑定tick事件
  50. xrScene.event.add('tick', this.handleTick.bind(this));
  51. },
  52. handleAssetsProgress: function({detail}) {
  53. console.log('assets progress', detail.value);
  54. },
  55. handleAssetsLoaded: function({detail}) {
  56. console.log('assets loaded', detail.value);
  57. this.setData({loaded: true});
  58. },
  59. handleTick: function () {
  60. },
  61. releaseGLTF() {
  62. if (this.data.gltfIdList && this.data.gltfIdList.length > 0) {
  63. const scene = this.scene
  64. // 声明使 gltf Mesh 移除
  65. this.setData({
  66. gltfLoaded: false
  67. });
  68. this.data.gltfIdList.map((id) => {
  69. // 释放加载过的资源
  70. scene.assets.releaseAsset('gltf',`gltf-${id}`);
  71. })
  72. }
  73. },
  74. async loadGLTF(gltfList) {
  75. const scene = this.scene
  76. if (gltfList.length > 0) {
  77. const gltfIdList = [];
  78. const gltfModel = await Promise.all(gltfList.map( (gltfItem) => {
  79. gltfIdList.push(gltfItem.id)
  80. const gtltfPromise = scene.assets.loadAsset({
  81. type: 'gltf',
  82. assetId: `gltf-${gltfItem.id}`,
  83. src: gltfItem.src,
  84. })
  85. return gtltfPromise;
  86. }));
  87. console.log('glTF asset loaded')
  88. this.setData({
  89. gltfIdList: gltfIdList,
  90. gltfLoaded: true
  91. })
  92. } else {
  93. this.setData({
  94. gltfIdList: [],
  95. gltfLoaded: false,
  96. });
  97. }
  98. },
  99. releaseVideo() {
  100. if (this.data.videoIdList && this.data.videoIdList.length > 0) {
  101. const scene = this.scene
  102. // 声明使视频 Mesh 移除
  103. this.setData({
  104. videoLoaded: false
  105. });
  106. this.data.videoIdList.map((id) => {
  107. // 释放加载过的资源
  108. scene.assets.releaseAsset('video-texture', `video-${id}`);
  109. scene.assets.releaseAsset('material', `video-mat-${id}`);
  110. })
  111. }
  112. },
  113. async loadVideo(videoList) {
  114. const scene = this.scene
  115. if (videoList.length > 0) {
  116. const videoIdList = [];
  117. const videos = await Promise.all(videoList.map((videoItem) =>{
  118. videoIdList.push(videoItem.id);
  119. return scene.assets.loadAsset({
  120. type: 'video-texture',
  121. assetId: `video-${videoItem.id}`,
  122. src: videoItem.src,
  123. options: { autoPlay: true, loop: true, abortAudio: false },
  124. })
  125. }))
  126. videos.map((videoTexture, index) => {
  127. const videoMat = scene.createMaterial(
  128. scene.assets.getAsset('effect', 'simple'),
  129. { u_baseColorMap: videoTexture.value.texture }
  130. )
  131. scene.assets.addAsset('material', `video-mat-${videoList[index].id}`, videoMat)
  132. })
  133. console.log('video asset loaded')
  134. this.setData({
  135. videoIdList: videoIdList,
  136. videoLoaded: true
  137. })
  138. } else {
  139. this.setData({
  140. videoIdList: [],
  141. videoLoaded: false
  142. })
  143. }
  144. },
  145. releaseImage() {
  146. if (this.data.imageIdList && this.data.imageIdList.length > 0) {
  147. const scene = this.scene
  148. // 声明使视频 Mesh 移除
  149. this.setData({
  150. imageLoaded: false
  151. });
  152. this.data.imageIdList.map((id) => {
  153. // 释放加载过的资源
  154. scene.assets.releaseAsset('texture', `texture-${id}`);
  155. scene.assets.releaseAsset('material', `texture-mat-${id}`);
  156. })
  157. }
  158. },
  159. async loadImage(imageList) {
  160. const scene = this.scene
  161. if (imageList.length > 0) {
  162. const xrFrameSystem = wx.getXrFrameSystem();
  163. const imageIdList = [];
  164. const images = await Promise.all(imageList.map((imageItem) =>{
  165. const id = imageItem.id;
  166. console.log(imageItem, id);
  167. imageIdList.push(id);
  168. if (id === 2) {
  169. // 走 asset 直接加载方式
  170. return scene.assets.loadAsset({
  171. type: 'texture',
  172. assetId: `texture-${imageItem.id}`,
  173. src: imageItem.src,
  174. })
  175. } else if (id === 3) {
  176. // 走 createImage 方式
  177. return new Promise(resolve => {
  178. const image = scene.createImage();
  179. image.src = imageItem.src;
  180. image.onload = () => {
  181. resolve({
  182. value: scene.createTexture({
  183. source: [image],
  184. width: image.width,
  185. height: image.height,
  186. // 按需加后面的
  187. magFilter: xrFrameSystem.EFilterMode.LINEAR_MIPMAP_LINEAR,
  188. minFilter: xrFrameSystem.EFilterMode.LINEAR_MIPMAP_LINEAR,
  189. wrapU: xrFrameSystem.EWrapMode.CLAMP_TO_EDGE,
  190. wrapV: xrFrameSystem.EWrapMode.CLAMP_TO_EDGE,
  191. anisoLevel:1
  192. })
  193. });
  194. };
  195. image.onerror = error => {};
  196. });
  197. }
  198. }));
  199. console.log(images);
  200. images.map((texture, index) => {
  201. const textureMat = scene.createMaterial(
  202. scene.assets.getAsset('effect', 'simple'),
  203. { u_baseColorMap: texture.value }
  204. )
  205. scene.assets.addAsset('material', `texture-mat-${imageList[index].id}`, textureMat)
  206. })
  207. this.setData({
  208. imageIdList: imageIdList,
  209. imageLoaded: true
  210. })
  211. } else {
  212. this.setData({
  213. imageIdList: [],
  214. imageLoaded: false
  215. })
  216. }
  217. }
  218. }
  219. })