index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Component({
  2. behaviors: [require('../../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. },
  6. data: {
  7. loaded: false
  8. },
  9. lifetimes: {},
  10. methods: {
  11. handleReady({detail}) {
  12. const xrScene = this.scene = detail.value;
  13. console.log('xr-scene', xrScene);
  14. },
  15. handleAssetsProgress: function({detail}) {
  16. console.log('assets progress', detail.value);
  17. },
  18. handleAssetsLoaded: function({detail}) {
  19. console.log('assets loaded', detail.value);
  20. this.setData({loaded: true});
  21. this.setBallon();
  22. },
  23. async setBallon() {
  24. const scene = this.scene;
  25. const xrSystem = wx.getXrFrameSystem();
  26. // 替换贴图
  27. const ballonElm = this.scene.getElementById('ballon');
  28. const ballonGLTF = ballonElm.getComponent(xrSystem.GLTF);
  29. const textureAsset = await scene.assets.loadAsset({
  30. type: 'texture',
  31. assetId: `texture-1`,
  32. src: 'https://mmbizwxaminiprogram-1258344707.cos.ap-guangzhou.myqcloud.com/xr-frame/demo/waifu.png',
  33. });
  34. for(const mesh of ballonGLTF.meshes) {
  35. console.log('textureAsset', textureAsset.value);
  36. mesh.material.setTexture('u_baseColorMap', textureAsset.value);
  37. }
  38. // 替换状态
  39. const ballonBlendElm = this.scene.getElementById('ballonBlend');
  40. const ballonBlendGLTF = ballonBlendElm.getComponent(xrSystem.GLTF);
  41. for(const mesh of ballonBlendGLTF.meshes) {
  42. // 清理模型金属度
  43. mesh.material.setVector('u_specularFactor', xrSystem.Vector3.createFromNumber(0, 0, 0));
  44. // 通过alphaMode 的 Setter 设置,或者写入renderState,但需要手动控制宏
  45. mesh.material.alphaMode = "BLEND";
  46. mesh.material.setVector('u_baseColorFactor', xrSystem.Vector4.createFromNumber(0, 0.5, 0, 0.5));
  47. }
  48. }
  49. }
  50. })