How to Use Material Definitions (.j3md)

Typically, you create a set of custom materials, and use them throughout the game. For example, you can initialize and configure your materials objects in the initSimpleApp() method, and then load 3D models (Geometries) and use setMaterial() on them.

Tip: If you use one custom material very often, additionally read about storing material configurations in user-friendly j3m Material Files.

Preparing a Material

  1. Choose a Material Definition from the Materials Overview list that has the features that you need.
    • Tip: If you don't know, you can always start with Unshaded.j3md.
  2. Look at the applicable parameters of the Material Definition and determine which ones you need to achieve the desired effect. Most parameters are optional.
    1. Create and save the necessary Texture files to the assets directory.
      • E.g. ColorMap; DiffuseMap, NormalMap, AlphaMap, etc…
    2. Determine the required values to achieve the effect that you want.
      • E.g. Colors, floats, booleans, etc…

Using a Material

  1. In you Java code, create a Material object based on the .j3md file: e.g.
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  2. Configure your Material by setting the appropriate values listed in the Materials Overview table. Here are examples of the methods that set the different data types:
    • mat.setColor( "Color", ColorRGBA.White );
    • mat.setTexture( "ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.png" ));
    • mat.setFloat( "Shininess", 5f);
    • mat.setBoolean( "SphereMap", true);
    • mat.setVector3( "NormalScale", new Vector3f(1f,1f,1f));
  3. Use your prepared material on a Geometry:
    myGeometry.setMaterial(mat);
  4. (Optional) Adjust the texture scale:
    geometry.scaleTextureCoordinates(new Vector2f(1f, .5f));

Examples

A simpled textured material.

Material mat = new Material(assetManager,
    "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture(
    "Interface/Logo/Monkey.jpg"));

A textured material with a color bleeding through transparent areas.

Material mat = new Material(assetManager,
    "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture(
    "Textures/ColoredTex/Monkey.png"));
mat.setColor("Color", ColorRGBA.Blue);

You can test these examples within the following code snippet. It creates a box and applies the material:

 Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry("Box", b);
// ... insert Material definition...
geom.setMaterial(mat);
rootNode.attachChild(geom);

Tip: You can find these and other common code snippets in the jMonkeyPlatform Code Palette. Drag and drop them into your source code.

view online version