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.
Unshaded.j3md
.Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
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));
myGeometry.setMaterial(mat);
geometry.scaleTextureCoordinates(new Vector2f(1f, .5f));
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.