hello_material.adoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. = jMonkeyEngine 3 Tutorial (6) - Hello Materials
  2. :author:
  3. :revnumber:
  4. :revdate: 2020/07/06
  5. :keywords: documentation, beginner, intro, model, material, color, texture, transparency
  6. 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 xref:beginner/hello_node.adoc[Hello Node]. Loading models that come with materials is covered in xref:beginner/hello_asset.adoc[Hello Asset]. In this tutorial you learn to create and use custom JME3 Material Definitions.
  7. image::beginner/beginner-materials.png[beginner-materials.png,320,240,align="center"]
  8. include::partial$add-testdata-tip.adoc[]
  9. == Sample Code
  10. [source,java]
  11. ----
  12. package jme3test.helloworld;
  13. import com.jme3.app.SimpleApplication;
  14. import com.jme3.light.DirectionalLight;
  15. import com.jme3.material.Material;
  16. import com.jme3.material.RenderState.BlendMode;
  17. import com.jme3.math.ColorRGBA;
  18. import com.jme3.math.Vector3f;
  19. import com.jme3.renderer.queue.RenderQueue.Bucket;
  20. import com.jme3.scene.Geometry;
  21. import com.jme3.scene.shape.Box;
  22. import com.jme3.scene.shape.Sphere;
  23. import com.jme3.texture.Texture;
  24. import com.jme3.util.TangentBinormalGenerator;
  25. /** Sample 6 - how to give an object's surface a material and texture.
  26. * How to make objects transparent. How to make bumpy and shiny surfaces. */
  27. public class HelloMaterial extends SimpleApplication {
  28. public static void main(String[] args) {
  29. HelloMaterial app = new HelloMaterial();
  30. app.start();
  31. }
  32. @Override
  33. public void simpleInitApp() {
  34. /** A simple textured cube -- in good MIP map quality. */
  35. Box cube1Mesh = new Box( 1f,1f,1f);
  36. Geometry cube1Geo = new Geometry("My Textured Box", cube1Mesh);
  37. cube1Geo.setLocalTranslation(new Vector3f(-3f,1.1f,0f));
  38. Material cube1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  39. Texture cube1Tex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
  40. cube1Mat.setTexture("ColorMap", cube1Tex);
  41. cube1Geo.setMaterial(cube1Mat);
  42. rootNode.attachChild(cube1Geo);
  43. /** A translucent/transparent texture, similar to a window frame. */
  44. Box cube2Mesh = new Box( 1f,1f,0.01f);
  45. Geometry cube2Geo = new Geometry("window frame", cube2Mesh);
  46. Material cube2Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  47. cube2Mat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
  48. cube2Mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency
  49. cube2Geo.setQueueBucket(Bucket.Transparent);
  50. cube2Geo.setMaterial(cube2Mat);
  51. rootNode.attachChild(cube2Geo);
  52. /* A bumpy rock with a shiny light effect. To make bumpy objects you must create a NormalMap. */
  53. Sphere sphereMesh = new Sphere(32,32, 2f);
  54. Geometry sphereGeo = new Geometry("Shiny rock", sphereMesh);
  55. sphereMesh.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
  56. TangentBinormalGenerator.generate(sphereMesh); // for lighting effect
  57. Material sphereMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  58. sphereMat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
  59. sphereMat.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
  60. sphereMat.setBoolean("UseMaterialColors",true);
  61. sphereMat.setColor("Diffuse",ColorRGBA.White);
  62. sphereMat.setColor("Specular",ColorRGBA.White);
  63. sphereMat.setFloat("Shininess", 64f); // [0,128]
  64. sphereGeo.setMaterial(sphereMat);
  65. //sphereGeo.setMaterial((Material) assetManager.loadMaterial("Materials/MyCustomMaterial.j3m"));
  66. sphereGeo.setLocalTranslation(0,2,-2); // Move it a bit
  67. sphereGeo.rotate(1.6f, 0, 0); // Rotate it a bit
  68. rootNode.attachChild(sphereGeo);
  69. /** Must add a light to make the lit object visible! */
  70. DirectionalLight sun = new DirectionalLight();
  71. sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
  72. sun.setColor(ColorRGBA.White);
  73. rootNode.addLight(sun);
  74. }
  75. }
  76. ----
  77. You should see
  78. * Left – A cube with a brown monkey texture.
  79. * Right – A translucent monkey picture in front of a shiny bumpy rock.
  80. Move around with the WASD keys to have a closer look at the translucency, and the rock's bumpiness.
  81. == Simple Unshaded Texture
  82. 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.
  83. [source,java]
  84. ----
  85. /** A simple textured cube -- in good MIP map quality. */
  86. Box cube1Mesh = new Box( 1f,1f,1f);
  87. Geometry cube1Geo = new Geometry("My Textured Box", cube1Mesh);
  88. cube1Geo.setLocalTranslation(new Vector3f(-3f,1.1f,0f));
  89. Material cube1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  90. Texture cube1Tex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
  91. cube1Mat.setTexture("ColorMap", cube1Tex);
  92. cube1Geo.setMaterial(cube1Mat);
  93. rootNode.attachChild(cube1Geo);
  94. ----
  95. Here is what we did: to create a textured box:
  96. . Create a Geometry `cube1Geo` from a Box mesh `cube1Mesh`.
  97. . Create a Material `cube1Mat` based on jME3's default `Unshaded.j3md` material definition.
  98. . Create a texture `cube1Tex` from the `Monkey.jpg` file in the `assets/Interface/Logo/` directory of the project.
  99. . Load the texture `cube1Tex` into the `ColorMap` layer of the material `cube1Mat`.
  100. . Apply the material to the cube, and attach the cube to the rootnode.
  101. == Transparent Unshaded Texture
  102. `Monkey.png` is the same texture as `Monkey.jpg`, 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.
  103. For a partially translucent/transparent texture, you need:
  104. * A Texture with alpha channel
  105. * A Texture with blend mode of `BlendMode.Alpha`
  106. * A Geometry in the `Bucket.Transparent` render bucket. +
  107. This bucket ensures that the transparent object is drawn on top of objects behind it, and they show up correctly under the transparent parts.
  108. [source,java]
  109. ----
  110. /** A translucent/transparent texture, similar to a window frame. */
  111. Box cube2Mesh = new Box( 1f,1f,0.01f);
  112. Geometry cube2Geo = new Geometry("window frame", cube2Mesh);
  113. Material cube2Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  114. cube2Mat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
  115. cube2Mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency
  116. cube2Geo.setQueueBucket(Bucket.Transparent);
  117. cube2Geo.setMaterial(cube2Mat);
  118. rootNode.attachChild(cube2Geo);
  119. ----
  120. 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't depend on the pixels under it, this is why opaque Geometries can be drawn in any order.
  121. What you did for the transparent texture is the same as before, with only one added step for the transparency.
  122. . Create a Geometry `cube2Geo` from a Box mesh `cube2Mesh`. This Box Geometry is flat upright box (because z=0.01f).
  123. . Create a Material `cube2Mat` based on jME3's default `Unshaded.j3md` material definition.
  124. . Create a texture `cube2Tex` from the `Monkey.png` file in the `assets/Textures/ColoredTex/` directory of the project. This PNG file must have an alpha layer.
  125. . *Activate transparency in the material by setting the blend mode to Alpha.*
  126. . *Set the QueueBucket of the Geometry to `Bucket.Transparent`.*
  127. . Load the texture `cube2Tex` into the `ColorMap` layer of the material `cube2Mat`.
  128. . Apply the material to the cube, and attach the cube to the rootnode.
  129. [TIP]
  130. ====
  131. Learn more about creating PNG images with an alpha layer in the help system of your graphic editor.
  132. ====
  133. == Shininess and Bumpiness
  134. 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:
  135. In a lit material, the standard texture layer is referred to as _DiffuseMap_, any material can use this layer. A lit material can additionally have lighting effects such as _Shininess_ used together with the _SpecularMap_ layer and _Specular_ color. And you can even get a realistically bumpy or cracked surface with help of the _NormalMap_ layer.
  136. Let's have a look at the part of the code example where you create the shiny bumpy rock.
  137. . Create a Geometry from a Sphere shape. Note that this shape is a normal smooth sphere mesh.
  138. +
  139. [source,java]
  140. ----
  141. Sphere sphereMesh = new Sphere(32,32, 2f);
  142. Geometry sphereGeo = new Geometry("Shiny rock", sphereMesh);
  143. ----
  144. .. (Only for Spheres) Change the sphere's TextureMode to make the square texture project better onto the sphere.
  145. +
  146. [source,java]
  147. ----
  148. sphereMesh.setTextureMode(Sphere.TextureMode.Projected);
  149. ----
  150. .. You must generate TangentBinormals for the mesh so you can use the NormalMap layer of the texture.
  151. +
  152. [source,java]
  153. ----
  154. TangentBinormalGenerator.generate(sphereMesh);
  155. ----
  156. . Create a material based on the `Lighting.j3md` default material.
  157. +
  158. [source,java]
  159. ----
  160. Material sphereMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  161. ----
  162. .. Set a standard rocky texture in the `DiffuseMap` layer.
  163. +
  164. image::https://github.com/jMonkeyEngine/jmonkeyengine/raw/445f7ed010199d30c484fe75bacef4b87f2eb38e/jme3-testdata/src/main/resources/Textures/Terrain/Pond/Pond.jpg[Pond.jpg,64,64,align="right"]
  165. +
  166. [source,java]
  167. ----
  168. sphereMat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
  169. ----
  170. .. Set the `NormalMap` layer that contains the bumpiness. The NormalMap was generated for this particular DiffuseMap with a special tool (e.g. Blender).
  171. +
  172. image::https://github.com/jMonkeyEngine/jmonkeyengine/raw/445f7ed010199d30c484fe75bacef4b87f2eb38e/jme3-testdata/src/main/resources/Textures/Terrain/Pond/Pond_normal.png[Pond_normal.png,64,64,align="right"]
  173. +
  174. [source,java]
  175. ----
  176. sphereMat.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
  177. ----
  178. .. Set the Material'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.
  179. +
  180. [source,java]
  181. ----
  182. sphereMat.setBoolean("UseMaterialColors",true);
  183. sphereMat.setColor("Diffuse",ColorRGBA.White); // minimum material color
  184. sphereMat.setColor("Specular",ColorRGBA.White); // for shininess
  185. sphereMat.setFloat("Shininess", 64f); // [1,128] for shininess
  186. ----
  187. . Assign your newly created material to the Geometry.
  188. +
  189. [source,java]
  190. ----
  191. sphereGeo.setMaterial(sphereMat);
  192. ----
  193. . Let's move and rotate the geometry a bit to position it better.
  194. +
  195. [source,java]
  196. ----
  197. sphereGeo.setLocalTranslation(0,2,-2); // Move it a bit
  198. sphereGeo.rotate(1.6f, 0, 0); // Rotate it a bit
  199. rootNode.attachChild(sphereGeo);
  200. ----
  201. Remember that any Lighting.j3md-based material requires a light source, as shown in the full code sample above.
  202. [TIP]
  203. ====
  204. To deactivate Shininess, do not set `Shininess` to 0, but instead set the `Specular` color to `ColorRGBA.Black`.
  205. ====
  206. == Default Material Definitions
  207. As you have seen, you can find the following default materials in `jme/core-data/Common/MatDefs/…`.
  208. [cols="20,40,40", options="header"]
  209. |===
  210. a| Default Definition
  211. a| Usage
  212. <a| Parameters
  213. a| `Misc/Unshaded.j3md`
  214. a| Colored: Use with mat.setColor() and ColorRGBA. +
  215. Textured: Use with mat.setTexture() and Texture.
  216. a| Color : Color +
  217. ColorMap : Texture2D
  218. <a| `Light/Lighting.j3md`
  219. a| Use with shiny Textures, Bump- and NormalMaps textures. +
  220. Requires a light source.
  221. a| Ambient, Diffuse, Specular : Color +
  222. DiffuseMap, NormalMap, SpecularMap : Texture2D +
  223. Shininess : Float
  224. |===
  225. For a game, you create custom Materials based on these existing MaterialDefintions – as you have just seen in the example with the shiny rock's material.
  226. == Exercises
  227. === Exercise 1: Custom .j3m Material
  228. Look at the shiny rocky sphere above again. It takes several lines to create and set the Material.
  229. * Note how it loads the `Lighting.j3md` Material definition.
  230. * Note how it sets the `DiffuseMap` and `NormalMap` to a texture path.
  231. * Note how it activates `UseMaterialColors` and sets `Specular` and `Diffuse` to 4 float values (RGBA color).
  232. * Note how it sets `Shininess` to 64.
  233. 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.
  234. You create a j3m file as follows:
  235. . Create a plain text file `assets/Materials/MyCustomMaterial.j3m` in your project directory, with the following content:
  236. +
  237. [source]
  238. ----
  239. Material My shiny custom material : Common/MatDefs/Light/Lighting.j3md {
  240. MaterialParameters {
  241. DiffuseMap : Textures/Terrain/Pond/Pond.jpg
  242. NormalMap : Textures/Terrain/Pond/Pond_normal.png
  243. UseMaterialColors : true
  244. Specular : 1.0 1.0 1.0 1.0
  245. Diffuse : 1.0 1.0 1.0 1.0
  246. Shininess : 64.0
  247. }
  248. }
  249. ----
  250. ** Note that `Material` is a fixed keyword.
  251. ** Note that `My shiny custom material` is a String that you can choose to describe the material.
  252. ** Note how the code sets all the same properties as before!
  253. . In the code sample, comment out the eight lines that have `sphereMat` in them.
  254. . Below this line, add the following line:
  255. +
  256. [source,java]
  257. ----
  258. sphereGeo.setMaterial((Material) assetManager.loadMaterial("Materials/MyCustomMaterial.j3m"));
  259. ----
  260. . Run the app. The result is the same.
  261. Using this new custom material `MyCustomMaterial.j3m` 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.
  262. === Exercise 2: Bumpiness and Shininess
  263. Go back to the bumpy rock sample above:
  264. . Comment out the DiffuseMap line, and run the app. (Uncomment it again.)
  265. ** Which property of the rock is lost?
  266. . Comment out the NormalMap line, and run the app. (Uncomment it again.)
  267. ** Which property of the rock is lost?
  268. . Change the value of Shininess to values like 0, 63, 127.
  269. ** What aspect of the Shininess changes?
  270. == Conclusion
  271. 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's `assets/Textures/` directory.
  272. 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's `assets/Materials/` directory.
  273. *See also:*
  274. * xref:core:material/how_to_use_materials.adoc[How to Use Materials]
  275. * xref:sdk:material_editing.adoc[Material Editing]
  276. * link:https://hub.jmonkeyengine.org/t/jmonkeyengine3-material-system-full-explanation/12947[Materials] forum thread
  277. //* link:http://nbviewer.jupyter.org/github/jMonkeyEngine/wiki/blob/master/src/docs/resources/tutorials/material/jME3_materials.pdf[jME3 Materials documentation (PDF)]
  278. * link: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?]
  279. * link:https://www.blender.org/support/tutorials/[Blender tutorials]