index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. let camera;
  2. let player;
  3. let xrFrameSystem;
  4. // 位移速度
  5. let speed = 5;
  6. // 视角旋转的速度
  7. let smoothSpeed = 8;
  8. // 世界坐标系下的标准方位
  9. let up;
  10. let left;
  11. // player相关
  12. let position;
  13. let quaternionP; //player的四元数
  14. let quaternionPIni; //player每次转动开始时的角度
  15. let quaternionPRes; //player每次需要转动到的角度
  16. let quaternionPTemp; //旋转四元数的临时变量
  17. // camera相关
  18. let quaternionC; //camera的四元数
  19. let quaternionCIni; //camera每次转动开始时的角度
  20. let quaternionCRes; //camera每次需要转动到的角度
  21. // 初始化是否完成的标记
  22. let initFinish = false;
  23. Component({
  24. behaviors: [require('../../common/share-behavior').default],
  25. properties: {
  26. width:Number,
  27. height:Number,
  28. transferData: {
  29. type:Object,
  30. observer(newVal, oldVal){
  31. if(newVal.biasRotX != undefined){
  32. this.biasRotX = newVal.biasRotX;
  33. this.biasRotY = newVal.biasRotY;
  34. }
  35. if(newVal.initRotX != undefined){
  36. this.initRotX = newVal.initRotX;
  37. this.initRotY = newVal.initRotY;
  38. }
  39. if(newVal.biasX != undefined){
  40. this.biasX = newVal.biasX;
  41. this.biasY = newVal.biasY;
  42. }
  43. },
  44. },
  45. reset: {
  46. type: Number,
  47. observer(newVal, oldVal) {
  48. //监听发生变化的reset后,执行重置逻辑
  49. position.set(xrFrameSystem.Vector3.createFromNumber(0, 1.6, 1));
  50. quaternionC.setFromYawRollPitch(0, 0, 0);
  51. quaternionP.setFromYawRollPitch(Math.PI, 0, 0);
  52. }
  53. }
  54. },
  55. data: {
  56. loaded: false
  57. },
  58. lifetimes: {},
  59. methods: {
  60. handleReady({
  61. detail
  62. }) {
  63. const xrScene = this.scene = detail.value;
  64. console.log('xr-scene', xrScene);
  65. xrFrameSystem = wx.getXrFrameSystem();
  66. camera = xrScene.getElementById("camera");
  67. player = xrScene.getElementById("player");
  68. up = xrFrameSystem.Vector3.createFromNumber(0, 1, 0);
  69. left = xrFrameSystem.Vector3.createFromNumber(1, 0, 0);
  70. quaternionC = camera.getComponent(xrFrameSystem.Transform).quaternion;
  71. quaternionP = player.getComponent(xrFrameSystem.Transform).quaternion;
  72. position = player.getComponent(xrFrameSystem.Transform).position;
  73. quaternionPIni = new xrFrameSystem.Quaternion();
  74. quaternionPIni.set(quaternionP);
  75. quaternionPRes = new xrFrameSystem.Quaternion();
  76. quaternionPRes.set(quaternionP);
  77. quaternionPTemp = new xrFrameSystem.Quaternion();
  78. quaternionPTemp.setFromYawRollPitch(0, 0, 0);
  79. quaternionCIni = new xrFrameSystem.Quaternion();
  80. quaternionCIni.set(quaternionC);
  81. quaternionCRes = new xrFrameSystem.Quaternion();
  82. quaternionCRes.set(quaternionC);
  83. this.biasRotX = 0;
  84. this.biasRotY = 0;
  85. this.initRotX = 0;
  86. this.initRotY = 0;
  87. this.biasX = 0;
  88. this.biasY = 0;
  89. initFinish = true;
  90. },
  91. handleAssetsProgress: function ({
  92. detail
  93. }) {
  94. console.log('assets progress', detail.value);
  95. },
  96. handleAssetsLoaded: function ({
  97. detail
  98. }) {
  99. console.log('assets loaded', detail.value);
  100. this.setData({
  101. loaded: true
  102. });
  103. },
  104. handleTick: function (dt) {
  105. //确保handleReady时期的初始化完成
  106. if (!initFinish)
  107. return;
  108. var deltaTime = dt.detail.value / 1000;
  109. //------摄像头旋转逻辑------//
  110. let rotX = (this.biasRotX - this.initRotX) / this.data.width * Math.PI;
  111. let rotY = (this.biasRotY - this.initRotY) / this.data.height * Math.PI;
  112. //水平方向旋转player node
  113. if (this.biasRotX == 0) {
  114. quaternionPIni.set(quaternionPTemp);
  115. quaternionPRes.set(quaternionPTemp);
  116. } else {
  117. quaternionPIni.multiply(xrFrameSystem.Quaternion.createFromAxisAngle(up, -rotX), quaternionPRes);
  118. }
  119. //垂直方向旋转camera node
  120. if (this.biasRotY == 0) {
  121. quaternionCIni.set(quaternionCRes);
  122. quaternionCRes.set(quaternionCRes);
  123. } else {
  124. quaternionCIni.multiply(xrFrameSystem.Quaternion.createFromAxisAngle(left, rotY), quaternionCRes);
  125. }
  126. quaternionPTemp.slerp(quaternionPRes, smoothSpeed * deltaTime, quaternionPTemp);
  127. quaternionC.slerp(quaternionPTemp.multiply(quaternionCRes), smoothSpeed * deltaTime, quaternionC);
  128. //------摄像头位移逻辑------//
  129. var x = this.biasX;
  130. var y = this.biasY;
  131. if (x || y) {
  132. var z = Math.sqrt(x * x + y * y);
  133. var ratio = z / 50; //此处除以50,因为摇杆盘半径为50
  134. ratio = ratio > 1 ? 1 : ratio < 0 ? 0 : ratio;
  135. var temp = xrFrameSystem.Vector3.createFromNumber(-x / z, 0, -y / z);
  136. temp = temp.scale(ratio * speed * deltaTime);
  137. //位移需要根据旋转角度做转化, 这里需要取得camera的世界旋转矩阵
  138. temp.applyQuaternion(camera.getComponent(xrFrameSystem.Transform).worldQuaternion);
  139. position.set(position.add(temp));
  140. }
  141. },
  142. }
  143. })