summaryrefslogtreecommitdiff
path: root/new-reality/public/assets/js/vendor/mx/mx.followingOrbitCamera.js
blob: 28a9f73535f01a4de49627765c9a4ad040f6c238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
MX.FollowingOrbitCamera = function(opt){
  var exports = {}, bound = false, portraitMode = false
  exports.opt = opt = defaults(opt, {
    el: window,                   // object to bind events on
    camera: scene.camera,         // camera object we'll be moving
    radius: 100,
    radiusRange: [ 10, 1000 ],
    rotationX: Math.PI/2,
    rotationY: 0,
    rotationXRange: [ 0, 1 ],
    rotationYRange: [ 0, 1 ],
    center: { x: 0, y: 0, z: 0 },
    sensitivity: 10,              // moving 1 pixel is like moving N radians
    wheelSensitivity: 10,
    ease: 10,
  })
  var rx, ry, radius, px, py, epsilon = 1e-5, dragging = false
  exports.init = function(){
    ry = opt.rotationY
    rx = opt.rotationX
    radius = opt.radius
    exports.wheel = new wheel({
      el: opt.el,
      update: function(e, delta){
        opt.radius = clamp( opt.radius + delta * opt.wheelSensitivity, opt.radiusRange[0], opt.radiusRange[1] )
      },
    })
    exports.bind()
  }
  exports.toggle = function(state){
    if (state) exports.bind()
    else exports.unbind()
  }
  exports.bind = function(){
    if (bound) return;
    bound = true
    // opt.el.addEventListener("mousedown", down)
    if (is_mobile) {
      if ('DeviceOrientationEvent' in window) {
        window.addEventListener("resize", resize)
        window.addEventListener("deviceorientation", deviceorientation)
        resize()
      }
    }
    else {
      window.addEventListener("mousemove", move)
    }
    // window.addEventListener("mouseup", up)
    exports.wheel.unlock()
  }
  exports.unbind = function(){
    if (! bound) return;
    bound = false
    // opt.el.removeEventListener("mousedown", down)
    window.removeEventListener("mousemove", move)
    // window.removeEventListener("mouseup", up)
    exports.wheel.lock()
  }
  // function down (e) {
  //  px = e.pageX
  //  py = e.pageY
  //  dragging = true
  // }
  function move (e) {
    // if (! dragging) return
    // exports.delta(px - e.pageX, py - e.pageY)
    exports.delta( e.pageX, e.pageY)
    px = e.pageX
    py = e.pageY
  }
  function resize (e) {
    var aspect = window.innerHeight / window.innerWidth;
    portraitMode = aspect >= 1;
  }
  function deviceorientation (e){
    if (e && 'beta' in e && e.beta) {
      var b, g
      if (portraitMode) {
        b = e.beta
        g = e.gamma
      } else {
        b = e.gamma
        g = e.beta
      }
      
      // console.log(b, g)
    }
  }
  // function up (e) {
  //   dragging = false
  // }
  exports.delta = function(x,y){
    // opt.rotationY += x/window.innerWidth * opt.sensitivity
    // opt.rotationX = clamp( opt.rotationX + y/window.innerHeight * opt.sensitivity, 0, Math.PI)
    opt.rotationY = lerp( x / window.innerWidth, opt.rotationYRange[0], opt.rotationYRange[1] )
    opt.rotationX = lerp( y / window.innerHeight, opt.rotationXRange[0], opt.rotationXRange[1] )
  }
  exports.zoom = function(r){
    opt.radius = r
  }
  exports.zoomDelta = function(r){
    opt.radius += r
  }
  exports.move = function(y, x){
    opt.rotationY = y
    if (typeof x == "number") { opt.rotationX = x }
  }
  exports.update = function(){
    if (Math.abs(ry - opt.rotationY) > epsilon) {
      ry = avg(ry, opt.rotationY, opt.ease)
    }
    else {
      ry = opt.rotationY
    }
    if (Math.abs(rx - opt.rotationX) > epsilon) {
      rx = avg(rx, opt.rotationX, opt.ease)
    }
    else {
      rx = opt.rotationX
    }
    if (Math.abs(radius - opt.radius) > epsilon) {
      radius = avg(radius, opt.radius, opt.ease)
    }
    else {
      radius = opt.radius
    }
    opt.camera.x = opt.center.x + radius * Math.sin(rx) * Math.cos(ry)
    opt.camera.z = opt.center.y + radius * Math.sin(rx) * Math.sin(ry)
    opt.camera.y = opt.center.z + radius * Math.cos(rx)
    opt.camera.rotationX = Math.PI/2 - rx
    opt.camera.rotationY = ry + Math.PI/2
  }
  return exports
}