@Override public Float64Vector getNormal(Coordinate point) { if (!point.zoomOut(Coordinate.OFFSET_BLOCK_CHUNK).equals(location)) { throw new IllegalArgumentException(); } point = point.subCoordinate(Coordinate.OFFSET_BLOCK_CHUNK); return normals[point.getX()][point.getY()]; }
private Float64Vector[][] fromNormalMap(NormalMap source) { Float64Vector[][] normals = new Float64Vector[16][16]; Coordinate chunkOrigin = location.zoomIn(Coordinate.OFFSET_BLOCK_CHUNK); for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { normals[x][y] = source.getNormal(chunkOrigin.plus(new Coordinate(x, y, Coordinate.LEVEL_BLOCK))); } } return normals; }
private static double computeDiffuseShading( ChunkSnapshot chunkSnapshot, int x, int z, NormalMap nm) { Float64Vector n = nm.getNormal( Coordinate.fromSnapshot(chunkSnapshot) .zoomIn(Coordinate.OFFSET_BLOCK_CHUNK) .plus(new Coordinate(x, z, Coordinate.LEVEL_BLOCK))); Float64Vector light = Float64Vector.valueOf(-1, -1, -1); if (n == null) { return -1; } double shading = n.times(light).divide((n.norm().times(light.norm()))).doubleValue(); return ((shading + 1) * 0.4) + 0.15; }
@Override public void render(ChunkSnapshot chunkSnapshot) { BufferedImage image = ImageProvider.createImage(Coordinate.SIZE_CHUNK); Graphics2D g = (Graphics2D) image.getGraphics(); for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { // TODO: Handle cases where we can start at an opaque non-terrain block for (int y = TerrainHelper.getTerrainHeight(x, z, chunkSnapshot); y <= chunkSnapshot.getHighestBlockYAt(x, z); y++) { Material material = Material.getMaterial(chunkSnapshot.getBlockTypeId(x, y, z)); Color color = getMaterialColor(material, chunkSnapshot, x, y, z); if (TerrainHelper.isTerrain(material)) { double shading = computeDiffuseShading(chunkSnapshot, x, z, this.normalMap); if (shading >= 0) { color = tintOrShadeColor(color, shading, 1.0); } } else if (TerrainHelper.isStructure(material)) { double shading = computeDiffuseShading(chunkSnapshot, x, z, this.structureMap); if (shading >= 0) { if (material.equals(Material.LEAVES)) { color = tintOrShadeColor(color, shading, 0.3); } else { color = tintOrShadeColor(color, shading, 1.0); } } } g.setColor(color); g.fillRect(x, z, 1, 1); } } } imageProvider.setImage(Coordinate.fromSnapshot(chunkSnapshot), image); }