| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <!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>Optimization: Texture Atlas</title><link rel="stylesheet" href="./asciidoctor.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
- <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/texture_atlas.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>Optimization: Texture Atlas</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="#sample-code">Sample Code</a></li><li><a href="#api">API</a></li><li><a href="#primary-and-secondary-maps">Primary and Secondary Maps</a></li><li><a href="#usage-examples-combine-geometries">Usage examples: Combine Geometries</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>The <code>jme3tools.optimize.TextureAtlas</code> allows combining multiple textures into one texture atlas. Loading one geometry with one material is much more efficient than handling several geometries and materials. Optimally, you already export your textures as texture atlas from e.g. Blender.</p></div>
- <div class="paragraph"><p><code>jme3tools.optimize.GeometryBatchFactory</code>, in contrast, only works if the geometries have only one material with textures.</p></div>
- <div class="olist arabic"><ol class="arabic"><li><p>Create a TextureAtlas.</p></li><li><p>Add textures to texture atlas, each texture goes onto one map (e.g. <code>DiffuseMap</code>). +The image data is stored in a byte array for each named texture map.</p></li><li><p>Later, you retrieve each map by name as a Texture, and use it in materials.</p></li></ol></div></div></div>
- <div class="sect1"><h2 id="sample-code">Sample Code</h2><div class="sectionbody"><div class="ulist"><ul><li><p><a href="https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/tools/TestTextureAtlas.java">TestTextureAtlas.java</a></p></li></ul></div></div></div>
- <div class="sect1"><h2 id="api">API</h2><div class="sectionbody"><table class="tableblock frame-all grid-all spread"><colgroup><col style="width: 50%;"><col style="width: 50%;"></colgroup><thead><tr><th class="tableblock halign-left valign-top">TextureAtlas method</th><th class="tableblock halign-left valign-top">Usage</th></tr></thead><tbody><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>addGeometry(g)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Add this geometry’s DiffuseMap (or ColorMap), NormalMap, and SpecularMap to the atlas (if they exist). The DiffuseMap will automatically be the master map.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>addTexture(t, mapname)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Add a texture to the named master map.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>addTexture(t1,mapName,t2)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Add a texture t1 to the named secondary map, and make the location of texture t1 correspond to texture t2 on the master map. t2 can be Texture object or the String name of the texture.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>applyCoords(g)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Applies the texture coordinates to the geometries mesh. Short for the default, <code>applyCoords(geom, 0, geom.getMesh()</code>.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>applyCoords(g,offset,mesh)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Applies the texture coordinates at the given texture coord buffer offset to the mesh, if the DiffuseMap or ColorMap of the input geometry g exist in the atlas. The mesh can be <code>g.getMesh()</code>. Target buffer offset is between 0 and buffer.size().</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>getAtlasTile(texture)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Get the <code>TextureAtlasTile</code> for the given Texture. The TextureAtlasTile objects contains info about this texture. such as size and location in the atlas.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>getAtlasTexture(mapName)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Creates a new atlas texture from the added textures for the given map name.</p></div></div></td></tr></tbody></table>
- <table class="tableblock frame-all grid-all spread"><colgroup><col style="width: 50%;"><col style="width: 50%;"></colgroup><thead><tr><th class="tableblock halign-left valign-top">TextureAtlasTile method</th><th class="tableblock halign-left valign-top">Usage</th></tr></thead><tbody><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>getHeight(), getWidth()</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Gets the size of the texture.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>getX(), getY()</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Gets the x and y coordinate inside the texture atlas where this texture can be found</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>transformTextureCoords(inBuf,offset,outBuf)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Transforms the texture coordinates in a buffer from their original 0-1 values to the new values fitting to the location of the tile on the atlas texture.</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>getLocation(l)</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Get the transformed texture coordinate for a given input location.</p></div></div></td></tr></tbody></table></div></div>
- <div class="sect1"><h2 id="primary-and-secondary-maps">Primary and Secondary Maps</h2><div class="sectionbody"><div class="paragraph"><p>The helper methods that work with Geometry objects automatically consider the <code>DiffuseMap</code> (for Lighting.j3md-based Materials) or ColorMap (for Unshaded.j3md-based Materials) the <strong>master map</strong>, and additionally treat the <code>NormalMap</code> and <code>SpecularMap</code> as secondary maps, if they exist in the Geometry.</p></div>
- <div class="ulist"><ul><li><p>The first map name that you supply becomes the <strong>master map</strong>. The master map defines locations on the atlas. Typically, you name the master map <code>DiffuseMap</code> (for Lighting.j3md) or <code>ColorMap</code> (for Unshaded.j3md). +In general, if you want to use the texture with a certain shader, you should supply the map names that are specified in this shader’s .j3md for clarity.</p></li><li><p>Secondary textures (other map names, for example <code>SpecularMap</code> and <code>NormalMap</code> for Lighting.j3md) have to reference a existing texture on the master map, so the Atlas knows where to position the added texture on the secondary map. This is necessary because the maps share texture coordinates and thus need to be placed at the same location on both maps. If you do not share texture coordinates in your maps, use separate TextureAtlas classes.</p></li></ul></div>
- <div class="paragraph"><p>You reference textures by their <strong>asset key name</strong>, this is what their “id is. For each texture, the atlas stores its location. A texture with an existing key name is never added more than once to the atlas. You can access the information for each texture or geometry texture via helper methods.</p></div>
- <div class="ulist"><ul><li><p>Textures are not scaled automatically, and your atlas needs to be created large enough to hold all textures. All methods that allow adding textures return false if the texture could not be added due to the atlas being full.</p></li><li><p>Secondary textures (normal maps, specular maps etc.) have to be the same size as the main (e.g. DiffuseMap) texture.</p></li><li><p>The TextureAtlas lets you change the texture coordinates of a mesh or geometry to point at the new locations of its texture inside the atlas (if the texture exists inside the atlas). ??</p></li></ul></div>
- <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>Note that models that use texture coordinates outside the 0-1 range (that is, they use repeating/wrapping textures) do not work correctly as their new coordinates leak into other parts of the atlas and thus display other textures instead of repeating the texture!</p></div></td></tr></table></div></div></div>
- <div class="sect1"><h2 id="usage-examples-combine-geometries">Usage examples: Combine Geometries</h2><div class="sectionbody"><div class="paragraph"><p>Use <code>makeAtlasBatch</code> to turn several geometries that are loaded from a j3o file into one geometry:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="comment">// scene contains many geometries ared attached to one node:</span>
- Node scene = assetManager.loadModel(<span class="string"><span class="delimiter">"</span><span class="content">Scenes/MyScene.j3o</span><span class="delimiter">"</span></span>);
- <span class="comment">// geom is one geometry containing all of these geometries together</span>
- Geometry geom = TextureAtlas.makeAtlasBatch(scene);
- rootNode.attachChild(geom);</code></pre></div></div>
- <div class="paragraph"><p>Create a texture atlas and change the texture coordinates of one geometry:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> Node scene = assetManager.loadModel(<span class="string"><span class="delimiter">"</span><span class="content">Scenes/MyScene.j3o</span><span class="delimiter">"</span></span>);
- <span class="comment">// Either auto-create texture Atlas from an existing node that has geometries...</span>
- TextureAtlas atlas = TextureAtlas.createAtlas(scene);
- <span class="comment">//... or create Atlas manually by adding textures (e.g. myDiffuseTexture) to the atlas,</span>
- TextureAtlas atlas = <span class="keyword">new</span> TextureAtlas(<span class="integer">1024</span>,<span class="integer">1024</span>);
- atlas.addTexture(myDiffuseTexture, <span class="string"><span class="delimiter">"</span><span class="content">DiffuseMap</span><span class="delimiter">"</span></span>);
- <span class="comment">// ... or create Atlas manually by adding textured geometries (e.g. myGeometry) to the atlas:</span>
- TextureAtlas atlas = <span class="keyword">new</span> TextureAtlas(<span class="integer">1024</span>,<span class="integer">1024</span>);
- atlas.addGeometry(MyGeometry);
- <span class="comment">// Create material, load texture from Atlas, apply texture to material map.</span>
- Material mat = <span class="keyword">new</span> Material(mgr, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Light/Lighting.j3md</span><span class="delimiter">"</span></span>);
- mat.setTexture(<span class="string"><span class="delimiter">"</span><span class="content">DiffuseMap</span><span class="delimiter">"</span></span>, atlas.getAtlasTexture(<span class="string"><span class="delimiter">"</span><span class="content">DiffuseMap</span><span class="delimiter">"</span></span>));
- <span class="comment">// Get one sub-geometry on which you want to use the Atlas, apply texture coordinates</span>
- <span class="comment">// of this geometry to the atlas, and replace the material.</span>
- Geometry geom = scene.getChild(<span class="string"><span class="delimiter">"</span><span class="content">MyGeometry</span><span class="delimiter">"</span></span>);
- atlas.applyCoords(geom);
- geom.setMaterial(mat);</code></pre></div></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({
- apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
- indexName: 'jmonkeyengine',
- inputSelector: '#doc-search',
- debug: false // Set debug to true if you want to inspect the dropdown
- });</script></body></html>
|