We can apply texture images to materials.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7WSXcgeklH8xP21bgqipMzNR5Mt5D7a2oJI2_ulqiSYrQDyN6JovtN-2BJa_CQLn_Krvwb47hhY0vGDdlcjs6pkjK7zkwJ4kOlgi8Wr260w8Ldi4scdix-xeX1z4vUmeI7pxikXXMeCQS/s1600/java26+-+1.png)
First some application settings are changed, such as the title. We also disable the display of stats and fps. We had earlier used a different method.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN5Qn8PSnKbWclCWOU5VohicX6RqQGOd_gxTaLMTKQwy1fx2noNR9SWskgFfwMj3MkpMbOfm8ZwpZtZrW7yry7nuQqdE8cOeHxERZa3EBWPPyXJ5kwZxthYH0MXlCCHLfh5yDg6i0fATun/s1600/java26+-+2.png)
// *** 1. Start (Application settings, start) AppSettings setting= new AppSettings(true); setting.setTitle("Brick Texture!"); JMonkey26 app = new JMonkey26(); app.setDisplayFps(false); app.setDisplayStatView(false); app.setSettings(setting); app.start(); // *** 1. End
The background is set as white. A box of dimension 2 by 2 by 2 is created. The material is unshaded. It next obtains a texture.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMbHdEZiVmogGGesLM1BrxhiRpOBqydap3tiiQJi9lSnPRIOlUuYLUN3oJ9FgtVcQdH8bcZ1YMEvpyWzta7O4SF9uoS3zHMin2nFvXrH9sZld9RPNTorpeyFtbWj7AfobWMAJWOC4dbxhy/s1600/java26+-+3.png)
// *** 2. Start (Background, box, material) viewPort.setBackgroundColor(ColorRGBA.White); Box b = new Box(1, 1, 1); Geometry geom = new Geometry("Box", b); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // *** 2. End
The texture provides a color map so it is not a single color. The file 'brick.jpg' is copied to the Interface folder in the project.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAaXHSo8G891OKVwy8QKHAylXEl4KmjVy6TrDBm_U_Wpr_HryKbpwTfBksfLIY-9X7HzcJPs8LNG3T3IWGPqg1hAKuGqjwc_WTijE5w8ERfVG8_W3RytklwjlAkiDO7yTi2Humx95UrDrC/s1600/java26+-+4.png)
// *** 3. Start (Texture) Texture tex = assetManager.loadTexture("Interface/brick.jpg"); mat.selectTechnique("Default", renderManager); mat.setColor("Color", ColorRGBA.White); mat.setTexture("ColorMap", tex); geom.setMaterial(mat); // *** 3. End
In the simpleUpdate, which is run for every frame, the rotation angle is slightly changed, randomly in either x, y, or z direction.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdAlaKivkKagJ8rPn6lbY6t2VRNOgdiuPn3Tb0_7RHJ40QhV-hfP_K6pfvSy0WLEChrO1yVm6C0K1iZdY2eiaTMy2nVp21eBvbF4eD9pP6Qw2p-m_8aiZmQhqLPAdCImGI7u-YiWkMnoYM/s1600/java26+-+5.png)
// *** 4. Start (Random Rotate) switch (FastMath.nextRandomInt(0, 2)) { case 0: rootNode.rotate(30*tpf*FastMath.DEG_TO_RAD,0,0); break; case 1: rootNode.rotate(0, 30*tpf*FastMath.DEG_TO_RAD, 0); break; case 2: rootNode.rotate(0,0,30*tpf*FastMath.DEG_TO_RAD); break; } // *** 4. End
// JMonkey26.java package mygame; import com.jme3.app.SimpleApplication; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.FastMath; import com.jme3.renderer.RenderManager; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Box; import com.jme3.system.AppSettings; import com.jme3.texture.Texture; public class JMonkey26 extends SimpleApplication { public static void main(String[] args) { // *** 1. Start (Application settings, start) AppSettings setting= new AppSettings(true); setting.setTitle("Brick Texture!"); JMonkey26 app = new JMonkey26(); app.setDisplayFps(false); app.setDisplayStatView(false); app.setSettings(setting); app.start(); // *** 1. End } @Override public void simpleInitApp() { flyCam.setEnabled(false); // *** 2. Start (Background, box, material) viewPort.setBackgroundColor(ColorRGBA.White); Box b = new Box(1, 1, 1); Geometry geom = new Geometry("Box", b); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); // *** 2. End // *** 3. Start (Texture) Texture tex = assetManager.loadTexture("Interface/brick.jpg"); mat.selectTechnique("Default", renderManager); mat.setColor("Color", ColorRGBA.White); mat.setTexture("ColorMap", tex); geom.setMaterial(mat); // *** 3. End rootNode.attachChild(geom); } @Override public void simpleUpdate(float tpf) { // *** 4. Start (Random Rotate) switch (FastMath.nextRandomInt(0, 2)) { case 0: rootNode.rotate(30*tpf*FastMath.DEG_TO_RAD,0,0); break; case 1: rootNode.rotate(0, 30*tpf*FastMath.DEG_TO_RAD, 0); break; case 2: rootNode.rotate(0,0,30*tpf*FastMath.DEG_TO_RAD); break; } // *** 4. End } @Override public void simpleRender(RenderManager rm) { //TODO: add render code } }
Output:
No comments:
Post a Comment