/vendor/assets/javascripts/o3d-webgl/render_node.js

https://github.com/dkim-95112/movie_rails · JavaScript · 217 lines · 61 code · 28 blank · 128 comment · 6 complexity · 5fbc41186c305a17e7411794c513851f MD5 · raw file

  1. /*
  2. * Copyright 2010, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * RenderNode is the base of all RenderNodes in the render graph.
  33. * RenderNodes are rendered in order of priority.
  34. *
  35. * @param {number} opt_priority The priority of this render node. Lower
  36. * priorities are rendered first.
  37. * @param {boolean} opt_active If true this node is processed. If false
  38. * it is not.
  39. * @constructor
  40. */
  41. o3d.RenderNode = function(opt_priority, opt_active) {
  42. o3d.ParamObject.call(this);
  43. /**
  44. * Sets the priority of this render node. lower priorities are
  45. * rendered first.
  46. *
  47. * @type {number}
  48. */
  49. this.priority = opt_priority || 0;
  50. /**
  51. * The immediate children of this RenderNode.
  52. *
  53. * Each access to this field gets the entire list so it is best to get it
  54. * just once. For example:
  55. *
  56. * var children = renderNode.children;
  57. * for (var i = 0; i < children.length; i++) {
  58. * var child = children[i];
  59. * }
  60. *
  61. * Note that modifications to this array [e.g. push()] will not affect
  62. * the underlying RenderNode, while modifications to the array's members
  63. * will affect them.
  64. *
  65. * @type {!Array.<o3d.RenderNode>}
  66. */
  67. this.children = [];
  68. /**
  69. * Setting false skips this render node. Setting true processes this render
  70. * node. (ie, renders whatever it's supposed to render)
  71. *
  72. * @type {boolean}
  73. */
  74. this.active = opt_active || true;
  75. /**
  76. * Sets the parent of the node by re-parenting the node under parent_node.
  77. * Setting parent_node to null removes the node and the entire subtree below
  78. * it from the render graph.
  79. *
  80. * @type {o3d.RenderNode}
  81. */
  82. this.parent = null;
  83. };
  84. o3d.inherit('RenderNode','ParamObject');
  85. o3d.ParamObject.setUpO3DParam_(o3d.RenderNode, 'priority', 'ParamFloat');
  86. o3d.ParamObject.setUpO3DParam_(o3d.RenderNode, 'active', 'ParamBoolean');
  87. o3d.RenderNode.prototype.__defineSetter__('parent',
  88. function(p) {
  89. if (this.parent_) {
  90. this.parent_.removeChild(this);
  91. }
  92. this.parent_ = p;
  93. if (this.parent_) {
  94. if (!this.parent_.addChild) {
  95. throw ('Parent of render node must be render node or null.');
  96. }
  97. this.parent_.addChild(this);
  98. }
  99. }
  100. );
  101. o3d.RenderNode.prototype.__defineGetter__('parent',
  102. function(p) {
  103. return this.parent_;
  104. }
  105. );
  106. /**
  107. * Adds a child node.
  108. * @param {o3d.RenderNode} child The child to add.
  109. */
  110. o3d.RenderNode.prototype.addChild = function(child) {
  111. this.children.push(child);
  112. };
  113. /**
  114. * Removes a child node.
  115. * @param {o3d.RenderNode} child The child to add.
  116. */
  117. o3d.RenderNode.prototype.removeChild = function(child) {
  118. o3d.removeFromArray(this.children, child);
  119. };
  120. /**
  121. * Returns this render node and all its descendants. Note that this render node
  122. * might not be in the render graph.
  123. *
  124. * Note that modifications to this array [e.g. push()] will not affect
  125. * the underlying RenderNode, while modifications to the array's members
  126. * will affect them.
  127. *
  128. * An array containing all render nodes of the subtree.
  129. */
  130. o3d.RenderNode.prototype.getRenderNodesInTree =
  131. function() {
  132. o3d.notImplemented();
  133. };
  134. /**
  135. * Searches for render nodes that match the given name in the hierarchy under
  136. * and including this render node. Since there can be several render nodes
  137. * with a given name the results are returned in an array.
  138. *
  139. * Note that modifications to this array [e.g. push()] will not affect
  140. * the underlying RenderNode, while modifications to the array's members
  141. * will affect them.
  142. *
  143. * @param {string} name Rendernode name to look for.
  144. * @return {Array.<!o3d.RenderNode>} An array containing all nodes among
  145. * this node and its decendants that have the given name.
  146. */
  147. o3d.RenderNode.prototype.getRenderNodesByNameInTree =
  148. function(name) {
  149. o3d.notImplemented();
  150. };
  151. /**
  152. * Searches for render nodes that match the given class name in the hierarchy
  153. * under and including this render node.
  154. *
  155. * Note that modifications to this array [e.g. push()] will not affect
  156. * the underlying RenderNode, while modifications to the array's members
  157. * will affect them.
  158. *
  159. * @param {string} class_name class name to look for.
  160. * @return {Array.<!o3d.RenderNode>} An array containing all nodes among
  161. * this node and its decendants whose type is class_name.
  162. */
  163. o3d.RenderNode.prototype.getRenderNodesByClassNameInTree =
  164. function(class_name) {
  165. o3d.notImplemented();
  166. };
  167. /**
  168. * Recursively traverses the render graph starting at this node.
  169. */
  170. o3d.RenderNode.prototype.render = function() {
  171. function compare(a, b) {
  172. return a.priority - b.priority;
  173. }
  174. this.children.sort(compare);
  175. var children = this.children;
  176. this.before();
  177. for (var i = 0; i < children.length; ++i) {
  178. children[i].render();
  179. }
  180. this.after();
  181. };
  182. /**
  183. * Called during the rendergraph traversal before the children are rendered.
  184. */
  185. o3d.RenderNode.prototype.before = function() { };
  186. /**
  187. * Called during the rendergraph traversal after the children are rendered.
  188. */
  189. o3d.RenderNode.prototype.after = function() { };