Aug 25, 2014

31. Binary j3o

The mesh file is a text file. The data has to be converted to binary before release, to run outside SDK.




You should right-click on the mesh.xml file and select the first option to convert to j3o binary.




This will create the mesh.j3o file inside the Textures, Pyramid folder. Move it to Models, Pyramid subfolder. This will get the project structure shown here.




To use the new file, we will have to change the load String.


// Load model
        Spatial pyramid = assetManager.loadModel(
                "Models/pyramid/pyramid.mesh.j3o");





30. UV Unwrap + Texture

Mesh surfaces are usually textured.




Texture is a 2D image, and the 3D surface has to be UV mapped into 2D space.




In Blender, we select all vertices in the Edit Mode. Then using Mesh header menu, we select UV Unwrap and Smart UV Project. The UV should now appear in the UV/Image Editor. We should create a new image file.




Next from the UVs header menu, we have to select Export UV Layout, and save the image.




Any image editing software can be used. We could even make the edits in UV/Image Editor in Blender. However, the software used most often is GIMP. The Bucket Fill tool quickly adds a pattern to the different slots for different surfaces. We have to make sure the patterns are tileable. If we need more control, we could also write a script to create a texture image.




In the GIMP, the different slot corresponding to different surfaces, are filled in with a suitable pattern.




After exporting the structure with a material and texture, we create a pyramid folder in Textures folder and put the files there, after renaming them.


// JMonkey30.java

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;

public class JMonkey30 extends SimpleApplication {
    public static void main(String[] args) {
        // App Settings
        AppSettings settings = new AppSettings(true);
        settings.setTitle("Pyramid!");
        settings.setFrameRate(60);
        JMonkey30 app = new JMonkey30();
        app.setSettings(settings);
        app.setDisplayFps(false);
        app.setDisplayStatView(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        flyCam.setEnabled(false); // disable camera inputs
        viewPort.setBackgroundColor(ColorRGBA.LightGray); // background
        // Load model
        Spatial pyramid = assetManager.loadModel(
                "Textures/pyramid/pyramid.mesh.xml");
        // Ambient light
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(ColorRGBA.White);
        rootNode.addLight(ambient);
        
        // Initial Rotation
        pyramid.rotate(45*FastMath.DEG_TO_RAD, 0, 0);
        rootNode.attachChild(pyramid);
    }

    @Override
    public void simpleUpdate(float tpf) {
        // Rotation per frame
        rootNode.rotate(0,0.005f,.005f);
    }

    @Override
    public void simpleRender(RenderManager rm) {
    }
}


Output: