index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. gltfIdList: [],
  12. videoIdList: [],
  13. },
  14. properties: {
  15. gltfListRaw: {
  16. type: Array,
  17. value: [],
  18. },
  19. videoListRaw: {
  20. type: Array,
  21. value: [],
  22. },
  23. },
  24. observers: {
  25. gltfListRaw(newVal) {
  26. this.releaseGLTF();
  27. this.loadGLTF(newVal);
  28. },
  29. videoListRaw(newVal) {
  30. this.releaseVideo();
  31. this.loadVideo(newVal);
  32. },
  33. },
  34. lifetimes: {},
  35. methods: {
  36. handleReady({detail}) {
  37. const xrScene = this.scene = detail.value;
  38. console.log('xr-scene', xrScene);
  39. // 绑定tick事件
  40. xrScene.event.add('tick', this.handleTick.bind(this));
  41. },
  42. handleAssetsProgress: function({detail}) {
  43. console.log('assets progress', detail.value);
  44. },
  45. handleAssetsLoaded: function({detail}) {
  46. console.log('assets loaded', detail.value);
  47. this.setData({loaded: true});
  48. },
  49. handleTick: function () {
  50. },
  51. releaseGLTF() {
  52. if (this.data.gltfIdList && this.data.gltfIdList.length > 0) {
  53. const scene = this.scene
  54. this.data.gltfIdList.map((id) => {
  55. // 释放加载过的资源
  56. scene.assets.releaseAsset('gltf',`gltf-${id}`);
  57. })
  58. }
  59. },
  60. async loadGLTF(gltfList) {
  61. const scene = this.scene
  62. if (gltfList.length > 0) {
  63. const gltfIdList = [];
  64. const gltfModel = await Promise.all(gltfList.map( (gltfItem) => {
  65. gltfIdList.push(gltfItem.id)
  66. const gtltfPromise = scene.assets.loadAsset({
  67. type: 'gltf',
  68. assetId: `gltf-${gltfItem.id}`,
  69. src: gltfItem.src,
  70. })
  71. return gtltfPromise;
  72. }));
  73. console.log('glTF asset loaded')
  74. this.setData({
  75. gltfIdList: gltfIdList,
  76. gltfLoaded: true
  77. })
  78. } else {
  79. this.setData({
  80. gltfIdList: [],
  81. gltfLoaded: false,
  82. });
  83. }
  84. },
  85. releaseVideo() {
  86. if (this.data.videoIdList && this.data.videoIdList.length > 0) {
  87. const scene = this.scene
  88. this.data.videoIdList.map((id) => {
  89. // 释放加载过的资源
  90. scene.assets.releaseAsset('video-texture', `video-${id}`);
  91. scene.assets.releaseAsset('material', `video-mat-${id}`);
  92. })
  93. }
  94. },
  95. async loadVideo(videoList) {
  96. const scene = this.scene
  97. if (videoList.length > 0) {
  98. const videoIdList = [];
  99. const videos = await Promise.all(videoList.map((videoItem) =>{
  100. videoIdList.push(videoItem.id);
  101. return scene.assets.loadAsset({
  102. type: 'video-texture',
  103. assetId: `video-${videoItem.id}`,
  104. src: videoItem.src,
  105. options: { autoPlay: true, loop: true },
  106. })
  107. }))
  108. videos.map((videoTexture, index) => {
  109. const videoMat = scene.createMaterial(
  110. scene.assets.getAsset('effect', 'standard'),
  111. { u_baseColorMap: videoTexture.value.texture }
  112. )
  113. scene.assets.addAsset('material', `video-mat-${videoList[index].id}`, videoMat)
  114. })
  115. console.log('video asset loaded')
  116. this.setData({
  117. videoIdList: videoIdList,
  118. videoLoaded: true
  119. })
  120. } else {
  121. this.setData({
  122. videoIdList: [],
  123. videoLoaded: false
  124. })
  125. }
  126. },
  127. }
  128. })