-
Notifications
You must be signed in to change notification settings - Fork 3
/
base.js
290 lines (260 loc) · 8.33 KB
/
base.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright 2010, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @fileoverview Base for all o3d classes implemented in javscript.
* Include just this file in a script tag and it will include all other
* source files needed by o3d.
* For more information about o3d see
* http://code.google.com/p/o3d.
*/
/**
* A namespace for all the o3d classes.
* @namespace
*/
var o3d = o3d || {};
/**
* Define this because the Google internal JSCompiler needs goog.typedef below.
*/
var goog = goog || {};
/**
* A macro for defining composite types.
*
* By assigning goog.typedef to a name, this tells Google internal JSCompiler
* that this is not the name of a class, but rather it's the name of a composite
* type.
*
* For example,
* /** @type {Array|NodeList} / goog.ArrayLike = goog.typedef;
* will tell JSCompiler to replace all appearances of goog.ArrayLike in type
* definitions with the union of Array and NodeList.
*
* Does nothing in uncompiled code.
*/
goog.typedef = true;
/**
* Reference to the global context. In most cases this will be 'window'.
*/
o3d.global = this;
/**
* Path for included scripts.
* @type {string}
*/
o3d.basePath = '';
/**
* Some javascripts don't support __defineGetter__ or __defineSetter__
* so we define some here so at least we don't get compile errors.
* We expect the initialzation code will check and complain. This stubs
* are just here to make sure we can actually get to the initialization code.
*/
if (!Object.prototype.__defineSetter__) {
Object.prototype.__defineSetter__ = function() {}
Object.prototype.__defineGetter__ = function() {}
}
/**
* Tries to detect the base path of the base.js script that
* bootstraps the o3d libraries.
* @private
*/
o3d.findBasePath_ = function() {
var doc = o3d.global.document;
if (typeof doc == 'undefined') {
return;
}
if (o3d.global.BASE_PATH) {
o3d.basePath = o3d.global.BASE_PATH;
return;
} else {
// HACK to hide compiler warnings :(
o3d.global.BASE_PATH = null;
}
var scripts = doc.getElementsByTagName('script');
for (var script, i = 0; script = scripts[i]; i++) {
var src = script.src;
var l = src.length;
var s = 'o3d-webgl/base.js';
var sl = s.length;
if (src.substr(l - sl) == s) {
o3d.basePath = src.substr(0, l - sl) + 'o3d-webgl/';
return;
}
}
};
/**
* Writes a script tag for the given o3d source file name
* to the document. (Must be called at execution time.)
* @param {string} src The full path to the source file.
* @private
*/
o3d.writeScriptTag_ = function(src) {
var doc = o3d.global.document;
if (typeof doc != 'undefined') {
doc.write('<script type="text/javascript" src="' +
src + '"></' + 'script>');
}
};
/**
* Filters any "o3d." prefix from the given type name.
* @param {string} type_name The type name to filter.
* @return {string} Filtered type name.
* @private
*/
o3d.filterTypeName_ = function(type_name) {
if (type_name.length >= 4 && type_name.substr(0, 4) == 'o3d.') {
type_name = type_name.substr(4);
}
return type_name;
};
/**
* Includes the file indicated by the rule by adding a script tag.
* @param {string} rule Rule to include, in the form o3d.package.part.
*/
o3d.include = function(rule) {
var parts = rule.split('.');
var path = parts[parts.length - 1] + '.js';
o3d.writeScriptTag_(o3d.basePath + path);
};
/**
* Makes one class inherit from another. Adds the member variables superClass
* and superClassName to the prototype of the sub class.
* @param {string} subClass Class that wants to inherit.
* @param {string} superClass Class to inherit from.
*/
o3d.inherit = function(subClassName, superClassName) {
var superClass = o3d.global.o3d[superClassName];
var subClass = o3d.global.o3d[subClassName];
if (!superClass)
throw ('Invalid superclass: ' + superClassName);
if (!subClass)
throw ('Invalid subclass: ' + subClassName);
subClass.prototype = new superClass;
subClass.prototype.superClassName = superClassName;
subClass.prototype.superClass = superClass;
subClass.prototype.className = subClassName;
};
/**
* Utility function to remove an object from an array.
* @param {!Array} array The array.
* @param {Object} object The thing to be removed.
*/
o3d.removeFromArray = function(array, object) {
var i = array.indexOf(object);
if (i >= 0) {
array.splice(i, 1);
}
};
/**
* Determine whether a value is an array. Do not use instanceof because that
* will not work for V8 arrays (the browser thinks they are Objects).
* @param {*} value A value.
* @return {boolean} Whether the value is an array.
*/
o3d.isArray_ = function(value) {
var valueAsObject = /** @type {!Object} **/ (value);
return typeof(value) === 'object' && value !== null &&
'length' in valueAsObject && 'splice' in valueAsObject;
};
/**
* Utility function to clone an object.
*
* @param {Object} object The object to clone.
* @return {Object} A clone of that object.
*/
o3d.clone = function(object) {
var result = o3d.isArray_(object) ? [] : {};
for (var name in object) {
var property = object[name];
if (typeof property == 'Object') {
result[name] = o3d.clone(property);
} else {
result[name] = property;
}
}
return result;
};
/**
* If an o3d function has not been implemented in javascript yet, it should
* call this function to throw an error because it's better than doing
* nothing.
*/
o3d.notImplemented = function() {
debugger;
throw 'Not implemented.';
};
// First find the path to the directory where all o3d-webgl sources live.
o3d.findBasePath_();
// Unlike o3djs, we include all o3d-webgl files at once, this way, an o3d
// developer converting to use these classes only has to include this
// javascript file.
o3d.include('object_base');
o3d.include('named_object_base');
o3d.include('named_object');
o3d.include('param_object');
o3d.include('param_array');
o3d.include('transform');
o3d.include('param');
o3d.include('event');
o3d.include('raw_data');
o3d.include('texture');
o3d.include('bitmap');
o3d.include('file_request');
o3d.include('client');
o3d.include('render_node');
o3d.include('clear_buffer');
o3d.include('state_set');
o3d.include('viewport');
o3d.include('tree_traversal');
o3d.include('draw_list');
o3d.include('draw_pass');
o3d.include('render_surface_set');
o3d.include('render_surface');
o3d.include('state');
o3d.include('draw_context');
o3d.include('ray_intersection_info');
o3d.include('sampler');
o3d.include('pack');
o3d.include('bounding_box');
o3d.include('draw_element');
o3d.include('element');
o3d.include('field');
o3d.include('buffer');
o3d.include('stream');
o3d.include('vertex_source');
o3d.include('stream_bank');
o3d.include('primitive');
o3d.include('shape');
o3d.include('effect');
o3d.include('material');
o3d.include('archive_request');
o3d.include('param_operation');
o3d.include('function');
o3d.include('counter');
o3d.include('curve');
o3d.include('skin');