forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
arview.js
209 lines (172 loc) · 5.58 KB
/
arview.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"use strict";
define(["three.min"], function() {
function Pickables() {
var registered = [];
function register( list ) {
list.forEach( function(mesh) {
registered.push( mesh );
});
}
function unregister( list ) {
list.forEach( function(mesh) {
var index = registered.indexOf( mesh );
if( index >= 0) {
registered.splice(index,1);
}
});
}
return {
registered:registered,
register:register,
unregister:unregister
};
}
function Objects() {
// Create marker objects associated with the desired marker ID.
var markerObjects = {};
function add( arId, object ) {
var objects = markerObjects[arId];
if( !objects ) {
objects = [];
markerObjects[arId] = objects;
}
if( objects ) {
var index = objects.indexOf( object );
if( index < 0) {
objects.push( object );
}
}
}
function remove( arId, object ) {
var objects = markerObjects[arId];
if( objects ) {
var index = objects.indexOf( object );
if( index >= 0 ) {
objects.splice(index,1);
}
}
}
function get( arId ) {
return markerObjects[arId];
}
return {
get:get,
add:add,
remove:remove,
};
}
var Reality = function(sourceCanvas){
// Create a default camera and scene.
var camera = new THREE.Camera();
var scene = new THREE.Scene();
// Create a plane geometry to hold the sourceCanvas texture
var geometry = new THREE.PlaneGeometry(2, 2, 0);
// Create a material textured with the contents of sourceCanvas.
var texture = new THREE.Texture(sourceCanvas.getElement());
var material = new THREE.MeshBasicMaterial({
map: texture,
depthTest: false,
depthWrite: false
});
// Build a mesh and add it to the scene.
var mesh = new THREE.Mesh( geometry, material );
scene.add(mesh);
// We need to notify ThreeJS when the texture has changed.
function update() {
texture.needsUpdate = true;
}
return {
camera: camera,
scene: scene,
update: update,
};
};
var Scene = function() {
// Create an augmented scene
var scene = new THREE.Scene();
// Create an occluder scene
var occluder = new THREE.Scene();
var light = new THREE.DirectionalLight( 0xFFFFFF );
light.position.x = 0;
light.position.y = 0;
light.position.z = 9000;
light.lookAt( scene.position );
scene.add( light );
function add( object ) {
if( object.model ) {
scene.add( object.model );
}
if( object.occluder ) {
occluder.add( object.occluder );
}
}
function remove( object ) {
if( object.model ) {
scene.remove( object.model );
}
if( object.occluder ) {
occluder.remove( object.occluder );
}
}
return {
scene:scene,
occluder:occluder,
add:add,
remove:remove,
};
};
var Renderer = function(dimensions, sourceCanvas) {
// Create a canvas which will be used for WebGL
var glCanvas = document.createElement('canvas');
// Initialize the renderer and attach it to the canvas
var renderer = new THREE.WebGLRenderer({
canvas:glCanvas,
antialias:true
});
renderer.setSize(dimensions.width, dimensions.height);
renderer.autoClear = false;
// Create a reality scene
var reality = new Reality(sourceCanvas);
var virtual;
function setScene( scene ) {
virtual = scene;
}
function render() {
// Render the reality scene
renderer.render(reality.scene, reality.camera);
// Deactivate color buffer renderering, leaving only depth buffer active.
renderer.context.colorMask(false,false,false,false);
// Render the occluder scene
renderer.render( virtual.occluder, virtualCamera);
// Reactivate color buffer rendering
renderer.context.colorMask(true,true,true,true);
// Render the augmented components on top of the reality scene.
renderer.render( virtual.scene, virtualCamera );
}
function update() {
// Notify the reality scene to update it's texture
reality.update();
}
var virtualCamera = new THREE.Camera();
function setCameraMatrix( matrix ) {
virtualCamera.projectionMatrix.fromArray( matrix );
}
function getCamera() {
return virtualCamera;
}
return {
setScene: setScene,
update: update,
render: render,
glCanvas: glCanvas,
setCameraMatrix: setCameraMatrix,
getCamera: getCamera,
};
};
return {
Renderer: Renderer,
Scene: Scene,
Objects: Objects,
Pickables: Pickables,
};
});