package com.hello; 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.scene.Geometry; import com.jme3.scene.shape.Box; import com.jme3.scene.shape.Sphere; import com.jme3.texture.Texture; import com.jme3.util.TangentBinormalGenerator; import com.jme3.renderer.queue.RenderQueue.Bucket;
/** 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. */ //普通的材质纹理处理,加载了一张.jpg文件 Box boxshape1 = new Box(new Vector3f(-3f,1.1f,0f), 1f,1f,1f); Geometry cube = new Geometry("My Textured Box", boxshape1); Material mat_stl = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg"); mat_stl.setTexture("ColorMap", tex_ml); cube.setMaterial(mat_stl); rootNode.attachChild(cube);
/** A translucent/transparent texture, similar to a window frame. */ //一个透明半透明的纹理。 Box boxshape3 = new Box(new Vector3f(0f,0f,0f), 1f,1f,0.01f); Geometry window_frame = new Geometry("window frame", boxshape3); Material mat_tt = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat_tt.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png")); //这里是关键,获得额外的绘制状态,设置为混合模式Alpha mat_tt.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); window_frame.setMaterial(mat_tt); /** Objects with transparency need to be in the render bucket for transparent objects: */ window_frame.setQueueBucket(Bucket.Transparent); rootNode.attachChild(window_frame);
/** A cube with base color "leaking" through a partially transparent texture */ //带底色的透明纹理 Box boxshape4 = new Box(new Vector3f(3f,-1f,0f), 1f,1f,1f); Geometry cube_leak = new Geometry("Leak-through color cube", boxshape4); Material mat_tl = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat_tl.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png")); //没有用混合模式,而是直接着色 mat_tl.setColor("Color", new ColorRGBA(1f,0f,1f, 1f)); // purple cube_leak.setMaterial(mat_tl); rootNode.attachChild(cube_leak);
/** A bumpy rock with a shiny light effect */ //崎岖的岩石和光泽效果 Sphere rock = new Sphere(32,32, 2f); Geometry shiny_rock = new Geometry("Shiny rock", rock); //纹理模式,包括三种模式,效果不详 rock.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres //用正切某生成器生成该Sphere的效果 TangentBinormalGenerator.generate(rock); // for lighting effect 光线效果 Material mat_lit = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md"); //材质的几种效果 mat_lit.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.png")); mat_lit.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png")); mat_lit.setFloat("Shininess", 5f); // [0,128] //加载材质调整位置 shiny_rock.setMaterial(mat_lit); shiny_rock.setLocalTranslation(0,2,-2); // Move it a bit shiny_rock.rotate(1.6f, 0, 0); // Rotate it a bit rootNode.attachChild(shiny_rock); /** 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);
} } |