scene.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>jMonkeyEngine SDK&#8201;&#8212;&#8201;The Scene :: jMonkeyEngine Docs</title>
  7. <link rel="canonical" href="https://wiki.jmonkeyengine.org/docs/sdk/development/scene.html">
  8. <meta name="generator" content="Antora 2.3.3">
  9. <link rel="stylesheet" href="../../../_/css/site.css">
  10. </head>
  11. <body class="article">
  12. <header class="header">
  13. <nav class="navbar">
  14. <div class="navbar-brand">
  15. <a class="navbar-item" href="https://wiki.jmonkeyengine.org">jMonkeyEngine Docs</a>
  16. <button class="navbar-burger" data-target="topbar-nav">
  17. <span></span>
  18. <span></span>
  19. <span></span>
  20. </button>
  21. </div>
  22. <div id="topbar-nav" class="navbar-menu">
  23. <div class="navbar-end">
  24. <a class="navbar-item" href="#">Home</a>
  25. <div class="navbar-item has-dropdown is-hoverable">
  26. <a class="navbar-link" href="#">Products</a>
  27. <div class="navbar-dropdown">
  28. <a class="navbar-item" href="#">Product A</a>
  29. <a class="navbar-item" href="#">Product B</a>
  30. <a class="navbar-item" href="#">Product C</a>
  31. </div>
  32. </div>
  33. <div class="navbar-item has-dropdown is-hoverable">
  34. <a class="navbar-link" href="#">Services</a>
  35. <div class="navbar-dropdown">
  36. <a class="navbar-item" href="#">Service A</a>
  37. <a class="navbar-item" href="#">Service B</a>
  38. <a class="navbar-item" href="#">Service C</a>
  39. </div>
  40. </div>
  41. <div class="navbar-item has-dropdown is-hoverable">
  42. <a class="navbar-link" href="#">Resources</a>
  43. <div class="navbar-dropdown">
  44. <a class="navbar-item" href="#">Resource A</a>
  45. <a class="navbar-item" href="#">Resource B</a>
  46. <a class="navbar-item" href="#">Resource C</a>
  47. </div>
  48. </div>
  49. <div class="navbar-item">
  50. <span class="control">
  51. <a class="button is-primary" href="#">Download</a>
  52. </span>
  53. </div>
  54. </div>
  55. </div>
  56. </nav>
  57. </header>
  58. <div class="body">
  59. <div class="nav-container" data-component="docs" data-version="master">
  60. <aside class="nav">
  61. <div class="panels">
  62. <div class="nav-panel-menu is-active" data-panel="menu">
  63. <nav class="nav-menu">
  64. <h3 class="title"><a href="../../jme3.html">Docs</a></h3>
  65. <ul class="nav-list">
  66. <li class="nav-item" data-depth="0">
  67. <ul class="nav-list">
  68. <li class="nav-item" data-depth="1">
  69. <a class="nav-link" href="../../documentation.html">Getting Started</a>
  70. </li>
  71. <li class="nav-item" data-depth="1">
  72. <a class="nav-link" href="https://javadoc.jmonkeyengine.org/v3.3.2-stable">JavaDoc</a>
  73. </li>
  74. <li class="nav-item" data-depth="1">
  75. <button class="nav-item-toggle"></button>
  76. <a class="nav-link" href="../../jme3.html">jMonkeyEngine 3</a>
  77. <ul class="nav-list">
  78. <li class="nav-item" data-depth="2">
  79. <button class="nav-item-toggle"></button>
  80. <span class="nav-text">Beginner Tutorials</span>
  81. <ul class="nav-list">
  82. <li class="nav-item" data-depth="3">
  83. <a class="nav-link" href="../../jme3/beginner/hello_simpleapplication.html">Hello SimpleApplication</a>
  84. </li>
  85. <li class="nav-item" data-depth="3">
  86. <a class="nav-link" href="../../jme3/beginner/hello_node.html">Hello Node</a>
  87. </li>
  88. <li class="nav-item" data-depth="3">
  89. <a class="nav-link" href="../../jme3/beginner/hello_asset.html">Hello Asset</a>
  90. </li>
  91. <li class="nav-item" data-depth="3">
  92. <a class="nav-link" href="../../jme3/beginner/hello_main_event_loop.html">Hello Main Event Loop</a>
  93. </li>
  94. <li class="nav-item" data-depth="3">
  95. <a class="nav-link" href="../../jme3/beginner/hello_input_system.html">Hello Input System</a>
  96. </li>
  97. <li class="nav-item" data-depth="3">
  98. <a class="nav-link" href="../../jme3/beginner/hello_material.html">Hello Material</a>
  99. </li>
  100. <li class="nav-item" data-depth="3">
  101. <a class="nav-link" href="../../jme3/beginner/hello_animation.html">Hello Animation</a>
  102. </li>
  103. <li class="nav-item" data-depth="3">
  104. <a class="nav-link" href="../../jme3/beginner/hello_picking.html">Hello Picking</a>
  105. </li>
  106. <li class="nav-item" data-depth="3">
  107. <a class="nav-link" href="../../jme3/beginner/hello_collision.html">Hello Collision</a>
  108. </li>
  109. <li class="nav-item" data-depth="3">
  110. <a class="nav-link" href="../../jme3/beginner/hello_terrain.html">Hello Terrain</a>
  111. </li>
  112. <li class="nav-item" data-depth="3">
  113. <a class="nav-link" href="../../jme3/beginner/hello_audio.html">Hello Audio</a>
  114. </li>
  115. <li class="nav-item" data-depth="3">
  116. <a class="nav-link" href="../../jme3/beginner/hello_effects.html">Hello Effects</a>
  117. </li>
  118. <li class="nav-item" data-depth="3">
  119. <a class="nav-link" href="../../jme3/beginner/hello_physics.html">Hello Physics</a>
  120. </li>
  121. </ul>
  122. </li>
  123. <li class="nav-item" data-depth="2">
  124. <button class="nav-item-toggle"></button>
  125. <span class="nav-text">Intermediate Tutorials</span>
  126. <ul class="nav-list">
  127. <li class="nav-item" data-depth="3">
  128. <button class="nav-item-toggle"></button>
  129. <span class="nav-text">Concepts</span>
  130. <ul class="nav-list">
  131. <li class="nav-item" data-depth="4">
  132. <a class="nav-link" href="../../jme3/intermediate/best_practices.html">Best Practices</a>
  133. </li>
  134. <li class="nav-item" data-depth="4">
  135. <a class="nav-link" href="../../jme3/intermediate/simpleapplication.html">Simple Application</a>
  136. </li>
  137. <li class="nav-item" data-depth="4">
  138. <a class="nav-link" href="../../jme3/features.html">Features</a>
  139. </li>
  140. <li class="nav-item" data-depth="4">
  141. <a class="nav-link" href="../../jme3/intermediate/optimization.html">Optimization</a>
  142. </li>
  143. <li class="nav-item" data-depth="4">
  144. <a class="nav-link" href="../../jme3/faq.html">FAQ</a>
  145. </li>
  146. </ul>
  147. </li>
  148. <li class="nav-item" data-depth="3">
  149. <button class="nav-item-toggle"></button>
  150. <span class="nav-text">Math Concepts</span>
  151. <ul class="nav-list">
  152. <li class="nav-item" data-depth="4">
  153. <a class="nav-link" href="../../jme3/math_for_dummies.html">Math For Dummies</a>
  154. </li>
  155. <li class="nav-item" data-depth="4">
  156. <a class="nav-link" href="../../jme3/intermediate/math.html">Math</a>
  157. </li>
  158. <li class="nav-item" data-depth="4">
  159. <a class="nav-link" href="../../jme3/math.html">More Math</a>
  160. </li>
  161. <li class="nav-item" data-depth="4">
  162. <a class="nav-link" href="../../jme3/rotate.html">Rotate</a>
  163. </li>
  164. <li class="nav-item" data-depth="4">
  165. <a class="nav-link" href="../../jme3/math_video_tutorials.html">Math Video Tutorials</a>
  166. </li>
  167. </ul>
  168. </li>
  169. <li class="nav-item" data-depth="3">
  170. <button class="nav-item-toggle"></button>
  171. <span class="nav-text">3D Graphics Concepts</span>
  172. <ul class="nav-list">
  173. <li class="nav-item" data-depth="4">
  174. <a class="nav-link" href="../../jme3/intermediate/multi-media_asset_pipeline.html">Multi-Media Asset Pipeline</a>
  175. </li>
  176. <li class="nav-item" data-depth="4">
  177. <a class="nav-link" href="../../jme3/scenegraph_for_dummies.html">Scenegraph for Dummies</a>
  178. </li>
  179. <li class="nav-item" data-depth="4">
  180. <a class="nav-link" href="../../jme3/beginner/hellovector.html">Hello Vector</a>
  181. </li>
  182. <li class="nav-item" data-depth="4">
  183. <a class="nav-link" href="../../jme3/terminology.html">Terminology</a>
  184. </li>
  185. <li class="nav-item" data-depth="4">
  186. <a class="nav-link" href="../../jme3/intermediate/how_to_use_materials.html">How to Use Materials</a>
  187. </li>
  188. <li class="nav-item" data-depth="4">
  189. <a class="nav-link" href="../../jme3/intermediate/transparency_sorting.html">Transparency and Sorting</a>
  190. </li>
  191. <li class="nav-item" data-depth="4">
  192. <a class="nav-link" href="../../jme3/external/blender.html">Importing from Blender</a>
  193. </li>
  194. <li class="nav-item" data-depth="4">
  195. <a class="nav-link" href="../../jme3/external/3dsmax.html">Importing from 3DS Max</a>
  196. </li>
  197. </ul>
  198. </li>
  199. </ul>
  200. </li>
  201. </ul>
  202. </li>
  203. <li class="nav-item" data-depth="1">
  204. <a class="nav-link" href="../../logo.html">Logo Usage</a>
  205. </li>
  206. <li class="nav-item" data-depth="1">
  207. <a class="nav-link" href="../../bsd_license.html">License</a>
  208. </li>
  209. <li class="nav-item" data-depth="1">
  210. <a class="nav-link" href="../../github_tips.html">Github Tips</a>
  211. </li>
  212. </ul>
  213. </li>
  214. <li class="nav-item" data-depth="0">
  215. <button class="nav-item-toggle"></button>
  216. <span class="nav-text">SDK</span>
  217. <ul class="nav-list">
  218. <li class="nav-item" data-depth="1">
  219. <a class="nav-link" href="../../sdk.html">jMonkeyEngine SDK</a>
  220. </li>
  221. </ul>
  222. </li>
  223. </ul>
  224. </nav>
  225. </div>
  226. <div class="nav-panel-explore" data-panel="explore">
  227. <div class="context">
  228. <span class="title">Docs</span>
  229. <span class="version">master</span>
  230. </div>
  231. <ul class="components">
  232. <li class="component is-current">
  233. <span class="title">Docs</span>
  234. <ul class="versions">
  235. <li class="version is-current is-latest">
  236. <a href="../../jme3.html">master</a>
  237. </li>
  238. </ul>
  239. </li>
  240. <li class="component">
  241. <span class="title">Wiki UI</span>
  242. <ul class="versions">
  243. <li class="version is-latest">
  244. <a href="../../../wiki-ui/index.html">master</a>
  245. </li>
  246. </ul>
  247. </li>
  248. </ul>
  249. </div>
  250. </div>
  251. </aside>
  252. </div>
  253. <main class="article">
  254. <div class="toolbar" role="navigation">
  255. <button class="nav-toggle"></button>
  256. <a href="../../documentation.html" class="home-link"></a>
  257. <nav class="breadcrumbs" aria-label="breadcrumbs">
  258. <ul>
  259. <li><a href="../../jme3.html">Docs</a></li>
  260. <li><a href="scene.html">jMonkeyEngine SDK&#8201;&#8212;&#8201;The Scene</a></li>
  261. </ul>
  262. </nav>
  263. <div class="edit-this-page"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/docs/modules/ROOT/pages/sdk/development/scene.adoc">Edit this Page</a></div>
  264. </div>
  265. <div class="content">
  266. <article class="doc">
  267. <h1 class="page">jMonkeyEngine SDK&#8201;&#8212;&#8201;The Scene</h1>
  268. <div id="preamble">
  269. <div class="sectionbody">
  270. <div class="paragraph">
  271. <p>To reduce system overhead the jMonkeyEngine SDK Core supplies one scene/jme3 application that is shared between plugins. Furthermore there&#8217;s the “SceneExplorer that shows a visual representation of the scenegraph and its objects properties across plugins.</p>
  272. </div>
  273. </div>
  274. </div>
  275. <div class="sect1">
  276. <h2 id="how-to-access-the-scene"><a class="anchor" href="#how-to-access-the-scene"></a>How to access the Scene</h2>
  277. <div class="sectionbody">
  278. <div class="paragraph">
  279. <p>There are several ways for your plugin to interact with the Scene:</p>
  280. </div>
  281. <div class="ulist">
  282. <ul>
  283. <li>
  284. <p>It listens for selected spatials / objects and offers options for those</p>
  285. </li>
  286. <li>
  287. <p>It requests the whole scene for itself and loads/arranges the content in it (e.g. a terrain editor or model animation plugin).</p>
  288. </li>
  289. </ul>
  290. </div>
  291. </div>
  292. </div>
  293. <div class="sect1">
  294. <h2 id="listening-for-node-selection"><a class="anchor" href="#listening-for-node-selection"></a>Listening for Node selection</h2>
  295. <div class="sectionbody">
  296. <div class="paragraph">
  297. <p>In the jMonkeyEngine SDK, all objects are wrapped into NetBeans “Nodes (different thing than jme Nodes!). Such nodes can have properties and icons and can be displayed and selected in the jMonkeyEngine SDK UI. The SceneExplorer shows a tree of Nodes that wrap the Spatials of the current scene and allows manipulating their properties on selection. A jME “Spatial is wrapped by a “JmeSpatial node, for example. One advantage of these Nodes is that one can manipulate properties of Spatials directly from the AWT thread.</p>
  298. </div>
  299. <div class="paragraph">
  300. <p>To listen to the current selection, implement org.openide.util.LookupListener and register like this:</p>
  301. </div>
  302. <div class="listingblock">
  303. <div class="content">
  304. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">private final Result&lt;JmeSpatial&gt; result;
  305. //method to register the listener;
  306. private void registerListener(){
  307. result = Utilities.actionsGlobalContext().lookupResult(JmeSpatial.class);
  308. result.addLookupListener(this);
  309. }
  310. //implements org.openide.util.LookupListener (called from AWT thread)
  311. public void resultChanged(LookupEvent ev) {
  312. Collection&lt;JmeSpatial&gt; items = (Collection&lt;JmeSpatial&gt;) result.allInstances();
  313. for (JmeSpatial jmeSpatial : items) {
  314. //Using the JmeSpatials properties you can modify the spatial directly from the AWT thread:
  315. jmeSpatial.getPropertySets()[0].setValue("Local Translation", Vector3f.ZERO);
  316. return;
  317. }
  318. }</code></pre>
  319. </div>
  320. </div>
  321. <div class="paragraph">
  322. <p>You can also access the “real spatial but since its part of the scenegraph you will have to modify it on that thread:</p>
  323. </div>
  324. <div class="listingblock">
  325. <div class="content">
  326. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">//retrieve the "real" spatial class from the JmeNode
  327. for (JmeSpatial jmeSpatial : items) {
  328. //the spatial is stored inside the JmeSpatials "Lookup", a general container for Objects
  329. final Spatial realSpatial = jmeSpatial.getLookup().lookup(Spatial.class);
  330. //use a Callable to execute on the render thread:
  331. SceneApplication.getApplication().enqueue(new Callable() {
  332. public Object call() throws Exception {
  333. realSpatial.setLocalTranslation(Vector3f.ZERO);
  334. return null;
  335. }
  336. });
  337. return;
  338. }</code></pre>
  339. </div>
  340. </div>
  341. </div>
  342. </div>
  343. <div class="sect1">
  344. <h2 id="requesting-the-scene"><a class="anchor" href="#requesting-the-scene"></a>Requesting the Scene</h2>
  345. <div class="sectionbody">
  346. <div class="paragraph">
  347. <p>If your plugin wants to use the scene by itself, it first has to implement SceneListener and register at the scene and then send a SceneRequest to the SceneApplication. When the SceneRequest has been approved and the current Scene has been closed, the SceneListener (your class) is called with its own SceneRequest as a parameter. When another plugin sends a SceneRequest it is also reported to you and its a hint that your RootNode has been removed from the Scene and you are no longer in control of it. You could also hook into the SceneRequests of other plugins to see if/when they are activated to display add-on plugins for that plugin.</p>
  348. </div>
  349. <div class="paragraph">
  350. <p>+
  351. The SceneRequest object has to contain several things. A thing that you must supply is a jme “Node wrapped into a “JmeNode object. This is your rootNode that you use to display and build your scene. As soon as you control the scene, you will have to control the camera etc. yourself.</p>
  352. </div>
  353. <div class="listingblock">
  354. <div class="content">
  355. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">com.jme3.scene.Node rootNode = new com.jme3.scene.Node("MyRootNode");
  356. private void registerSceneListener(){
  357. SceneApplication.getApplication().addSceneListener(this);
  358. }
  359. private void requestScene(){
  360. //create a jmeNode from the rootNode using the NodeUtility
  361. JmeNode jmeNode = NodeUtility.createNode(rootNode);
  362. //create the scene request
  363. SceneRequest request=new SceneRequest(this, jmeNode, assetManager);
  364. //request the scene
  365. SceneApplication.getApplication().openScene(request);
  366. }
  367. //implements SceneListener (called from AWT thread)
  368. public void sceneOpened(SceneRequest request){
  369. //check if its our request
  370. if (request.getRequester() == this) {
  371. //we now own the scene, any operations on the scene have to be done via Callables
  372. }
  373. }
  374. public void sceneClosed(SceneRequest request) {
  375. if (request.getRequester() == this) {
  376. //we have to close the scene, any operations on the scene have to be done via Callables
  377. }
  378. }</code></pre>
  379. </div>
  380. </div>
  381. </div>
  382. </div>
  383. <div class="sect1">
  384. <h2 id="undoredo-support"><a class="anchor" href="#undoredo-support"></a>Undo/Redo support</h2>
  385. <div class="sectionbody">
  386. <div class="paragraph">
  387. <p>The jMonkeyEngine SDK has a global undo/redo queue that activates the undo/redo buttons. To use it in your TopComponent, add the following method:</p>
  388. </div>
  389. <div class="listingblock">
  390. <div class="content">
  391. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Override
  392. public UndoRedo getUndoRedo() {
  393. return Lookup.getDefault().lookup(SceneUndoRedoManager.class);
  394. }</code></pre>
  395. </div>
  396. </div>
  397. <div class="paragraph">
  398. <p>To add a undo/redo event that modifies objects on the Scenegraph, theres a special version of AbstractUndoableEdit which executes the undo/redo calls on the scene thread. Simply implement that class and add it to the queue like this:</p>
  399. </div>
  400. <div class="listingblock">
  401. <div class="content">
  402. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Lookup.getDefault().lookup(SceneUndoRedoManager.class).addEdit(this, new AbstractUndoableSceneEdit() {
  403. @Override
  404. public void sceneUndo() {
  405. //undo stuff in scene here
  406. }
  407. @Override
  408. public void sceneRedo() {
  409. //redo stuff in scene here
  410. }
  411. @Override
  412. public void awtUndo() {
  413. //undo stuff on awt thread here (updating of visual nodes etc, called post scene edit)
  414. }
  415. @Override
  416. public void awtRedo() {
  417. //redo stuff on awt thread here
  418. }
  419. });</code></pre>
  420. </div>
  421. </div>
  422. <div class="paragraph">
  423. <p>Note: Its important that you use the method addEdit(Object source, UndoableEdit edit);</p>
  424. </div>
  425. </div>
  426. </div>
  427. </article>
  428. <aside class="toc sidebar" data-title="Contents" data-levels="2">
  429. <div class="toc-menu"></div>
  430. </aside>
  431. </div>
  432. </main>
  433. </div>
  434. <footer class="footer">
  435. <p>This page was built using the Antora default UI.</p>
  436. <p>The source code for this UI is licensed under the terms of the MPL-2.0 license.</p>
  437. </footer>
  438. <script src="../../../_/js/site.js"></script>
  439. <script async src="../../../_/js/vendor/highlight.js"></script>
  440. </body>
  441. </html>