PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ruhlamat_website/o3djs/rendergraph.js

http://demoasp.googlecode.com/
JavaScript | 420 lines | 161 code | 41 blank | 218 comment | 16 complexity | 7a888603e8559e85af2b16a966863abd MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. /*
  2. * Copyright 2009, 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. * @fileoverview This file contains various functions for helping create
  33. * render graphs for o3d. It puts them in the "rendergraph" module on
  34. * the o3djs object.
  35. *
  36. * Note: This library is only a sample. It is not meant to be some official
  37. * library. It is provided only as example code.
  38. *
  39. */
  40. o3djs.provide('o3djs.rendergraph');
  41. /**
  42. * A Module for creating render graphs.
  43. * @namespace
  44. */
  45. o3djs.rendergraph = o3djs.rendergraph || {};
  46. /**
  47. * Creates a basic render graph setup to draw opaque and transparent
  48. * 3d objects.
  49. * @param {!o3d.Pack} pack Pack to manage created objects.
  50. * @param {!o3d.Transform} treeRoot root Transform of tree to render.
  51. * @param {!o3d.RenderNode} opt_parent RenderNode to build this view under.
  52. * @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
  53. * @param {number} opt_priority Optional base priority for created objects.
  54. * @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
  55. * @param {!o3d.DrawList} opt_performanceDrawList Optional DrawList to
  56. * use for performanceDrawPass.
  57. * @param {!o3d.DrawList} opt_zOrderedDrawList Optional DrawList to
  58. * use for zOrderedDrawPass.
  59. * @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about
  60. * everything created.
  61. */
  62. o3djs.rendergraph.createView = function(pack,
  63. treeRoot,
  64. opt_parent,
  65. opt_clearColor,
  66. opt_priority,
  67. opt_viewport,
  68. opt_performanceDrawList,
  69. opt_zOrderedDrawList) {
  70. return new o3djs.rendergraph.ViewInfo(pack,
  71. treeRoot,
  72. opt_parent,
  73. opt_clearColor,
  74. opt_priority,
  75. opt_viewport,
  76. opt_performanceDrawList,
  77. opt_zOrderedDrawList);
  78. };
  79. /**
  80. * Creates a basic render graph setup to draw opaque and transparent
  81. * 3d objects.
  82. * @param {!o3d.Pack} pack Pack to manage created objects.
  83. * @param {!o3d.Transform} treeRoot root Transform of tree to render.
  84. * @param {!o3d.RenderNode} opt_parent RenderNode to build this view under.
  85. * @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
  86. * @param {number} opt_priority Optional base priority for created objects.
  87. * @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
  88. * @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about
  89. * everything created.
  90. */
  91. o3djs.rendergraph.createBasicView = function(pack,
  92. treeRoot,
  93. opt_parent,
  94. opt_clearColor,
  95. opt_priority,
  96. opt_viewport) {
  97. return o3djs.rendergraph.createView(pack,
  98. treeRoot,
  99. opt_parent,
  100. opt_clearColor,
  101. opt_priority,
  102. opt_viewport);
  103. };
  104. /**
  105. * Creates an extra view render graph setup to draw opaque and transparent
  106. * 3d objects based on a previously created view. It uses the previous view
  107. * to share draw lists and to set the priority.
  108. * @param {!o3djs.rendergraph.ViewInfo} viewInfo ViewInfo returned from
  109. * createBasicView.
  110. * @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
  111. * @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
  112. * @param {number} opt_priority base priority for created objects.
  113. * @return {!o3djs.rendergraph.ViewInfo} A ViewInfo object with info about
  114. * everything created.
  115. */
  116. o3djs.rendergraph.createExtraView = function(viewInfo,
  117. opt_viewport,
  118. opt_clearColor,
  119. opt_priority) {
  120. return o3djs.rendergraph.createView(viewInfo.pack,
  121. viewInfo.treeRoot,
  122. viewInfo.renderGraphRoot,
  123. opt_clearColor,
  124. opt_priority,
  125. opt_viewport,
  126. viewInfo.performanceDrawList,
  127. viewInfo.zOrderedDrawList);
  128. };
  129. /**
  130. * A ViewInfo object creates the standard o3d objects needed for
  131. * a single 3d view. Those include a ClearBuffer followed by a TreeTraveral
  132. * followed by 2 DrawPasses all of which are children of a Viewport. On top of
  133. * those a DrawContext and optionally 2 DrawLists although you can pass in your
  134. * own DrawLists if there is a reason to reuse the same DrawLists such was with
  135. * mulitple views of the same scene.
  136. *
  137. * The render graph created is something like:
  138. * <pre>
  139. * [Viewport]
  140. * |
  141. * +------+--------+------------------+---------------------+
  142. * | | | |
  143. * [ClearBuffer] [TreeTraversal] [Performance StateSet] [ZOrdered StateSet]
  144. * | |
  145. * [Performance DrawPass] [ZOrdered DrawPass]
  146. * </pre>
  147. *
  148. * @constructor
  149. * @param {!o3d.Pack} pack Pack to manage created objects.
  150. * @param {!o3d.Transform} treeRoot root Transform of tree to render.
  151. * @param {!o3d.RenderNode} opt_parent RenderNode to build this view under.
  152. * @param {!o3djs.math.Vector4} opt_clearColor color to clear view.
  153. * @param {number} opt_priority Optional base priority for created objects.
  154. * @param {!o3djs.math.Vector4} opt_viewport viewport settings for view.
  155. * @param {!o3d.DrawList} opt_performanceDrawList DrawList to use for
  156. * performanceDrawPass.
  157. * @param {!o3d.DrawList} opt_zOrderedDrawList DrawList to use for
  158. * zOrderedDrawPass.
  159. */
  160. o3djs.rendergraph.ViewInfo = function(pack,
  161. treeRoot,
  162. opt_parent,
  163. opt_clearColor,
  164. opt_priority,
  165. opt_viewport,
  166. opt_performanceDrawList,
  167. opt_zOrderedDrawList) {
  168. var clearColor = opt_clearColor || [0.5, 0.5, 0.5, 1.0];
  169. var viewPriority = opt_priority || 0;
  170. var priority = 0;
  171. // Create Viewport.
  172. var viewport = pack.createObject('Viewport');
  173. if (opt_viewport) {
  174. viewport.viewport = opt_viewport;
  175. }
  176. viewport.priority = viewPriority;
  177. // Create a clear buffer.
  178. var clearBuffer = pack.createObject('ClearBuffer');
  179. clearBuffer.clearColor = clearColor;
  180. clearBuffer.priority = priority++;
  181. clearBuffer.parent = viewport;
  182. // Create DrawLists
  183. var performanceDrawList;
  184. if (opt_performanceDrawList) {
  185. performanceDrawList = opt_performanceDrawList;
  186. this.ownPerformanceDrawList_ = false;
  187. } else {
  188. performanceDrawList = pack.createObject('DrawList');
  189. this.ownPerformanceDrawList_ = true;
  190. }
  191. var zOrderedDrawList;
  192. if (opt_zOrderedDrawList) {
  193. zOrderedDrawList = opt_zOrderedDrawList;
  194. this.ownZOrderedDrawList_ = false;
  195. } else {
  196. zOrderedDrawList = pack.createObject('DrawList');
  197. this.ownZOrderedDrawList_ = true;
  198. }
  199. // Create DrawContext.
  200. var drawContext = pack.createObject('DrawContext');
  201. // Creates a TreeTraversal and parents it to the root.
  202. var treeTraversal = pack.createObject('TreeTraversal');
  203. treeTraversal.priority = priority++;
  204. treeTraversal.parent = viewport;
  205. // Creates an empty stateSet to parent the performanceDrawPass to
  206. // just to make it easier to do certain effects.
  207. var performanceStateSet = pack.createObject('StateSet');
  208. var performanceState = pack.createObject('State');
  209. performanceStateSet.state = performanceState;
  210. performanceStateSet.priority = priority++;
  211. performanceStateSet.parent = viewport;
  212. performanceState.getStateParam('ColorWriteEnable').value = 7;
  213. // Creates a DrawPass for performance shapes.
  214. var performanceDrawPass = pack.createObject('DrawPass');
  215. performanceDrawPass.drawList = performanceDrawList;
  216. performanceDrawPass.parent = performanceStateSet;
  217. // Creates a StateSet so everything on the zOrderedDrawPass is assumed
  218. // to need alpha blending with the typical settings.
  219. var zOrderedStateSet = pack.createObject('StateSet');
  220. var zOrderedState = pack.createObject('State');
  221. zOrderedState.getStateParam('AlphaBlendEnable').value = true;
  222. zOrderedState.getStateParam('SourceBlendFunction').value =
  223. o3djs.base.o3d.State.BLENDFUNC_SOURCE_ALPHA;
  224. zOrderedState.getStateParam('DestinationBlendFunction').value =
  225. o3djs.base.o3d.State.BLENDFUNC_INVERSE_SOURCE_ALPHA;
  226. zOrderedState.getStateParam('AlphaTestEnable').value = true;
  227. zOrderedState.getStateParam('AlphaComparisonFunction').value =
  228. o3djs.base.o3d.State.CMP_GREATER;
  229. zOrderedState.getStateParam('ColorWriteEnable').value = 7;
  230. zOrderedStateSet.state = zOrderedState;
  231. zOrderedStateSet.priority = priority++;
  232. zOrderedStateSet.parent = viewport;
  233. // Creates a DrawPass for zOrdered shapes.
  234. var zOrderedDrawPass = pack.createObject('DrawPass');
  235. zOrderedDrawPass.drawList = zOrderedDrawList;
  236. zOrderedDrawPass.sortMethod = o3djs.base.o3d.DrawList.BY_Z_ORDER;
  237. zOrderedDrawPass.parent = zOrderedStateSet;
  238. // Register the passlists and drawcontext with the TreeTraversal
  239. treeTraversal.registerDrawList(performanceDrawList, drawContext, true);
  240. treeTraversal.registerDrawList(zOrderedDrawList, drawContext, true);
  241. treeTraversal.transform = treeRoot;
  242. /**
  243. * Pack that manages the objects created for this ViewInfo.
  244. * @type {!o3d.Pack}
  245. */
  246. this.pack = pack;
  247. /**
  248. * The RenderNode this ViewInfo render graph subtree is parented under.
  249. * @type {(!o3d.RenderNode|undefined)}
  250. */
  251. this.renderGraphRoot = opt_parent;
  252. /**
  253. * The root node of the transform graph this ViewInfo renders.
  254. * @type {!o3d.Transform}
  255. */
  256. this.treeRoot = treeRoot;
  257. /**
  258. * The root of the subtree of the render graph this ViewInfo is managing.
  259. * If you want to set the priority of a ViewInfo's rendergraph subtree use
  260. * <pre>
  261. * viewInfo.root.priority = desiredPriority;
  262. * </pre>
  263. * @type {!o3d.RenderGraph}
  264. */
  265. this.root = viewport;
  266. /**
  267. * The Viewport RenderNode created for this ViewInfo.
  268. * @type {!o3d.Viewport}
  269. */
  270. this.viewport = viewport;
  271. /**
  272. * The ClearBuffer RenderNode created for this ViewInfo.
  273. * @type {!o3d.ClearBuffer}
  274. */
  275. this.clearBuffer = clearBuffer;
  276. /**
  277. * The StateSet RenderNode above the performance DrawPass in this ViewInfo
  278. * @type {!o3d.StateSet}
  279. */
  280. this.performanceStateSet = performanceStateSet;
  281. /**
  282. * The State object used by the performanceStateSet object in this ViewInfo.
  283. * By default, no states are set here.
  284. * @type {!o3d.State}
  285. */
  286. this.performanceState = performanceState;
  287. /**
  288. * The DrawList used for the performance draw pass. Generally for opaque
  289. * materials.
  290. * @type {!o3d.DrawList}
  291. */
  292. this.performanceDrawList = performanceDrawList;
  293. /**
  294. * The StateSet RenderNode above the ZOrdered DrawPass in this ViewInfo
  295. * @type {!o3d.StateSet}
  296. */
  297. this.zOrderedStateSet = zOrderedStateSet;
  298. /**
  299. * The State object used by the zOrderedStateSet object in this ViewInfo.
  300. * By default AlphaBlendEnable is set to true, SourceBlendFucntion is set to
  301. * State.BLENDFUNC_SOURCE_ALPHA and DestinationBlendFunction is set to
  302. * State.BLENDFUNC_INVERSE_SOURCE_ALPHA
  303. * @type {!o3d.State}
  304. */
  305. this.zOrderedState = zOrderedState;
  306. /**
  307. * The DrawList used for the zOrdered draw pass. Generally for transparent
  308. * materials.
  309. * @type {!o3d.DrawList}
  310. */
  311. this.zOrderedDrawList = zOrderedDrawList;
  312. /**
  313. * The DrawContext used by this ViewInfo.
  314. * @type {!o3d.DrawContext}
  315. */
  316. this.drawContext = drawContext;
  317. /**
  318. * The TreeTraversal used by this ViewInfo.
  319. * @type {!o3d.TreeTraversal}
  320. */
  321. this.treeTraversal = treeTraversal;
  322. /**
  323. * The DrawPass used with the performance DrawList created by this ViewInfo.
  324. * @type {!o3d.DrawPass}
  325. */
  326. this.performanceDrawPass = performanceDrawPass;
  327. /**
  328. * The DrawPass used with the zOrdered DrawList created by this ViewInfo.
  329. * @type {!o3d.DrawPass}
  330. */
  331. this.zOrderedDrawPass = zOrderedDrawPass;
  332. /**
  333. * The highest priority used for objects under the Viewport RenderNode created
  334. * by this ViewInfo.
  335. * @type {number}
  336. */
  337. this.priority = priority;
  338. // Parent whatever the root is to the parent passed in.
  339. if (opt_parent) {
  340. this.root.parent = opt_parent;
  341. }
  342. };
  343. /**
  344. * Destroys the various objects created for the view.
  345. *
  346. * @param {Boolean} opt_destroyDrawContext True if you want view's DrawContext
  347. * destroyed. Default = true.
  348. * @param {Boolean} opt_destroyDrawList True if you want view's DrawLists
  349. * destroyed. Default = true.
  350. */
  351. o3djs.rendergraph.ViewInfo.prototype.destroy = function(
  352. opt_destroyDrawContext,
  353. opt_destroyDrawList) {
  354. if (opt_destroyDrawContext === undefined) {
  355. opt_destroyDrawContext = true;
  356. }
  357. if (opt_destroyDrawList === undefined) {
  358. opt_destroyDrawList = true;
  359. }
  360. // Remove everything we created from the pack.
  361. this.pack.removeObject(this.viewport);
  362. this.pack.removeObject(this.clearBuffer);
  363. if (this.ownPerformanceDrawList_ && opt_destroyDrawList) {
  364. this.pack.removeObject(this.performanceDrawList);
  365. }
  366. if (this.ownZOrderedDrawList_ && opt_destroyDrawList) {
  367. this.pack.removeObject(this.zOrderedDrawList);
  368. }
  369. if (opt_destroyDrawContext) {
  370. this.pack.removeObject(this.drawContext);
  371. }
  372. this.pack.removeObject(this.treeTraversal);
  373. this.pack.removeObject(this.performanceDrawPass);
  374. this.pack.removeObject(this.zOrderedDrawPass);
  375. // Remove our substree from its parent.
  376. this.viewport.parent = null;
  377. // At this point, IF nothing else is referencing any of these objects
  378. // they should get removed.
  379. };