index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const colorPattern1 = [
  2. [0.129, 0.612, 0.565],
  3. [0.914, 0.722, 0.141],
  4. [0.933, 0.576, 0.133],
  5. [0.847, 0.247, 0.192],
  6. ]
  7. const colorPattern2 = [
  8. [0.392, 0.6, 0.914],
  9. [0.62, 0.867, 1],
  10. [0.651, 0.965, 1],
  11. [0.745, 1, 0.969],
  12. ]
  13. Component({
  14. behaviors: [require('../../common/share-behavior').default],
  15. properties: {
  16. a: Number,
  17. },
  18. data: {
  19. loaded: false,
  20. arReady: false,
  21. },
  22. lifetimes: {
  23. async attached() {
  24. console.log('data', this.data)
  25. }
  26. },
  27. methods: {
  28. handleReady({detail}) {
  29. const xrScene = this.scene = detail.value;
  30. console.log('xr-scene', xrScene);
  31. },
  32. handleARReady: function({detail}) {
  33. console.log('arReady', this.scene.ar.arVersion);
  34. const xr = wx.getXrFrameSystem();
  35. const scene = this.scene;
  36. const root = scene.getElementById('root');
  37. const cursor = scene.getElementById('cursor');
  38. const cursorTrs = cursor.getComponent(xr.Transform);
  39. const geometryPoint = scene.assets.getAsset('geometry', 'sphere');
  40. const geometryLine = scene.assets.getAsset('geometry', 'cube');
  41. const effectStandard = scene.assets.getAsset('effect', 'standard');
  42. let preCubeTrs = undefined;
  43. let touchFlag = false;
  44. // 缓存这些常量
  45. const forward = xr.Vector3.createFromNumber(0,0,0);
  46. const up = xr.Vector3.createFromNumber(0, 1, 0);
  47. const useQuaternion = xr.Quaternion.createFromNumber(0, 0, 0, 0);
  48. scene.event.add('touchstart', (e) => {
  49. // 点击开始后,放置
  50. if (touchFlag) {
  51. // 控制点击频率
  52. return;
  53. }
  54. touchFlag = true;
  55. // 新的cube
  56. const cubeEle = scene.createElement(xr.XRNode, {
  57. });
  58. const cubeTrs = cubeEle.getComponent(xr.Transform);
  59. const mat = scene.createMaterial(effectStandard);
  60. const color1 = colorPattern1[Math.floor(Math.random() * colorPattern1.length)];
  61. mat.setVector('u_baseColorFactor', xr.Vector4.createFromNumber(color1[0], color1[1], color1[2], 1.0));
  62. mat.setRenderState('cullFace', xr.ECullMode.BACK);
  63. const mesh = cubeEle.addComponent(xr.Mesh, {
  64. geometry: geometryPoint,
  65. material: mat,
  66. });
  67. // 加到场上
  68. root.addChild(cubeEle);
  69. // 获取相机 cursor 位置,并设置到新元素
  70. cubeTrs.setLocalMatrix(cursorTrs.worldMatrix);
  71. // 延时,控制点击频率,并保证矩阵信息完备
  72. setTimeout(() => {
  73. touchFlag = false;
  74. if (preCubeTrs) {
  75. // 存在上一个,进行连线
  76. const preWorldPosition = preCubeTrs.worldPosition;
  77. const worldPosition = cubeTrs.worldPosition;
  78. // console.log(preWorldPosition.x, preWorldPosition.y, preWorldPosition.z, worldPosition.x, worldPosition.y, worldPosition.z)
  79. // 算中点
  80. const posX = (preWorldPosition.x + worldPosition.x) / 2
  81. const posY = (preWorldPosition.y + worldPosition.y) / 2
  82. const posZ = (preWorldPosition.z + worldPosition.z) / 2
  83. // forwad 向量
  84. preWorldPosition.sub(worldPosition, forward);
  85. console.log(forward.x, forward.y, forward.z);
  86. // 向量的模
  87. const module = preWorldPosition.distanceTo(worldPosition);
  88. // 方向四元数
  89. xr.Quaternion.lookRotation(forward, up, useQuaternion);
  90. console.log(useQuaternion.x, useQuaternion.y, useQuaternion.z, useQuaternion.w)
  91. const lineSize = 0.02;
  92. // line
  93. const lineEle = scene.createElement(xr.XRNode, {
  94. position: `${posX} ${posY} ${posZ}`,
  95. scale: `${lineSize} ${lineSize} ${module}`
  96. });
  97. // 加到场上
  98. root.addChild(lineEle);
  99. const lineTrs = lineEle.getComponent(xr.Transform);
  100. lineTrs.quaternion.x = useQuaternion.x;
  101. lineTrs.quaternion.y = useQuaternion.y;
  102. lineTrs.quaternion.z = useQuaternion.z;
  103. lineTrs.quaternion.w = useQuaternion.w;
  104. const matLine = scene.createMaterial(effectStandard);
  105. const color2 = colorPattern2[Math.floor(Math.random() * colorPattern2.length)];
  106. matLine.setVector('u_baseColorFactor', xr.Vector4.createFromNumber(color2[0], color2[1], color2[2], 1.0));
  107. matLine.setRenderState('cullFace', xr.ECullMode.FRONT);
  108. const meshLine = lineEle.addComponent(xr.Mesh, {
  109. geometry: geometryLine,
  110. material: matLine,
  111. });
  112. preCubeTrs = cubeTrs
  113. }else {
  114. preCubeTrs = cubeTrs
  115. }
  116. }, 100);
  117. });
  118. }
  119. }
  120. })