index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. Component({
  2. behaviors: [require('../common/share-behavior').default],
  3. shadowRoot: null,
  4. gltfModle: null,
  5. properties: {
  6. meshCount: {
  7. type: Number,
  8. value: 0,
  9. observer: function(newVal, oldVal) {
  10. if (newVal > oldVal) {
  11. this.addOne();
  12. } else if (newVal < oldVal) {
  13. this.removeOne();
  14. }
  15. }
  16. }
  17. },
  18. data: {
  19. loaded: false,
  20. arReady: false
  21. },
  22. methods: {
  23. async handleReady({detail}) {
  24. const scene = this.scene = detail.value;
  25. console.log('xr-scene', scene);
  26. this.meshList = [];
  27. const xrFrameSystem = wx.getXrFrameSystem()
  28. this.shadowRoot = scene.getElementById('shadow-root');
  29. const {value: envData} = await scene.assets.loadAsset({type: 'env-data', assetId: 'env1', src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/env-test.bin'});
  30. const envElement = scene.createElement(xrFrameSystem.XREnv);
  31. this.shadowRoot.addChild(envElement);
  32. const envComp = envElement.getComponent(xrFrameSystem.Env);
  33. envComp.setData({envData: envData});
  34. const {value: model} = await scene.assets.loadAsset({type: 'gltf', assetId: 'damage-helmet', src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/damage-helmet/index.glb'});
  35. this.gltfModle = model;
  36. const gltfElement = scene.createElement(xrFrameSystem.XRGLTF);
  37. this.shadowRoot.addChild(gltfElement);
  38. const gltfComp = gltfElement.getComponent(xrFrameSystem.GLTF);
  39. gltfComp.setData({model: model});
  40. const cameraElement = scene.createElement(xrFrameSystem.XRCamera);
  41. this.shadowRoot.addChild(cameraElement);
  42. cameraElement.getComponent(xrFrameSystem.Transform).position.setValue(0, 0, 9);
  43. cameraElement.getComponent(xrFrameSystem.Camera).setData({
  44. target: gltfElement.getComponent(xrFrameSystem.Transform),
  45. background: 'skybox'
  46. });
  47. cameraElement.addComponent(xrFrameSystem.CameraOrbitControl, {});
  48. },
  49. addOne() {
  50. const xrFrameSystem = wx.getXrFrameSystem()
  51. const pos = [Math.random(), Math.random(), Math.random()].map(v => (v * 2 - 1) * 6);
  52. const gltfElement = this.scene.createElement(xrFrameSystem.XRGLTF);
  53. this.shadowRoot.addChild(gltfElement);
  54. gltfElement.getComponent(xrFrameSystem.Transform).position.setArray(pos);
  55. gltfElement.getComponent(xrFrameSystem.GLTF).setData({model: this.gltfModle});
  56. this.meshList.push(gltfElement);
  57. },
  58. removeOne() {
  59. const element = this.meshList.pop();
  60. if (element) {
  61. this.shadowRoot.removeChild(element);
  62. }
  63. },
  64. handleTick: function({detail}) {
  65. const {el, value} = detail;
  66. },
  67. handleDesotry() {
  68. },
  69. }
  70. })