j3m_material_files.adoc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. = Saving and Loading Materials with .j3m Files
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :keywords: material, texture, file, sdk, wireframe, documentation
  6. :relfileprefix: ../../
  7. :imagesdir: ../..
  8. :experimental:
  9. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  10. In the <<jme3/advanced/material_definitions#,Material Definitions>> article you learned how to configure <<jme3/advanced/materials_overview#,Materials>> programmatically in Java code. If you have certain commonly used Materials that never change, you can clean up the amount of Java code that clutters your init method, by moving material settings into .j3m files. Then later in your code, you only need to call one setter instead of several to apply the material.
  11. If you want to colorize simple shapes (one texture all around), then .j3m are the most easily customizable solution. J3m files can contain texture mapped materials, but as usual you have to create the textures in an external editor, especially if you use UV-mapped textures.
  12. == Writing the .j3m File
  13. . For every Material, create a file and give it a name that describes it: e.g. `SimpleBump.j3m`
  14. . Place the file in your project's `assets/Materials/` directory, e.g. `MyGame/src/assets/Materials/SimpleBump.j3m`
  15. . Edit the file and add content using the following Syntax, e.g.:
  16. [source]
  17. ----
  18. Material shiny bumpy rock : Common/MatDefs/Light/Lighting.j3md {
  19. MaterialParameters {
  20. Shininess: 8.0
  21. NormalMap: Textures/bump_rock_normal.png
  22. UseMaterialColors : true
  23. Ambient : 0.0 0.0 0.0 1.0
  24. Diffuse : 1.0 1.0 1.0 1.0
  25. Specular : 0.0 0.0 0.0 1.0
  26. }
  27. }
  28. ----
  29. How this file is structured:
  30. . Header
  31. .. `Material` is a fixed keyword, keep it.
  32. .. `shiny bumpy rock` is a descriptive string that you can make up. Choose a name to help you remember for what you intend to use this material.
  33. .. After the colon, specify on which <<jme3/advanced/materials_overview#,Material>> definition you base this Material.
  34. . Now look up the choosen Material Definition's parameters and their parameter types from the <<jme3/advanced/materials_overview#,Material>> table. Add one line for each parameter.
  35. ** For example: The series of four numbers in the example above represent RGBA color values.
  36. . Check the detailed syntax reference below if you are unsure.
  37. [TIP]
  38. ====
  39. In the jMonkeyEngine SDK, use menu:File[New File > Material > Empty Material File] to create .j3m files. You can edit .j3m files directly in the SDK. On the other hand, they are plain text files, so you can also create them in any plain text editor.
  40. ====
  41. == How to Use .j3m Materials
  42. This is how you use the prepared .j3m Material on a Spatial. Since you have saved the .j3m file to your project's Assets directory, the .j3m path is relative to `MyGame/src/assets/…`.
  43. [source,java]
  44. ----
  45. myGeometry.setMaterial(assetManager.loadMaterial("Materials/SimpleBump.j3m"));
  46. ----
  47. [TIP]
  48. ====
  49. In the jMonkeyEngine SDK, open menu:Windows[Palette] and drag the `JME Material: Set J3M` snippet into your code.
  50. ====
  51. == Syntax Reference for .j3m Files
  52. === Paths
  53. Make sure to get the paths to the textures (.png, .jpg) and material definitions (.j3md) right.
  54. * The paths to the built-in .j3md files are relative to jME3's Core Data directory. Just copy the path stated in the <<jme3/advanced/materials_overview#,Material>> table. +
  55. `Common/MatDefs/Misc/Unshaded.j3md` is resolved to `jme3/src/src/core-data/Common/MatDefs/Misc/Unshaded.j3md`.
  56. * The paths to your textures are relative to your project's assets directory. +
  57. `Textures/bump_rock_normal.png` is resolved to `MyGame/src/assets/Textures/bump_rock_normal.png`
  58. === Data Types
  59. All data types (except Color) are specified in com.jme3.shader.VarType.
  60. "`Color`" is specified as Vector4 in J3MLoader.java.
  61. [cols="3", options="header"]
  62. |===
  63. a|Name
  64. a|jME Java class
  65. a|.j3m file syntax
  66. a| Float
  67. a| (basic Java type)
  68. a| a float (e.g. 0.72) , no comma or parentheses
  69. a| Vector2
  70. a| `com.jme3.math.Vector2f`
  71. a| Two floats, no comma or parentheses
  72. a| Vector3
  73. a| `com.jme3.math.Vector3f`
  74. a| Three floats, no comma or parentheses
  75. a| Vector4
  76. a| `com.jme3.math.Vector4f`
  77. a| Four floats, no comma or parentheses
  78. a| Texture2D
  79. a| `com.jme3.texture.Texture2D`
  80. a| Path to texture in `assets` directory, no quotation marks
  81. a| Texture3D
  82. a| `com.jme3.texture.Texture3D`
  83. a| Same as texture 2D except it is interpreted as a 3D texture
  84. a| TextureCubeMap
  85. a| `com.jme3.texture.TextureCubeMap`
  86. a| Same as texture 2D except it is interpreted as a cubemap texture
  87. a| Boolean
  88. a| (basic Java type)
  89. a| `true` or `false`
  90. a| Int
  91. a| (basic Java type)
  92. a| Integer number, no comma or parentheses
  93. a| Color
  94. a| `com.jme3.math.ColorRGBA`
  95. a| Four floats, no comma or parentheses
  96. a| FloatArray
  97. a|
  98. a| (Currently not supported in J3M)
  99. a| Vector2Array
  100. a|
  101. a| (Currently not supported in J3M)
  102. a| Vector3Array
  103. a|
  104. a| (Currently not supported in J3M)
  105. a| Vector4Array
  106. a|
  107. a| (Currently not supported in J3M)
  108. a| Matrix3
  109. a|
  110. a| (Currently not supported in J3M)
  111. a| Matrix4
  112. a|
  113. a| (Currently not supported in J3M)
  114. a| Matrix3Array
  115. a|
  116. a| (Currently not supported in J3M)
  117. a| Matrix4Array
  118. a|
  119. a| (Currently not supported in J3M)
  120. a| TextureBuffer
  121. a|
  122. a| (Currently not supported in J3M)
  123. a| TextureArray
  124. a|
  125. a| (Currently not supported in J3M)
  126. |===
  127. === Flip and Repeat Syntax
  128. * A texture can be flipped using the following syntax `NormalMap: Flip Textures/bump_rock_normal.png`
  129. * A texture can be set to repeat using the following syntax `NormalMap: Repeat Textures/bump_rock_normal.png`
  130. * If a texture is set to both being flipped and repeated, Flip must come before Repeat
  131. === Syntax for Additional Render States
  132. * A Boolean can be "`On`" or "`Off`"
  133. * Float is "`123.0`" etc
  134. * Enum - values depend on the enum
  135. See the link:{link-javadoc}/com/jme3/material/RenderState.html[RenderState] javadoc for a detailed explanation of render states.
  136. [cols="3", options="header"]
  137. |===
  138. a|Name
  139. a|Type
  140. a|Purpose
  141. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setWireframe-boolean-[Wireframe]
  142. a|(Boolean)
  143. a| Enable wireframe rendering mode
  144. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setFaceCullMode-com.jme3.material.RenderState.FaceCullMode-[FaceCull]
  145. a|(Enum: FaceCullMode)
  146. a| Set face culling mode (Off, Front, Back, FrontAndBack)
  147. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setDepthWrite-boolean-[DepthWrite]
  148. a|(Boolean)
  149. a| Enable writing depth to the depth buffer
  150. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setDepthTest-boolean-[DepthTest]
  151. a|(Boolean)
  152. a| Enable depth testing
  153. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setBlendMode-com.jme3.material.RenderState.BlendMode-[Blend]
  154. a|(Enum: BlendMode)
  155. a| Set the blending mode
  156. a| link:{link-javadoc}/com/jme3/material/Material.html#setFloat-java.lang.String-float-[AlphaDiscardThreshold]
  157. a|(Float)
  158. a| Set the alpha testing alpha falloff value (if set, it will enable alpha testing) +
  159. mat.setFloat("AlphaDiscardThreshold", 2f);
  160. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setPolyOffset-float-float-[PolyOffset]
  161. a|(Float, Float)
  162. a| Set the polygon offset factor and units
  163. a| link:{link-javadoc}/com/jme3/material/RenderState.html#setColorWrite-boolean-[ColorWrite]
  164. a|(Boolean)
  165. a| Enable color writing
  166. |===
  167. == Examples
  168. === Example 1: Shiny
  169. [source,java]
  170. ----
  171. Spatial signpost = (Spatial) assetManager.loadAsset(
  172. new OgreMeshKey("Models/Sign Post/Sign Post.mesh.xml", null));
  173. signpost.setMaterial( assetManager.loadMaterial(
  174. new AssetKey("Models/Sign Post/Sign Post.j3m")));
  175. TangentBinormalGenerator.generate(signpost);
  176. rootNode.attachChild(signpost);
  177. ----
  178. The file `assets/Models/Sign Post/Sign Post.j3m` contains:
  179. [source]
  180. ----
  181. Material Signpost : Common/MatDefs/Light/Lighting.j3md {
  182. MaterialParameters {
  183. Shininess: 4.0
  184. DiffuseMap: Models/Sign Post/Sign Post.jpg
  185. NormalMap: Models/Sign Post/Sign Post_normal.jpg
  186. SpecularMap: Models/Sign Post/Sign Post_specular.jpg
  187. UseMaterialColors : true
  188. Ambient : 0.5 0.5 0.5 1.0
  189. Diffuse : 1.0 1.0 1.0 1.0
  190. Specular : 1.0 1.0 1.0 1.0
  191. }
  192. }
  193. ----
  194. The JPG files are in the same directory, `assets/Models/Sign Post/…`.
  195. === Example 2: Repeating Texture
  196. [source,java]
  197. ----
  198. Material mat = assetManager.loadMaterial(
  199. "Textures/Terrain/Pond/Pond.j3m");
  200. mat.setColor("Ambient", ColorRGBA.DarkGray);
  201. mat.setColor("Diffuse", ColorRGBA.White);
  202. mat.setBoolean("UseMaterialColors", true);
  203. ----
  204. The file `assets/Textures/Terrain/Pond/Pond.j3m` contains:
  205. [source]
  206. ----
  207. Material Pong Rock : Common/MatDefs/Light/Lighting.j3md {
  208. MaterialParameters {
  209. Shininess: 8.0
  210. DiffuseMap: Repeat Textures/Terrain/Pond/Pond.png
  211. NormalMap: Repeat Textures/Terrain/Pond/Pond_normal.png
  212. }
  213. }
  214. ----
  215. The PNG files are in the same directory, `assets/Textures/Terrain/Pond/`
  216. === Example 3: Transparent
  217. The file `assets/Models/Tree/Leaves.j3m` contains:
  218. [source]
  219. ----
  220. Material Leaves : Common/MatDefs/Light/Lighting.j3md {
  221. Transparent On
  222. MaterialParameters {
  223. DiffuseMap : Models/Tree/Leaves.png
  224. UseAlpha : true
  225. AlphaDiscardThreshold : 0.5
  226. UseMaterialColors : true
  227. Ambient : .5 .5 .5 .5
  228. Diffuse : 0.7 0.7 0.7 1
  229. Specular : 0 0 0 1
  230. Shininess : 16
  231. }
  232. AdditionalRenderState {
  233. Blend Alpha
  234. AlphaTestFalloff 0.50
  235. FaceCull Off
  236. }
  237. }
  238. ----
  239. The PNG file is in the same directory, `assets/Models/Tree/…`
  240. == Related Links
  241. * <<jme3/advanced/material_specification#,Developer specification of the jME3 material system (.j3md,.j3m)>>