index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // 做个简单的延时,保证glTF构建完成
  22. setTimeout(()=>{
  23. this.setUVAnimation();
  24. },200);
  25. },
  26. setUVAnimation() {
  27. const scene = this.scene;
  28. const xrSystem = wx.getXrFrameSystem();
  29. // 获取元素
  30. const twaElm = this.scene.getElementById('twa');
  31. const twaGLTF = twaElm.getComponent(xrSystem.GLTF);
  32. // console.log(twaGLTF);
  33. const changeMesh = twaGLTF.getPrimitivesByNodeName('Cube_0')[0];
  34. const changeMaterial = changeMesh.material;
  35. let offsetX = 0;
  36. let offsetY = 0;
  37. let scaleX = 1;
  38. let scaleY = 1;
  39. let rotation = 0;
  40. // 这里采用一个固定计时器方便编写,建议使用tick
  41. setInterval(()=>{
  42. // 这里做一个 1% 每帧的偏移。
  43. offsetX = offsetX > 1 ? 0 : offsetX + 0.01;
  44. offsetY = offsetY > 1 ? 0 : offsetY + 0.01;
  45. // 这里做一个每帧的缩放
  46. // scaleX = scaleX > 2 ? 1 : scaleX + 0.01;
  47. // scaleY = scaleY > 2 ? 1 : scaleY + 0.01;
  48. // 这里做一个每帧的旋转。
  49. // rotation = rotation > Math.PI ? 0 : rotation + Math.PI / 360;
  50. const uvMatrix = xrSystem.Matrix4.createFromArray(this.getUvTransform(offsetX, offsetY, scaleX, scaleY, rotation))
  51. // 设置uv矩阵
  52. changeMaterial.setMatrix('u_uvTransform', uvMatrix);
  53. }, 40);
  54. const uvMatrix = xrSystem.Matrix4.createFromArray(this.getUvTransform(offsetX, offsetY, scaleX, scaleY, rotation))
  55. // 设置uv矩阵
  56. changeMaterial.setMatrix('u_uvTransform', uvMatrix);
  57. // 开启使用uv矩阵的宏
  58. changeMaterial.setMacro('WX_USE_UVTRANSFORM', true );
  59. changeMaterial.setMacro('WX_USE_UVTRANSFORM_BASECOLOR', true );
  60. }
  61. ,
  62. /**
  63. * 获取UV变化矩阵,列主序
  64. *
  65. * @param {number} tx x轴偏移
  66. * @param {number} ty y轴偏移
  67. * @param {number} sx x轴缩放
  68. * @param {number} sy y轴缩放
  69. * @param {number} rotation 旋转
  70. * @return {Array} uvMatrixArray
  71. */
  72. getUvTransform(tx, ty, sx, sy, rotation) {
  73. const c = Math.cos( rotation );
  74. const s = Math.sin( rotation );
  75. return [
  76. sx * c, -sx * s, 0, 0,
  77. sy * s, sy * c, 0, 0,
  78. 0, 0, 1, 0,
  79. tx, ty, 0, 1,
  80. ];
  81. }
  82. }
  83. })