TextureKey.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. import com.jme3.asset.cache.AssetCache;
  34. import com.jme3.asset.cache.WeakRefCloneAssetCache;
  35. import com.jme3.export.InputCapsule;
  36. import com.jme3.export.JmeExporter;
  37. import com.jme3.export.JmeImporter;
  38. import com.jme3.export.OutputCapsule;
  39. import com.jme3.texture.Image;
  40. import com.jme3.texture.Texture;
  41. import com.jme3.texture.Texture.Type;
  42. import com.jme3.texture.TextureProcessor;
  43. import java.io.IOException;
  44. /**
  45. * Used to load textures from image files such as JPG or PNG.
  46. * Note that texture loaders actually load the asset as an {@link Image}
  47. * object, which is then converted to a {@link Texture} in the
  48. * {@link TextureProcessor#postProcess(com.jme3.asset.AssetKey, java.lang.Object) }
  49. * method. Since textures are cloneable smart assets, the texture stored
  50. * in the cache will be collected when all clones of the texture become
  51. * unreachable.
  52. *
  53. * @author Kirill Vainer
  54. */
  55. public class TextureKey extends AssetKey<Texture> {
  56. private boolean generateMips;
  57. private boolean flipY;
  58. private boolean asCube;
  59. private boolean asTexture3D;
  60. private int anisotropy;
  61. private Texture.Type textureTypeHint = Texture.Type.TwoDimensional;
  62. public TextureKey(String name, boolean flipY) {
  63. super(name);
  64. this.flipY = flipY;
  65. }
  66. public TextureKey(String name) {
  67. super(name);
  68. this.flipY = true;
  69. }
  70. public TextureKey() {
  71. }
  72. @Override
  73. public String toString() {
  74. return name + (flipY ? " (Flipped)" : "") + (asCube ? " (Cube)" : "") + (generateMips ? " (Mipmapped)" : "");
  75. }
  76. @Override
  77. public Class<? extends AssetCache> getCacheType(){
  78. return WeakRefCloneAssetCache.class;
  79. }
  80. @Override
  81. public Class<? extends AssetProcessor> getProcessorType(){
  82. return TextureProcessor.class;
  83. }
  84. public boolean isFlipY() {
  85. return flipY;
  86. }
  87. public int getAnisotropy() {
  88. return anisotropy;
  89. }
  90. public void setAnisotropy(int anisotropy) {
  91. this.anisotropy = anisotropy;
  92. }
  93. public boolean isAsCube() {
  94. return asCube;
  95. }
  96. public void setAsCube(boolean asCube) {
  97. this.asCube = asCube;
  98. }
  99. public boolean isGenerateMips() {
  100. return generateMips;
  101. }
  102. public void setGenerateMips(boolean generateMips) {
  103. this.generateMips = generateMips;
  104. }
  105. public boolean isAsTexture3D() {
  106. return asTexture3D;
  107. }
  108. public void setAsTexture3D(boolean asTexture3D) {
  109. this.asTexture3D = asTexture3D;
  110. }
  111. public Type getTextureTypeHint() {
  112. return textureTypeHint;
  113. }
  114. public void setTextureTypeHint(Type textureTypeHint) {
  115. this.textureTypeHint = textureTypeHint;
  116. }
  117. @Override
  118. public boolean equals(Object obj) {
  119. if (obj == null) {
  120. return false;
  121. }
  122. if (getClass() != obj.getClass()) {
  123. return false;
  124. }
  125. final TextureKey other = (TextureKey) obj;
  126. if (!super.equals(obj)) {
  127. return false;
  128. }
  129. if (this.generateMips != other.generateMips) {
  130. return false;
  131. }
  132. if (this.flipY != other.flipY) {
  133. return false;
  134. }
  135. if (this.asCube != other.asCube) {
  136. return false;
  137. }
  138. if (this.asTexture3D != other.asTexture3D) {
  139. return false;
  140. }
  141. if (this.anisotropy != other.anisotropy) {
  142. return false;
  143. }
  144. if (this.textureTypeHint != other.textureTypeHint) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. @Override
  150. public int hashCode() {
  151. int hash = 7;
  152. hash = 17 * hash + (super.hashCode());
  153. hash = 17 * hash + (this.generateMips ? 1 : 0);
  154. hash = 17 * hash + (this.flipY ? 1 : 0);
  155. hash = 17 * hash + (this.asCube ? 1 : 0);
  156. hash = 17 * hash + (this.asTexture3D ? 1 : 0);
  157. hash = 17 * hash + this.anisotropy;
  158. hash = 17 * hash + (this.textureTypeHint != null ? this.textureTypeHint.hashCode() : 0);
  159. return hash;
  160. }
  161. @Override
  162. public void write(JmeExporter ex) throws IOException {
  163. super.write(ex);
  164. OutputCapsule oc = ex.getCapsule(this);
  165. oc.write(flipY, "flip_y", false);
  166. oc.write(generateMips, "generate_mips", false);
  167. oc.write(asCube, "as_cubemap", false);
  168. oc.write(anisotropy, "anisotropy", 0);
  169. }
  170. @Override
  171. public void read(JmeImporter im) throws IOException {
  172. super.read(im);
  173. InputCapsule ic = im.getCapsule(this);
  174. flipY = ic.readBoolean("flip_y", false);
  175. generateMips = ic.readBoolean("generate_mips", false);
  176. asCube = ic.readBoolean("as_cubemap", false);
  177. anisotropy = ic.readInt("anisotropy", 0);
  178. }
  179. }