material_definitions.adoc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. = How to Use Material Definitions (.j3md)
  2. :revnumber: 2.1
  3. :revdate: 2020/07/23
  4. :keywords: Material, SDK, MatDef, file, documentation
  5. 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.
  6. 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 xref:material/material_specification.adoc[in-depth developer specification of the jME3 material system].
  7. *Example:*
  8. [source,java]
  9. ----
  10. Spatial myGeometry = assetManager.loadModel("Models/Teapot/Teapot.j3o");
  11. Material mat = new Material(assetManager, // Create new material and...
  12. "Common/MatDefs/Misc/Unshaded.j3md"); // ... specify a Material Definition file, here "Unshaded.j3md"!
  13. mat.setColor("Color", ColorRGBA.Blue); // Set one or more material parameters.
  14. myGeometry.setMaterial(mat); // Use material on this Geometry.
  15. ----
  16. [TIP]
  17. ====
  18. If you use one custom material with certain settings very often, learn about storing material settings in xref:material/j3m_material_files.adoc[j3m Material Files]. You either xref:sdk:material_editing.adoc[use the jMonkeyEngine SDK to create .j3m files] (user-friendly), or you xref:material/j3m_material_files.adoc[write .j3m files in a text editor] (IDE-independent).
  19. ====
  20. == Preparing a Material
  21. In the xref:material/materials_overview.adoc[Materials Overview] list:
  22. . Choose a Material Definition that has the features that you need.
  23. ** Tip: If you don't know, start with `Unshaded.j3md` or `Lighting.j3md`.
  24. . 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!
  25. . Create and save the necessary Texture files to your `assets/Textures` directory.
  26. ** E.g. mytex_diffuse.png as ColorMap / DiffuseMap, mytex_normal.png as NormalMap, mytex_alpha.png as AlphaMap, etc…
  27. . Determine the required values to achieve the effect that you want.
  28. ** E.g. set colors, floats, booleans, etc…
  29. == Using a Material
  30. In your Java code,
  31. . Create a Material object based on the chosen Material Definition (.j3md file):
  32. +
  33. [source,java]
  34. ----
  35. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  36. ----
  37. . Configure your Material by setting the appropriate values listed in the xref:material/materials_overview.adoc[Materials Overview] table.
  38. +
  39. [source,java]
  40. ----
  41. mat.setColor("Color", ColorRGBA.Yellow ); // and more
  42. ----
  43. . Apply your prepared Material to a Geometry:
  44. +
  45. [source,java]
  46. ----
  47. myGeometry.setMaterial(mat);
  48. ----
  49. . (Optional) Adjust the texture scale of the mesh:
  50. +
  51. [source,java]
  52. ----
  53. myGeometryMesh.scaleTextureCoordinates(new Vector2f(2f, 2f));
  54. ----
  55. For details see also: xref:material/how_to_use_materials.adoc[How to Use Materials]
  56. === Examples
  57. Here are examples of the methods that set the different data types:
  58. * `mat.setColor( "Color", ColorRGBA.White );`
  59. * `mat.setTexture( "ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.png" ));`
  60. * `mat.setFloat( "Shininess", 5f);`
  61. * `mat.setBoolean( "SphereMap", true);`
  62. * `mat.setVector3( "NormalScale", new Vector3f(1f,1f,1f));`
  63. A simple textured material.
  64. [source,java]
  65. ----
  66. Material mat = new Material(assetManager,
  67. "Common/MatDefs/Misc/Unshaded.j3md");
  68. mat.setTexture("ColorMap", assetManager.loadTexture(
  69. "Interface/Logo/Monkey.jpg"));
  70. ----
  71. A textured material with a color bleeding through transparent areas.
  72. [source,java]
  73. ----
  74. Material mat = new Material(assetManager,
  75. "Common/MatDefs/Misc/Unshaded.j3md");
  76. mat.setTexture("ColorMap", assetManager.loadTexture(
  77. "Textures/ColoredTex/Monkey.png"));
  78. mat.setColor("Color", ColorRGBA.Blue);
  79. ----
  80. You can test these examples within the following code snippet. It creates a box and applies the material:
  81. [source,java]
  82. ----
  83. Box b = new Box(Vector3f.ZERO, 1, 1, 1);
  84. Geometry geom = new Geometry("Box", b);
  85. // ... insert Material definition...
  86. geom.setMaterial(mat);
  87. rootNode.attachChild(geom);
  88. ----
  89. [TIP]
  90. ====
  91. You can find these and other common code snippets in the jMonkeyEngine SDK Code Palette. Drag and drop them into your source code.
  92. ====
  93. == Creating a Custom Material Definition
  94. First read the xref:material/material_specification.adoc[developer specification of the jME3 material system (.j3md,.j3m)]. Also check out the xref:ROOT:getting-started/build_from_sources.adoc[engine source code] and have a look at how some Material Definitions are implemented.
  95. You can create your own Material Definitions and place them in your project's `assets/MatDefs` directory.
  96. . Find the existing MatDefs in `engine/src/core-data/Common/MatDefs/`.
  97. . Open a Something.j3md file in a text editor. You see that this .j3md file defines Material Parameters and Techniques.
  98. ** Material Parameters are the ones that you set in Materials, as shown in the examples above.
  99. ** The Techniques rely on VertexShaders and FragmentShaders: You find those in the files Something.vert and Something.frag in the same directory.
  100. . Learn about GLSL (OpenGL Shading Language) to understand the .vert and .frag syntax, then write your own.
  101. == Related Links
  102. * xref:material/material_specification.adoc[Developer specification of the jME3 material system (.j3md,.j3m)]