private Image loadImageSafe(AssetManager assetManager, TextureKey texKey) { try { return assetManager.loadTexture(texKey).getImage(); } catch (AssetNotFoundException ex) { return null; } catch (AssetLoadException ex) { logger.log(Level.WARNING, "Error when loading image: " + texKey, ex); return null; } }
public static Image getPlaceholderImage(AssetManager assetManager){ return assetManager.loadTexture("Common/Textures/MissingTexture.png").getImage(); }
/** * Add a texture for a specific map name at the location of another existing texture (on the master map). * @param texture A texture to add to the atlas. * @param mapName A freely chosen map name that can be later retrieved as a Texture. * @param sourceTextureName Name of the master map used for the location. */ public void addTexture(Texture texture, String mapName, String sourceTextureName) { if (texture == null) { throw new IllegalStateException("Texture cannot be null!"); } String name = textureName(texture); if (texture.getImage() != null && name != null) { addImage(texture.getImage(), name, mapName, sourceTextureName); } else { throw new IllegalStateException("Texture has no asset key name!"); } }
/** * Add a texture for a specific map name * @param texture A texture to add to the atlas. * @param mapName A freely chosen map name that can be later retrieved as a Texture. The first map name supplied will be the master map. * @return false if the atlas is full. */ public boolean addTexture(Texture texture, String mapName) { if (texture == null) { throw new IllegalStateException("Texture cannot be null!"); } String name = textureName(texture); if (texture.getImage() != null && name != null) { return addImage(texture.getImage(), name, mapName, null); } else { throw new IllegalStateException("Texture has no asset key name!"); } }
/** * This method loads a image which is packed into the blender file. * It makes use of all the registered AssetLoaders * * @param inputStream * blender input stream * @param startPosition * position in the stream where the image data starts * @param flipY * if the image should be flipped (does not work with DirectX image) * @return loaded image or null if it could not be loaded * @deprecated This method has only been left in for API compability. * Use loadTexture instead */ public Image loadImage(AssetManager assetManager, BlenderInputStream inputStream, int startPosition, boolean flipY) { Texture tex = loadTexture(assetManager, inputStream, startPosition, flipY); if (tex == null) { return null; } else { return tex.getImage(); } }
private void checkSetTexture(Texture tex, boolean depth){ Image img = tex.getImage(); if (img == null) throw new IllegalArgumentException("Texture not initialized with RTT."); if (depth && !img.getFormat().isDepthFormat()) throw new IllegalArgumentException("Texture image format must be depth."); else if (!depth && img.getFormat().isDepthFormat()) throw new IllegalArgumentException("Texture image format must be color/luminance."); // check that resolution matches texture resolution if (width != img.getWidth() || height != img.getHeight()) throw new IllegalArgumentException("Texture image resolution " + "must match FB resolution"); if (samples != tex.getImage().getMultiSamples()) throw new IllegalStateException("Texture samples must match framebuffer samples"); }
@Override public void simpleUpdate(float tpf) { if (time < 0) { if (deletedTex != null) { deletedTex.getImage().resetObject(); } return; } time += tpf; if (time > 5) { System.out.println("Assiging texture to deleted object!"); deletedTex = assetManager.loadTexture("Interface/Logo/Monkey.png"); BufferUtils.destroyDirectBuffer(deletedTex.getImage().getData(0)); mat.setTexture("ColorMap", deletedTex); time = -1; } } }
/** * This method blends the texture. * * @param texture * the texture to be blended * @param textureBlender * blending definition for the texture * @param blenderContext * the blender context */ private void blend(Texture texture, TextureBlender textureBlender, BlenderContext blenderContext) { if (texture instanceof TriangulatedTexture) { ((TriangulatedTexture) texture).blend(textureBlender, null, blenderContext); } else if (texture instanceof Texture2D) { Image blendedImage = textureBlender.blend(texture.getImage(), null, blenderContext); texture.setImage(blendedImage); } else { throw new IllegalArgumentException("Invalid type for texture to blend!"); } }
/** * Creates a shared image object from a jME3 texture. * The returned image shares the same memory with the jME3 texture, changes * in one view are visible in the other view. * This can be used to modify textures and images directly from OpenCL * (e.g. for post processing effects and other texture effects). * <br> * <b>Note:</b> The image must already been uploaded to the GPU, * i.e. it must be used at least once for drawing. * <p> * Before the returned image can be used, it must be acquried explicitly * by {@link Image#acquireImageForSharingAsync(com.jme3.opencl.CommandQueue) } * and after modifying it, released by {@link Image#releaseImageForSharingAsync(com.jme3.opencl.CommandQueue) } * This is needed so that OpenGL and OpenCL operations do not interfer with each other. * <p> * This method is equivalent to calling * {@code bindImage(texture.getImage(), texture.getType(), miplevel, access)}. * * @param texture the jME3 texture * @param miplevel the mipmap level that should be shared * @param access the allowed memory access for kernels * @return the OpenCL image */ public Image bindImage(Texture texture, int miplevel, MemoryAccess access) { return bindImage(texture.getImage(), texture.getType(), miplevel, access); } /**
private void readTextureUnit(Statement statement){ String[] split = statement.getLine().split(" ", 2); // name is optional if (split.length == 2){ texName = split[1]; }else{ texName = null; } textures[texUnit] = new Texture2D(); for (Statement texUnitStat : statement.getContents()){ readTextureUnitStatement(texUnitStat); } if (textures[texUnit].getImage() != null){ texUnit++; }else{ // no image was loaded, ignore textures[texUnit] = null; } }
@Override public void setTexture(int unit, Texture tex) { Image image = tex.getImage(); if (image.isUpdateNeeded() || (image.isGeneratedMipmapsRequired() && !image.isMipmapsGenerated())) { // Check NPOT requirements boolean scaleToPot = false; try { checkNonPowerOfTwo(tex); } catch (RendererException ex) { if (logger.isLoggable(Level.WARNING)) { int nextWidth = FastMath.nearestPowerOfTwo(tex.getImage().getWidth()); int nextHeight = FastMath.nearestPowerOfTwo(tex.getImage().getHeight()); logger.log(Level.WARNING, "Non-power-of-2 textures are not supported! Scaling texture '" + tex.getName() + "' of size " + tex.getImage().getWidth() + "x" + tex.getImage().getHeight() + " to " + nextWidth + "x" + nextHeight); } scaleToPot = true; } updateTexImageData(image, tex.getType(), unit, scaleToPot); } int texId = image.getId(); assert texId != -1; setupTextureParams(unit, tex); }
protected static String formatMatParamTexture(MatParamTexture param) { StringBuilder ret = new StringBuilder(); Texture tex = (Texture) param.getValue(); TextureKey key; if (tex != null) { key = (TextureKey) tex.getKey(); if (key != null && key.isFlipY()) { ret.append("Flip "); } ret.append(formatWrapMode(tex, Texture.WrapAxis.S)); ret.append(formatWrapMode(tex, Texture.WrapAxis.T)); ret.append(formatWrapMode(tex, Texture.WrapAxis.R)); //Min and Mag filter Texture.MinFilter def = Texture.MinFilter.BilinearNoMipMaps; if (tex.getImage().hasMipmaps() || (key != null && key.isGenerateMips())) { def = Texture.MinFilter.Trilinear; } if (tex.getMinFilter() != def) { ret.append("Min").append(tex.getMinFilter().name()).append(" "); } if (tex.getMagFilter() != Texture.MagFilter.Bilinear) { ret.append("Mag").append(tex.getMagFilter().name()).append(" "); } ret.append("\"").append(key.getName()).append("\""); } return ret.toString(); }
private void checkTextureParamColorSpace(String name, Texture value) { MatParamTexture paramDef = (MatParamTexture) def.getMaterialParam(name); if (paramDef.getColorSpace() != null && paramDef.getColorSpace() != value.getImage().getColorSpace()) { value.getImage().setColorSpace(paramDef.getColorSpace()); logger.log(Level.FINE, "Material parameter {0} needs a {1} texture, " + "texture {2} was switched to {3} color space.", new Object[]{name, paramDef.getColorSpace().toString(), value.getName(), value.getImage().getColorSpace().name()}); } else if (paramDef.getColorSpace() == null && value.getName() != null && value.getImage().getColorSpace() == ColorSpace.Linear) { logger.log(Level.WARNING, "The texture {0} has linear color space, but the material " + "parameter {2} specifies no color space requirement, this may " + "lead to unexpected behavior.\nCheck if the image " + "was not set to another material parameter with a linear " + "color space, or that you did not set the ColorSpace to " + "Linear using texture.getImage.setColorSpace().", new Object[]{value.getName(), value.getImage().getColorSpace().name(), name}); } }
image = loadedTex.getImage(); } catch (AssetNotFoundException ex){ Logger.getLogger(Texture.class.getName()).log(Level.SEVERE, "Cannot locate texture {0}", key);
@Override public void simpleInitApp() { // create a simple plane/quad Quad quadMesh = new Quad(1, 1); quadMesh.updateGeometry(1, 1, true); Geometry quad = new Geometry("Textured Quad", quadMesh); assetManager.registerLocator("https://raw.githubusercontent.com/jMonkeyEngine/BookSamples/master/assets/Textures/", UrlLocator.class); TextureKey key = new TextureKey("mucha-window.png", false); key.setGenerateMips(true); Texture tex = assetManager.loadTexture(key); Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat.setTexture("ColorMap", tex); quad.setMaterial(mat); float aspect = tex.getImage().getWidth() / (float) tex.getImage().getHeight(); quad.setLocalScale(new Vector3f(aspect * 1.5f, 1.5f, 1)); quad.center(); rootNode.attachChild(quad); }
Texture tex = environment.getApplication().getAssetManager().loadTexture(texture); mouseImage.setTexture(environment.getApplication().getAssetManager(), (Texture2D)tex, true); ySize = tex.getImage().getHeight(); mouseImage.setHeight(ySize); mouseImage.setWidth(tex.getImage().getWidth()); mouseImage.getMaterial().getAdditionalRenderState().setBlendMode(BlendMode.Alpha); mouseImage.getMaterial().getAdditionalRenderState().setDepthWrite(false); Texture tex = environment.getApplication().getAssetManager().loadTexture(texture); mouseImage.setTexture(environment.getApplication().getAssetManager(), (Texture2D)tex, true); ySize = tex.getImage().getHeight(); mouseImage.setHeight(ySize); mouseImage.setWidth(tex.getImage().getWidth()); mouseImage.getMaterial().getAdditionalRenderState().setBlendMode(BlendMode.Alpha); mouseImage.getMaterial().getAdditionalRenderState().setDepthWrite(false);
continue; Image image = texture.getImage(); if (image == null) { continue;
@Test public void testSortByTexture() { Material texture1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); Material texture2Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); Material texture3Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); Texture tex1 = createTexture("A"); tex1.getImage().setId(1); Texture tex2 = createTexture("B"); tex2.getImage().setId(2); Texture tex3 = createTexture("C"); tex3.getImage().setId(3); texture1Mat.setName("TexA"); texture1Mat.setTexture("ColorMap", tex1); texture2Mat.setName("TexB"); texture2Mat.setTexture("ColorMap", tex2); texture3Mat.setName("TexC"); texture3Mat.setTexture("ColorMap", tex3); testSort(texture1Mat, texture2Mat, texture3Mat); }
public void updateRenderTexture(FrameBuffer fb, RenderBuffer rb) { Texture tex = rb.getTexture(); Image image = tex.getImage(); if (image.isUpdateNeeded()) { // Check NPOT requirements checkNonPowerOfTwo(tex); updateTexImageData(image, tex.getType(), 0, false); // NOTE: For depth textures, sets nearest/no-mips mode // Required to fix "framebuffer unsupported" // for old NVIDIA drivers! setupTextureParams(0, tex); } if (rb.getLayer() < 0){ glfbo.glFramebufferTexture2DEXT(GLFbo.GL_FRAMEBUFFER_EXT, convertAttachmentSlot(rb.getSlot()), convertTextureType(tex.getType(), image.getMultiSamples(), rb.getFace()), image.getId(), 0); } else { glfbo.glFramebufferTextureLayerEXT(GLFbo.GL_FRAMEBUFFER_EXT, convertAttachmentSlot(rb.getSlot()), image.getId(), 0, rb.getLayer()); } }
Image imageCustomMip = texCustomMip.getImage().clone(); MipMapGenerator.generateMipMaps(imageCustomMip); texCustomMip.setImage(imageCustomMip);