asset_manager.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>AssetManager</title><link rel="stylesheet" href="./asciidoctor.css">
  2. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  3. <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/advanced/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/asset_manager.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>AssetManager</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#context">Context</a><ul class="sectlevel2"><li><a href="#usage">Usage</a></li><li><a href="#asset-directory">Asset Directory</a></li><li><a href="#example-code-loading-assets">Example Code: Loading Assets</a></li><li><a href="#common-assetmanager-tasks">Common AssetManager Tasks</a></li><li><a href="#nullpointerexception-cannot-locate-resource">NullPointerException: Cannot locate resource?</a></li><li><a href="#asset-handling-for-other-ides-codeless-projects">Asset Handling For Other IDEs: Codeless Projects</a></li></ul></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>By assets we mean multi-media files, such as 3D models, materials, textures, scenes, custom shaders, music and sound files, and custom fonts. JME3 has an integrated asset manager that helps you keep your project assets organized. Think of the asset manager as the filesystem of your game, independent of the actual deployment platform. By default, store your assets in the `MyGame/assets/ ` directory of your project.</p></div>
  4. <div class="paragraph"><p>Advantages of the AssetManager:</p></div>
  5. <div class="ulist"><ul><li><p>The paths stay the same, no matter whether the game runs on Windows, Mac, Linux, etc!</p></li><li><p>The AssetManager automatically caches and optimizes the handling of OpenGL objects.<br>
  6. For example, the same textures are not uploaded to the graphics card multiple times when multiple models use them.</p></li><li><p>The <a href="../../sdk/default_build_script.html">default build script</a> automatically bundles the contents of the <code>assets</code> directory into the executable.</p></li></ul></div>
  7. <div class="paragraph"><p>Advanced users can write a custom build and packaging script, and can register custom paths to the AssetManager, but this is up to you then.</p></div></div></div>
  8. <div class="sect2"><h3 id="context">Context</h3><div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>jMonkeyProjects/MyGame/assets/ # You store assets in subfolders here! &lt;------
  9. jMonkeyProjects/MyGame/build/ # SDK generates built classes here (*)
  10. jMonkeyProjects/MyGame/build.xml # You customize Ant build script here
  11. jMonkeyProjects/MyGame/nbproject/ # SDK stores default build.xml and meta data (*)
  12. jMonkeyProjects/MyGame/dist/ # SDK generates executable distribution here (*)
  13. jMonkeyProjects/MyGame/src/ # You store Java sources here
  14. jMonkeyProjects/MyGame/test/ # You store test classes here (optional)
  15. (*) Managed by jMonkeyEngine SDK -- don't edit!</code></pre></div></div>
  16. <div class="paragraph"><p>See also <a href="../../jme3/intermediate/best_practices.html">Best Practices</a>.</p></div>
  17. <div class="sect2"><h3 id="usage">Usage</h3><div class="paragraph"><p>The <code>assetManager</code> object is an com.jme3.asset.AssetManager instance that every com.jme3.app.Application can access. It maintains a root that also includes your project&#8217;s classpath by default, so you can load any asset that&#8217;s on the classpath, that is, the top level of your project directory.</p></div>
  18. <div class="paragraph"><p>You can use the inherited <code>assetManager</code> object directly, or use the accessor <code>app.getAssetManager()</code>.</p></div>
  19. <div class="paragraph"><p>Here is an example how you load assets using the AssetManager. This lines loads a default Material from the built-in <code>Common/</code> directory:</p></div>
  20. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material mat = (Material) assetManager.loadAsset(
  21. <span class="keyword">new</span> AssetKey(<span class="string"><span class="delimiter">&quot;</span><span class="content">Common/Materials/RedColor.j3m</span><span class="delimiter">&quot;</span></span>));</code></pre></div></div>
  22. <div class="paragraph"><p>This Material is &#8220;somewhere&#8221; in the jME3 JAR; the default Asset Manager is configured to handle a <code>Common/…</code> path correctly, so you don&#8217;t have to specify the whole path when referring to built-in assets (such as default Materials).</p></div>
  23. <div class="paragraph"><p>Additionally, you can configure the Asset Manager and add any path to its root. This means, you can load assets from any project directory you specify. The next example shows how you load assets from your project&#8217;s assets directory.</p></div></div>
  24. <div class="sect2"><h3 id="asset-directory">Asset Directory</h3><div class="paragraph"><p>By default, jME3 searches for models in a directory named <code>assets</code>.</p></div>
  25. <div class="admonitionblock important"><table><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>In Java projects created with the jMonkeyEngine SDK, an <code>assets</code> folder is created by default in your project directory. If you are using any other IDE, or the command line, you simply create an <code>assets</code> directory manually (see the Codeless Project tip below).</p></div></td></tr></table></div>
  26. <div class="paragraph"><p>This is our recommended directory structure for storing assets:</p></div>
  27. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>jMonkeyProjects/MyGame/src/... # Packages, .java source code.
  28. jMonkeyProjects/MyGame/assets/... # The assets directory:
  29. jMonkeyProjects/MyGame/assets/Interface/ # .font, .jpg, .png, .xml
  30. jMonkeyProjects/MyGame/assets/MatDefs/ # .j3md
  31. jMonkeyProjects/MyGame/assets/Materials/ # .j3m
  32. jMonkeyProjects/MyGame/assets/Models/ # .j3o
  33. jMonkeyProjects/MyGame/assets/Scenes/ # .j3o
  34. jMonkeyProjects/MyGame/assets/Shaders/ # .j3f, .vert, .frag
  35. jMonkeyProjects/MyGame/assets/Sounds/ # .ogg, .wav
  36. jMonkeyProjects/MyGame/assets/Textures/ # .jpg, .png; also .mesh.xml+.material, .mtl+.obj, .blend (!)</code></pre></div></div>
  37. <div class="paragraph"><p>These subdirectories are just the most common examples.</p></div>
  38. <div class="admonitionblock important"><table><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>You can rename/delete/add (sub)directories inside the <code>assets</code> directory in any way you like. Note however that there is no automatic refactoring for asset paths in the SDK, so if you modify them late in the development process, you have to refactor all paths manually.</p></div></td></tr></table></div>
  39. <div class="paragraph"><p><strong>Examples:</strong> You can rename <code>assets/Sounds</code> to <code>assets/Audio</code>, you can delete <code>assets/MatDefs</code> if you don&#8217;t use it, you can create <code>assets/AIscripts</code>, etc. You can rename/move the <code>assets/Textures</code> directory or its subdirectories, but then you have to re-export all models, and re-convert them all to .j3o, so plan ahead!</p></div>
  40. <div class="admonitionblock important"><table><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>Store textures in <code>assets/Textures/</code> before you work with them in a mesh editor! Export and save 3D model files (.mesh.xml+.material, .mtl+.obj, .blend) into the <code>assets/Textures/</code> (!) before you convert the model to binary format (.j3o)! This ensures that texture paths correctly point to the <code>assets/Textures</code> directory.<br>
  41. After the conversion, you move the .j3o file into the <code>assets/Models/</code> or <code>assets/Scenes/</code> directories. This way, you can reuse textures, your binaries consistently link the correct textures, and the <code>assets/Models</code> and <code>assets/Scenes</code> directories don&#8217;t become cluttered.</p></div></td></tr></table></div></div>
  42. <div class="sect2"><h3 id="example-code-loading-assets">Example Code: Loading Assets</h3><div class="paragraph"><p>Creating a material instance with the definition &#8220;Unshaded.j3md&#8221;:</p></div>
  43. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material mat_brick = <span class="keyword">new</span> Material(
  44. assetManager, <span class="string"><span class="delimiter">&quot;</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">&quot;</span></span>);</code></pre></div></div>
  45. <div class="paragraph"><p>Applying a texture to the material:</p></div>
  46. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">mat_brick.setTexture(<span class="string"><span class="delimiter">&quot;</span><span class="content">ColorMap</span><span class="delimiter">&quot;</span></span>,
  47. assetManager.loadTexture(<span class="string"><span class="delimiter">&quot;</span><span class="content">Textures/Terrain/BrickWall/BrickWall.jpg</span><span class="delimiter">&quot;</span></span>));</code></pre></div></div>
  48. <div class="paragraph"><p>Loading a font:</p></div>
  49. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">guiFont = assetManager.loadFont(<span class="string"><span class="delimiter">&quot;</span><span class="content">Interface/Fonts/Default.fnt</span><span class="delimiter">&quot;</span></span>);</code></pre></div></div>
  50. <div class="paragraph"><p>Loading a model:</p></div>
  51. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Spatial ninja = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">Models/Ninja/Ninja.mesh.xml</span><span class="delimiter">&quot;</span></span>);</code></pre></div></div>
  52. <div class="paragraph"><p>Loading a scene from an Ogre3D dotScene file stored inside a zip:</p></div>
  53. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">assetManager.registerLocator(<span class="string"><span class="delimiter">&quot;</span><span class="content">town.zip</span><span class="delimiter">&quot;</span></span>, ZipLocator.class);
  54. Spatial scene = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">main.scene</span><span class="delimiter">&quot;</span></span>);
  55. rootNode.attachChild(scene);</code></pre></div></div>
  56. <div class="paragraph"><p>Alternatively to ZipLocator, there is also a HttpZipLocator that can stream models from a zip file online:</p></div>
  57. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">assetManager.registerLocator(<span class="string"><span class="delimiter">&quot;</span><span class="content">https://storage.googleapis.com/</span><span class="delimiter">&quot;</span></span>
  58. + <span class="string"><span class="delimiter">&quot;</span><span class="content">google-code-archive-downloads/v2/code.google.com/</span><span class="delimiter">&quot;</span></span>
  59. + <span class="string"><span class="delimiter">&quot;</span><span class="content">jmonkeyengine/wildhouse.zip</span><span class="delimiter">&quot;</span></span>, HttpZipLocator.class);
  60. Spatial scene = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">main.scene</span><span class="delimiter">&quot;</span></span>);
  61. rootNode.attachChild(scene);</code></pre></div></div>
  62. <div class="paragraph"><p>jME3 also offers a ClasspathLocator, ZipLocator, FileLocator, HttpZipLocator, and UrlLocator (see <code>com.jme3.asset.plugins</code>).</p></div>
  63. <div class="admonitionblock important"><table><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>The custom build script does not automatically include all ZIP files in the executable build. See &#8220;Cannot Locate Resource&#8221; solution below.</p></div></td></tr></table></div></div>
  64. <div class="sect2"><h3 id="common-assetmanager-tasks">Common AssetManager Tasks</h3><table class="tableblock frame-all grid-all spread"><colgroup><col style="width: 15%;"><col style="width: 85%;"></colgroup><thead><tr><th class="tableblock halign-left valign-top">Task?</th><th class="tableblock halign-left valign-top">Solution!</th></tr></thead><tbody><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Load a model with materials</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Use the asset manager&#8217;s <code>loadModel()</code> method and attach the Spatial to the rootNode.</p></div>
  65. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Spatial elephant = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">Models/Elephant/Elephant.mesh.xml</span><span class="delimiter">&quot;</span></span>);
  66. rootNode.attachChild(elephant);</code></pre></div></div>
  67. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Spatial elephant = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">Models/Elephant/Elephant.j3o</span><span class="delimiter">&quot;</span></span>);
  68. rootNode.attachChild(elephant);</code></pre></div></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Load a model without materials</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>If you have a model without materials, you have to add a default material to make it visible.</p></div>
  69. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Spatial teapot = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">Models/Teapot/Teapot.obj</span><span class="delimiter">&quot;</span></span>);
  70. Material mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">&quot;</span><span class="content">Common/MatDefs/Misc/ShowNormals.j3md</span><span class="delimiter">&quot;</span></span>);
  71. teapot.setMaterial(mat);
  72. rootNode.attachChild(teapot);</code></pre></div></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Load a scene</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>You load scenes just like you load models:</p></div>
  73. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Spatial scene = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">Scenes/house/main.scene</span><span class="delimiter">&quot;</span></span>);
  74. rootNode.attachChild(scene);</code></pre></div></div></div></td></tr></tbody></table></div>
  75. <div class="sect2"><h3 id="nullpointerexception-cannot-locate-resource">NullPointerException: Cannot locate resource?</h3><div class="paragraph"><p><strong>Problem:</strong></p></div>
  76. <div class="paragraph"><p>My game runs fine when I run it right from the jMonkeyEngine SDK. But when I run the stand-alone executables (.jar, .jnlp .exe, .app), a DesktopAssetManager error message occurs in the console, and it quits?</p></div>
  77. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code>com.jme3.asset.DesktopAssetManager loadAsset
  78. WARNING: Cannot locate resource: Scenes/town/main.scene
  79. com.jme3.app.Application handleError
  80. SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
  81. java.lang.NullPointerException</code></pre></div></div>
  82. <div class="paragraph"><p><strong>Reason:</strong></p></div>
  83. <div class="paragraph"><p>If you use the default build script, <strong>original models and scenes (.mesh.xml, .obj, .blend, .zip), are excluded</strong> from the distribution automatically. A stand-alone executable includes converted <strong>.j3o files</strong> (models and scenes) only. The default build script makes sure to bundle existing .j3o files in the distribution, but you need to remember to convert the models (from mesh.xml–&gt;.j3o, or .obj–&gt;.j3o, etc) yourself.</p></div>
  84. <div class="paragraph"><p><strong>Solution</strong></p></div>
  85. <div class="paragraph"><p>Before building the executable, you must use the jMonkeyEngine SDK&#8217;s context menu action to <a href="../../sdk/model_loader_and_viewer.html">convert 3D models to .j3o binary format</a>.</p></div>
  86. <div class="olist arabic"><ol class="arabic"><li><p>Save your original models (.mesh.xml, .scene, .blend, or .obj files, plus textures) into <code>assets/Textures/</code>. (!)</p></li><li><p>Open the jME3 project in the jMonkeyEngine SDK.</p></li><li><p>Browse to the <code>assets</code> directory in the Projects window.</p></li><li><p>Right-click an original model in <code>assets/Textures/</code>, and choose &#8220;Convert to JME3 binary&#8221;.</p></li><li><p>The converted file appears in the same directory as the original file. It has the same name and a <code>.j3o</code> suffix.</p></li><li><p>Move the .j3o file into the <code>assets/Models/</code> or <code>assets/Scenes/</code> directory.</p></li><li><p>Use the assetManager&#8217;s <code>load()</code> method to load the <code>.j3o</code> file.</p></li></ol></div>
  87. <div class="paragraph"><p>This ensures that the model&#8217;s Texture paths keep working between your 3D mesh editor and JME3.</p></div>
  88. <div class="admonitionblock important"><table><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>If you must load custom assets from a non-.j3o ZIP file, you must manually ammend the <a href="../../sdk/default_build_script.html">default build script</a> to copy ZIP files into your distribution. ZIPs are skipped by default.</p></div></td></tr></table></div></div>
  89. <div class="sect2"><h3 id="asset-handling-for-other-ides-codeless-projects">Asset Handling For Other IDEs: Codeless Projects</h3><div class="paragraph"><p><strong>Problem:</strong></p></div>
  90. <div class="paragraph"><p>I use another IDE than jMonkeyEngine SDK for coding (Eclipse, IntelliJ, text editor). Where is my <code>asset</code> folder and .j3o converter?</p></div>
  91. <div class="paragraph"><p><strong>Solution:</strong></p></div>
  92. <div class="paragraph"><p>You can code in any IDE, but you must create a so-called codeless project in the jMonkeyEngine SDK to maintain assets. <strong>A code-less jMonkeyEngine project does not meddle with your sources or custom build scripts.</strong> You merely use it to convert models to .j3o binaries.</p></div>
  93. <div class="olist arabic"><ol class="arabic"><li><p>Create your (Eclipse or whatever) project as you like.</p></li><li><p>Create a directory in your project folder and name it, for example, <code>assets</code>.<br>
  94. Store your assets there as described above.</p></li><li><p>Download and install the jMonkeyEngine SDK.</p></li><li><p>In the SDK, go to File → Import Projects → External Project Assets.</p></li><li><p>Select your (Eclipse or whatever) project and your assets folder in the Import Wizard.</p></li><li><p>You can now open this (Eclipse or whatever) project in the jMonkeyEngine SDK.<br>
  95. Convert assets as described above.</p></li></ol></div>
  96. <div class="admonitionblock important"><table><tr><td class="icon"><i class="fa icon-important" title="Important"></i></td><td class="content"><div class="paragraph"><p>If you don&#8217;t use the SDK for some reason, you can still convert models to j3o format: Load any model in Ogre3D or Wavefront format with the AssetManager.loadModel() as a spatial. Then save the spatial as j3o file using <a href="../../jme3/advanced/save_and_load.html">BinaryExporter</a>.</p></div></td></tr></table></div>
  97. <div class="admonitionblock tip"><table><tr><td class="icon"><i class="fa icon-tip" title="Tip"></i></td><td class="content"><div class="paragraph"><p>Use file version control and let team members check out the project. Your developers open the project in Eclipse (etc) as they are used to. Additionally to their graphic tools, ask your graphic designers to install the jMonkeyEngine SDK, and to check out the codeless project that you just prepared. This makes it easy for non-coding team member to browse and preview game assets, to arrange scenes, and to convert files. At the same time, non-coders don&#8217;t accidentally mess with code, and developers don&#8217;t accidentally mess with assets. :)</p></div></td></tr></table></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2019-12-20 23:30:51 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
  98. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  99. indexName: 'jmonkeyengine',
  100. inputSelector: '#doc-search',
  101. debug: false // Set debug to true if you want to inspect the dropdown
  102. });</script></body></html>