index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var touch = false;
  2. Component({
  3. behaviors: [require('../../common/share-behavior').default],
  4. properties: {
  5. itemIndex: {
  6. type: Number,
  7. observer(newVal, oldVal) {
  8. if (newVal != undefined) {
  9. switch (newVal) {
  10. case 0:
  11. this.pause()
  12. break;
  13. case 1:
  14. this.resume()
  15. break;
  16. default:
  17. //前两位给了暂停、继续状态, 这里需要-2
  18. this.play(newVal - 2)
  19. this.itemIndex = newVal - 2;
  20. break;
  21. }
  22. }
  23. },
  24. },
  25. },
  26. data: {
  27. loaded: false,
  28. },
  29. lifetimes: {},
  30. methods: {
  31. handleReady({
  32. detail
  33. }) {
  34. const xrScene = this.scene = detail.value;
  35. console.log('xr-scene', xrScene);
  36. },
  37. handleAssetsProgress: function ({
  38. detail
  39. }) {
  40. console.log('assets progress', detail.value);
  41. },
  42. handleAssetsLoaded: function ({
  43. detail
  44. }) {
  45. console.log('assets loaded', detail.value);
  46. this.setData({
  47. loaded: true
  48. });
  49. },
  50. handleTouchModel(e) {
  51. touch = true
  52. const xrScene = this.scene;
  53. const xrFrameSystem = wx.getXrFrameSystem();
  54. const myModel = xrScene.getElementById('myModel');
  55. this.myModelAnimator = myModel.getComponent(xrFrameSystem.Animator);
  56. const clips = this.myModelAnimator._clips;
  57. this.clipName = []
  58. clips.forEach((v, key) => {
  59. if (key.indexOf('pose') == -1) {
  60. this.clipName.push(key)
  61. }
  62. })
  63. this.triggerEvent('infoListener', {
  64. clipName: this.clipName,
  65. });
  66. },
  67. handleTick: function (dt) {
  68. if (touch) {
  69. const xrFrameSystem = wx.getXrFrameSystem();
  70. const camera = this.scene.getElementById('camera').getComponent(xrFrameSystem.Camera)
  71. const myModel = this.scene.getElementById('myModel');
  72. const modelTRS = myModel.getComponent(xrFrameSystem.Transform)
  73. const trackerPos = camera.convertWorldPositionToClip(modelTRS.worldPosition.add(xrFrameSystem.Vector3.createFromNumber(0, 2, 0)))
  74. const dirX = camera.convertWorldPositionToClip(xrFrameSystem.Vector3.createFromNumber(1, 0, 0))
  75. const dirO = camera.convertWorldPositionToClip(xrFrameSystem.Vector3.createFromNumber(0, 0, 0))
  76. const dirZ = camera.convertWorldPositionToClip(xrFrameSystem.Vector3.createFromNumber(0, 0, 1))
  77. var len1 = dirX.sub(dirO).length()
  78. var len2 = dirZ.sub(dirO).length()
  79. var lenRes = len1 > len2 ? len1 : len2
  80. lenRes = lenRes < 0 ? 0 : lenRes > 1 ? 1 : lenRes
  81. this.triggerEvent('infoListener', {
  82. position: {
  83. 'x': trackerPos.x,
  84. 'y': trackerPos.y,
  85. },
  86. len: 0.5 + lenRes
  87. });
  88. }
  89. },
  90. handleGltfLoaded: async function () {
  91. const xrScene = this.scene;
  92. const xrFrameSystem = wx.getXrFrameSystem();
  93. const myModel = xrScene.getElementById('myModel');
  94. const shadowNode = myModel.getComponent("gltf").getInternalNodeByName("mixamorig:RightHandIndex1")
  95. const el = this.scene.createElement(xrFrameSystem.XRMesh, {
  96. geometry: "cube",
  97. scale: "20 20 20",
  98. uniforms: "u_baseColorFactor:1 0 0 1",
  99. position: "0 0 0",
  100. rotation: "0 0 0"
  101. });
  102. shadowNode.addChild(el);
  103. },
  104. play(index) {
  105. console.log("play:", this.clipName[index])
  106. if (this.clipName.length != 0) {
  107. this.myModelAnimator.play(this.clipName[index], {
  108. loop: 10,
  109. });
  110. }
  111. },
  112. pause() {
  113. console.log("pause:", this.clipName[this.itemIndex])
  114. // this.myModelAnimator.pause(this.clipName[this.itemIndex])
  115. this.myModelAnimator.pause()
  116. },
  117. resume() {
  118. console.log("resume:", this.clipName[this.itemIndex])
  119. // this.myModelAnimator.resume(this.clipName[this.itemIndex])
  120. this.myModelAnimator.resume()
  121. }
  122. }
  123. })