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); } }
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 static MeshData generateIconMeshData(TextureRegion tex, int alphaLimit, boolean withContour, Vector4f colorContour) { ByteBuffer buffer = tex.getTexture().getData().getBuffers()[0];
/** * 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); }
@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); } }
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 = () -> { if (seed != null) { worldGenerator.setWorldSeed(seed.getText()); } int zoom = TeraMath.floorToInt(zoomSlider.getValue()); TextureData data = texture.getData(); if (zoneSelector.isVisible()) { previewGen = zoneSelector.getSelection().preview(worldGenerator); } previewGen.render(data, zoom, progressListener); return data; }; popup.onSuccess(texture::reload); popup.startOperation(operation, true); }