materials_overview.adoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. = Material Definition Properties
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: material, texture, MatDefs, light, culling, RenderStates, documentation
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  9. In jMonkeyEngine 3, colors and textures are represented as Material objects.
  10. * All Geometries must have Materials. To improve performance, reuse Materials for similar models, don't create a new Material object for every Geometry. (E.g. use one bark Material for several tree models.)
  11. * Each Material is based on one of jme3's default Material Definitions (.j3md files) that are included in the engine. Advanced users can create additional custom Material Definitions (see how it's done in the <<jme3/build_from_sources#,jme3 sources>>).
  12. [TIP]
  13. ====
  14. Find out quickly <<jme3/intermediate/how_to_use_materials#,How to Use Materials>>, including the most commonly used code samples and RenderStates. +Or find more background info on <<jme3/advanced/material_definitions#,How to use Material Definitions>>.
  15. ====
  16. == All Materials Definition Properties
  17. The following Materials table shows you the Material Definitions that jMonkeyEngine 3 supports.
  18. [TIP]
  19. ====
  20. Looks confusing? +1) Start learning about `Unshaded.j3md` and `Lighting.j3md`, they cover 90% of the cases. +2) Use <<sdk/material_editing#,the SDK's visual material editor>> to try out and save material settings easily. +3) The <<sdk/code_editor#,SDK's Palette>> contains drag&drop code snippets for loading materials.
  21. ====
  22. Most Material parameters are optional. For example, it is okay to specify solely the `DiffuseMap` and `NormalMap` when using `Lighting.j3md`, and leave the other texture maps empty. In this case, you are only using a subset of the possible features, but that's fine if it already makes in the material look the way that you want. You can always add more texture maps later.
  23. === Unshaded Coloring and Textures
  24. jMonkeyEngine supports illuminated and unshaded Material Definitions.
  25. * Phong Illuminated materials look more naturalistic.
  26. * Unshaded materials look more abstract.
  27. “Unshaded materials look somewhat abstract because they ignore lighting and shading. Unshaded Materials work even if the scene does not include a light source. These Materials can be single-colored or textured. For example, they are used for cards and tiles, for the sky, billboards and UI elements, for toon-style games, or for testing.
  28. [cols="3", options="header"]
  29. |===
  30. a| Standard Unshaded Material Definition
  31. a| Usage
  32. <a| Material Parameters
  33. a| Common/MatDefs/Misc/Unshaded.j3md
  34. a| Standard, non-illuminated Materials. +Use this for simple coloring and texturing, glow, and transparency. +See also: <<jme3/beginner/hello_material#,Hello Material>>
  35. a| *Texture Maps* +setTexture(“ColorMap, assetManager.loadTexture(“)); + setBoolean(“SeparateTexCoord,true); +setTexture(“LightMap, assetManager.loadTexture(“)); +*Colors* +setColor(“Color, ColorRGBA.White); +setBoolean(“VertexColor,true); +*Glow* +setTexture(“GlowMap, assetManager.loadTexture(“)); +setColor(“GlowColor, ColorRGBA.White);
  36. |===
  37. Other useful, but less commonly used material definitions:
  38. [cols="3", options="header"]
  39. |===
  40. a| Special Unshaded Material Definitions
  41. a| Usage
  42. <a| Material Parameters
  43. <a| Common/MatDefs/Misc/Sky.j3md
  44. a| A solid skyblue, or use with a custom SkyDome texture. +See also: <<jme3/advanced/sky#,Sky>>
  45. a| setTexture(“TextureCubeMap, assetManager.loadTexture(“)); + setBoolean(“SphereMap,true); +setVector3(“NormalScale, new Vector3f(0,0,0));
  46. a| Common/MatDefs/Terrain/Terrain.j3md
  47. a| Splat textures for e.g. terrains. +See also: <<jme3/beginner/hello_terrain#,Hello Terrain>>
  48. a| setTexture(“Tex1, assetManager.loadTexture(“)); (red) + setFloat(“Tex1Scale,1f); + setTexture(“Tex2, assetManager.loadTexture(“)); (green) + setFloat(“Tex2Scale,1f); +setTexture(“Tex3, assetManager.loadTexture(“)); (blue) + setFloat(“Tex3Scale,1f); +setTexture(“Alpha, assetManager.loadTexture(“));
  49. a|Common/MatDefs/Terrain/HeightBasedTerrain.j3md
  50. a|A multi-layered texture for terrains. +Specify four textures and a Vector3f describing the region in which each texture should appear: +X = start height, +Y = end height, +Z = texture scale. +Texture regions can overlap. +For example: Specify a seafloor texture for the lowest areas, +a sandy texture for the beaches, +a grassy texure for inland areas, +and a rocky texture for mountain tops.
  51. a| setFloat(“terrainSize,512f); +setTexture(“region1ColorMap, assetManager.loadTexture(“)); +setTexture(“region2ColorMap, assetManager.loadTexture(“)); +setTexture(“region3ColorMap, assetManager.loadTexture(“)); +setTexture(“region4ColorMap, assetManager.loadTexture(“)); +setVector3(“region1, new Vector3f(0,0,0)); + setVector3(“region2, new Vector3f(0,0,0)); + setVector3(“region3, new Vector3f(0,0,0)); + setVector3(“region4, new Vector3f(0,0,0)); +*Settings for steep areas:* +setTexture(“slopeColorMap, assetManager.loadTexture(“)); + setFloat(“slopeTileFactor,1f);
  52. <a| Common/MatDefs/Misc/Particle.j3md
  53. a| Used with texture masks for particle effects, or for point sprites. +The Quadratic value scales the particle for perspective view (link:http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core/com/jme3/effect/ParticleEmitter.java[formula]). +Does support an optional colored glow effect. +See also: <<jme3/beginner/hello_effects#,Hello Effects>>
  54. a| setTexture(“Texture, assetManager.loadTexture(“)); +setTexture(“GlowMap, assetManager.loadTexture(“)); +setColor(“GlowColor, ColorRGBA.White); + setFloat(“Quadratic,1f); + setBoolean(“PointSprite,true);
  55. |===
  56. === Phong Illuminated
  57. jMonkeyEngine supports illuminated and unshaded Material Definitions.
  58. * Phong Illuminated materials look more naturalistic.
  59. * Unshaded materials look more abstract.
  60. Illuminated materials require a <<jme3/advanced/light_and_shadow#,light source>> added to at least one of their parent nodes! (e.g. rootNode.) Illuminated materials are darker on the sides facing away from light sources. They use Phong illumination model (default), or the Ward isotropic gaussian specular shader (WardIso) which looks more like plastic. They do not cast <<jme3/advanced/light_and_shadow#,drop shadows>> unless you use a FilterPostProcessor.
  61. [cols="3", options="header"]
  62. |===
  63. a|Standard Illuminated Material Definition
  64. a| Usage
  65. a| Material Parameters
  66. <a| Common/MatDefs/Light/Lighting.j3md
  67. a| Commonly used Material with Phong illumination. +Use this material together with DiffuseMap, SpecularMap, BumpMap (NormalMaps, ParalaxMap) textures. +Supports shininess, transparency, and plain material colors (Diffuse, Ambient, Specular colors). +See also: <<jme3/beginner/hello_material#,Hello Material>>
  68. <a| *Texture Maps* +setTexture(“DiffuseMap, assetManager.loadTexture(“)); +setBoolean(“UseAlpha,true);footnote:[UseAlpha specifies whether DiffuseMap uses the alpha channel] +setTexture(“NormalMap, assetManager.loadTexture(“)); +setBoolean(“LATC,true); footnote:[LATC Specifies whether NormalMap is BC5/ATI2n/LATC/3Dc-compressed] +setTexture(“SpecularMap, assetManager.loadTexture(“)); + setFloat(“Shininess,64f); +setTexture(“ParallaxMap, assetManager.loadTexture(“)); +setTexture(“AlphaMap, assetManager.loadTexture(“)); + setFloat(“AlphaDiscardThreshold,1f); +setTexture(“ColorRamp, assetManager.loadTexture(“)); +*Glow* +setTexture(“GlowMap, assetManager.loadTexture(“)); +setColor(“GlowColor, ColorRGBA.White); +*Performance and quality* +setBoolean(“VertexLighting,true); + setBoolean(“UseVertexColor,true); + setBoolean(“LowQuality,true); + setBoolean(“HighQuality,true); +*Material Colors* + setBoolean(“UseMaterialColors,true); +setColor(“Diffuse, ColorRGBA.White); + setColor(“Ambient, ColorRGBA.White); +setColor(“Specular, ColorRGBA.White); +*Tangent shading:* + setBoolean(“VTangent,true); + setBoolean(“Minnaert,true);footnote:[Minnaert is a shader type.] +setBoolean(“WardIso,true);footnote:[WardIso is a shader type.]
  69. |===
  70. [cols="3", options="header"]
  71. |===
  72. a|Special Illuminated Material Definitions
  73. a| Usage
  74. a| Material Parameters
  75. a|Common/MatDefs/Terrain/TerrainLighting.j3md
  76. a|Same kind of multi-layered splat texture as Terrain.j3md, but with illumination and shading. +Typically used for terrains, but works on any mesh. +For every 3 splat textures, you need one alpha map. +You can use a total of 11 texture maps in the terrain's splat texture: +Note that diffuse and normal maps all count against that. +For example, you can use a maximum of 9 diffuse textures, two of which can have normal maps; +or, five textures with both diffuse and normal maps.
  77. a|*Texture Splat Maps* + setTexture(“DiffuseMap, assetManager.loadTexture(“)); + setFloat(“DiffuseMap_0_scale,1f); +setTexture(“NormalMap, assetManager.loadTexture(“)); +setTexture(“DiffuseMap_1, assetManager.loadTexture(“)); + setFloat(“DiffuseMap_1_scale,1f); +setTexture(“NormalMap_1, assetManager.loadTexture(“)); +setTexture(“DiffuseMap_2, assetManager.loadTexture(“)); + setFloat(“DiffuseMap_2_scale,1f); +setTexture(“NormalMap_2, assetManager.loadTexture(“)); +setTexture(“DiffuseMap_3, assetManager.loadTexture(“)); + setFloat(“DiffuseMap_3_scale,1f); +setTexture(“NormalMap_3, assetManager.loadTexture(“)); +etc, up to 11. +*Alpha Maps* +setTexture(“AlphaMap, assetManager.loadTexture(“)); +setTexture(“AlphaMap_1, assetManager.loadTexture(“)); +setTexture(“AlphaMap_2, assetManager.loadTexture(“)); +*Glowing* +setTexture(“GlowMap, assetManager.loadTexture(“)); +setColor(“GlowColor, ColorRGBA.White); +*Miscellaneous* +setColor(“Diffuse, ColorRGBA.White); +setColor(“Ambient, ColorRGBA.White); +setFloat(“Shininess,64f); +setColor(“Specular, ColorRGBA.White); +setTexture(“SpecularMap, assetManager.loadTexture(“)); +setBoolean(“WardIso,true); + setBoolean(“useTriPlanarMapping,true); + setBoolean(“isTerrainGrid,true);
  78. <a| Common/MatDefs/Light/Reflection.j3md
  79. a| Reflective glass material with environment map (CubeMap/SphereMap). See also: link:http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/test/jme3test/texture/TestCubeMap.java[TestCubeMap.java]
  80. a| setTexture(“Texture, assetManager.loadTexture(“)); + setBoolean(“SphereMap,true);
  81. |===
  82. === Other: Test and Debug
  83. [cols="2", options="header"]
  84. |===
  85. <a| Material Definition
  86. a| Usage
  87. <a| Common/MatDefs/Misc/ShowNormals.j3md
  88. a| A color gradient calculated from the model's surface normals. You can use this built-in material to debug the generation of normals in meshes, to preview models that have no material and no lights, or as fall-back default material. This built-in material has no parameters.
  89. |===
  90. == RenderStates
  91. === Transparency
  92. [cols="3", options="header"]
  93. |===
  94. a|Material Option
  95. a|Description
  96. a|Example
  97. a|getAdditionalRenderState().setBlendMode(BlendMode.Off);
  98. a|This is the default, no transparency.
  99. a|Use for all opaque objects like walls, floors, people…
  100. a|getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
  101. a|Interpolates the background pixel with the current pixel by using the current pixel's alpha.
  102. a|Use this for normal every-day translucency: Frosted window panes, ice, glass, alpha-blended vegetation textures…
  103. a|getAdditionalRenderState().setDepthWrite(false);
  104. a|Disables writing of the pixel's depth value to the depth buffer.
  105. a|Use this on Materials if you have several transparent/translucent objects obscuring one another, but you want to see through both.
  106. a|getAdditionalRenderState().setAlphaFallOff(0.5f); +getAdditionalRenderState().setAlphaTest(true)
  107. a|Enables Alpha Testing with a “AlphaDiscardThreshold in the AlphaMap.
  108. a|Activate Alpha Testing for (partially) *transparent* objects such as foliage, hair, etc. +Deactivate Alpha Testing for gradually *translucent* objects, such as colored glass, smoked glass, ghosts.
  109. a|getAdditionalRenderState().setBlendMode(BlendMode.Additive);
  110. a|Additive alpha blending adds colors in a commutative way, i.e. the result does not depend on the order of transparent layers, since it adds the scene's background pixel color to the current pixel color. This is useful if you have lots of transparent textures overlapping and don't care about the order. +*Note:* Viewed in front of a white background, Additive textures become fully transparent!
  111. a| This is the default for Particle.j3md-based textures that have a black color background.
  112. a|getAdditionalRenderState().setBlendMode(BlendMode.AlphaAdditive);
  113. a|Same as “Additive, except first it multiplies the current pixel color by the pixel alpha.
  114. a|This can be used for particle effects that have alpha as background.
  115. a|getAdditionalRenderState().setBlendMode(BlendMode.Color);
  116. a|Blends by color.
  117. a|Generally useless.
  118. a|getAdditionalRenderState().setBlendMode(BlendMode.Modulate);
  119. a|Multiplies the background pixel by the current pixel.
  120. a|?
  121. a|getAdditionalRenderState().setBlendMode(BlendMode.ModulateX2);
  122. a|Same as “Modulate, except the result is doubled.
  123. a|?
  124. a|getAdditionalRenderState().setBlendMode(BlendMode.PremultAlpha);
  125. a|Pre-multiplied alpha blending. E.g. if the color of the object has already been multiplied by its alpha, this is used instead of “Alpha blend mode.
  126. a|For use with Premult Alpha textures.
  127. |===
  128. If the DiffuseMap has an alpha channel, use:
  129. [source,java]
  130. ----
  131. mat.setBoolean("UseAlpha",true);
  132. ----
  133. Later, put the Geometry (not the Material!) in the appropriate render queue
  134. *
  135. [source,java]
  136. ----
  137. geo.setQueueBucket(Bucket.Translucent);
  138. ----
  139. *
  140. [source,java]
  141. ----
  142. geo.setQueueBucket(Bucket.Transparent);
  143. ----
  144. === Culling
  145. [cols="3", options="header"]
  146. |===
  147. a|Material Option
  148. a|Usage
  149. a|Example
  150. a|getAdditionalRenderState().setFaceCullMode(FaceCullMode.Back);
  151. a|Activates back-face culling. Mesh faces that are facing away from the camera are not rendered, which saves time. *Backface culling is activated by default as a major optimization.*
  152. a|The invisible backsides and insides of models are not calculated.
  153. a|getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
  154. a|No meshes are culled. Both mesh faces are rendered, even if they face away from the camera. Slow.
  155. a|Sometimes used to debug custom meshes if you messed up some of the polygon sides, or for special shadow effects.
  156. a|getAdditionalRenderState().setFaceCullMode(FaceCullMode.Front);
  157. a|Activates front-face culling. Mesh faces facing the camera are not rendered.
  158. a|No example – Typically not used because you wouldn't see anything meaningful.
  159. a|getAdditionalRenderState().setFaceCullMode(FaceCullMode.FrontAndBack)
  160. a|Culls both backfaces and frontfaces.
  161. a|Use this as an efficient way to make an object temporarily invisible, while keeping all its other in-game properties (such as node attachment, collision shapes, interactions, etc) active.
  162. |===
  163. === Miscellaneous
  164. [cols="3", options="header"]
  165. |===
  166. a|getAdditionalRenderState().setColorWrite(false);
  167. a|Disable writing the color of pixels.
  168. a|Use this together with setDepthWrite(true) to write pixels only to the depth buffer, for example.
  169. a|getAdditionalRenderState().setPointSprite(true);
  170. a|Enables point-sprite mode, e.g. meshes with “Mode.Points will be rendered as textured sprites. Note that gl_PointCoord must be set in the shader.
  171. a|Point sprites are used internally for hardware accelerated particle effects.
  172. a|getAdditionalRenderState().setPolyOffset();
  173. a|Enable polygon offset.
  174. a|Use this when you have meshes that have triangles really close to each over (e.g. link:http://en.wikipedia.org/wiki/Coplanarity[Coplanar]), it will shift the depth values to prevent link:http://en.wikipedia.org/wiki/Z-fighting[Z-fighting].
  175. |===
  176. *Related Links*
  177. * <<jme3/advanced/material_specification#,Developer specification of the jME3 material system (.j3md,.j3m)>>