= Saving and Loading Materials with .j3m Files
:author:
:revnumber:
:revdate: 2016/03/17 20:48
:keywords: material, texture, file, sdk, wireframe, documentation
:relfileprefix: ../../
:imagesdir: ../..
:experimental:
ifdef::env-github,env-browser[:outfilesuffix: .adoc]


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.

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.


== Writing the .j3m File

.  For every Material, create a file and give it a name that describes it: e.g. `SimpleBump.j3m`
.  Place the file in your project's `assets/Materials/` directory, e.g. `MyGame/src/assets/Materials/SimpleBump.j3m`
.  Edit the file and add content using the following Syntax, e.g.:
[source]
----

Material shiny bumpy rock : Common/MatDefs/Light/Lighting.j3md {
     MaterialParameters {
         Shininess: 8.0
         NormalMap: Textures/bump_rock_normal.png
         UseMaterialColors : true
         Ambient  : 0.0 0.0 0.0 1.0
         Diffuse  : 1.0 1.0 1.0 1.0
         Specular : 0.0 0.0 0.0 1.0
     }
}

----


How this file is structured:

.  Header
..  `Material` is a fixed keyword, keep it.
..  `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.
..  After the colon, specify on which <<jme3/advanced/materials_overview#,Material>> definition you base this Material.

.  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.
**  For example: The series of four numbers in the example above represent RGBA color values.

.  Check the detailed syntax reference below if you are unsure.


[TIP]
====
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.
====



== How to Use .j3m Materials

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/…`.

[source,java]
----
myGeometry.setMaterial(assetManager.loadMaterial("Materials/SimpleBump.j3m"));
----

[TIP]
====
In the jMonkeyEngine SDK, open menu:Windows[Palette] and drag the `JME Material: Set J3M` snippet into your code.
====


== Syntax Reference for .j3m Files


=== Paths

Make sure to get the paths to the textures (.png, .jpg) and material definitions (.j3md) right.

*  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. +
`Common/MatDefs/Misc/Unshaded.j3md` is resolved to `jme3/src/src/core-data/Common/MatDefs/Misc/Unshaded.j3md`.
*  The paths to your textures are relative to your project's assets directory. +
`Textures/bump_rock_normal.png` is resolved to `MyGame/src/assets/Textures/bump_rock_normal.png`


=== Data Types

All data types (except Color) are specified in com.jme3.shader.VarType.
"`Color`" is specified as Vector4 in J3MLoader.java.
[cols="3", options="header"]
|===

a|Name
a|jME Java class
a|.j3m file syntax

a| Float
a| (basic Java type)
a| a float (e.g. 0.72) , no comma or parentheses

a| Vector2
a| `com.jme3.math.Vector2f`
a| Two floats, no comma or parentheses

a| Vector3
a| `com.jme3.math.Vector3f`
a| Three floats, no comma or parentheses

a| Vector4
a| `com.jme3.math.Vector4f`
a| Four floats, no comma or parentheses

a| Texture2D
a| `com.jme3.texture.Texture2D`
a| Path to texture in `assets` directory, no quotation marks

a| Texture3D
a| `com.jme3.texture.Texture3D`
a| Same as texture 2D except it is interpreted as a 3D texture

a| TextureCubeMap
a| `com.jme3.texture.TextureCubeMap`
a| Same as texture 2D except it is interpreted as a cubemap texture

a| Boolean
a| (basic Java type)
a| `true` or `false`

a| Int
a| (basic Java type)
a| Integer number, no comma or parentheses

a| Color
a| `com.jme3.math.ColorRGBA`
a| Four floats, no comma or parentheses

a| FloatArray
a|
a| (Currently not supported in J3M)

a| Vector2Array
a|
a| (Currently not supported in J3M)

a| Vector3Array
a|
a| (Currently not supported in J3M)

a| Vector4Array
a|
a| (Currently not supported in J3M)

a| Matrix3
a|
a| (Currently not supported in J3M)

a| Matrix4
a|
a| (Currently not supported in J3M)

a| Matrix3Array
a|
a| (Currently not supported in J3M)

a| Matrix4Array
a|
a| (Currently not supported in J3M)

a| TextureBuffer
a|
a| (Currently not supported in J3M)

a| TextureArray
a|
a| (Currently not supported in J3M)

|===


=== Flip and Repeat Syntax

*  A texture can be flipped using the following syntax `NormalMap: Flip Textures/bump_rock_normal.png`
*  A texture can be set to repeat using the following syntax `NormalMap: Repeat Textures/bump_rock_normal.png`
*  If a texture is set to both being flipped and repeated, Flip must come before Repeat


=== Syntax for Additional Render States

*  A Boolean can be "`On`" or "`Off`"
*  Float is "`123.0`" etc
*  Enum - values depend on the enum

See the link:{link-javadoc}/com/jme3/material/RenderState.html[RenderState] javadoc for a detailed explanation of render states.
[cols="3", options="header"]
|===

a|Name
a|Type
a|Purpose

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setWireframe-boolean-[Wireframe]
a|(Boolean)
a| Enable wireframe rendering mode

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setFaceCullMode-com.jme3.material.RenderState.FaceCullMode-[FaceCull]
a|(Enum: FaceCullMode)
a| Set face culling mode (Off, Front, Back, FrontAndBack)

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setDepthWrite-boolean-[DepthWrite]
a|(Boolean)
a| Enable writing depth to the depth buffer

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setDepthTest-boolean-[DepthTest]
a|(Boolean)
a| Enable depth testing

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setBlendMode-com.jme3.material.RenderState.BlendMode-[Blend]
a|(Enum: BlendMode)
a| Set the blending mode

a| link:{link-javadoc}/com/jme3/material/Material.html#setFloat-java.lang.String-float-[AlphaDiscardThreshold]
a|(Float)
a| Set the alpha testing alpha falloff value (if set, it will enable alpha testing) +
 mat.setFloat("AlphaDiscardThreshold", 2f);

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setPolyOffset-float-float-[PolyOffset]
a|(Float, Float)
a| Set the polygon offset factor and units

a| link:{link-javadoc}/com/jme3/material/RenderState.html#setColorWrite-boolean-[ColorWrite]
a|(Boolean)
a| Enable color writing

|===


== Examples


=== Example 1: Shiny

[source,java]
----

Spatial signpost = (Spatial) assetManager.loadAsset(
    new OgreMeshKey("Models/Sign Post/Sign Post.mesh.xml", null));
signpost.setMaterial( assetManager.loadMaterial(
    new AssetKey("Models/Sign Post/Sign Post.j3m")));
TangentBinormalGenerator.generate(signpost);
rootNode.attachChild(signpost);

----

The file `assets/Models/Sign Post/Sign Post.j3m` contains:

[source]
----

Material Signpost : Common/MatDefs/Light/Lighting.j3md {
    MaterialParameters {
         Shininess: 4.0
         DiffuseMap:  Models/Sign Post/Sign Post.jpg
         NormalMap:   Models/Sign Post/Sign Post_normal.jpg
         SpecularMap: Models/Sign Post/Sign Post_specular.jpg
         UseMaterialColors : true
         Ambient  : 0.5 0.5 0.5 1.0
         Diffuse  : 1.0 1.0 1.0 1.0
         Specular : 1.0 1.0 1.0 1.0
    }
}

----

The JPG files are in the same directory, `assets/Models/Sign Post/…`.


=== Example 2: Repeating Texture

[source,java]
----

Material mat = assetManager.loadMaterial(
    "Textures/Terrain/Pond/Pond.j3m");
mat.setColor("Ambient", ColorRGBA.DarkGray);
mat.setColor("Diffuse", ColorRGBA.White);
mat.setBoolean("UseMaterialColors", true);

----

The file `assets/Textures/Terrain/Pond/Pond.j3m` contains:

[source]
----

Material Pong Rock : Common/MatDefs/Light/Lighting.j3md {
     MaterialParameters {
         Shininess: 8.0
         DiffuseMap: Repeat Textures/Terrain/Pond/Pond.png
         NormalMap:  Repeat Textures/Terrain/Pond/Pond_normal.png
     }
}

----

The PNG files are in the same directory, `assets/Textures/Terrain/Pond/`


=== Example 3: Transparent

The file `assets/Models/Tree/Leaves.j3m` contains:

[source]
----

Material Leaves : Common/MatDefs/Light/Lighting.j3md {

    Transparent On

    MaterialParameters {
        DiffuseMap : Models/Tree/Leaves.png
        UseAlpha : true
        AlphaDiscardThreshold : 0.5
        UseMaterialColors : true
        Ambient : .5 .5 .5 .5
        Diffuse : 0.7 0.7 0.7 1
        Specular : 0 0 0 1
        Shininess : 16
    }
    AdditionalRenderState {
        Blend Alpha
        AlphaTestFalloff 0.50
        FaceCull Off
    }
}

----

The PNG file is in the same directory, `assets/Models/Tree/…`


== Related Links

*  <<jme3/advanced/material_specification#,Developer specification of the jME3 material system (.j3md,.j3m)>>
