HelloMaterial.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2009-2012 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. package jme3test.helloworld;
  33. import com.jme3.app.SimpleApplication;
  34. import com.jme3.light.DirectionalLight;
  35. import com.jme3.material.Material;
  36. import com.jme3.material.RenderState.BlendMode;
  37. import com.jme3.math.ColorRGBA;
  38. import com.jme3.math.Vector3f;
  39. import com.jme3.renderer.queue.RenderQueue.Bucket;
  40. import com.jme3.scene.Geometry;
  41. import com.jme3.scene.shape.Box;
  42. import com.jme3.scene.shape.Sphere;
  43. import com.jme3.texture.Texture;
  44. import com.jme3.util.TangentBinormalGenerator;
  45. /** Sample 6 - how to give an object's surface a material and texture.
  46. * How to make objects transparent, or let colors "leak" through partially
  47. * transparent textures. How to make bumpy and shiny surfaces. */
  48. public class HelloMaterial extends SimpleApplication {
  49. public static void main(String[] args) {
  50. HelloMaterial app = new HelloMaterial();
  51. app.start();
  52. }
  53. @Override
  54. public void simpleInitApp() {
  55. /** A simple textured cube -- in good MIP map quality. */
  56. Box box1Mesh = new Box(1f,1f,1f);
  57. Geometry cubePlainGeo = new Geometry("My Textured Box", box1Mesh);
  58. Material stlMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  59. Texture monkeyTex = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
  60. stlMat.setTexture("ColorMap", monkeyTex);
  61. cubePlainGeo.setMaterial(stlMat);
  62. cubePlainGeo.move(new Vector3f(-3f,1.1f,0f));
  63. rootNode.attachChild(cubePlainGeo);
  64. /** A translucent/transparent texture, similar to a window frame. */
  65. Box box3Mesh = new Box(1f,1f,0.01f);
  66. Geometry windowGeo = new Geometry("window frame", box3Mesh);
  67. Material ttMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  68. ttMat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
  69. ttMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency
  70. windowGeo.setQueueBucket(Bucket.Transparent);
  71. windowGeo.setMaterial(ttMat);
  72. rootNode.attachChild(windowGeo);
  73. /** A cube with its base color "bleeding" through a partially transparent texture */
  74. Box box4Mesh = new Box(1f,1f,1f);
  75. Geometry bleedCubeGeo = new Geometry("bleed-through colored cube", box4Mesh);
  76. Material tlMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  77. tlMat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png"));
  78. tlMat.setColor("Color", new ColorRGBA(1f,0f,1f, 1f)); // purple
  79. bleedCubeGeo.setMaterial(tlMat);
  80. bleedCubeGeo.move(new Vector3f(3f,-1f,0f));
  81. rootNode.attachChild(bleedCubeGeo);
  82. //bleedCubeGeo.setMaterial((Material) assetManager.loadAsset( "Materials/BleedThrough.j3m"));
  83. /** A bumpy rock with a shiny light effect. To make bumpy objects you must create a NormalMap. */
  84. Sphere sphereMesh = new Sphere(32,32, 2f);
  85. Geometry shinyRockGeo = new Geometry("Shiny rock", sphereMesh);
  86. sphereMesh.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres
  87. TangentBinormalGenerator.generate(sphereMesh); // for lighting effect
  88. Material litMat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
  89. litMat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
  90. litMat.setTexture("NormalMap", assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
  91. litMat.setBoolean("UseMaterialColors",true);
  92. litMat.setColor("Specular",ColorRGBA.White);
  93. litMat.setColor("Diffuse",ColorRGBA.White);
  94. litMat.setFloat("Shininess", 64f); // [0,128]
  95. shinyRockGeo.setMaterial(litMat);
  96. shinyRockGeo.setLocalTranslation(0,2,-2); // Move it a bit
  97. shinyRockGeo.rotate(1.6f, 0, 0); // Rotate it a bit
  98. rootNode.attachChild(shinyRockGeo);
  99. /** Must add a light to make the lit object visible! */
  100. DirectionalLight sun = new DirectionalLight();
  101. sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
  102. sun.setColor(ColorRGBA.White);
  103. rootNode.addLight(sun);
  104. }
  105. }