summaryrefslogtreecommitdiff
path: root/js/vendor/mx.scene.js
blob: 3cd34b5663c200ddc91590107d8edf9b0a00658e (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// NOTE
//
// This is not a fully functional 3d scene as you might expect.
// The camera can only do pitch (rotationX) and yaw (rotationY), but no roll (rotationZ)
// because I haven't implemented alternative euler orders or quaternions.
//
// For serious 3D scenes with more functionalities you should use
// THREE.js with CSS3D Renderer.

MX.Scene = (function () {

    var add = MX.Object3D.prototype.add,
        remove = MX.Object3D.prototype.remove

    function Scene () {

        this.el = document.createElement('div')
        this.el.classList.add('mx-scene')

        var s = this.el.style

        s[MX.transformProp] = 'preserve-3d'

        s.webkitPerspectiveOrigin = '50% 50%'
        s.mozPerspectiveOrigin = '50% 50%'
        s.perspectiveOrigin = '50% 50%'

        s.webkitUserSelect = 'none'
        s.mozUserSelect = 'none'
        s.userSelect = 'none'

        s.overflow = 'hidden'

        this.inner = new MX.Object3D().addTo(this.el)
        this.inner.el.style.width = '0'
        this.inner.el.style.height = '0'

        var self = this
        var width, height, perspective

        Object.defineProperty(this, 'width', {
            get: function () {
                return width
            },
            set: function (val) {
                width = val
                self.el.style.width = val + 'px'
            }
        })

        Object.defineProperty(this, 'height', {
            get: function () {
                return height
            },
            set: function (val) {
                height = val
                self.el.style.height = val + 'px'
            }
        })

        Object.defineProperty(this, 'perspective', {
            get: function () {
                return perspective
            },
            set: function (val) {
                perspective = val
                self.el.style[MX.perspectiveProp] = val + 'px'
                self.inner.z = -val - self.camera.z
                self.inner.rotationOrigin.z = -val
            }
        })

        var cam = this.camera = new MX.Object3D()
        cam.el = null

        // cam's lookAt is a bit different
        // ignoring rotationZ
        cam.getLookAtEuler = getCameraEuler.bind(cam)

        this.inner.rotationOrigin = { x:0, y:0, z:0 }

        this.perspective = 0
    }

    Scene.prototype = {

        constructor: Scene,

        add: function () {
            add.apply(this.inner, arguments)
            return this
        },

        remove: function () {
            remove.apply(this.inner, arguments)
            return this
        },

        addTo: function (target) {
            if (typeof target === 'string') {
                target = document.querySelector(target)
            }
            if (target instanceof HTMLElement && target.appendChild) {
                target.appendChild(this.el)
            } else {
                console.warn('You can only add a Scene to an HTML element.')
            }
            return this
        },

        update: function () {
            // update inner based on camera

            var i = this.inner,
                c = this.camera

            c.update()

            i.z = -this.perspective - c.z
            i.x = -c.x
            i.y = -c.y

            i.rotationX = -c.rotationX
            i.rotationY = -c.rotationY
            //i.rotationZ = -c.rotationZ

            i.update()
            return this
        }

    }

    function getCameraEuler (target) {
        var dx = target.x - this.x,
            dy = target.y - this.y,
            dz = target.z - this.z
            r = {}
        r.y = Math.atan2(-dx, dz)
        r.x = Math.atan2(-dy, Math.sqrt(dx*dx + dz*dz))
        r.z = 0
        if (MX.rotationUnit === 'deg') {
            r.x = MX.toDeg(r.x)
            r.y = MX.toDeg(r.y)
        }
        return r
    }

    return Scene

})()