material_definitions.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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"><meta name="keywords" content="Material, SDK, MatDef, file, documentation"><title>How to Use Material Definitions (.j3md)</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/material_definitions.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>How to Use Material Definitions (.j3md)</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="#preparing-a-material">Preparing a Material</a></li><li><a href="#using-a-material">Using a Material</a><ul class="sectlevel2"><li><a href="#examples">Examples</a></li></ul></li><li><a href="#creating-a-custom-material-definition">Creating a Custom Material Definition</a></li><li><a href="#related-links">Related Links</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>All Geometries need a Material to be visible. Every Material is based on a Material Definition. Material definitions provide the “logic for the material, and a shader draws the material according to the parameters specified in the definition. The J3MD file abstracts the shader and its configuration away from the user, allowing a simple interface where the user can simply set a few parameters on the material to change its appearance and the way its handled by the shaders.</p></div>
  4. <div class="paragraph"><p>The most common Material Definitions are included in the engine, advanced users can create custom ones. In this case you will also be interested in the <a href="../../jme3/advanced/material_specification.html">in-depth developer specification of the jME3 material system</a>.</p></div>
  5. <div class="paragraph"><p><strong>Example:</strong></p></div>
  6. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Spatial myGeometry = assetManager.loadModel(<span class="string"><span class="delimiter">&quot;</span><span class="content">Models/Teapot/Teapot.j3o</span><span class="delimiter">&quot;</span></span>);
  7. Material mat = <span class="keyword">new</span> Material(assetManager, <span class="comment">// Create new material and...</span>
  8. <span class="string"><span class="delimiter">&quot;</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">&quot;</span></span>); <span class="comment">// ... specify a Material Definition file, here &quot;Unshaded.j3md&quot;!</span>
  9. mat.setColor(<span class="string"><span class="delimiter">&quot;</span><span class="content">Color</span><span class="delimiter">&quot;</span></span>, ColorRGBA.Blue); <span class="comment">// Set one or more material parameters.</span>
  10. myGeometry.setMaterial(mat); <span class="comment">// Use material on this Geometry.</span></code></pre></div></div>
  11. <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>If you use one custom material with certain settings very often, learn about storing material settings in <a href="../../jme3/advanced/j3m_material_files.html">j3m Material Files</a>. You either <a href="../../sdk/material_editing.html">use the jMonkeyEngine SDK to create .j3m files</a> (user-friendly), or you <a href="../../jme3/advanced/j3m_material_files.html">write .j3m files in a text editor</a> (IDE-independent).</p></div></td></tr></table></div></div></div>
  12. <div class="sect1"><h2 id="preparing-a-material">Preparing a Material</h2><div class="sectionbody"><div class="paragraph"><p>In the <a href="../../jme3/advanced/materials_overview.html">Materials Overview</a> list:</p></div>
  13. <div class="olist arabic"><ol class="arabic"><li><p>Choose a Material Definition that has the features that you need.</p><div class="ulist"><ul><li><p>Tip: If you don&#8217;t know, start with <code>Unshaded.j3md</code> or <code>Lighting.j3md</code>.</p></li></ul></div></li><li><p>Look at the applicable parameters of the Material Definition and determine which parameters you need to achieve the desired effect (e.g. “glow or “color). Most parameters are optional!</p></li><li><p>Create and save the necessary Texture files to your <code>assets/Textures</code> directory.</p><div class="ulist"><ul><li><p>E.g. mytex_diffuse.png as ColorMap / DiffuseMap, mytex_normal.png as NormalMap, mytex_alpha.png as AlphaMap, etc…</p></li></ul></div></li><li><p>Determine the required values to achieve the effect that you want.</p><div class="ulist"><ul><li><p>E.g. set colors, floats, booleans, etc…</p></li></ul></div></li></ol></div></div></div>
  14. <div class="sect2"><h3 id="using-a-material">Using a Material</h3><div class="paragraph"><p>In your Java code,</p></div>
  15. <div class="olist arabic"><ol class="arabic"><li><p>Create a Material object based on the chosen Material Definition (.j3md file):</p><div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material mat = <span class="keyword">new</span> Material(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></li><li><p>Configure your Material by setting the appropriate values listed in the <a href="../../jme3/advanced/materials_overview.html">Materials Overview</a> table.</p><div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">mat.setColor(<span class="string"><span class="delimiter">&quot;</span><span class="content">Color</span><span class="delimiter">&quot;</span></span>, ColorRGBA.Yellow ); <span class="comment">// and more</span></code></pre></div></div></li><li><p>Apply your prepared Material to a Geometry:</p><div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">myGeometry.setMaterial(mat);</code></pre></div></div></li><li><p>(Optional) Adjust the texture scale of the mesh:</p><div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">myGeometryMesh.scaleTextureCoordinates(<span class="keyword">new</span> Vector2f(<span class="float">2f</span>, <span class="float">2f</span>));</code></pre></div></div></li></ol></div>
  16. <div class="paragraph"><p>For details see also: <a href="../../jme3/intermediate/how_to_use_materials.html">How to Use Materials</a></p></div>
  17. <div class="sect2"><h3 id="examples">Examples</h3><div class="paragraph"><p>Here are examples of the methods that set the different data types:</p></div>
  18. <div class="ulist"><ul><li><p><code>mat.setColor( “Color, ColorRGBA.White );</code></p></li><li><p><code>mat.setTexture( “ColorMap, assetManager.loadTexture(“Interface/Logo/Monkey.png ));</code></p></li><li><p><code>mat.setFloat( “Shininess, 5f);</code></p></li><li><p><code>mat.setBoolean( “SphereMap, true);</code></p></li><li><p><code>mat.setVector3( “NormalScale, new Vector3f(1f,1f,1f));</code></p></li></ul></div>
  19. <div class="paragraph"><p>A simpled textured material.</p></div>
  20. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material mat = <span class="keyword">new</span> Material(assetManager,
  21. <span class="string"><span class="delimiter">&quot;</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">&quot;</span></span>);
  22. mat.setTexture(<span class="string"><span class="delimiter">&quot;</span><span class="content">ColorMap</span><span class="delimiter">&quot;</span></span>, assetManager.loadTexture(
  23. <span class="string"><span class="delimiter">&quot;</span><span class="content">Interface/Logo/Monkey.jpg</span><span class="delimiter">&quot;</span></span>));</code></pre></div></div>
  24. <div class="paragraph"><p>A textured material with a color bleeding through transparent areas.</p></div>
  25. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material mat = <span class="keyword">new</span> Material(assetManager,
  26. <span class="string"><span class="delimiter">&quot;</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">&quot;</span></span>);
  27. mat.setTexture(<span class="string"><span class="delimiter">&quot;</span><span class="content">ColorMap</span><span class="delimiter">&quot;</span></span>, assetManager.loadTexture(
  28. <span class="string"><span class="delimiter">&quot;</span><span class="content">Textures/ColoredTex/Monkey.png</span><span class="delimiter">&quot;</span></span>));
  29. mat.setColor(<span class="string"><span class="delimiter">&quot;</span><span class="content">Color</span><span class="delimiter">&quot;</span></span>, ColorRGBA.Blue);</code></pre></div></div>
  30. <div class="paragraph"><p>You can test these examples within the following code snippet. It creates a box and applies the material:</p></div>
  31. <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="predefined-type">Box</span> b = <span class="keyword">new</span> <span class="predefined-type">Box</span>(Vector3f.ZERO, <span class="integer">1</span>, <span class="integer">1</span>, <span class="integer">1</span>);
  32. Geometry geom = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">&quot;</span><span class="content">Box</span><span class="delimiter">&quot;</span></span>, b);
  33. <span class="comment">// ... insert Material definition...</span>
  34. geom.setMaterial(mat);
  35. rootNode.attachChild(geom);</code></pre></div></div>
  36. <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>You can find these and other common code snippets in the jMonkeyEngine SDK Code Palette. Drag and drop them into your source code.</p></div></td></tr></table></div></div></div>
  37. <div class="sect1"><h2 id="creating-a-custom-material-definition">Creating a Custom Material Definition</h2><div class="sectionbody"><div class="paragraph"><p>First read the <a href="../../jme3/advanced/material_specification.html">developer specification of the jME3 material system (.j3md,.j3m)</a>. Also check out the <a href="../../jme3/build_from_sources.html">engine source code</a> and have a look at how some Material Definitions are implemented.</p></div>
  38. <div class="paragraph"><p>You can create your own Material Definitions and place them in your project&#8217;s <code>assets/MatDefs</code> directory.</p></div>
  39. <div class="olist arabic"><ol class="arabic"><li><p>Find the existing MatDefs in <code>engine/src/core-data/Common/MatDefs/</code>.</p></li><li><p>Open a Something.j3md file in a text editor. You see that this .j3md file defines Material Parameters and Techniques.</p><div class="ulist"><ul><li><p>Material Parameters are the ones that you set in Materials, as shown in the examples above.</p></li><li><p>The Techniques rely on VertexShaders and FragmentShaders: You find those in the files Something.vert and Something.frag in the same directory.</p></li></ul></div></li><li><p>Learn about GLSL (OpenGL Shading Language) to understand the .vert and .frag syntax, then write your own.</p></li></ol></div></div></div>
  40. <div class="sect1"><h2 id="related-links">Related Links</h2><div class="sectionbody"><div class="ulist"><ul><li><p><a href="../../jme3/advanced/material_specification.html">Developer specification of the jME3 material system (.j3md,.j3m)</a></p></li></ul></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({
  41. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  42. indexName: 'jmonkeyengine',
  43. inputSelector: '#doc-search',
  44. debug: false // Set debug to true if you want to inspect the dropdown
  45. });</script></body></html>