This repository has been archived by the owner on May 15, 2019. It is now read-only.
forked from troffmo5/voxel-oculus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voxel-oculus.js
144 lines (120 loc) · 4.15 KB
/
voxel-oculus.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
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
module.exports = function (game, opts) {
var THREE = game.THREE;
var renderer = game.view.renderer;
this.separation = 10;
this.distortion = 1;
this.aspectFactor = 1;
this.enabled = false;
if (opts) {
if (opts.separation !== undefined) this.separation = opts.separation;
if (opts.distortion !== undefined) this.distortion = opts.distortion;
if (opts.aspectFactor !== undefined) this.aspectFactor = opts.aspectFactor;
}
var _width, _height;
var _pCamera = new THREE.PerspectiveCamera();
_pCamera.matrixAutoUpdate = false;
_pCamera.target = new THREE.Vector3();
var _scene = new THREE.Scene();
var _oCamera = new THREE.OrthographicCamera( -1, 1, 1, -1, 1, 1000 );
_oCamera.position.z = 1;
_scene.add( _oCamera );
// initialization
renderer.autoClear = true;
var _params = { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
var _renderTarget = new THREE.WebGLRenderTarget( 800, 600, _params );
var _material = new THREE.ShaderMaterial( {
uniforms: {
"tex": { type: "t", value: _renderTarget },
"c": { type: "f", value: this.distortion }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
" vUv = uv;",
" gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
// Formula used from the paper: "Applying and removing lens distortion in post production"
// by Gergely Vass , Tamás Perlaki
// http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.136.3745
fragmentShader: [
"uniform float c;",
"uniform sampler2D tex;",
"varying vec2 vUv;",
"void main()",
"{",
" vec2 uv = vUv;",
" vec2 vector = uv * 2.0 - 1.0;",
" float factor = 1.0/(1.0+c);",
" float vectorLen = length(vector);",
" vec2 direction = vector / vectorLen;",
" float newLen = vectorLen + c * pow(vectorLen,3.0);",
" vec2 newVector = direction * newLen * factor;",
" newVector = (newVector + 1.0) / 2.0;",
" if (newVector.x < 0.0 || newVector.x > 1.0 || newVector.y < 0.0 || newVector.y > 1.0)",
" gl_FragColor = vec4(0.0,0.0,0.0,1.0);",
" else",
" gl_FragColor = texture2D(tex, newVector);",
"}"
].join("\n")
} );
var mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), _material );
_scene.add( mesh );
this.setSize = function ( width, height ) {
_width = width / 2;
_height = height;
_renderTarget = new THREE.WebGLRenderTarget( width, height, _params );
_material.uniforms[ "tex" ].value = _renderTarget;
renderer.setSize( width, height );
};
this.setSize(game.width, game.height);
this.render = function ( scene, camera ) {
renderer.clear();
_material.uniforms['c'].value = this.distortion;
// camera parameters
//if (camera.matrixAutoUpdate) camera.updateMatrix();
camera.updateMatrixWorld();
_pCamera.fov = camera.fov;
_pCamera.aspect = camera.aspect / (2*this.aspectFactor);
_pCamera.near = camera.near;
_pCamera.far = camera.far;
_pCamera.updateProjectionMatrix();
// Render left
_pCamera.matrix.copy(camera.matrixWorld);
_pCamera.applyMatrix(new THREE.Matrix4().makeTranslation(-this.separation, 0, 0));
_pCamera.matrixWorldNeedsUpdate = true;
renderer.setViewport( 0, 0, _width, _height );
renderer.render( scene, _pCamera, _renderTarget, true );
renderer.render( _scene, _oCamera );
// Render right
_pCamera.matrix.copy(camera.matrixWorld);
_pCamera.applyMatrix(new THREE.Matrix4().makeTranslation(this.separation, 0, 0));
_pCamera.matrixWorldNeedsUpdate = true;
renderer.setViewport( _width, 0, _width, _height );
renderer.render( scene, _pCamera, _renderTarget, true );
renderer.render( _scene, _oCamera );
};
this.enable = function() {
this.originalRenderer = game.view.renderer;
this.setSize(game.width, game.height);
game.view.renderer = this;
renderer.autoClear = false;
this.enabled = true;
};
this.disable = function() {
if (!this.originalRenderer) {
return false;
}
game.view.renderer = this.originalRenderer;
renderer.autoClear = true;
this.setSize(game.width, game.height);
this.enabled = false;
};
this.toggle = function() {
if (!this.enabled) {
this.enable();
} else {
this.disable();
}
}
};