hello_material.html 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 3 Tutorial (6) - Hello Materials :: jMonkeyEngine Docs</title>
  7. <link rel="canonical" href="https://wiki.jmonkeyengine.org/docs/jme3/beginner/hello_material.html">
  8. <link rel="prev" href="hello_input_system.html">
  9. <link rel="next" href="hello_animation.html">
  10. <meta name="keywords" content="documentation, beginner, intro, model, material, color, texture, transparency">
  11. <meta name="generator" content="Antora 2.3.3">
  12. <link rel="stylesheet" href="../../../_/css/site.css">
  13. <meta property="og:image" content="https://wiki.jmonkeyengine.org/_/img/iconx128.png">
  14. <meta property="og:description" content="jMonkeyEngine 3 Tutorial (6) - Hello Materials">
  15. <meta property="og:title" content="jMonkeyEngine Docs">
  16. <link rel="stylesheet" href="../../../_/css/site-extra.css">
  17. <link rel="stylesheet" href="../../../_/css/vendor/docsearch.min.css">
  18. <!-- fetched from https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css -->
  19. <link rel="icon" href="../../../_/img/favicon.ico" type="image/x-icon">
  20. </head>
  21. <body class="article">
  22. <header class="header">
  23. <nav class="navbar">
  24. <div class="navbar-brand">
  25. <a class="navbar-item" href="https://wiki.jmonkeyengine.org">
  26. <img alt="" src="../../../_/img/jme-logo.png" height="32" type="image/x-icon">
  27. </a>
  28. <div class="navbar-item hide-for-print">
  29. <input id="search-input" type="text" placeholder="Search docs">
  30. </div>
  31. <button class="navbar-burger" data-target="topbar-nav">
  32. <span></span>
  33. <span></span>
  34. <span></span>
  35. </button>
  36. </div>
  37. <div id="topbar-nav" class="navbar-menu">
  38. <div class="navbar-end">
  39. <div class="navbar-item theme-switch-wrapper">
  40. <label class="theme-switch" for="checkbox">
  41. <input type="checkbox" id="checkbox" />
  42. <div class="slider round"></div>
  43. </label>
  44. </div>
  45. <a class="navbar-item" href="https://github.com/jmonkeyengine/wiki">Github</a>
  46. </div>
  47. </div>
  48. </nav>
  49. </header>
  50. <div class="body">
  51. <div class="nav-container" data-component="docs" data-version="master">
  52. <aside class="nav">
  53. <div class="panels">
  54. <div class="nav-panel-menu is-active" data-panel="menu">
  55. <nav class="nav-menu">
  56. <h3 class="title"><a href="../../documentation.html">Docs</a></h3>
  57. <ul class="nav-list">
  58. <li class="nav-item" data-depth="0">
  59. <ul class="nav-list">
  60. <li class="nav-item" data-depth="1">
  61. <a class="nav-link" href="../../documentation.html">Getting Started</a>
  62. </li>
  63. <li class="nav-item" data-depth="1">
  64. <a class="nav-link" href="https://javadoc.jmonkeyengine.org/v3.3.2-stable">JavaDoc</a>
  65. </li>
  66. <li class="nav-item" data-depth="1">
  67. <button class="nav-item-toggle"></button>
  68. <a class="nav-link" href="../../jme3.html">jMonkeyEngine 3</a>
  69. <ul class="nav-list">
  70. <li class="nav-item" data-depth="2">
  71. <button class="nav-item-toggle"></button>
  72. <span class="nav-text">Beginner Tutorials</span>
  73. <ul class="nav-list">
  74. <li class="nav-item" data-depth="3">
  75. <a class="nav-link" href="hello_simpleapplication.html">Hello SimpleApplication</a>
  76. </li>
  77. <li class="nav-item" data-depth="3">
  78. <a class="nav-link" href="hello_node.html">Hello Node</a>
  79. </li>
  80. <li class="nav-item" data-depth="3">
  81. <a class="nav-link" href="hello_asset.html">Hello Asset</a>
  82. </li>
  83. <li class="nav-item" data-depth="3">
  84. <a class="nav-link" href="hello_main_event_loop.html">Hello Main Event Loop</a>
  85. </li>
  86. <li class="nav-item" data-depth="3">
  87. <a class="nav-link" href="hello_input_system.html">Hello Input System</a>
  88. </li>
  89. <li class="nav-item is-current-page" data-depth="3">
  90. <a class="nav-link" href="hello_material.html">Hello Material</a>
  91. </li>
  92. <li class="nav-item" data-depth="3">
  93. <a class="nav-link" href="hello_animation.html">Hello Animation</a>
  94. </li>
  95. <li class="nav-item" data-depth="3">
  96. <a class="nav-link" href="hello_picking.html">Hello Picking</a>
  97. </li>
  98. <li class="nav-item" data-depth="3">
  99. <a class="nav-link" href="hello_collision.html">Hello Collision</a>
  100. </li>
  101. <li class="nav-item" data-depth="3">
  102. <a class="nav-link" href="hello_terrain.html">Hello Terrain</a>
  103. </li>
  104. <li class="nav-item" data-depth="3">
  105. <a class="nav-link" href="hello_audio.html">Hello Audio</a>
  106. </li>
  107. <li class="nav-item" data-depth="3">
  108. <a class="nav-link" href="hello_effects.html">Hello Effects</a>
  109. </li>
  110. <li class="nav-item" data-depth="3">
  111. <a class="nav-link" href="hello_physics.html">Hello Physics</a>
  112. </li>
  113. </ul>
  114. </li>
  115. <li class="nav-item" data-depth="2">
  116. <button class="nav-item-toggle"></button>
  117. <span class="nav-text">Intermediate Tutorials</span>
  118. <ul class="nav-list">
  119. <li class="nav-item" data-depth="3">
  120. <button class="nav-item-toggle"></button>
  121. <span class="nav-text">Concepts</span>
  122. <ul class="nav-list">
  123. <li class="nav-item" data-depth="4">
  124. <a class="nav-link" href="../intermediate/best_practices.html">Best Practices</a>
  125. </li>
  126. <li class="nav-item" data-depth="4">
  127. <a class="nav-link" href="../intermediate/simpleapplication.html">Simple Application</a>
  128. </li>
  129. <li class="nav-item" data-depth="4">
  130. <a class="nav-link" href="../features.html">Features</a>
  131. </li>
  132. <li class="nav-item" data-depth="4">
  133. <a class="nav-link" href="../intermediate/optimization.html">Optimization</a>
  134. </li>
  135. <li class="nav-item" data-depth="4">
  136. <a class="nav-link" href="../faq.html">FAQ</a>
  137. </li>
  138. </ul>
  139. </li>
  140. <li class="nav-item" data-depth="3">
  141. <button class="nav-item-toggle"></button>
  142. <span class="nav-text">Math Concepts</span>
  143. <ul class="nav-list">
  144. <li class="nav-item" data-depth="4">
  145. <a class="nav-link" href="../math_for_dummies.html">Math For Dummies</a>
  146. </li>
  147. <li class="nav-item" data-depth="4">
  148. <a class="nav-link" href="../intermediate/math.html">Math</a>
  149. </li>
  150. <li class="nav-item" data-depth="4">
  151. <a class="nav-link" href="../math.html">More Math</a>
  152. </li>
  153. <li class="nav-item" data-depth="4">
  154. <a class="nav-link" href="../rotate.html">Rotate</a>
  155. </li>
  156. <li class="nav-item" data-depth="4">
  157. <a class="nav-link" href="../math_video_tutorials.html">Math Video Tutorials</a>
  158. </li>
  159. </ul>
  160. </li>
  161. <li class="nav-item" data-depth="3">
  162. <button class="nav-item-toggle"></button>
  163. <span class="nav-text">3D Graphics Concepts</span>
  164. <ul class="nav-list">
  165. <li class="nav-item" data-depth="4">
  166. <a class="nav-link" href="../intermediate/multi-media_asset_pipeline.html">Multi-Media Asset Pipeline</a>
  167. </li>
  168. <li class="nav-item" data-depth="4">
  169. <a class="nav-link" href="../scenegraph_for_dummies.html">Scenegraph for Dummies</a>
  170. </li>
  171. <li class="nav-item" data-depth="4">
  172. <a class="nav-link" href="hellovector.html">Hello Vector</a>
  173. </li>
  174. <li class="nav-item" data-depth="4">
  175. <a class="nav-link" href="../terminology.html">Terminology</a>
  176. </li>
  177. <li class="nav-item" data-depth="4">
  178. <a class="nav-link" href="../intermediate/how_to_use_materials.html">How to Use Materials</a>
  179. </li>
  180. <li class="nav-item" data-depth="4">
  181. <a class="nav-link" href="../intermediate/transparency_sorting.html">Transparency and Sorting</a>
  182. </li>
  183. <li class="nav-item" data-depth="4">
  184. <a class="nav-link" href="../external/blender.html">Importing from Blender</a>
  185. </li>
  186. <li class="nav-item" data-depth="4">
  187. <a class="nav-link" href="../external/3dsmax.html">Importing from 3DS Max</a>
  188. </li>
  189. </ul>
  190. </li>
  191. </ul>
  192. </li>
  193. </ul>
  194. </li>
  195. <li class="nav-item" data-depth="1">
  196. <a class="nav-link" href="../../logo.html">Logo Usage</a>
  197. </li>
  198. <li class="nav-item" data-depth="1">
  199. <a class="nav-link" href="../../bsd_license.html">License</a>
  200. </li>
  201. <li class="nav-item" data-depth="1">
  202. <a class="nav-link" href="../../github_tips.html">Github Tips</a>
  203. </li>
  204. </ul>
  205. </li>
  206. <li class="nav-item" data-depth="0">
  207. <button class="nav-item-toggle"></button>
  208. <span class="nav-text">SDK</span>
  209. <ul class="nav-list">
  210. <li class="nav-item" data-depth="1">
  211. <a class="nav-link" href="../../sdk.html">jMonkeyEngine SDK</a>
  212. </li>
  213. </ul>
  214. </li>
  215. </ul>
  216. </nav>
  217. </div>
  218. <div class="nav-panel-explore" data-panel="explore">
  219. <div class="context">
  220. <span class="title">Docs</span>
  221. <span class="version">master</span>
  222. </div>
  223. <ul class="components">
  224. <li class="component is-current">
  225. <span class="title">Docs</span>
  226. <ul class="versions">
  227. <li class="version is-current is-latest">
  228. <a href="../../documentation.html">master</a>
  229. </li>
  230. </ul>
  231. </li>
  232. <li class="component">
  233. <span class="title">Wiki UI</span>
  234. <ul class="versions">
  235. <li class="version is-latest">
  236. <a href="../../../wiki-ui/index.html">master</a>
  237. </li>
  238. </ul>
  239. </li>
  240. </ul>
  241. </div>
  242. </div>
  243. </aside>
  244. </div>
  245. <main class="article">
  246. <div class="toolbar" role="navigation">
  247. <button class="nav-toggle"></button>
  248. <nav class="breadcrumbs" aria-label="breadcrumbs">
  249. <ul>
  250. <li><a href="../../documentation.html">Docs</a></li>
  251. <li><a href="../../jme3.html">jMonkeyEngine 3</a></li>
  252. <li>Beginner Tutorials</li>
  253. <li><a href="hello_material.html">Hello Material</a></li>
  254. </ul>
  255. </nav>
  256. <div class="edit-this-page"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/docs/modules/ROOT/pages/jme3/beginner/hello_material.adoc">Edit this Page</a></div>
  257. </div>
  258. <div class="content">
  259. <article class="doc">
  260. <h1 class="page">jMonkeyEngine 3 Tutorial (6) - Hello Materials</h1>
  261. <div id="preamble">
  262. <div class="sectionbody">
  263. <div class="paragraph">
  264. <p>Previous: <a href="hello_input_system.html" class="page">Hello Input System</a>,
  265. Next: <a href="hello_animation.html" class="page">Hello Animation</a></p>
  266. </div>
  267. <div class="paragraph">
  268. <p>The term Material includes everything that influences what the surface of a 3D model looks like: The color, texture, shininess, and opacity/transparency. Plain coloring is covered in <a href="hello_node.html" class="page">Hello Node</a>. Loading models that come with materials is covered in <a href="hello_asset.html" class="page">Hello Asset</a>. In this tutorial you learn to create and use custom JME3 Material Definitions.</p>
  269. </div>
  270. <div class="imageblock text-center">
  271. <div class="content">
  272. <img src="../../_images/jme3/beginner/beginner-materials.png" alt="beginner-materials.png" width="320" height="240">
  273. </div>
  274. </div>
  275. <div class="admonitionblock tip">
  276. <table>
  277. <tr>
  278. <td class="icon">
  279. <i class="fa icon-tip" title="Tip"></i>
  280. </td>
  281. <td class="content">
  282. <div class="paragraph">
  283. <p>To use the example assets in a new jMonkeyEngine SDK project, right-click your project, select “Properties, go to “Libraries, press “Add Library and add the “jme3-test-data library.</p>
  284. </div>
  285. </td>
  286. </tr>
  287. </table>
  288. </div>
  289. </div>
  290. </div>
  291. <div class="sect1">
  292. <h2 id="sample-code"><a class="anchor" href="#sample-code"></a>Sample Code</h2>
  293. <div class="sectionbody">
  294. <div class="listingblock">
  295. <div class="content">
  296. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">package jme3test.helloworld;
  297. import com.jme3.app.SimpleApplication;
  298. import com.jme3.light.DirectionalLight;
  299. import com.jme3.material.Material;
  300. import com.jme3.material.RenderState.BlendMode;
  301. import com.jme3.math.ColorRGBA;
  302. import com.jme3.math.Vector3f;
  303. import com.jme3.renderer.queue.RenderQueue.Bucket;
  304. import com.jme3.scene.Geometry;
  305. import com.jme3.scene.shape.Box;
  306. import com.jme3.scene.shape.Sphere;
  307. import com.jme3.texture.Texture;
  308. import com.jme3.util.TangentBinormalGenerator;
  309. /** Sample 6 - how to give an object's surface a material and texture.
  310. * How to make objects transparent. How to make bumpy and shiny surfaces. */
  311. public class HelloMaterial extends SimpleApplication {
  312. public static void main(String[] args) {
  313. HelloMaterial app = new HelloMaterial();
  314. app.start();
  315. }
  316. @Override
  317. public void simpleInitApp() {
  318. /** A simple textured cube -- in good MIP map quality. */
  319. Box cube1Mesh = new Box( 1f,1f,1f);
  320. Geometry cube1Geo = new Geometry("My Textured Box", cube1Mesh);
  321. cube1Geo.setLocalTranslation(new Vector3f(-3f,1.1f,0f));
  322. Material cube1Mat = new Material(assetManager,
  323. "Common/MatDefs/Misc/Unshaded.j3md");
  324. Texture cube1Tex = assetManager.loadTexture(
  325. "Interface/Logo/Monkey.jpg");
  326. cube1Mat.setTexture("ColorMap", cube1Tex);
  327. cube1Geo.setMaterial(cube1Mat);
  328. rootNode.attachChild(cube1Geo);
  329. /** A translucent/transparent texture, similar to a window frame. */
  330. Box cube2Mesh = new Box( 1f,1f,0.01f);
  331. Geometry cube2Geo = new Geometry("window frame", cube2Mesh);
  332. Material cube2Mat = new Material(assetManager,
  333. "Common/MatDefs/Misc/Unshaded.j3md");
  334. cube2Mat.setTexture("ColorMap",
  335. assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
  336. cube2Mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  337. cube2Geo.setQueueBucket(Bucket.Transparent);
  338. cube2Geo.setMaterial(cube2Mat);
  339. rootNode.attachChild(cube2Geo);
  340. /** A bumpy rock with a shiny light effect.*/
  341. Sphere sphereMesh = new Sphere(32,32, 2f);
  342. Geometry sphereGeo = new Geometry("Shiny rock", sphereMesh);
  343. sphereMesh.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
  344. TangentBinormalGenerator.generate(sphereMesh); // for lighting effect
  345. Material sphereMat = new Material(assetManager,
  346. "Common/MatDefs/Light/Lighting.j3md");
  347. sphereMat.setTexture("DiffuseMap",
  348. assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
  349. sphereMat.setTexture("NormalMap",
  350. assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
  351. sphereMat.setBoolean("UseMaterialColors",true);
  352. sphereMat.setColor("Diffuse",ColorRGBA.White);
  353. sphereMat.setColor("Specular",ColorRGBA.White);
  354. sphereMat.setFloat("Shininess", 64f); // [0,128]
  355. sphereGeo.setMaterial(sphereMat);
  356. sphereGeo.setLocalTranslation(0,2,-2); // Move it a bit
  357. sphereGeo.rotate(1.6f, 0, 0); // Rotate it a bit
  358. rootNode.attachChild(sphereGeo);
  359. /** Must add a light to make the lit object visible! */
  360. DirectionalLight sun = new DirectionalLight();
  361. sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
  362. sun.setColor(ColorRGBA.White);
  363. rootNode.addLight(sun);
  364. }
  365. }</code></pre>
  366. </div>
  367. </div>
  368. <div class="paragraph">
  369. <p>You should see</p>
  370. </div>
  371. <div class="ulist">
  372. <ul>
  373. <li>
  374. <p>Left – A cube with a brown monkey texture.</p>
  375. </li>
  376. <li>
  377. <p>Right – A translucent monkey picture in front of a shiny bumpy rock.</p>
  378. </li>
  379. </ul>
  380. </div>
  381. <div class="paragraph">
  382. <p>Move around with the WASD keys to have a closer look at the translucency, and the rock&#8217;s bumpiness.</p>
  383. </div>
  384. </div>
  385. </div>
  386. <div class="sect1">
  387. <h2 id="simple-unshaded-texture"><a class="anchor" href="#simple-unshaded-texture"></a>Simple Unshaded Texture</h2>
  388. <div class="sectionbody">
  389. <div class="paragraph">
  390. <p>Typically you want to give objects in your scene textures: It can be rock, grass, brick, wood, water, metal, paper… A texture is a normal image file in JPG or PNG format. In this example, you create a box with a simple unshaded Monkey texture as material.</p>
  391. </div>
  392. <div class="listingblock">
  393. <div class="content">
  394. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> /** A simple textured cube -- in good MIP map quality. */
  395. Box cube1Mesh = new Box( 1f,1f,1f);
  396. Geometry cube1Geo = new Geometry("My Textured Box", cube1Mesh);
  397. cube1Geo.setLocalTranslation(new Vector3f(-3f,1.1f,0f));
  398. Material cube1Mat = new Material(assetManager,
  399. "Common/MatDefs/Misc/Unshaded.j3md");
  400. Texture cube1Tex = assetManager.loadTexture(
  401. "Interface/Logo/Monkey.jpg");
  402. cube1Mat.setTexture("ColorMap", cube1Tex);
  403. cube1Geo.setMaterial(cube1Mat);
  404. rootNode.attachChild(cube1Geo);</code></pre>
  405. </div>
  406. </div>
  407. <div class="paragraph">
  408. <p>Here is what we did: to create a textured box:</p>
  409. </div>
  410. <div class="olist arabic">
  411. <ol class="arabic">
  412. <li>
  413. <p>Create a Geometry <code>cube1Geo</code> from a Box mesh <code>cube1Mesh</code>.</p>
  414. </li>
  415. <li>
  416. <p>Create a Material <code>cube1Mat</code> based on jME3&#8217;s default <code>Unshaded.j3md</code> material definition.</p>
  417. </li>
  418. <li>
  419. <p>Create a texture <code>cube1Tex</code> from the <code>Monkey.jpg</code> file in the <code>assets/Interface/Logo/</code> directory of the project.</p>
  420. </li>
  421. <li>
  422. <p>Load the texture <code>cube1Tex</code> into the <code>ColorMap</code> layer of the material <code>cube1Mat</code>.</p>
  423. </li>
  424. <li>
  425. <p>Apply the material to the cube, and attach the cube to the rootnode.</p>
  426. </li>
  427. </ol>
  428. </div>
  429. </div>
  430. </div>
  431. <div class="sect1">
  432. <h2 id="transparent-unshaded-texture"><a class="anchor" href="#transparent-unshaded-texture"></a>Transparent Unshaded Texture</h2>
  433. <div class="sectionbody">
  434. <div class="paragraph">
  435. <p><code>Monkey.png</code> is the same texture as <code>Monkey.jpg</code>, but with an added alpha channel. The alpha channel allows you to specify which areas of the texture you want to be opaque or transparent: Black areas of the alpha channel remain opaque, gray areas become translucent, and white areas become transparent.</p>
  436. </div>
  437. <div class="paragraph">
  438. <p>For a partially translucent/transparent texture, you need:</p>
  439. </div>
  440. <div class="ulist">
  441. <ul>
  442. <li>
  443. <p>A Texture with alpha channel</p>
  444. </li>
  445. <li>
  446. <p>A Texture with blend mode of <code>BlendMode.Alpha</code></p>
  447. </li>
  448. <li>
  449. <p>A Geometry in the <code>Bucket.Transparent</code> render bucket.<br>
  450. This bucket ensures that the transparent object is drawn on top of objects behind it, and they show up correctly under the transparent parts.</p>
  451. </li>
  452. </ul>
  453. </div>
  454. <div class="listingblock">
  455. <div class="content">
  456. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> /** A translucent/transparent texture, similar to a window frame. */
  457. Box cube2Mesh = new Box( 1f,1f,0.01f);
  458. Geometry cube2Geo = new Geometry("window frame", cube2Mesh);
  459. Material cube2Mat = new Material(assetManager,
  460. "Common/MatDefs/Misc/Unshaded.j3md");
  461. cube2Mat.setTexture("ColorMap",
  462. assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
  463. cube2Mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // !
  464. cube2Geo.setQueueBucket(Bucket.Transparent); // !
  465. cube2Geo.setMaterial(cube2Mat);
  466. rootNode.attachChild(cube2Geo);</code></pre>
  467. </div>
  468. </div>
  469. <div class="paragraph">
  470. <p>For non-transparent objects, the drawing order is not so important, because the z-buffer already keeps track of whether a pixel is behind something else or not, and the color of an opaque pixel doesn&#8217;t depend on the pixels under it, this is why opaque Geometries can be drawn in any order.</p>
  471. </div>
  472. <div class="paragraph">
  473. <p>What you did for the transparent texture is the same as before, with only one added step for the transparency.</p>
  474. </div>
  475. <div class="olist arabic">
  476. <ol class="arabic">
  477. <li>
  478. <p>Create a Geometry <code>cube2Geo</code> from a Box mesh <code>cube2Mesh</code>. This Box Geometry is flat upright box (because z=0.01f).</p>
  479. </li>
  480. <li>
  481. <p>Create a Material <code>cube2Mat</code> based on jME3&#8217;s default <code>Unshaded.j3md</code> material definition.</p>
  482. </li>
  483. <li>
  484. <p>Create a texture <code>cube2Tex</code> from the <code>Monkey.png</code> file in the <code>assets/Textures/ColoredTex/</code> directory of the project. This PNG file must have an alpha layer.</p>
  485. </li>
  486. <li>
  487. <p><strong>Activate transparency in the material by setting the blend mode to Alpha.</strong></p>
  488. </li>
  489. <li>
  490. <p><strong>Set the QueueBucket of the Geometry to <code>Bucket.Transparent</code>.</strong></p>
  491. </li>
  492. <li>
  493. <p>Load the texture <code>cube2Tex</code> into the <code>ColorMap</code> layer of the material <code>cube2Mat</code>.</p>
  494. </li>
  495. <li>
  496. <p>Apply the material to the cube, and attach the cube to the rootnode.</p>
  497. </li>
  498. </ol>
  499. </div>
  500. <div class="admonitionblock tip">
  501. <table>
  502. <tr>
  503. <td class="icon">
  504. <i class="fa icon-tip" title="Tip"></i>
  505. </td>
  506. <td class="content">
  507. <div class="paragraph">
  508. <p>Learn more about creating PNG images with an alpha layer in the help system of your graphic editor.</p>
  509. </div>
  510. </td>
  511. </tr>
  512. </table>
  513. </div>
  514. </div>
  515. </div>
  516. <div class="sect1">
  517. <h2 id="shininess-and-bumpiness"><a class="anchor" href="#shininess-and-bumpiness"></a>Shininess and Bumpiness</h2>
  518. <div class="sectionbody">
  519. <div class="paragraph">
  520. <p>But textures are not all. Have a close look at the shiny sphere – you cannot get such a nice bumpy material with just a plain texture. You see that JME3 also supports so-called Phong-illuminated materials:</p>
  521. </div>
  522. <div class="paragraph">
  523. <p>In a lit material, the standard texture layer is refered to as <em>DiffuseMap</em>, any material can use this layer. A lit material can additionally have lighting effects such as <em>Shininess</em> used together with the <em>SpecularMap</em> layer and <em>Specular</em> color. And you can even get a realistically bumpy or cracked surface with help of the <em>NormalMap</em> layer.</p>
  524. </div>
  525. <div class="paragraph">
  526. <p>Let&#8217;s have a look at the part of the code example where you create the shiny bumpy rock.</p>
  527. </div>
  528. <div class="olist arabic">
  529. <ol class="arabic">
  530. <li>
  531. <p>Create a Geometry from a Sphere shape. Note that this shape is a normal smooth sphere mesh.</p>
  532. <div class="listingblock">
  533. <div class="content">
  534. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> Sphere sphereMesh = new Sphere(32,32, 2f);
  535. Geometry sphereGeo = new Geometry("Shiny rock", sphereMesh);</code></pre>
  536. </div>
  537. </div>
  538. <div class="olist loweralpha">
  539. <ol class="loweralpha" type="a">
  540. <li>
  541. <p>(Only for Spheres) Change the sphere&#8217;s TextureMode to make the square texture project better onto the sphere.</p>
  542. <div class="listingblock">
  543. <div class="content">
  544. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> sphereMesh.setTextureMode(Sphere.TextureMode.Projected);</code></pre>
  545. </div>
  546. </div>
  547. </li>
  548. <li>
  549. <p>You must generate TangentBinormals for the mesh so you can use the NormalMap layer of the texture.</p>
  550. <div class="listingblock">
  551. <div class="content">
  552. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> TangentBinormalGenerator.generate(sphereMesh);</code></pre>
  553. </div>
  554. </div>
  555. </li>
  556. </ol>
  557. </div>
  558. </li>
  559. <li>
  560. <p>Create a material based on the <code>Lighting.j3md</code> default material.</p>
  561. <div class="listingblock">
  562. <div class="content">
  563. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> Material sphereMat = new Material(assetManager,
  564. "Common/MatDefs/Light/Lighting.j3md");</code></pre>
  565. </div>
  566. </div>
  567. <div class="olist loweralpha">
  568. <ol class="loweralpha" type="a">
  569. <li>
  570. <p>Set a standard rocky texture in the <code>DiffuseMap</code> layer.</p>
  571. <div class="imageblock text-right">
  572. <div class="content">
  573. <img src="https://github.com/jMonkeyEngine/jmonkeyengine/raw/445f7ed010199d30c484fe75bacef4b87f2eb38e/jme3-testdata/src/main/resources/Textures/Terrain/Pond/Pond.jpg" alt="Pond.jpg" width="64" height="64">
  574. </div>
  575. </div>
  576. <div class="listingblock">
  577. <div class="content">
  578. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> sphereMat.setTexture("DiffuseMap",
  579. assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));</code></pre>
  580. </div>
  581. </div>
  582. </li>
  583. <li>
  584. <p>Set the <code>NormalMap</code> layer that contains the bumpiness. The NormalMap was generated for this particular DiffuseMap with a special tool (e.g. Blender).</p>
  585. <div class="imageblock text-right">
  586. <div class="content">
  587. <img src="https://github.com/jMonkeyEngine/jmonkeyengine/raw/445f7ed010199d30c484fe75bacef4b87f2eb38e/jme3-testdata/src/main/resources/Textures/Terrain/Pond/Pond_normal.png" alt="Pond_normal.png" width="64" height="64">
  588. </div>
  589. </div>
  590. <div class="listingblock">
  591. <div class="content">
  592. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> sphereMat.setTexture("NormalMap",
  593. assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));</code></pre>
  594. </div>
  595. </div>
  596. </li>
  597. <li>
  598. <p>Set the Material&#8217;s Shininess to a value between 1 and 128. For a rock, a low fuzzy shininess is appropriate. Use material colors to define the shiny Specular color.</p>
  599. <div class="listingblock">
  600. <div class="content">
  601. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> sphereMat.setBoolean("UseMaterialColors",true);
  602. sphereMat.setColor("Diffuse",ColorRGBA.White); // minimum material color
  603. sphereMat.setColor("Specular",ColorRGBA.White); // for shininess
  604. sphereMat.setFloat("Shininess", 64f); // [1,128] for shininess</code></pre>
  605. </div>
  606. </div>
  607. </li>
  608. </ol>
  609. </div>
  610. </li>
  611. <li>
  612. <p>Assign your newly created material to the Geometry.</p>
  613. <div class="listingblock">
  614. <div class="content">
  615. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> sphereGeo.setMaterial(sphereMat);</code></pre>
  616. </div>
  617. </div>
  618. </li>
  619. <li>
  620. <p>Let&#8217;s move and rotate the geometry a bit to position it better.</p>
  621. <div class="listingblock">
  622. <div class="content">
  623. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> sphereGeo.setLocalTranslation(0,2,-2); // Move it a bit
  624. sphereGeo.rotate(1.6f, 0, 0); // Rotate it a bit
  625. rootNode.attachChild(sphereGeo);</code></pre>
  626. </div>
  627. </div>
  628. </li>
  629. </ol>
  630. </div>
  631. <div class="paragraph">
  632. <p>Remember that any Lighting.j3md-based material requires a light source, as shown in the full code sample above.</p>
  633. </div>
  634. <div class="admonitionblock tip">
  635. <table>
  636. <tr>
  637. <td class="icon">
  638. <i class="fa icon-tip" title="Tip"></i>
  639. </td>
  640. <td class="content">
  641. <div class="paragraph">
  642. <p>To deactivate Shininess, do not set <code>Shininess</code> to 0, but instead set the <code>Specular</code> color to <code>ColorRGBA.Black</code>.</p>
  643. </div>
  644. </td>
  645. </tr>
  646. </table>
  647. </div>
  648. </div>
  649. </div>
  650. <div class="sect1">
  651. <h2 id="default-material-definitions"><a class="anchor" href="#default-material-definitions"></a>Default Material Definitions</h2>
  652. <div class="sectionbody">
  653. <div class="paragraph">
  654. <p>As you have seen, you can find the following default materials in <code>jme/core-data/Common/MatDefs/…</code>.</p>
  655. </div>
  656. <table class="tableblock frame-all grid-all stretch">
  657. <colgroup>
  658. <col style="width: 20%;">
  659. <col style="width: 40%;">
  660. <col style="width: 40%;">
  661. </colgroup>
  662. <thead>
  663. <tr>
  664. <th class="tableblock halign-left valign-top">Default Definition</th>
  665. <th class="tableblock halign-left valign-top">Usage</th>
  666. <th class="tableblock halign-left valign-top">Parameters</th>
  667. </tr>
  668. </thead>
  669. <tbody>
  670. <tr>
  671. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  672. <p><code>Misc/Unshaded.j3md</code></p>
  673. </div></div></td>
  674. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  675. <p>Colored: Use with mat.setColor() and ColorRGBA.<br>
  676. Textured: Use with mat.setTexture() and Texture.</p>
  677. </div></div></td>
  678. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  679. <p>Color : Color<br>
  680. ColorMap : Texture2D</p>
  681. </div></div></td>
  682. </tr>
  683. <tr>
  684. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  685. <p><code>Light/Lighting.j3md</code></p>
  686. </div></div></td>
  687. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  688. <p>Use with shiny Textures, Bump- and NormalMaps textures.<br>
  689. Requires a light source.</p>
  690. </div></div></td>
  691. <td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
  692. <p>Ambient, Diffuse, Specular : Color<br>
  693. DiffuseMap, NormalMap, SpecularMap : Texture2D<br>
  694. Shininess : Float</p>
  695. </div></div></td>
  696. </tr>
  697. </tbody>
  698. </table>
  699. <div class="paragraph">
  700. <p>For a game, you create custom Materials based on these existing MaterialDefintions – as you have just seen in the example with the shiny rock&#8217;s material.</p>
  701. </div>
  702. </div>
  703. </div>
  704. <div class="sect1">
  705. <h2 id="exercises"><a class="anchor" href="#exercises"></a>Exercises</h2>
  706. <div class="sectionbody">
  707. <div class="sect2">
  708. <h3 id="exercise-1-custom-j3m-material"><a class="anchor" href="#exercise-1-custom-j3m-material"></a>Exercise 1: Custom .j3m Material</h3>
  709. <div class="paragraph">
  710. <p>Look at the shiny rocky sphere above again. It takes several lines to create and set the Material.</p>
  711. </div>
  712. <div class="ulist">
  713. <ul>
  714. <li>
  715. <p>Note how it loads the <code>Lighting.j3md</code> Material definition.</p>
  716. </li>
  717. <li>
  718. <p>Note how it sets the <code>DiffuseMap</code> and <code>NormalMap</code> to a texture path.</p>
  719. </li>
  720. <li>
  721. <p>Note how it activates <code>UseMaterialColors</code> and sets <code>Specular</code> and <code>Diffuse</code> to 4 float values (RGBA color).</p>
  722. </li>
  723. <li>
  724. <p>Note how it sets <code>Shininess</code> to 64.</p>
  725. </li>
  726. </ul>
  727. </div>
  728. <div class="paragraph">
  729. <p>If you want to use one custom material for several models, you can store it in a .j3m file, and save a few lines of code every time.</p>
  730. </div>
  731. <div class="paragraph">
  732. <p>You create a j3m file as follows:</p>
  733. </div>
  734. <div class="olist arabic">
  735. <ol class="arabic">
  736. <li>
  737. <p>Create a plain text file <code>assets/Materials/MyCustomMaterial.j3m</code> in your project directory, with the following content:</p>
  738. <div class="listingblock">
  739. <div class="content">
  740. <pre class="highlightjs highlight"><code>Material My shiny custom material : Common/MatDefs/Light/Lighting.j3md {
  741. MaterialParameters {
  742. DiffuseMap : Textures/Terrain/Pond/Pond.jpg
  743. NormalMap : Textures/Terrain/Pond/Pond_normal.png
  744. UseMaterialColors : true
  745. Specular : 1.0 1.0 1.0 1.0
  746. Diffuse : 1.0 1.0 1.0 1.0
  747. Shininess : 64.0
  748. }
  749. }</code></pre>
  750. </div>
  751. </div>
  752. <div class="ulist">
  753. <ul>
  754. <li>
  755. <p>Note that <code>Material</code> is a fixed keyword.</p>
  756. </li>
  757. <li>
  758. <p>Note that <code>My shiny custom material</code> is a String that you can choose to describe the material.</p>
  759. </li>
  760. <li>
  761. <p>Note how the code sets all the same properties as before!</p>
  762. </li>
  763. </ul>
  764. </div>
  765. </li>
  766. <li>
  767. <p>In the code sample, comment out the eight lines that have <code>sphereMat</code> in them.</p>
  768. </li>
  769. <li>
  770. <p>Below this line, add the following line:</p>
  771. <div class="listingblock">
  772. <div class="content">
  773. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">sphereGeo.setMaterial((Material) assetManager.loadMaterial(
  774. "Materials/MyCustomMaterial.j3m"));</code></pre>
  775. </div>
  776. </div>
  777. </li>
  778. <li>
  779. <p>Run the app. The result is the same.</p>
  780. </li>
  781. </ol>
  782. </div>
  783. <div class="paragraph">
  784. <p>Using this new custom material <code>MyCustomMaterial.j3m</code> only takes one line. You have replaced the eight lines of an on-the-fly material definition with one line that loads a custom material from a file. Using .j3m files is very handy if you use the same material often.</p>
  785. </div>
  786. </div>
  787. <div class="sect2">
  788. <h3 id="exercise-2-bumpiness-and-shininess"><a class="anchor" href="#exercise-2-bumpiness-and-shininess"></a>Exercise 2: Bumpiness and Shininess</h3>
  789. <div class="paragraph">
  790. <p>Go back to the bumpy rock sample above:</p>
  791. </div>
  792. <div class="olist arabic">
  793. <ol class="arabic">
  794. <li>
  795. <p>Comment out the DiffuseMap line, and run the app. (Uncomment it again.)</p>
  796. <div class="ulist">
  797. <ul>
  798. <li>
  799. <p>Which property of the rock is lost?</p>
  800. </li>
  801. </ul>
  802. </div>
  803. </li>
  804. <li>
  805. <p>Comment out the NormalMap line, and run the app. (Uncomment it again.)</p>
  806. <div class="ulist">
  807. <ul>
  808. <li>
  809. <p>Which property of the rock is lost?</p>
  810. </li>
  811. </ul>
  812. </div>
  813. </li>
  814. <li>
  815. <p>Change the value of Shininess to values like 0, 63, 127.</p>
  816. <div class="ulist">
  817. <ul>
  818. <li>
  819. <p>What aspect of the Shininess changes?</p>
  820. </li>
  821. </ul>
  822. </div>
  823. </li>
  824. </ol>
  825. </div>
  826. </div>
  827. </div>
  828. </div>
  829. <div class="sect1">
  830. <h2 id="conclusion"><a class="anchor" href="#conclusion"></a>Conclusion</h2>
  831. <div class="sectionbody">
  832. <div class="paragraph">
  833. <p>You have learned how to create a Material, specify its properties, and use it on a Geometry. You know how to load an image file (.png, .jpg) as texture into a material. You know to save texture files in a subfolder of your project&#8217;s <code>assets/Textures/</code> directory.</p>
  834. </div>
  835. <div class="paragraph">
  836. <p>You have also learned that a material can be stored in a .j3m file. The file references a built-in MaterialDefinition and specifies values for properties of that MaterialDefinition. You know to save your custom .j3m files in your project&#8217;s <code>assets/Materials/</code> directory.</p>
  837. </div>
  838. <div class="paragraph">
  839. <p>Now that you know how to load models and how to assign good-looking materials to them, let&#8217;s have a look at how to animate models in the next chapter, <a href="hello_animation.html" class="page">Hello Animation</a>.</p>
  840. </div>
  841. <hr>
  842. <div class="paragraph">
  843. <p>See also</p>
  844. </div>
  845. <div class="ulist">
  846. <ul>
  847. <li>
  848. <p><a href="../intermediate/how_to_use_materials.html" class="page">How to Use Materials</a></p>
  849. </li>
  850. <li>
  851. <p><a href="../../sdk/material_editing.html" class="page">Material Editing</a></p>
  852. </li>
  853. <li>
  854. <p><a href="https://hub.jmonkeyengine.org/t/jmonkeyengine3-material-system-full-explanation/12947">Materials</a> forum thread</p>
  855. </li>
  856. <li>
  857. <p><a href="http://www.youtube.com/watch?v=Feu3-mrpolc">Video Tutorial: Editing and Assigning Materials to Models in jMonkeyEngine SDK (from 2010, is there a newer one?</a></p>
  858. </li>
  859. <li>
  860. <p><a href="https://www.blender.org/support/tutorials/">Creating textures in Blender</a></p>
  861. </li>
  862. <li>
  863. <p><a href="http://www.shaders.co.uk/ifw2_textures/whatsin10.htm">Various Material screenshots</a> (Not done with JME3, this is just to show the fantastic range of Material parameters in the hands of an expert, until we have a JME3 demo for it.)</p>
  864. </li>
  865. </ul>
  866. </div>
  867. </div>
  868. </div>
  869. <nav class="pagination">
  870. <span class="prev"><a href="hello_input_system.html">Hello Input System</a></span>
  871. <span class="next"><a href="hello_animation.html">Hello Animation</a></span>
  872. </nav>
  873. </article>
  874. <aside class="toc sidebar" data-title="Contents" data-levels="2">
  875. <div class="toc-menu"></div>
  876. </aside>
  877. </div>
  878. </main>
  879. </div>
  880. <footer class="footer">
  881. <p>Copyright 2020 jMonkeyEngine Wiki Contributors. Licensed BSD-3.</p>
  882. </footer>
  883. <script src="../../../_/js/vendor/docsearch.min.js"></script>
  884. <!-- fetched from https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js -->
  885. <script>
  886. var search = docsearch({
  887. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  888. indexName: 'jmonkeyengine',
  889. inputSelector: '#search-input',
  890. autocompleteOptions: { hint: false, keyboardShortcuts: ['s'] },
  891. algoliaOptions: { hitsPerPage: 10 }
  892. }).autocomplete
  893. search.on('autocomplete:closed', function () { search.autocomplete.setVal() })
  894. function focusSearchInput () { document.querySelector('#search-input').focus() }
  895. if (document.querySelector('.home-link.is-current')) window.addEventListener('load', focusSearchInput)
  896. </script>
  897. <script src="../../../_/js/site.js"></script>
  898. <script async src="../../../_/js/vendor/highlight.js"></script>
  899. </body>
  900. </html>