-
Notifications
You must be signed in to change notification settings - Fork 3
/
rendergraph.js
580 lines (518 loc) · 20.2 KB
/
rendergraph.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* Copyright 2009, 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 This file contains various functions for helping create
* render graphs for o3d. It puts them in the "rendergraph" module on
* the o3djs object.
*
* Note: This library is only a sample. It is not meant to be some official
* library. It is provided only as example code.
*
*/
o3djs.provide('o3djs.rendergraph');
/**
* A Module for creating render graphs.
* @namespace
*/
o3djs.rendergraph = o3djs.rendergraph || {};
/**
* Creates a basic render graph setup to draw opaque and transparent
* 3d objects.
* @param {!o3d.Pack} pack Pack to manage created objects.
* @param {!o3d.Transform} treeRoot root Transform of tree to render.
* @param {!o3d.RenderNode} opt_parent RenderNode to build this view under.
* @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
* @param {number} opt_priority Optional base priority for created objects.
* @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
* @param {!o3d.DrawList} opt_performanceDrawList Optional DrawList to
* use for performanceDrawPass.
* @param {!o3d.DrawList} opt_zOrderedDrawList Optional DrawList to
* use for zOrderedDrawPass.
* @param {!o3d.DrawContext} opt_drawContext Optional DrawContext to
* use. If not passed in one is created.
* @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about
* everything created.
*/
o3djs.rendergraph.createView = function(pack,
treeRoot,
opt_parent,
opt_clearColor,
opt_priority,
opt_viewport,
opt_performanceDrawList,
opt_zOrderedDrawList,
opt_drawContext) {
return new o3djs.rendergraph.ViewInfo(pack,
treeRoot,
opt_parent,
opt_clearColor,
opt_priority,
opt_viewport,
opt_performanceDrawList,
opt_zOrderedDrawList,
opt_drawContext);
};
/**
* Creates a basic render graph setup to draw opaque and transparent
* 3d objects.
* @param {!o3d.Pack} pack Pack to manage created objects.
* @param {!o3d.Transform} treeRoot root Transform of tree to render.
* @param {!o3d.RenderNode} opt_parent RenderNode to build this view under.
* @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
* @param {number} opt_priority Optional base priority for created objects.
* @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
* @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about
* everything created.
*/
o3djs.rendergraph.createBasicView = function(pack,
treeRoot,
opt_parent,
opt_clearColor,
opt_priority,
opt_viewport) {
return o3djs.rendergraph.createView(pack,
treeRoot,
opt_parent,
opt_clearColor,
opt_priority,
opt_viewport);
};
/**
* Creates an extra view render graph setup to draw opaque and transparent
* 3d objects based on a previously created view. It uses the previous view
* to share draw lists and to set the priority.
* @param {!o3djs.rendergraph.ViewInfo} viewInfo ViewInfo returned from
* createBasicView.
* @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
* @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
* @param {number} opt_priority base priority for created objects.
* @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about
* everything created.
*/
o3djs.rendergraph.createExtraView = function(viewInfo,
opt_viewport,
opt_clearColor,
opt_priority) {
return o3djs.rendergraph.createView(viewInfo.pack,
viewInfo.treeRoot,
viewInfo.renderGraphRoot,
opt_clearColor,
opt_priority,
opt_viewport,
viewInfo.performanceDrawList,
viewInfo.zOrderedDrawList);
};
/**
* A ViewInfo object creates the standard o3d objects needed for
* a single 3d view. Those include a ClearBuffer followed by a TreeTraveral
* followed by 2 DrawPasses all of which are children of a Viewport. On top of
* those a DrawContext and optionally 2 DrawLists although you can pass in your
* own DrawLists if there is a reason to reuse the same DrawLists such was with
* mulitple views of the same scene.
*
* The render graph created is something like:
* <pre>
* [Viewport]
* |
* +------+--------+------------------+---------------------+
* | | | |
* [ClearBuffer] [TreeTraversal] [Performance StateSet] [ZOrdered StateSet]
* | |
* [Performance DrawPass] [ZOrdered DrawPass]
* </pre>
*
* @constructor
* @param {!o3d.Pack} pack Pack to manage created objects.
* @param {!o3d.Transform} treeRoot root Transform of tree to render.
* @param {!o3d.RenderNode} opt_parent RenderNode to build this view under.
* @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
* @param {number} opt_priority Optional base priority for created objects.
* @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
* @param {!o3d.DrawList} opt_performanceDrawList DrawList to use for
* performanceDrawPass.
* @param {!o3d.DrawList} opt_zOrderedDrawList DrawList to use for
* zOrderedDrawPass.
* @param {!o3d.DrawContext} opt_drawContext Optional DrawContext to
* use. If not passed in one is created.
*/
o3djs.rendergraph.ViewInfo = function(pack,
treeRoot,
opt_parent,
opt_clearColor,
opt_priority,
opt_viewport,
opt_performanceDrawList,
opt_zOrderedDrawList,
opt_drawContext) {
var that = this;
var clearColor = opt_clearColor || [0.5, 0.5, 0.5, 1.0];
var viewPriority = opt_priority || 0;
var priority = 0;
// Create Viewport.
var viewport = pack.createObject('Viewport');
if (opt_viewport) {
viewport.viewport = opt_viewport;
}
viewport.priority = viewPriority;
// Create a clear buffer.
var clearBuffer = pack.createObject('ClearBuffer');
clearBuffer.clearColor = clearColor;
clearBuffer.priority = priority++;
clearBuffer.parent = viewport;
// Creates a TreeTraversal and parents it to the root.
var treeTraversal = pack.createObject('TreeTraversal');
treeTraversal.priority = priority++;
treeTraversal.parent = viewport;
treeTraversal.transform = treeRoot;
this.drawPassInfos_ = [];
/**
* Pack that manages the objects created for this ViewInfo.
* @type {!o3d.Pack}
*/
this.pack = pack;
/**
* The RenderNode this ViewInfo render graph subtree is parented under.
* @type {(!o3d.RenderNode|undefined)}
*/
this.renderGraphRoot = opt_parent;
/**
* The root node of the transform graph this ViewInfo renders.
* @type {!o3d.Transform}
*/
this.treeRoot = treeRoot;
/**
* The root of the subtree of the render graph this ViewInfo is managing.
* If you want to set the priority of a ViewInfo's rendergraph subtree use
* <pre>
* viewInfo.root.priority = desiredPriority;
* </pre>
* @type {!o3d.RenderNode}
*/
this.root = viewport;
/**
* The Viewport RenderNode created for this ViewInfo.
* @type {!o3d.Viewport}
*/
this.viewport = viewport;
/**
* The ClearBuffer RenderNode created for this ViewInfo.
* @type {!o3d.ClearBuffer}
*/
this.clearBuffer = clearBuffer;
// Create DrawContext.
var drawContext = opt_drawContext || pack.createObject('DrawContext');
/**
* The DrawContext used by this ViewInfo.
* @type {!o3d.DrawContext}
*/
this.drawContext = drawContext;
/**
* The TreeTraversal used by this ViewInfo.
* @type {!o3d.TreeTraversal}
*/
this.treeTraversal = treeTraversal;
/**
* The highest priority used for objects under the Viewport RenderNode created
* by this ViewInfo.
* @type {number}
*/
this.priority = priority;
/**
* This function is here just because the inside use case of
* ViewInfo.createDrawPass is the less common case.
* @param {o3d.DrawList.SortMethod} sortMethod how to sort.
* @param {!o3d.DrawList} opt_drawList DrawList to use.
*/
function createDrawPass(sortMethod, opt_drawList) {
return that.createDrawPass(
sortMethod,
undefined,
undefined,
undefined,
opt_drawList);
}
// Setup a Performance Ordered DrawPass
var performanceDrawPassInfo = createDrawPass(
o3djs.base.o3d.DrawList.BY_PERFORMANCE,
opt_performanceDrawList);
var performanceState = performanceDrawPassInfo.state;
// Setup a z Ordered DrawPass
var zOrderedDrawPassInfo = createDrawPass(
o3djs.base.o3d.DrawList.BY_Z_ORDER,
opt_zOrderedDrawList);
var zOrderedState = zOrderedDrawPassInfo.state;
zOrderedState.getStateParam('AlphaBlendEnable').value = true;
zOrderedState.getStateParam('SourceBlendFunction').value =
o3djs.base.o3d.State.BLENDFUNC_SOURCE_ALPHA;
zOrderedState.getStateParam('DestinationBlendFunction').value =
o3djs.base.o3d.State.BLENDFUNC_INVERSE_SOURCE_ALPHA;
zOrderedState.getStateParam('AlphaTestEnable').value = true;
zOrderedState.getStateParam('AlphaComparisonFunction').value =
o3djs.base.o3d.State.CMP_GREATER;
// Parent whatever the root is to the parent passed in.
if (opt_parent) {
this.root.parent = opt_parent;
}
/**
* The DrawPassInfo for the performance draw pass.
* @type {!o3djs.rendergraph.DrawPassInfo}
*/
this.performanceDrawPassInfo = performanceDrawPassInfo;
/**
* The DrawPassInfo for the zOrdered draw pass.
* @type {!o3djs.rendergraph.DrawPassInfo}
*/
this.zOrderedDrawPassInfo = zOrderedDrawPassInfo;
// Legacy properties
/**
* The StateSet RenderNode above the performance DrawPass in this ViewInfo
* @type {!o3d.StateSet}
*/
this.performanceStateSet = performanceDrawPassInfo.stateSet;
/**
* The State object used by the performanceStateSet object in this ViewInfo.
* By default, no states are set here.
* @type {!o3d.State}
*/
this.performanceState = performanceState;
/**
* The DrawList used for the performance draw pass. Generally for opaque
* materials.
* @type {!o3d.DrawList}
*/
this.performanceDrawList = performanceDrawPassInfo.drawList;
/**
* The StateSet RenderNode above the ZOrdered DrawPass in this ViewInfo
* @type {!o3d.StateSet}
*/
this.zOrderedStateSet = zOrderedDrawPassInfo.stateSet;
/**
* The State object used by the zOrderedStateSet object in this ViewInfo.
* By default AlphaBlendEnable is set to true, SourceBlendFucntion is set to
* State.BLENDFUNC_SOURCE_ALPHA and DestinationBlendFunction is set to
* State.BLENDFUNC_INVERSE_SOURCE_ALPHA
* @type {!o3d.State}
*/
this.zOrderedState = zOrderedState;
/**
* The DrawList used for the zOrdered draw pass. Generally for transparent
* materials.
* @type {!o3d.DrawList}
*/
this.zOrderedDrawList = zOrderedDrawPassInfo.drawList;
/**
* The DrawPass used with the performance DrawList created by this ViewInfo.
* @type {!o3d.DrawPass}
*/
this.performanceDrawPass = performanceDrawPassInfo.drawPass;
/**
* The DrawPass used with the zOrdered DrawList created by this ViewInfo.
* @type {!o3d.DrawPass}
*/
this.zOrderedDrawPass = zOrderedDrawPassInfo.drawPass;
/**
* A flag whether or not we created the DrawContext for this DrawPassInfo.
* @private
* @type {boolean}
*/
this.ownDrawContext_ = opt_drawContext ? false : true;
};
/**
* Destroys the various objects created for the view.
*
* @param {boolean} opt_destroyDrawContext True if you want view's DrawContext
* destroyed. Default = true.
* @param {boolean} opt_destroyDrawList True if you want view's DrawLists
* destroyed. Default = true.
*/
o3djs.rendergraph.ViewInfo.prototype.destroy = function(
opt_destroyDrawContext,
opt_destroyDrawList) {
if (opt_destroyDrawContext === undefined) {
opt_destroyDrawContext = true;
}
for (var ii = 0; ii < this.drawPassInfos_.length; ++ii) {
this.drawPassInfos_[ii].destroy();
}
// Remove everything we created from the pack.
this.pack.removeObject(this.viewport);
this.pack.removeObject(this.clearBuffer);
if (opt_destroyDrawContext && this.ownDrawContext_) {
this.pack.removeObject(this.drawContext);
}
this.pack.removeObject(this.treeTraversal);
// Remove our substree from its parent.
this.viewport.parent = null;
// At this point, IF nothing else is referencing any of these objects
// they should get removed.
};
/**
* Creates a draw pass in this ViewInfo.
*
* @param {o3d.DrawList.SortMethod} sortMethod How to sort this draw pass's
* DrawElements.
* @param {!o3d.DrawContext} opt_drawContext The DrawContext for this draw pass.
* If not passed in the default DrawContext for this ViewInfo will be used.
* @param {number} opt_priority The priority for this draw pass. If not passed
* in the priority will be the next priority for this ViewInfo.
* @param {!o3d.RenderNode} opt_parent The RenderNode to parent this draw pass
* under. If not passed in the draw pass will be parented under the
* ViewInfo's viewport RenderNode.
* @param {!o3d.DrawList} opt_drawList The DrawList for this draw pass. If not
* passed in one will be created.
* @return {!o3djs.rendergraph.DrawPassInfo}
*/
o3djs.rendergraph.ViewInfo.prototype.createDrawPass = function(
sortMethod,
opt_drawContext,
opt_priority,
opt_parent,
opt_drawList) {
opt_drawContext = opt_drawContext || this.drawContext;
opt_parent = opt_parent || this.viewport;
opt_priority = (typeof opt_priority !== 'undefined') ? opt_priority :
this.priority++;
var drawPassInfo = o3djs.rendergraph.createDrawPassInfo(
this.pack,
opt_drawContext,
sortMethod,
opt_parent,
opt_drawList);
drawPassInfo.root.priority = opt_priority;
this.treeTraversal.registerDrawList(
drawPassInfo.drawList, opt_drawContext, true);
this.drawPassInfos_.push(drawPassInfo);
return drawPassInfo;
};
/**
* Creates a DrawPassInfo to manage a draw pass.
*
* @param {!o3d.Pack} pack Pack to manage created objects.
* @param {!o3d.DrawContext} drawContext The DrawContext for this draw pass.
* @param {o3d.DrawList.SortMethod} sortMethod How to sort this draw pass's
* DrawElements.
* @param {!o3d.DrawList} opt_drawList The DrawList for this draw pass. If not
* passed in one will be created.
* @param {!o3d.RenderNode} opt_parent The RenderNode to parent this draw pass
* under. If not passed the draw pass will not be parented.
* @return {!o3djs.rendergraph.DrawPassInfo}
*/
o3djs.rendergraph.createDrawPassInfo = function(
pack,
drawContext,
sortMethod,
opt_parent,
opt_drawList) {
return new o3djs.rendergraph.DrawPassInfo(
pack, drawContext, sortMethod, opt_parent, opt_drawList);
};
/**
* A class to manage a draw pass.
* @constructor
* @param {!o3d.Pack} pack Pack to manage created objects.
* @param {!o3d.DrawContext} drawContext The DrawContext for this draw pass.
* @param {o3d.DrawList.SortMethod} sortMethod How to sort this draw pass's
* DrawElements.
* @param {!o3d.DrawList} opt_drawList The DrawList for this draw pass. If not
* passed in one will be created.
* @param {!o3d.RenderNode} opt_parent The RenderNode to parent this draw pass
* under. If not passed the draw pass will not be parented.
* @return {!o3djs.rendergraph.DrawPassInfo}
*/
o3djs.rendergraph.DrawPassInfo = function(pack,
drawContext,
sortMethod,
opt_parent,
opt_drawList) {
var ownDrawList = opt_drawList ? false : true;
opt_parent = opt_parent || null;
opt_drawList = opt_drawList || pack.createObject('DrawList');
var stateSet = pack.createObject('StateSet');
var state = pack.createObject('State');
stateSet.state = state;
stateSet.parent = opt_parent;
var drawPass = pack.createObject('DrawPass');
drawPass.drawList = opt_drawList;
drawPass.sortMethod = sortMethod;
drawPass.parent = stateSet;
/**
* The pack managing the objects created for this DrawPassInfo.
* @type {!o3d.Pack}
*/
this.pack = pack;
/**
* The State that affects all things drawn in this DrawPassInfo.
* @type {!o3d.State}
*/
this.state = state;
/**
* The StateSet that applies the state for this DrawPassInfo.
* @type {!o3d.StateSet}
*/
this.stateSet = stateSet;
/**
* The DrawPass for this DrawPassInfo.
* @type {!o3d.DrawPass}
*/
this.drawPass = drawPass;
/**
* The DrawList for this DrawPassInfo.
* @type {!o3d.DrawList}
*/
this.drawList = opt_drawList;
/**
* The root RenderNode of this DrawPassInfo. This is the RenderNdoe you should
* use if you want to turn this draw pass off or reparent it.
* @type {!o3d.RenderNode}
*/
this.root = stateSet;
/**
* A flag whether or not we created the DrawList for this DrawPassInfo.
* @private
* @type {boolean}
*/
this.ownDrawList_ = ownDrawList;
};
/**
* Frees the resources created for this DrawPassInfo.
*/
o3djs.rendergraph.DrawPassInfo.prototype.destroy = function() {
// Remove everything we created from the pack.
if (this.ownDrawList_) {
this.drawPass.drawList = null;
this.pack.removeObject(this.drawList);
}
this.drawPass.parent = null;
this.stateSet.parent = null;
this.pack.removeObject(this.drawPass);
this.pack.removeObject(this.stateSet);
this.pack.removeObject(this.state);
};