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 package com.jme3.asset; 33 34 import com.jme3.export.InputCapsule; 35 import com.jme3.export.JmeExporter; 36 import com.jme3.export.JmeImporter; 37 import com.jme3.export.OutputCapsule; 38 import com.jme3.texture.Texture.Type; 39 import com.jme3.texture.*; 40 import java.io.IOException; 41 import java.nio.ByteBuffer; 42 43 public class TextureKey extends AssetKey<Texture> { 44 45 private boolean generateMips; 46 private boolean flipY; 47 private boolean asCube; 48 private boolean asTexture3D; 49 private int anisotropy; 50 private Texture.Type textureTypeHint=Texture.Type.TwoDimensional; 51 TextureKey(String name, boolean flipY)52 public TextureKey(String name, boolean flipY) { 53 super(name); 54 this.flipY = flipY; 55 } 56 TextureKey(String name)57 public TextureKey(String name) { 58 super(name); 59 this.flipY = true; 60 } 61 TextureKey()62 public TextureKey() { 63 } 64 65 @Override toString()66 public String toString() { 67 return name + (flipY ? " (Flipped)" : "") + (asCube ? " (Cube)" : "") + (generateMips ? " (Mipmaped)" : ""); 68 } 69 70 /** 71 * Enable smart caching for textures 72 * @return true to enable smart cache 73 */ 74 @Override useSmartCache()75 public boolean useSmartCache() { 76 return true; 77 } 78 79 @Override createClonedInstance(Object asset)80 public Object createClonedInstance(Object asset) { 81 Texture tex = (Texture) asset; 82 return tex.createSimpleClone(); 83 } 84 85 @Override postProcess(Object asset)86 public Object postProcess(Object asset) { 87 Image img = (Image) asset; 88 if (img == null) { 89 return null; 90 } 91 92 Texture tex; 93 if (isAsCube()) { 94 if (isFlipY()) { 95 // also flip -y and +y image in cubemap 96 ByteBuffer pos_y = img.getData(2); 97 img.setData(2, img.getData(3)); 98 img.setData(3, pos_y); 99 } 100 tex = new TextureCubeMap(); 101 } else if (isAsTexture3D()) { 102 tex = new Texture3D(); 103 } else { 104 tex = new Texture2D(); 105 } 106 107 // enable mipmaps if image has them 108 // or generate them if requested by user 109 if (img.hasMipmaps() || isGenerateMips()) { 110 tex.setMinFilter(Texture.MinFilter.Trilinear); 111 } 112 113 tex.setAnisotropicFilter(getAnisotropy()); 114 tex.setName(getName()); 115 tex.setImage(img); 116 return tex; 117 } 118 isFlipY()119 public boolean isFlipY() { 120 return flipY; 121 } 122 getAnisotropy()123 public int getAnisotropy() { 124 return anisotropy; 125 } 126 setAnisotropy(int anisotropy)127 public void setAnisotropy(int anisotropy) { 128 this.anisotropy = anisotropy; 129 } 130 isAsCube()131 public boolean isAsCube() { 132 return asCube; 133 } 134 setAsCube(boolean asCube)135 public void setAsCube(boolean asCube) { 136 this.asCube = asCube; 137 } 138 isGenerateMips()139 public boolean isGenerateMips() { 140 return generateMips; 141 } 142 setGenerateMips(boolean generateMips)143 public void setGenerateMips(boolean generateMips) { 144 this.generateMips = generateMips; 145 } 146 isAsTexture3D()147 public boolean isAsTexture3D() { 148 return asTexture3D; 149 } 150 setAsTexture3D(boolean asTexture3D)151 public void setAsTexture3D(boolean asTexture3D) { 152 this.asTexture3D = asTexture3D; 153 } 154 155 @Override equals(Object other)156 public boolean equals(Object other) { 157 if (!(other instanceof TextureKey)) { 158 return false; 159 } 160 return super.equals(other) && isFlipY() == ((TextureKey) other).isFlipY(); 161 } 162 getTextureTypeHint()163 public Type getTextureTypeHint() { 164 return textureTypeHint; 165 } 166 setTextureTypeHint(Type textureTypeHint)167 public void setTextureTypeHint(Type textureTypeHint) { 168 this.textureTypeHint = textureTypeHint; 169 } 170 171 write(JmeExporter ex)172 public void write(JmeExporter ex) throws IOException { 173 super.write(ex); 174 OutputCapsule oc = ex.getCapsule(this); 175 oc.write(flipY, "flip_y", false); 176 oc.write(generateMips, "generate_mips", false); 177 oc.write(asCube, "as_cubemap", false); 178 oc.write(anisotropy, "anisotropy", 0); 179 } 180 181 @Override read(JmeImporter im)182 public void read(JmeImporter im) throws IOException { 183 super.read(im); 184 InputCapsule ic = im.getCapsule(this); 185 flipY = ic.readBoolean("flip_y", false); 186 generateMips = ic.readBoolean("generate_mips", false); 187 asCube = ic.readBoolean("as_cubemap", false); 188 anisotropy = ic.readInt("anisotropy", 0); 189 } 190 } 191