@Override public TextureData getData() { return new TextureData(textureData); }
@Override public TextureData getData() { return new TextureData(resources.loadedTextureInfo.getTextureData()); }
/** * 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);
private void genTexture() { int imgWidth = 384; int imgHeight = 384; ByteBuffer buffer = ByteBuffer.allocateDirect(imgWidth * imgHeight * Integer.BYTES); ByteBuffer[] data = new ByteBuffer[]{buffer}; ResourceUrn uri = new ResourceUrn("engine:terrainPreview"); TextureData texData = new TextureData(imgWidth, imgHeight, data, Texture.WrapMode.CLAMP, Texture.FilterMode.LINEAR); texture = Assets.generateAsset(uri, texData, Texture.class); previewImage = find("preview", UIImage.class); previewImage.setImage(texture); }
/** * Generates a texture and sets it to the image view, thus previewing the world. */ private void genTexture() { int imgWidth = 384; int imgHeight = 384; ByteBuffer buffer = ByteBuffer.allocateDirect(imgWidth * imgHeight * Integer.BYTES); ByteBuffer[] data = new ByteBuffer[]{buffer}; ResourceUrn uri = new ResourceUrn("engine:terrainPreview"); TextureData texData = new TextureData(imgWidth, imgHeight, data, Texture.WrapMode.CLAMP, Texture.FilterMode.LINEAR); texture = Assets.generateAsset(uri, texData, Texture.class); previewImage = find("preview", UIImage.class); previewImage.setImage(texture); }
public static TextureData createWhiteNoiseTexture(int size, long seed, int min, int max) { int width = size; int height = size; ByteBuffer data = ByteBuffer.allocateDirect(4 * width * height); Random rng = new FastRandom(seed); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255)); data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255)); data.put((byte) TeraMath.clamp(rng.nextInt(min, max), 0, 255)); data.put((byte) 255); } } // The buffer must be reset back to the initial position before passing it onward. data.rewind(); return new TextureData(width, height, new ByteBuffer[]{data}, WrapMode.REPEAT, FilterMode.NEAREST); } }
private Texture generateNoiseTexture() { Optional<Texture> texture = Assets.getTexture("engine:ssaoNoise"); if (!texture.isPresent()) { ByteBuffer noiseValues = BufferUtils.createByteBuffer(SSAO_NOISE_SIZE * SSAO_NOISE_SIZE * 4); for (int i = 0; i < SSAO_NOISE_SIZE * SSAO_NOISE_SIZE; ++i) { Vector3f noiseVector = new Vector3f(randomGenerator.nextFloat(-1.0f, 1.0f), randomGenerator.nextFloat(-1.0f, 1.0f), 0.0f); noiseVector.normalize(); noiseValues.put((byte) ((noiseVector.x * 0.5 + 0.5) * 255.0f)); noiseValues.put((byte) ((noiseVector.y * 0.5 + 0.5) * 255.0f)); noiseValues.put((byte) ((noiseVector.z * 0.5 + 0.5) * 255.0f)); noiseValues.put((byte) 0x0); } noiseValues.flip(); return Assets.generateAsset(new ResourceUrn("engine:ssaoNoise"), new TextureData(SSAO_NOISE_SIZE, SSAO_NOISE_SIZE, new ByteBuffer[]{noiseValues}, Texture.WrapMode.REPEAT, Texture.FilterMode.NEAREST), Texture.class); } return texture.get(); } }
private Texture generateTexture(ResourceUrn urn) { ByteBuffer buffer = ByteBuffer.allocateDirect(size.x() * size.y() * Integer.BYTES); ByteBuffer[] mipmaps = new ByteBuffer[]{buffer}; TextureData data = new TextureData(size.x(), size.y(), mipmaps, Texture.WrapMode.CLAMP, Texture.FilterMode.NEAREST); return Assets.generateAsset(urn, data, Texture.class); }
@Override public TextureData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException { try (InputStream pngStream = inputs.get(0).openStream()) { PNGDecoder decoder = new PNGDecoder(pngStream); ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight()); decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.Format.RGBA); buf.flip(); int height = decoder.getHeight(); int width = decoder.getWidth(); Texture.FilterMode filterMode = defaultFilterMode; Texture.WrapMode wrapMode = Texture.WrapMode.CLAMP; return new TextureData(width, height, new ByteBuffer[]{buf}, wrapMode, filterMode); } catch (UnsupportedOperationException e) { throw new IOException(e); } }
private void buildAtlas() { calculateAtlasSizes(); int numMipMaps = getNumMipmaps(); ByteBuffer[] data = createAtlasMipmaps(numMipMaps, TRANSPARENT_COLOR, tiles, "tiles.png"); ByteBuffer[] dataNormal = createAtlasMipmaps(numMipMaps, UNIT_Z_COLOR, tilesNormal, "tilesNormal.png", tilesGloss); ByteBuffer[] dataHeight = createAtlasMipmaps(numMipMaps, MID_RED_COLOR, tilesHeight, "tilesHeight.png"); TextureData terrainTexData = new TextureData(atlasSize, atlasSize, data, Texture.WrapMode.CLAMP, Texture.FilterMode.NEAREST); Texture terrainTex = Assets.generateAsset(new ResourceUrn("engine:terrain"), terrainTexData, Texture.class); TextureData terrainNormalData = new TextureData(atlasSize, atlasSize, dataNormal, Texture.WrapMode.CLAMP, Texture.FilterMode.NEAREST); Assets.generateAsset(new ResourceUrn("engine:terrainNormal"), terrainNormalData, Texture.class); TextureData terrainHeightData = new TextureData(atlasSize, atlasSize, dataHeight, Texture.WrapMode.CLAMP, Texture.FilterMode.LINEAR); Assets.generateAsset(new ResourceUrn("engine:terrainHeight"), terrainHeightData, Texture.class); MaterialData terrainMatData = new MaterialData(Assets.getShader("engine:block").get()); terrainMatData.setParam("textureAtlas", terrainTex); terrainMatData.setParam("colorOffset", new float[]{1, 1, 1}); terrainMatData.setParam("textured", true); Assets.generateAsset(new ResourceUrn("engine:terrain"), terrainMatData, Material.class); createTextureAtlas(terrainTex); }