1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package jme3test.helloworld; 34 35 import com.jme3.app.SimpleApplication; 36 import com.jme3.light.DirectionalLight; 37 import com.jme3.material.Material; 38 import com.jme3.material.RenderState.BlendMode; 39 import com.jme3.math.ColorRGBA; 40 import com.jme3.math.Vector3f; 41 import com.jme3.renderer.queue.RenderQueue.Bucket; 42 import com.jme3.scene.Geometry; 43 import com.jme3.scene.shape.Box; 44 import com.jme3.scene.shape.Sphere; 45 import com.jme3.texture.Texture; 46 import com.jme3.util.TangentBinormalGenerator; 47 48 /** Sample 6 - how to give an object's surface a material and texture. 49 * How to make objects transparent, or let colors "leak" through partially 50 * transparent textures. How to make bumpy and shiny surfaces. */ 51 public class HelloMaterial extends SimpleApplication { 52 main(String[] args)53 public static void main(String[] args) { 54 HelloMaterial app = new HelloMaterial(); 55 app.start(); 56 } 57 58 @Override simpleInitApp()59 public void simpleInitApp() { 60 61 /** A simple textured cube -- in good MIP map quality. */ 62 Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f); 63 Geometry cube = new Geometry("My Textured Box", boxshape1); 64 Material mat_stl = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 65 Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg"); 66 mat_stl.setTexture("ColorMap", tex_ml); 67 cube.setMaterial(mat_stl); 68 rootNode.attachChild(cube); 69 70 /** A translucent/transparent texture, similar to a window frame. */ 71 Box boxshape3 = new Box(new Vector3f(0f,0f,0f), 1f,1f,0.01f); 72 Geometry window_frame = new Geometry("window frame", boxshape3); 73 Material mat_tt = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 74 mat_tt.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png")); 75 mat_tt.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency 76 window_frame.setQueueBucket(Bucket.Transparent); 77 window_frame.setMaterial(mat_tt); 78 rootNode.attachChild(window_frame); 79 80 /** A cube with its base color "leaking" through a partially transparent texture */ 81 Box boxshape4 = new Box(new Vector3f(3f,-1f,0f), 1f,1f,1f); 82 Geometry cube_leak = new Geometry("Leak-through color cube", boxshape4); 83 Material mat_tl = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); 84 mat_tl.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png")); 85 mat_tl.setColor("Color", new ColorRGBA(1f,0f,1f, 1f)); // purple 86 cube_leak.setMaterial(mat_tl); 87 rootNode.attachChild(cube_leak); 88 // cube_leak.setMaterial((Material) assetManager.loadAsset( "Materials/LeakThrough.j3m")); 89 90 /** A bumpy rock with a shiny light effect. To make bumpy objects you must create a NormalMap. */ 91 Sphere rock = new Sphere(32,32, 2f); 92 Geometry shiny_rock = new Geometry("Shiny rock", rock); 93 rock.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres 94 TangentBinormalGenerator.generate(rock); // for lighting effect 95 Material mat_lit = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); 96 mat_lit.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg")); 97 mat_lit.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png")); 98 mat_lit.setBoolean("UseMaterialColors",true); 99 mat_lit.setColor("Specular",ColorRGBA.White); 100 mat_lit.setColor("Diffuse",ColorRGBA.White); 101 mat_lit.setFloat("Shininess", 5f); // [0,128] 102 shiny_rock.setMaterial(mat_lit); 103 shiny_rock.setLocalTranslation(0,2,-2); // Move it a bit 104 shiny_rock.rotate(1.6f, 0, 0); // Rotate it a bit 105 rootNode.attachChild(shiny_rock); 106 107 /** Must add a light to make the lit object visible! */ 108 DirectionalLight sun = new DirectionalLight(); 109 sun.setDirection(new Vector3f(1,0,-2).normalizeLocal()); 110 sun.setColor(ColorRGBA.White); 111 rootNode.addLight(sun); 112 113 } 114 } 115