forked from xml3d/xml3d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-observer.js
More file actions
176 lines (146 loc) · 5.84 KB
/
data-observer.js
File metadata and controls
176 lines (146 loc) · 5.84 KB
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
// box.js
(function($) {
// Is native?
if($) return;
var c_XflowObserverList = [];
var XML3DDataObserver = function(callback){
this.callback = callback;
this.observed = [];
}
window.XML3DDataObserver = XML3DDataObserver;
XML3DDataObserver.prototype.observe = function(node, options){
if(!node)
throw new Error("The node to observe is null.");
if(!node._configured)
throw new Error("Note to observe is not configured (yet). Make sure to pass an XML3D node and to execute " +
"this function after XML3D has been configured e.g. inside a DOMContentLoaded listener.");
var dataAdapter = XML3D.base.resourceManager.getAdapter(node, XML3D.data);
if(!dataAdapter)
throw new Error("Can't observe node. XML3DataObserver can only observe data containers such as <data>, <mesh> or <shader>");
if(this.observed.length == 0)
c_XflowObserverList.push(this);
var entry = {
node: node,
changed: false,
request: null
};
var names = options && options['names'];
var typeOfNames = Object.prototype.toString.call(names).slice(8, -1);
if (typeOfNames === "String") {
names = [names];
}
entry.request = dataAdapter.getComputeRequest(names, function(request, changeType){
entry.changed = true;
});
// Fetch result to synchronize Xflow structures and connect to callbacks
// TODO: Find an option to connect request to callback structure without computing result
entry.request.getResult();
this.observed.push(entry);
}
XML3DDataObserver.prototype.disconnect = function(){
for(var i = 0; i < this.observed.length; ++i){
this.observed[i].request.clear();
}
this.observed = [];
var i = c_XflowObserverList.length;
while(i--){
if(c_XflowObserverList[i] == this)
c_XflowObserverList.splice(i, 1);
}
}
XML3D.updateXflowObserver = function(){
for(var i = 0; i < c_XflowObserverList.length; ++i){
var observer = c_XflowObserverList[i];
var records = [];
for(var j = 0; j < observer.observed.length; ++j){
var entry = observer.observed[j];
if(entry.changed){
entry.changed = false;
var result = entry.request.getResult();
var dataResult = new XML3DDataResult(result);
records.push( new XML3DDataRecord(entry.node, dataResult));
}
}
if(records.length > 0 && observer.callback){
observer.callback(records, observer);
}
}
}
var XML3DDataRecord = function(target, result){
this.target = target;
this.result = result;
}
window.XML3DDataRecord = XML3DDataRecord;
var XML3DDataResult = function(result){
this._entries = {};
constructDataResult(this, result);
}
window.XML3DDataResult = XML3DDataResult;
XML3DDataResult.prototype.getValue = function(name) {
if (this._entries[name])
return this._entries[name].value;
return null;
}
XML3DDataResult.prototype.getType = function(name) {
if (this._entries[name])
return this._entries[name].type;
return null;
}
XML3DDataResult.prototype.getNames = function(){
var result = [];
for(var name in this._entries){
result.push(name);
}
return result;
}
XML3DDataResult.FLOAT = 0;
XML3DDataResult.FLOAT2 = 1;
XML3DDataResult.FLOAT3 = 2;
XML3DDataResult.FLOAT4 = 3;
XML3DDataResult.FLOAT4X4 = 4;
XML3DDataResult.INT = 10;
XML3DDataResult.INT4 = 11;
XML3DDataResult.BOOL = 20;
XML3DDataResult.TEXTURE = 30;
XML3DDataResult.BYTE = 40;
XML3DDataResult.UBYTE = 50;
function constructDataResult(dataResult, result){
for(var i = 0; i < result.outputNames.length; ++i){
var name = result.outputNames[i];
var entry = result.getOutputData(name);
var value = entry && entry.getValue();
if (value !== null) {
var type = getXML3DDataType(entry.type);
dataResult._entries[name] = { type: type, value: value};
}
}
}
function getXML3DDataType(type){
switch(type){
case Xflow.DATA_TYPE.FLOAT : return XML3DDataResult.FLOAT;
case Xflow.DATA_TYPE.FLOAT2 : return XML3DDataResult.FLOAT2;
case Xflow.DATA_TYPE.FLOAT3 : return XML3DDataResult.FLOAT3;
case Xflow.DATA_TYPE.FLOAT4 : return XML3DDataResult.FLOAT4;
case Xflow.DATA_TYPE.FLOAT4X4 : return XML3DDataResult.FLOAT4X4;
case Xflow.DATA_TYPE.INT : return XML3DDataResult.INT;
case Xflow.DATA_TYPE.INT4 : return XML3DDataResult.INT4;
case Xflow.DATA_TYPE.BOOL : return XML3DDataResult.BOOL;
case Xflow.DATA_TYPE.TEXTURE : return XML3DDataResult.TEXTURE;
case Xflow.DATA_TYPE.BYTE : return XML3DDataResult.BYTE;
case Xflow.DATA_TYPE.UBYTE : return XML3DDataResult.UBYTE;
default: throw new Error("WHAT IS THIS I DON'T EVEN...");
}
}
var XML3DDataChannelInfo = function(type, origin, originalName, seqLength, seqMinKey, seqMaxKey){
this.type = getXML3DDataType(type);
this.origin = origin;
this.originalName = originalName;
this.seqLength = seqLength;
this.seqMinKey = seqMinKey;
this.seqMaxKey = seqMaxKey;
}
window.XML3DDataChannelInfo = XML3DDataChannelInfo;
XML3DDataChannelInfo.ORIGIN_CHILD = 1;
XML3DDataChannelInfo.ORIGIN_COMPUTE = 2;
XML3DDataChannelInfo.ORIGIN_PROTO = 3;
}(XML3D._native));