-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
87 lines (70 loc) · 2.57 KB
/
index.js
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
var THREE, temporaryPosition, temporaryVector
module.exports = function(three, opts) {
temporaryPosition = new three.Vector3
temporaryVector = new three.Vector3
return new View(three, opts)
}
function View(three, opts) {
THREE = three // three.js doesn't support multiple instances on a single page
this.fov = opts.fov || 60
this.width = opts.width || 512
this.height = opts.height || 512
this.aspectRatio = opts.aspectRatio || this.width/this.height
this.nearPlane = opts.nearPlane || 1
this.farPlane = opts.farPlane || 10000
this.skyColor = opts.skyColor || 0xBFD1E5
this.ortho = opts.ortho
this.camera = this.ortho?(new THREE.OrthographicCamera(this.width/-2, this.width/2, this.height/2, this.height/-2, this.nearPlane, this.farPlane)):(new THREE.PerspectiveCamera(this.fov, this.aspectRatio, this.nearPlane, this.farPlane))
this.camera.lookAt(new THREE.Vector3(0, 0, 0))
if (!process.browser) return
this.createRenderer(opts)
this.element = this.renderer.domElement
}
View.prototype.createRenderer = function(opts) {
opts = opts || {}
opts.antialias = opts.antialias || true
this.renderer = new THREE.WebGLRenderer(opts)
this.renderer.setSize(this.width, this.height)
this.renderer.setClearColorHex(this.skyColor, 1.0)
this.renderer.clear()
}
View.prototype.bindToScene = function(scene) {
scene.add(this.camera)
}
View.prototype.getCamera = function() {
return this.camera
}
View.prototype.cameraPosition = function() {
temporaryPosition.multiplyScalar(0)
temporaryPosition.applyMatrix4(this.camera.matrixWorld)
return [temporaryPosition.x, temporaryPosition.y, temporaryPosition.z]
}
View.prototype.cameraVector = function() {
temporaryVector.multiplyScalar(0)
temporaryVector.z = -1
temporaryVector.transformDirection( this.camera.matrixWorld )
return [temporaryVector.x, temporaryVector.y, temporaryVector.z]
}
View.prototype.resizeWindow = function(width, height) {
if (this.element.parentElement) {
width = width || this.element.parentElement.clientWidth
height = height || this.element.parentElement.clientHeight
}
this.camera.aspect = this.aspectRatio = width/height
this.width = width
this.height = height
this.camera.updateProjectionMatrix()
this.renderer.setSize( width, height )
}
View.prototype.render = function(scene) {
this.renderer.render(scene, this.camera)
}
View.prototype.appendTo = function(element) {
if (typeof element === 'object') {
element.appendChild(this.element)
}
else {
document.querySelector(element).appendChild(this.element)
}
this.resizeWindow(this.width,this.height)
}