Skip to content

Instantly share code, notes, and snippets.

@zacharycarter
Created December 16, 2016 22:38
Show Gist options
  • Save zacharycarter/841decbddade3b5d55c54c565294fef1 to your computer and use it in GitHub Desktop.
Save zacharycarter/841decbddade3b5d55c54c565294fef1 to your computer and use it in GitHub Desktop.
private static Texture generateNormalMapTexture(int width, int height, Tile[][] tiles) {
pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
texture = new Texture(width, height, Pixmap.Format.RGBA8888);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Vector3 normal;
// Ignore border pixels
if(
x == 0 || y == 0 || x == width-1 || y == height-1) {
normal = new Vector3(1,1,1);
System.out.print(normal);
continue;
}
// Sample neighbors
float hl = tiles[x-1][y].heightValue;
float hr = tiles[x+1][y].heightValue;
float ht = tiles[x][y+1].heightValue;
float hb = tiles[x][y-1].heightValue;
Vector3 dx = new Vector3(-1.0f, 0.0f, hr-hl);
Vector3 dy = new Vector3(0.0f, -1.0f, ht - hb);
// Compute sobel
normal = dx.crs(dy);
normal.z *= .5;
normal = normal.nor();
normal.add(1f).scl(0.5f);
pixmap.drawPixel(x, y, Color.rgba8888(new Color(normal.x, normal.y, normal.z, 1)));
}
}
PixmapIO.writePNG(Gdx.files.getFileHandle("normalmap.png", Files.FileType.Local), pixmap);
texture.setWrap(Texture.TextureWrap.ClampToEdge, Texture.TextureWrap.ClampToEdge);
texture.draw(pixmap, 0, 0);
return texture;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment