index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Component({
  2. behaviors: [require('../common/share-behavior').default],
  3. properties: {
  4. a: Number,
  5. },
  6. data: {
  7. loaded: false,
  8. displayBricks: false
  9. },
  10. lifetimes: {},
  11. methods: {
  12. handleReady({detail}) {
  13. const xrScene = this.scene = detail.value;
  14. this.cubeGeometry = this.scene.assets.getAsset("geometry", "cube");
  15. this.bricksRoot = this.scene.getElementById("bricksRoot");
  16. console.log('xr-scene', xrScene);
  17. this.matMap = new Map();
  18. },
  19. handleAssetsProgress: function({detail}) {
  20. console.log('assets progress', detail.value);
  21. },
  22. handleAssetsLoaded: function({detail}) {
  23. console.log('assets loaded', detail.value);
  24. this.setData({loaded: true});
  25. },
  26. handleARTrackerState({detail}) {console.log("ar state", detail.value.state);
  27. // 事件的值即为`ARTracker`实例
  28. const tracker = detail.value;
  29. // 获取当前状态和错误信息
  30. const {state, errorMessage} = tracker;
  31. if (state === 2 && !this.inited) {
  32. this.initBricks();
  33. this.inited = true;
  34. }
  35. },
  36. initBricks() {
  37. const xr = wx.getXrFrameSystem();
  38. const logo = this.scene.assets.getAsset("raw", "logo");
  39. const logoView = new Uint8Array(logo);
  40. if (logoView[0] !== 0x42 || logoView[1] !== 0x4D) {
  41. console.error("Not a valid bmp file!");
  42. return;
  43. }
  44. const dataOffset = logoView[10];
  45. const width = logoView[18];
  46. const height = logoView[22];
  47. for (let i = 0; i < width; i++) {
  48. for (let j = 0; j < height; j++) {
  49. const b = logoView[dataOffset + (j * width + i) * 4 + 0];
  50. const g = logoView[dataOffset + (j * width + i) * 4 + 1];
  51. const r = logoView[dataOffset + (j * width + i) * 4 + 2];
  52. if (r !== 0xff || g !== 0x0 || b !== 0x80) {
  53. // not transparent pixel
  54. this.makeBrick((i - width * 0.5) * 0.11, (j - height * 0.5) * 0.11, r, g, b);
  55. }
  56. }
  57. }
  58. this.scene.event.add('touchstart', (e) => {
  59. const touch0 = e.touches[0];
  60. this.shoot(((touch0.pageX / this.scene.frameWidth) * 2 - 1) * 1, ((1.0 - touch0.pageY / this.scene.frameHeight) * 2 - 1) * 1);
  61. });
  62. },
  63. makeBrick(x, y, r, g, b) {
  64. const xr = wx.getXrFrameSystem();
  65. let mat = this.matMap.get(r * 256 * 256 + g * 256 + b);
  66. if (!mat) {
  67. mat = new xr.Material(this.scene);
  68. mat.initByEffect(this.scene.assets.getAsset("effect", "standard"));
  69. mat.setVector("u_baseColorFactor", xr.Vector4.createFromNumber(r / 256, g / 256, b / 256, 1));
  70. this.matMap.set(r * 256 * 256 + g * 256 + b, mat);
  71. }
  72. const meshDesc = {
  73. geometry: this.cubeGeometry,
  74. material: mat
  75. };
  76. const brickEl = this.scene.createElement(xr.XRNode);
  77. this.bricksRoot.addChild(brickEl);
  78. const brickMesh = brickEl.addComponent(xr.Mesh, meshDesc);
  79. const transform = brickEl.getComponent("transform");
  80. transform.position.x = x;
  81. transform.position.y = y;
  82. transform.position.z = 0;
  83. transform.scale.setValue(0.1, 0.1, 0.1);
  84. const rigidbody = brickEl.addComponent(xr.Rigidbody, { useGravity: false });
  85. brickEl.addComponent(xr.CubeShape, { autoFit: true });
  86. brickEl.addComponent(xr.ShapeInteract, { collide: true, bounciness: 0 });
  87. rigidbody.sleep();
  88. return brickMesh;
  89. },
  90. shoot(x, y) {
  91. const xr = wx.getXrFrameSystem();
  92. const camera = this.scene.getElementById("camera").getComponent("camera");
  93. const origin = this.scene.getElementById("camera").getComponent("transform");
  94. const meshDesc = {
  95. geometry: this.scene.assets.getAsset("geometry", "sphere")
  96. };
  97. const bulletEl = this.scene.createElement(xr.XRNode);
  98. this.bricksRoot.addChild(bulletEl);
  99. const bulletMesh = bulletEl.addComponent(xr.Mesh, meshDesc);
  100. const transform = bulletEl.getComponent("transform");
  101. transform.position.set(origin.position);
  102. transform.scale.setValue(0.1, 0.1, 0.1);
  103. const rigidbody = bulletEl.addComponent(xr.Rigidbody, { useGravity: false });
  104. bulletEl.addComponent(xr.SphereShape, { autoFit: true });
  105. bulletEl.addComponent(xr.ShapeInteract, { collide: true, bounciness: 0 });
  106. let ray = xr.Vector3.createFromNumber(0, 0, 0);
  107. camera.convertClipPositionToWorld(xr.Vector3.createFromNumber(x, y, 0), ray);
  108. ray = ray.sub(origin.position);
  109. ray = ray.scale(10 / ray.length());
  110. rigidbody.addForce(ray, 1);
  111. }
  112. }
  113. })