forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
arscene.js
101 lines (87 loc) · 3.03 KB
/
arscene.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
"use strict";
define(['picker','ardetector','arview' ], function(picker,ardetector,arview ) {
function View() {
return {
pickables: new arview.Pickables(),
objects: new arview.Objects(),
scene: new arview.Scene(),
};
}
function Scene( element, imageSource ) {
var detector = ardetector.create( imageSource.scratchcanvas );
var renderer = new arview.Renderer( imageSource.dimensions, imageSource.scratchcanvas );
renderer.setCameraMatrix( detector.getCameraMatrix( 5, 10000 ) );
element.appendChild( renderer.glCanvas );
var objectPicker = new picker.Picker( renderer.getCamera(), renderer.glCanvas );
var view;
function setView( newView ) {
view = newView;
objectPicker.setList( view.pickables.registered );
renderer.setScene( view.scene );
detector.reset();
update();
}
function update() {
detector.detect(
onMarkerCreated,
onMarkerUpdated,
onMarkerDestroyed
);
renderer.update();
}
function render() {
renderer.render();
}
function add( object ) {
view.pickables.register( object.pickables );
view.scene.add( object );
}
function remove( object ) {
view.pickables.unregister( object.pickables );
view.scene.remove( object );
}
// This function is called when a marker is initally detected on the stream
function onMarkerCreated( marker ) {
var objects = view.objects.get( marker.id );
if( objects ) {
objects.forEach( function(object){
if( !object.transform ) {
console.log("DEVELOPMENT: found an object attached to an AR marker but without transform() method.");
console.log( object );
}
else {
object.transform( marker.matrix );
add( object );
}
});
}
}
// This function is called when an existing marker is repositioned
function onMarkerUpdated(marker) {
var objects = view.objects.get( marker.id );
if( objects ) {
objects.forEach( function( object ) {
object.transform( marker.matrix );
});
}
}
// This function is called when a marker disappears from the stream.
function onMarkerDestroyed(marker) {
var objects = view.objects.get( marker.id );
if( objects ) {
objects.forEach( remove );
}
}
return {
add: add,
remove: remove,
update: update,
render: render,
setView: setView,
};
}
return {
Scene:Scene,
View:View,
};
});