3d-view
Version:
3D camera view controller
155 lines (137 loc) • 4.02 kB
JavaScript
'use strict'
module.exports = createViewController
var createTurntable = require('turntable-camera-controller')
var createOrbit = require('orbit-camera-controller')
var createMatrix = require('matrix-camera-controller')
function ViewController(controllers, mode) {
this._controllerNames = Object.keys(controllers)
this._controllerList = this._controllerNames.map(function(n) {
return controllers[n]
})
this._mode = mode
this._active = controllers[mode]
if(!this._active) {
this._mode = 'turntable'
this._active = controllers.turntable
}
this.modes = this._controllerNames
this.computedMatrix = this._active.computedMatrix
this.computedEye = this._active.computedEye
this.computedUp = this._active.computedUp
this.computedCenter = this._active.computedCenter
this.computedRadius = this._active.computedRadius
}
var proto = ViewController.prototype
proto.flush = function(a0) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].flush(a0)
}
}
proto.idle = function(a0) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].idle(a0)
}
}
proto.lookAt = function(a0, a1, a2, a3) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].lookAt(a0, a1, a2, a3)
}
}
proto.rotate = function(a0, a1, a2, a3) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].rotate(a0, a1, a2, a3)
}
}
proto.pan = function(a0, a1, a2, a3) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].pan(a0, a1, a2, a3)
}
}
proto.translate = function(a0, a1, a2, a3) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].translate(a0, a1, a2, a3)
}
}
proto.setMatrix = function(a0, a1) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].setMatrix(a0, a1)
}
}
proto.setDistanceLimits = function(a0, a1) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].setDistanceLimits(a0, a1)
}
}
proto.setDistance = function(a0, a1) {
var cc = this._controllerList
for (var i = 0; i < cc.length; ++i) {
cc[i].setDistance(a0, a1)
}
}
proto.recalcMatrix = function(t) {
this._active.recalcMatrix(t)
}
proto.getDistance = function(t) {
return this._active.getDistance(t)
}
proto.getDistanceLimits = function(out) {
return this._active.getDistanceLimits(out)
}
proto.lastT = function() {
return this._active.lastT()
}
proto.setMode = function(mode) {
if(mode === this._mode) {
return
}
var idx = this._controllerNames.indexOf(mode)
if(idx < 0) {
return
}
var prev = this._active
var next = this._controllerList[idx]
var lastT = Math.max(prev.lastT(), next.lastT())
prev.recalcMatrix(lastT)
next.setMatrix(lastT, prev.computedMatrix)
this._active = next
this._mode = mode
//Update matrix properties
this.computedMatrix = this._active.computedMatrix
this.computedEye = this._active.computedEye
this.computedUp = this._active.computedUp
this.computedCenter = this._active.computedCenter
this.computedRadius = this._active.computedRadius
}
proto.getMode = function() {
return this._mode
}
function createViewController(options) {
options = options || {}
var eye = options.eye || [0,0,1]
var center = options.center || [0,0,0]
var up = options.up || [0,1,0]
var limits = options.distanceLimits || [0, Infinity]
var mode = options.mode || 'turntable'
var turntable = createTurntable()
var orbit = createOrbit()
var matrix = createMatrix()
turntable.setDistanceLimits(limits[0], limits[1])
turntable.lookAt(0, eye, center, up)
orbit.setDistanceLimits(limits[0], limits[1])
orbit.lookAt(0, eye, center, up)
matrix.setDistanceLimits(limits[0], limits[1])
matrix.lookAt(0, eye, center, up)
return new ViewController({
turntable: turntable,
orbit: orbit,
matrix: matrix
}, mode)
}