LoadedTextureInfo(int width, int height, int depth, TextureData data) { this.width = width; this.height = height; this.depth = depth; this.wrapMode = data.getWrapMode(); this.filterMode = data.getFilterMode(); this.textureData = data; }
@Override public TextureData getData() { return new TextureData(textureData); }
private void testRepeatedColorInDataTexture(Color expectedColor, int expectedRed, int expectedGreen, int expectedBlue, int expectedAlpha) { TextureData textureData = TextureDataFactory.newInstance(expectedColor); ByteBuffer[] buffers = textureData.getBuffers(); assertEquals(1, buffers.length); ByteBuffer buffer = buffers[0]; for (int offsetCounter = 0; offsetCounter < 4 * 16; offsetCounter++) { int offset = offsetCounter * 4; assertEquals(expectedRed, buffer.get(0 + offset)); assertEquals(expectedGreen, buffer.get(1 + offset)); assertEquals(expectedBlue, buffer.get(2 + offset)); assertEquals(expectedAlpha, buffer.get(3 + offset)); } assertEquals(16, textureData.getWidth()); assertEquals(16, textureData.getHeight()); assertEquals(FilterMode.NEAREST, textureData.getFilterMode()); assertEquals(WrapMode.REPEAT, textureData.getWrapMode()); assertEquals(Texture.Type.TEXTURE2D, textureData.getType()); } }
@Override protected void doReload(TextureData data) { switch (data.getType()) { if (data.getWidth() % data.getHeight() != 0 || data.getWidth() / data.getHeight() != data.getHeight()) { throw new RuntimeException("3D texture must be cubic (height^3) - width must thus be a multiple of height"); int size = data.getHeight(); final int index = x * strideX + z * strideZ + strideY * y; alignedBuffer.put(data.getBuffers()[0].get(index)); alignedBuffer.put(data.getBuffers()[0].get(index + 1)); alignedBuffer.put(data.getBuffers()[0].get(index + 2)); alignedBuffer.put(data.getBuffers()[0].get(index + 3)); int width = data.getWidth(); int height = data.getHeight(); resources.loadedTextureInfo = new LoadedTextureInfo(width, height, 1, data); if (resources.id == 0) { resources.graphicsManager.createTexture2D(data.getBuffers(), getWrapMode(), getFilterMode(), width, height, (newId) -> { synchronized (this) { if (resources.id != 0) { resources.graphicsManager.reloadTexture2D(resources.id, data.getBuffers(), getWrapMode(), getFilterMode(), width, height);
@Test public void successTest() throws IOException { BufferedImage image = createBufferedImage(2, 3, BufferedImage.TYPE_3BYTE_BGR); TextureData textureData = AWTTextureFormat.convertToTextureData(image, Texture.FilterMode.LINEAR); Assert.assertNotNull(textureData); Assert.assertEquals(2, textureData.getWidth()); Assert.assertEquals(3, textureData.getHeight()); Assert.assertEquals(Texture.FilterMode.LINEAR, textureData.getFilterMode()); }
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 ByteBuffer render(TextureData texData, int scale, ProgressListener progressListener) throws InterruptedException { int width = texData.getWidth(); int height = texData.getWidth(); final int offX = -width * scale / 2; final int offY = -height * scale / 2; ByteBuffer byteBuffer = texData.getBuffers()[0]; byteBuffer.asIntBuffer().put(data);
@Override public void apply(AssetDataFile input, TextureData assetData) throws IOException { try (InputStreamReader reader = new InputStreamReader(input.openStream(), Charsets.UTF_8)) { TextureMetadata metadata = gson.fromJson(reader, TextureMetadata.class); if (metadata.filterMode != null) { assetData.setFilterMode(metadata.filterMode); } if (metadata.wrapMode != null) { assetData.setWrapMode(metadata.wrapMode); } if (metadata.type != null) { assetData.setType(metadata.type); } } }
@Override public int getHeight() { return textureData.getHeight(); }
@Override public WrapMode getWrapMode() { return textureData.getWrapMode(); }
@Override public FilterMode getFilterMode() { return textureData.getFilterMode(); }
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];
@Override public TextureData getData() { return new TextureData(resources.loadedTextureInfo.getTextureData()); }
@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); } }
/** * Create TextureData for a Texture all of a single color. * @param color to use for creating TextureData * @return TextureData created using specified color */ public static TextureData newInstance(Color color) { byte red = (byte) color.r(); byte green = (byte) color.g(); byte blue = (byte) color.b(); byte alpha = (byte) color.a(); ByteBuffer data = ByteBuffer.allocateDirect(4 * TEXTURE_WIDTH * TEXTURE_HEIGHT); for (int height = 0; height < TEXTURE_HEIGHT; height++) { for (int width = 0; width < TEXTURE_WIDTH; width++) { data.put(red).put(green).put(blue).put(alpha); } } // The buffer must be reset back to the initial position before passing it onward. data.rewind(); return new TextureData(TEXTURE_WIDTH, TEXTURE_HEIGHT, new ByteBuffer[]{data}, WrapMode.REPEAT, FilterMode.NEAREST); }
return new TextureData(image.getWidth(), image.getHeight(), new ByteBuffer[]{buf}, Texture.WrapMode.CLAMP, filterMode);