| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- * Copyright (c) 2009-2012 jMonkeyEngine
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package jme3test.helloworld;
- import com.jme3.app.SimpleApplication;
- import com.jme3.light.DirectionalLight;
- import com.jme3.material.Material;
- import com.jme3.material.RenderState.BlendMode;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.queue.RenderQueue.Bucket;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.shape.Box;
- import com.jme3.scene.shape.Sphere;
- import com.jme3.texture.Texture;
- import com.jme3.util.TangentBinormalGenerator;
- /** Sample 6 - how to give an object's surface a material and texture.
- * How to make objects transparent, or let colors "leak" through partially
- * transparent textures. How to make bumpy and shiny surfaces. */
- public class HelloMaterial extends SimpleApplication {
- public static void main(String[] args) {
- HelloMaterial app = new HelloMaterial();
- app.start();
- }
- @Override
- public void simpleInitApp() {
- /** A simple textured cube -- in good MIP map quality. */
- Box box1Mesh = new Box(1f,1f,1f);
- Geometry cubePlainGeo = new Geometry("My Textured Box", box1Mesh);
- Material stlMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- Texture monkeyTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
- stlMat.setTexture("ColorMap", monkeyTex);
- cubePlainGeo.setMaterial(stlMat);
- cubePlainGeo.move(new Vector3f(-3f,1.1f,0f));
- rootNode.attachChild(cubePlainGeo);
- /** A translucent/transparent texture, similar to a window frame. */
- Box box3Mesh = new Box(1f,1f,0.01f);
- Geometry windowGeo = new Geometry("window frame", box3Mesh);
- Material ttMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- ttMat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
- ttMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency
- windowGeo.setQueueBucket(Bucket.Transparent);
- windowGeo.setMaterial(ttMat);
- rootNode.attachChild(windowGeo);
- /** A cube with its base color "bleeding" through a partially transparent texture */
- Box box4Mesh = new Box(1f,1f,1f);
- Geometry bleedCubeGeo = new Geometry("bleed-through colored cube", box4Mesh);
- Material tlMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- tlMat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
- tlMat.setColor("Color", new ColorRGBA(1f,0f,1f, 1f)); // purple
- bleedCubeGeo.setMaterial(tlMat);
- bleedCubeGeo.move(new Vector3f(3f,-1f,0f));
- rootNode.attachChild(bleedCubeGeo);
- //bleedCubeGeo.setMaterial((Material) assetManager.loadAsset( "Materials/BleedThrough.j3m"));
- /** A bumpy rock with a shiny light effect. To make bumpy objects you must create a NormalMap. */
- Sphere sphereMesh = new Sphere(32,32, 2f);
- Geometry shinyRockGeo = new Geometry("Shiny rock", sphereMesh);
- sphereMesh.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
- TangentBinormalGenerator.generate(sphereMesh); // for lighting effect
- Material litMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
- litMat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
- litMat.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
- litMat.setBoolean("UseMaterialColors",true);
- litMat.setColor("Specular",ColorRGBA.White);
- litMat.setColor("Diffuse",ColorRGBA.White);
- litMat.setFloat("Shininess", 64f); // [0,128]
- shinyRockGeo.setMaterial(litMat);
- shinyRockGeo.setLocalTranslation(0,2,-2); // Move it a bit
- shinyRockGeo.rotate(1.6f, 0, 0); // Rotate it a bit
- rootNode.attachChild(shinyRockGeo);
-
- /** Must add a light to make the lit object visible! */
- DirectionalLight sun = new DirectionalLight();
- sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
- sun.setColor(ColorRGBA.White);
- rootNode.addLight(sun);
- }
- }
|