-
Notifications
You must be signed in to change notification settings - Fork 1
/
arview.js
221 lines (176 loc) · 6.32 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
210
211
212
213
214
215
216
217
218
219
220
221
"use strict";
define(["lib/three.min"], function() {
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);
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() {
var scene = new THREE.Scene();
var camera = new THREE.Camera();
function add(object) {
scene.add(object);
}
function remove(object) {
scene.remove(object);
}
function setProjectionMatrix(matrix) {
camera.projectionMatrix.setFromArray( matrix );
}
return {
scene:scene,
camera:camera,
add:add,
remove:remove,
setProjectionMatrix:setProjectionMatrix,
}
}
var Reticle = function(scene) {
// Maintain the state of tracked objects.
var targets = {};
// Add a mesh target to be tracked
function add(mesh, callback) {
targets[mesh.id] = {
mesh:mesh,
isSelected:false,
callback:callback,
};
}
// Remove a tracked mesh target
function remove(mesh) {
var target = targets[mesh.id];
// If the target is selected, notify that it's now deselected.
if( target.isSelected ) {
target.callback(false);
}
delete targets[mesh.id];
}
// Check a specific target for a ray intersection
function checkTarget(target) {
// Center the reticule in the middle of the screen
var reticule = new THREE.Vector3(0,0,0);
var projector = new THREE.Projector();
projector.unprojectVector( reticule, scene.camera );
var raycaster = new THREE.Raycaster(
scene.camera.position,
reticule.sub( scene.camera.position ).normalize()
);
var hits = raycaster.intersectObject( target.mesh );
return(hits.length > 0);
}
// Check all the targets, and notify of changes
function update() {
for( var id in targets ) {
var target = targets[id];
var state = checkTarget(target);
// Invoke the callback only if the state has changed
if( target.isSelected !== state ) {
target.callback(state);
}
// Update the target selection state
target.isSelected = state;
}
}
return {
add:add,
remove:remove,
update:update,
}
}
var create = 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});
renderer.setSize(dimensions.width, dimensions.height);
renderer.autoClear = false;
// Create a reality scene
var reality = new Reality(sourceCanvas);
// Create an augmented scene
var virtual = new Scene();
// Create an occluder scene
var occluder = new Scene();
var light = new THREE.SpotLight(0xffffff);
light.position.set(0, 0, 9000);
light.lookAt( new THREE.Vector3(0,0,0) );
virtual.scene.add(light);
// Create a Reticle attached to the virtual scene.
var reticle = new Reticle(virtual);
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( occluder.scene, occluder.camera);
// 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, virtual.camera);
}
function update() {
// Notify the reality scene to update it's texture
reality.update();
// Check for any reticle target changes
reticle.update();
}
function setCameraMatrix( matrix ) {
virtual.setProjectionMatrix( matrix );
occluder.setProjectionMatrix( matrix );
}
function add( object, selectionCallback ) {
if( object.model ) {
virtual.add( object.model );
}
if( object.occluder ) {
occluder.add( object.occluder );
}
if( object.hitbox ) {
reticle.add( object.hitbox, selectionCallback);
}
}
function remove( object ) {
if( object.model ) {
virtual.remove( object.model );
}
if( object.occluder ) {
occluder.remove( object.occluder );
}
if( object.hitbox ) {
reticle.remove( object.hitbox );
}
}
return {
add: add,
remove: remove,
update: update,
render: render,
glCanvas: glCanvas,
setCameraMatrix: setCameraMatrix,
}
}
return {
create: create,
}
});