how_to_use_materials.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. = How to Use Materials
  2. :revnumber: 2.0
  3. :revdate: 2020/07/13
  4. :keywords: material, texture, effect, wireframe, light, documentation
  5. A Geometry (mesh) is just the shape of the object. jMonkeyEngine cannot render a shape without knowing anything about its surface properties. You need to apply a color or texture to the surface of your Geometries to make them visible. In jMonkeyEngine, colors and textures are represented as Material objects. (An alternative would also be to load a model that includes materials generated by a mesh editor, such as Blender.)
  6. * All Geometries must have Materials that defines color or texture.
  7. * Each Material is based on a Material Definition file. +
  8. Examples of included Material Definitions: Lighting.j3md, Unshaded.j3md
  9. You want to make the most of your 3D models by specifying good looking material parameters. The developers must be in contact with the graphic designers regarding which of the xref:ROOT:jme3/advanced/materials_overview.adoc[Material properties] they intend to use in their 3D models. You must have an understanding what <<intermediate/terminology.adoc#materials-textures,texture maps>> are, to be able to use texture-mapped materials.
  10. [IMPORTANT]
  11. ====
  12. Don't forget to add a xref:ROOT:jme3/advanced/light_and_shadow.adoc[Light Source] to the scene! All Materials (except "`Unshaded`" ones) are *invisible* without a light source.
  13. ====
  14. If you want more advanced background info: You can learn more about xref:ROOT:jme3/advanced/material_definitions.adoc[Material Definitions] in general here. You can find the full list of Material Parameters in the xref:ROOT:jme3/advanced/materials_overview.adoc[Material Definitions Properties] overview. The following sections introduce you to the most commonly used cases. You typically initialize Material objects in the `simpleInitApp()` method, and configure them using the setters described here. Then load the Materials using `myGeometry.setMaterial(mat)`.
  15. == Code Sample
  16. The following samples assume that you loaded a Geometry called myGeometry, and want to assign a material to it.
  17. This example creates a simple unshaded blue material: Use it for abstract objects that need no illumination/shading, e.g. sky, +++<abbr title="Graphical User Interface">GUI</abbr>+++ and billboard nodes, tiles/cards, or toons.
  18. [source,java]
  19. ----
  20. Spatial myGeometry = assetManager.loadModel("Models/Teapot/Teapot.j3o");
  21. Material mat = new Material(assetManager, // Create new material and...
  22. "Common/MatDefs/Misc/Unshaded.j3md"); // ... specify .j3md file to use (unshaded).
  23. mat.setColor("Color", ColorRGBA.Blue); // Set some parameters, e.g. blue.
  24. myGeometry.setMaterial(mat); // Use new material on this Geometry.
  25. ----
  26. This example creates a link:http://en.wikipedia.org/wiki/Phong_reflection_model[Phong]-illuminated blue material. Use it for illuminated, naturalistic objects, such as characters, buildings, terrains, vehicles. Needs a light source, otherwise it will be invisible.
  27. [source,java]
  28. ----
  29. Spatial myGeometry = assetManager.loadModel("Models/Teapot/Teapot.j3o");
  30. Material mat = new Material(assetManager, // Create new material and...
  31. "Common/MatDefs/Light/Lighting.j3md"); // ... specify .j3md file to use (illuminated).
  32. mat.setBoolean("UseMaterialColors",true); // Set some parameters, e.g. blue.
  33. mat.setColor("Ambient", ColorRGBA.Blue); // ... color of this object
  34. mat.setColor("Diffuse", ColorRGBA.Blue); // ... color of light being reflected
  35. myGeometry.setMaterial(mat); // Use new material on this Geometry.
  36. ----
  37. [TIP]
  38. ====
  39. Do you reuse Materials? You can xref:sdk:material_editing.adoc[store Material properties in a .j3m file] and load all properties in one line using
  40. [source,java]
  41. ----
  42. myGeometry.setMaterial( assetManager.loadMaterial("Materials/myMaterial.j3m"));
  43. ----
  44. ====
  45. == Colored or Textured
  46. Every Material must have at least Material Colors or Textures. Some optional material features also require a combination of both.
  47. === Colored
  48. To give an unshaded material a color:
  49. * Specify the color property
  50. +
  51. [source,java]
  52. ----
  53. mat.setColor("Color", ColorRGBA.Blue); // with Unshaded.j3md
  54. ----
  55. To give an Phong-illuminated material a color:
  56. . Activate material colors:
  57. +
  58. [source,java]
  59. ----
  60. mat.setBoolean("UseMaterialColors",true); // with Lighting.j3md
  61. ----
  62. . Specify at least Diffuse and Ambient colors. Set both to the same color in the standard case.
  63. +
  64. [source,java]
  65. ----
  66. mat.setColor("Diffuse", ColorRGBA.Blue ); // with Lighting.j3md
  67. mat.setColor("Ambient", ColorRGBA.Blue ); // with Lighting.j3md
  68. ----
  69. === Textured
  70. To give an unshaded material a texture:
  71. * Specify at least a ColorMap:
  72. +
  73. [source,java]
  74. ----
  75. mat.setTexture("ColorMap", assetManager.loadTexture("Textures/monkey.png")); // with Unshaded.j3md
  76. ----
  77. To give a Phong-illuminated material a texture:
  78. * Specify at least the DiffuseMap texture:
  79. +
  80. [source,java]
  81. ----
  82. mat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/wood_diffuse.png")); // with Lighting.j3md
  83. ----
  84. [TIP]
  85. ====
  86. It can happen that you load textures at different scales, for example, your blades of grass may look as big as twigs, or your spaceship's heat tiles may look like small bathroom tiles. Then you need to adjust the texture scale, either bigger (> 1.0f) or smaller (< 1.0f).
  87. [source,java]
  88. ----
  89. geometry.scaleTextureCoordinates(new Vector2f(0.5f, 0.5f));
  90. ----
  91. ====
  92. All other Texture Maps or Material settings are optional. If used skillfully, they make your model look really spiffy.
  93. == (Optional) Bumpy
  94. A NormalMap (also called BumpMap) is an extra colored texture that describes the fine bumpy details of the Material surface. E.g. fine cracks, pores, creases, notches. Using a BumpMap is more efficient than trying to shape the mesh to be bumpy.
  95. To add a BumpMap (this only makes sense for illuminated Materials):
  96. . Generate normal vectors information for the Mesh (not for the Geometry!) using `com.jme3.util.TangentBinormalGenerator`.
  97. +
  98. [source,java]
  99. ----
  100. TangentBinormalGenerator.generate(mesh);
  101. ----
  102. . Specify the `NormalMap` texture for the Material.
  103. +
  104. [source,java]
  105. ----
  106. mat.setTexture("NormalMap", assetManager.loadTexture("Textures/wood_normal.png")); // with Lighting.j3md
  107. ----
  108. link:http://en.wikipedia.org/wiki/Bump_mapping[Learn more about creating and using NormalMaps and BumpMaps here.]
  109. == (Optional) Shiny
  110. To activate Shininess (this only makes sense for illuminated Materials):
  111. . Specify the `Shininess` intensity the Material. +
  112. Shininess is a float value between 1 (rough surface with blurry shininess) and 128 (very smooth surface with focused shininess)
  113. +
  114. [source,java]
  115. ----
  116. mat.setFloat("Shininess", 5f);
  117. ----
  118. . Activate material colors:
  119. +
  120. [source,java]
  121. ----
  122. mat.setBoolean("UseMaterialColors",true);
  123. ----
  124. . Specify the `Specular` and `Diffuse` colors of the shiny spot. +
  125. Typically you set Specular to the ColorRGBA value of the light source, often RGBA.White.
  126. +
  127. [source,java]
  128. ----
  129. mat.setColor("Specular",ColorRGBA.White);
  130. mat.setColor("Diffuse",ColorRGBA.White);
  131. ----
  132. . (Optional) Specify a `SpecularMap` texture. +
  133. You optionally hand-draw this grayscale texture to outline in detail where the surface should be more shiny (whiter grays) and where less (blacker grays). If you don't supply a SpecularMap, the whole material is shiny everywhere.
  134. +
  135. [source,java]
  136. ----
  137. mat.setTexture("SpecularMap", assetManager.loadTexture("Textures/metal_spec.png")); // with Lighting.j3md
  138. ----
  139. To deactivate shininess
  140. * Set the `Specular` color to `ColorRGBA.Black`. Do not just set `Shininess` to 0.
  141. +
  142. [source,java]
  143. ----
  144. mat.setColor("Specular",ColorRGBA.Black);
  145. ----
  146. == (Optional) Glow
  147. To activate glow:
  148. . Add one xref:ROOT:jme3/advanced/bloom_and_glow.adoc[BloomFilter PostProcessor] in your simpleInitApp() method (only once, it is used by all glowing objects).
  149. +
  150. [source,java]
  151. ----
  152. FilterPostProcessor fpp=new FilterPostProcessor(assetManager);
  153. BloomFilter bloom = new BloomFilter(BloomFilter.GlowMode.Objects);
  154. fpp.addFilter(bloom);
  155. viewPort.addProcessor(fpp);
  156. ----
  157. . Specify a `Glow` color. +
  158. A ColorRGBA value of your choice, e.g. choose a warm or cold color for different effects, or white for a neutral glow.
  159. +
  160. [source,java]
  161. ----
  162. mat.setColor("GlowColor",ColorRGBA.White);
  163. ----
  164. . (Optional) Specify a `GlowMap` texture. +
  165. This texture outlines in detail where the DiffuseMap texture glows. If you don't supply a GlowMap, the whole material glows everwhere.
  166. +
  167. [source,java]
  168. ----
  169. mat.setTexture("GlowMap", assetManager.loadTexture("Textures/alien_glow.png"));
  170. ----
  171. To deactivate glow:
  172. * Set the `Glow` color to `ColorRGBA.Black`.
  173. +
  174. [source,java]
  175. ----
  176. mat.setColor("GlowColor", ColorRGBA.Black);
  177. ----
  178. Learn more about xref:ROOT:jme3/advanced/bloom_and_glow.adoc[Bloom and Glow].
  179. == (Optional) Transparent
  180. Most Material Definitions support an alpha channel to make a model opaque, translucent, or transparent.
  181. * Alpha=1.0f makes the color opaque (default),
  182. * Alpha=0.0f make the color fully transparent
  183. * Alpha between 0f and 1f makes the color more or less translucent.
  184. To make a Geometry transparent or translucent:
  185. . Specify which areas you want to be transparent or translucent by specifying the alpha channel:
  186. ** (For colored Materials) In any RGBA color, the first three are Red-Green-Blue, and the last float is the Alpha channel. For example, to replace ColorRGBA.Red with a translucent red:
  187. +
  188. [source,java]
  189. ----
  190. mat.setColor("Color", new ColorRGBA(1,0,0,0.5f));
  191. ----
  192. ** (For textured Materials) Supply an AlphaMap that outlines which areas are transparent.
  193. +
  194. [source,java]
  195. ----
  196. mat.setTexture("AlphaMap", assetManager.loadTexture("Textures/window_alpha.png"));
  197. ----
  198. ** (For textured Materials) If the DiffuseMap has an alpha channel, use:
  199. +
  200. [source,java]
  201. ----
  202. mat.setBoolean("UseAlpha",true);
  203. ----
  204. . Specify BlendMode Alpha for the Material.
  205. +
  206. [source,java]
  207. ----
  208. mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  209. ----
  210. . Put the Geometry (not the Material!) in the appropriate render queue bucket. +
  211. ** Objects in the translucent bucket (e.g. particles) are not affected by SceneProcessors (e.g. shadows).
  212. +
  213. [source,java]
  214. ----
  215. geo.setQueueBucket(Bucket.Translucent);
  216. ----
  217. ** Objects in the transparent bucket (e.g. foliage) are affected by SceneProcessors (e.g. shadows).
  218. +
  219. [source,java]
  220. ----
  221. geo.setQueueBucket(Bucket.Transparent);
  222. ----
  223. . (Optional) Specify other material settings.
  224. [cols="3", options="header"]
  225. |===
  226. a|Standard Material Transparency
  227. a|Description
  228. a|Example
  229. a|getAdditionalRenderState().setBlendMode(BlendMode.Off);
  230. a|This is the default, no transparency.
  231. a|Use for all opaque objects like walls, floors, people…
  232. a|getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  233. a|Interpolates the background pixel with the current pixel by using the current pixel's alpha.
  234. a|This is the most commonly used BlendMode for transparency and translucency: Frosted window panes, ice, glass, alpha-blended vegetation textures…
  235. a|getAdditionalRenderState().setDepthWrite(false);
  236. a|Disables writing of the pixel's depth value to the depth buffer.
  237. a|Deactivate this on Materials if you expect two or more transparent/translucent objects to be obscuring one another, but you want to see through both.
  238. a|getAdditionalRenderState().setAlphaTest(true) +
  239. getAdditionalRenderState().setAlphaFallOff(0.5f);
  240. a|Enables Alpha Testing and uses an AlphaDiscardThreshold as alpha fall-off value. This means that gradients in the AlphaMap are no longer interpreted as soft translucency, but parts of the texture become either fully opaque or fully transparent. Only pixels above the alpha threshold (e.g. 0.5f) are rendered.
  241. a|Activate Alpha Testing for (partially) *transparent* objects such as foliage, hair, etc. +
  242. Deactivate Alpha Testing for gradually *translucent* objects, such as colored glass, smoked glass, ghosts.
  243. |===
  244. [TIP]
  245. ====
  246. It is possible to load a DiffuseMap texture that has an Alpha channel, and combine it with an underlying Material Color.
  247. [source,java]
  248. ----
  249. mat.setBoolean("UseAlpha",true);
  250. ----
  251. The Material Color bleeds through the transparent areas of the top-layer DiffuseMap texture. In this case you do not need BlendMode Alpha – because it's not the whole Material that is transparent, but only one of the texture layers. You use this bleed-through effect, for example, to generate differently colored uniforms, animals, or plants, where each Material uses the same "`template`" DiffuseMap texture but combines it with a different color.
  252. ====
  253. == (Optional) Wireframe
  254. Additionally to the above settings, you can switch off and on a wireframe rendering of the mesh. Since a wireframe has no faces, this temporarily disables the other Texture Maps.
  255. [cols="3", options="header"]
  256. |===
  257. a|Material Property
  258. a|Description
  259. a|Example
  260. a|getAdditionalRenderState().setWireframe(true);
  261. a|Switch to showing the (textured) Material in wireframe mode. The wireframe optionally uses the Material's `Color` value.
  262. a|Use wireframes to debug meshes, or for a "`matrix`" or "`holodeck`" effect.
  263. a|getAdditionalRenderState().setLineWidth(2f);
  264. a|When in wireframe mode, sets the line width of the mesh.
  265. a|
  266. |===