private void reloadHeightmap() { logger.info("Reading height map '{}'", configuration.heightMap); ResourceUrn urn = new ResourceUrn("core", configuration.heightMap); Texture texture = Assets.getTexture(urn).get(); ByteBuffer[] bb = texture.getData().getBuffers(); IntBuffer intBuf = bb[0].asIntBuffer(); mapWidth = texture.getWidth(); mapHeight = texture.getHeight(); heightmap = new float[mapWidth][mapHeight]; while (intBuf.position() < intBuf.limit()) { int pos = intBuf.position(); long val = intBuf.get() & 0xFFFFFFFFL; heightmap[pos % mapWidth][pos / mapWidth] = val / (256 * 256 * 256 * 256f); } }
@Override public boolean isRenderable() { for (Texture texture : textureMap.valueCollection()) { if (!texture.isLoaded()) { return false; } } return true; }
public void beginRenderOverlay() { if (effectsTexture == null || !effectsTexture.isLoaded()) { return; } defaultTextured.activateFeature(ShaderProgramFeature.FEATURE_ALPHA_REJECT); defaultTextured.enable(); glBindTexture(GL11.GL_TEXTURE_2D, effectsTexture.getId()); glEnable(GL11.GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }
public void setEffectsTexture(Texture newEffectsTexture) { if ((effectsTexture.getWidth() == newEffectsTexture.getWidth()) && (effectsTexture.getHeight() == newEffectsTexture.getHeight())) { this.effectsTexture = newEffectsTexture; } else { // This should not be possible with the current BlockSelectionRenderSystem implementation throw new RuntimeException("New effectsTexture must have same height and width as the original effectsTexture"); } }
@Override protected void doReload(SubtextureData data) { data.getTexture().subscribeToDisposal(disposalAction); if (texture != null) { texture.unsubscribeToDisposal(disposalAction); } this.texture = data.getTexture(); this.subregion = data.getRegion(); }
public static BufferedImage convertToImage(TextureRegion textureRegion) { final int width = textureRegion.getWidth(); final int height = textureRegion.getHeight(); final Rect2i pixelRegion = textureRegion.getPixelRegion(); final Texture texture = textureRegion.getTexture(); ByteBuffer textureBytes = texture.getData().getBuffers()[0]; int stride = texture.getWidth() * 4; int posX = pixelRegion.minX(); int posY = pixelRegion.minY(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int r = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4)); int g = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 1)); int b = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 2)); int a = UnsignedBytes.toInt(textureBytes.get((posY + y) * stride + (posX + x) * 4 + 3)); int argb = (a << 24) + (r << 16) + (g << 8) + b; image.setRGB(x, y, argb); } } return image; }
public float getTexWidth() { return ((float) width) / page.getWidth(); }
public float getTexHeight() { return ((float) height) / page.getHeight(); }
/** * Updates the preview according to any changes made to the configurator. * Also pops up a message and keeps track of percentage world preview prepared. */ private void updatePreview() { final NUIManager manager = context.get(NUIManager.class); final WaitPopup<TextureData> popup = manager.pushScreen(WaitPopup.ASSET_URI, WaitPopup.class); popup.setMessage("Updating Preview", "Please wait ..."); ProgressListener progressListener = progress -> popup.setMessage("Updating Preview", String.format("Please wait ... %d%%", (int) (progress * 100f))); Callable<TextureData> operation = () -> { int zoom = TeraMath.floorToInt(zoomSlider.getValue()); TextureData data = texture.getData(); previewGen.render(data, zoom, progressListener); return data; }; popup.onSuccess(texture::reload); popup.startOperation(operation, true); }
/** * The constructor, to be used in the initialise method of a node. * * Sample use: * addDesiredStateChange(new SetInputTexture(GL_TEXTURE_2D, 0, "engine:water", "engine:prog.chunk", "textureWater")); * * @param textureType an opengl constant, can be GL_TEXTURE_2D, GL_TEXTURE_3D and any other texture type listed in https://www.khronos.org/opengl/wiki/Texture#Theory * @param textureSlot a 0-based integer. Notice that textureUnit = GL_TEXTURE0 + textureSlot. See OpenGL spects for maximum allowed values. * @param textureUrn a String identifying a loaded texture, whose id will then be used by this StateChange. * @param materialUrn a ResourceURN object uniquely identifying a Material asset. * @param materialParameter a String representing the variable within the shader holding the texture. */ protected SetInputTexture(int textureType, int textureSlot, String textureUrn, ResourceUrn materialUrn, String materialParameter) { this.textureType = textureType; this.textureSlot = textureSlot; this.materialUrn = materialUrn; this.materialParameter = materialParameter; this.material = getMaterial(materialUrn); Optional<Texture> optionalTexture = Assets.getTexture(textureUrn); if (optionalTexture.isPresent()) { this.textureId = optionalTexture.get().getId(); } else { this.textureId = 0; // TODO: Maybe throw some exception or use Logger.error()? } // TODO: take advantage of Texture.subscribeToDisposal(Runnable) to reobtain the asset if necessary }
@Override public Optional<MeshData> getAssetData(ResourceUrn urn) throws IOException { if (ICON_DISCRIMINATOR.equals(urn.getResourceName())) { ResourceUrn textureUrn = new ResourceUrn(urn.getModuleName().toString() + ResourceUrn.RESOURCE_SEPARATOR + urn.getFragmentName().toString()); Optional<TextureRegionAsset> textureRegionAsset = assetManager.getAsset(textureUrn, TextureRegionAsset.class); if (textureRegionAsset.isPresent() && !textureRegionAsset.get().getTexture().isDisposed()) { return Optional.of(IconMeshFactory.generateIconMeshData(textureRegionAsset.get())); } } return Optional.empty(); }
public FontDataBuilder endCharacter() { Texture page = pages.get(characterPage); FontCharacter character = new FontCharacter(((float) characterX / page.getWidth()), ((float) characterY / page.getHeight()), characterWidth, characterHeight, characterXOffset, characterYOffset, characterXAdvance, page, pageMats.get(characterPage)); characters.put(currentCharacterId, character); return this; }
@Override public int getWidth() { return TeraMath.ceilToInt(texture.getWidth() * region.width()); }
@Override public int getHeight() { return TeraMath.ceilToInt(texture.getHeight() * subregion.height()); }
@Test public void testColorTextures() { Random r = new FastRandom(123456); for (int i = 0; i < 10; i++) { int rgba = r.nextInt(); Color red = new Color(rgba); ResourceUrn textureUriForColor = TextureUtil.getTextureUriForColor(red); String simpleString = textureUriForColor.toString(); Optional<Texture> tex = Assets.getTexture(simpleString); assertTrue(tex.isPresent()); ByteBuffer dataBuffer = tex.get().getData().getBuffers()[0]; int firstPixel = dataBuffer.asIntBuffer().get(0); Assert.assertEquals(rgba, firstPixel); } }
@Override public void bindTexture(int slot, Texture texture) { if (activeMaterial != null && !activeMaterial.isDisposed()) { GL13.glActiveTexture(GL13.GL_TEXTURE0 + slot); // TODO: Need to be cubemap aware, only need to clear bind when switching from cubemap to 2D and vice versa, // TODO: Don't bind if already bound to the same GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId()); GL13.glActiveTexture(GL13.GL_TEXTURE0); } }
@Override public void bindTextures() { if (isDisposed()) { return; } enable(); for (int slot : textureMap.keys()) { Texture texture = textureMap.get(slot); if (texture.isDisposed()) { logger.error("Attempted to bind disposed texture {}", texture); } else { shaderManager.bindTexture(slot, texture); } } }
/** * @param atlas The texture atlas that is being used * @param atlasSize How many textures the atlas contains in it's width and height. * @param validTextures Indices of the valid textures */ public TextureOffsetGeneratorComponent(final Texture atlas, final Vector2i atlasSize, final Vector2i[] validTextures) { final float textureWidth = atlas.getWidth() / (float) atlasSize.getX(); final float textureHeight = atlas.getHeight() / (float) atlasSize.getY(); Function<Vector2i, Vector2f> absolute2Relative = (absolute) -> new Vector2f( absolute.getX() * textureWidth, absolute.getY() * textureHeight ); this.validOffsets = new LinkedList<>(); for (Vector2i offset : validTextures) { this.validOffsets.add(absolute2Relative.apply(offset)); } } }
@Override public int getWidth() { return TeraMath.ceilToInt(texture.getWidth() * subregion.width()); }