plane-ar-v2-marker.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import getBehavior from './behavior'
  2. import yuvBehavior from './yuvBehavior'
  3. const NEAR = 0.001
  4. const FAR = 1000
  5. let time = 0;
  6. let countNumber = 20;
  7. let count = 0;
  8. Component({
  9. behaviors: [getBehavior(), yuvBehavior],
  10. data: {
  11. theme: 'light',
  12. },
  13. lifetimes: {
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. detached() {
  18. console.log("页面detached")
  19. if (wx.offThemeChange) {
  20. wx.offThemeChange()
  21. }
  22. },
  23. ready() {
  24. console.log("页面准备完全")
  25. this.setData({
  26. theme: wx.getSystemInfoSync().theme || 'light'
  27. })
  28. if (wx.onThemeChange) {
  29. wx.onThemeChange(({theme}) => {
  30. this.setData({theme})
  31. })
  32. }
  33. },
  34. },
  35. methods: {
  36. init() {
  37. this.initGL()
  38. },
  39. render(frame) {
  40. let start = Date.now()
  41. this.renderGL(frame)
  42. const camera = frame.camera
  43. // 修改光标位置
  44. const reticle = this.reticle
  45. if (reticle) {
  46. const hitTestRes = this.session.hitTest(0.5, 0.5)
  47. if (hitTestRes.length) {
  48. reticle.matrixAutoUpdate = false
  49. reticle.matrix.fromArray(hitTestRes[0].transform)
  50. reticle.matrix.decompose(reticle.position, reticle.quaternion, reticle.scale)
  51. if(reticle.position.z !=0 )
  52. reticle.visible = true
  53. } else {
  54. reticle.visible = false
  55. }
  56. }
  57. // 更新动画
  58. this.updateAnimation()
  59. // 相机
  60. if (camera) {
  61. this.camera.matrixAutoUpdate = false
  62. this.camera.matrixWorldInverse.fromArray(camera.viewMatrix)
  63. this.camera.matrixWorld.getInverse(this.camera.matrixWorldInverse)
  64. const projectionMatrix = camera.getProjectionMatrix(NEAR, FAR)
  65. this.camera.projectionMatrix.fromArray(projectionMatrix)
  66. this.camera.projectionMatrixInverse.getInverse(this.camera.projectionMatrix)
  67. }
  68. this.renderer.autoClearColor = false
  69. this.renderer.render(this.scene, this.camera)
  70. this.renderer.state.setCullFace(this.THREE.CullFaceNone)
  71. let end = Date.now()
  72. time += end-start
  73. count++
  74. // if(count >= countNumber){
  75. // console.log(`${count}次平均耗时统计为${time/count}ms`)
  76. // count = 0
  77. // time = 0
  78. // }
  79. },
  80. },
  81. })