Search in sources :

Example 1 with ImmutableVector2i

use of org.terasology.math.geom.ImmutableVector2i in project Terasology by MovingBlocks.

the class FacetLayerPreview method render.

@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;
    // trigger building the World now
    worldGenerator.getWorld();
    Rect2i worldArea = Rect2i.createFromMinAndSize(offX, offY, width * scale, height * scale);
    Rect2i tileArea = worldToTileArea(worldArea);
    AtomicInteger tilesComplete = new AtomicInteger(0);
    int tileCount = tileArea.area();
    int[] masks = colorModel.getMasks();
    DataBufferInt imageBuffer = new DataBufferInt(width * height);
    WritableRaster raster = Raster.createPackedRaster(imageBuffer, width, height, width, masks, null);
    BufferedImage view = new BufferedImage(colorModel, raster, false, null);
    Graphics2D g = view.createGraphics();
    g.scale(1f / scale, 1f / scale);
    g.translate(-offX, -offY);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    Map<ImmutableVector2i, Future<BufferedImage>> imageFutures = new HashMap<>(tileCount);
    for (int z = tileArea.minY(); z < tileArea.maxY(); z++) {
        for (int x = tileArea.minX(); x < tileArea.maxX(); x++) {
            ImmutableVector2i pos = new ImmutableVector2i(x, z);
            imageFutures.put(pos, threadPool.submit(() -> {
                Region createRegion = createRegion(pos);
                BufferedImage image = rasterize(createRegion);
                if (progressListener != null) {
                    progressListener.onProgress(tilesComplete.incrementAndGet() / (float) tileCount);
                }
                return image;
            }));
        }
    }
    for (int z = tileArea.minY(); z < tileArea.maxY(); z++) {
        for (int x = tileArea.minX(); x < tileArea.maxX(); x++) {
            ImmutableVector2i pos = new ImmutableVector2i(x, z);
            try {
                BufferedImage tileImage = imageFutures.get(pos).get();
                g.drawImage(tileImage, x * TILE_SIZE_X, z * TILE_SIZE_Y, null);
            } catch (ExecutionException e) {
                logger.warn("Could not rasterize tile {}", pos, e);
            }
            if (Thread.currentThread().isInterrupted()) {
                throw new InterruptedException();
            }
        }
    }
    // draw coordinate lines through 0 / 0
    g.setColor(Color.GRAY);
    g.drawLine(worldArea.minX(), 0, worldArea.maxX(), 0);
    g.drawLine(0, worldArea.minY(), 0, worldArea.maxY());
    g.dispose();
    int[] data = imageBuffer.getData();
    ByteBuffer byteBuffer = texData.getBuffers()[0];
    byteBuffer.asIntBuffer().put(data);
    return byteBuffer;
}
Also used : HashMap(java.util.HashMap) DataBufferInt(java.awt.image.DataBufferInt) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) Rect2i(org.terasology.math.geom.Rect2i) ImmutableVector2i(org.terasology.math.geom.ImmutableVector2i) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WritableRaster(java.awt.image.WritableRaster) Future(java.util.concurrent.Future) Region(org.terasology.world.generation.Region) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ImmutableVector2i

use of org.terasology.math.geom.ImmutableVector2i in project Terasology by MovingBlocks.

the class PerlinFacetedWorldGenerator method createWorld.

@Override
protected WorldBuilder createWorld() {
    int seaLevel = 32;
    // as used by the spawner
    ImmutableVector2i spawnPos = new ImmutableVector2i(0, 0);
    return new WorldBuilder(worldGeneratorPluginLibrary).setSeaLevel(seaLevel).addProvider(new SeaLevelProvider(seaLevel)).addProvider(new PerlinHumidityProvider()).addProvider(new PerlinSurfaceTemperatureProvider()).addProvider(new PerlinBaseSurfaceProvider()).addProvider(new PerlinRiverProvider()).addProvider(new PerlinOceanProvider()).addProvider(new PerlinHillsAndMountainsProvider()).addProvider(new BiomeProvider()).addProvider(new SurfaceToDensityProvider()).addProvider(new DefaultFloraProvider()).addProvider(new DefaultTreeProvider()).addProvider(new PlateauProvider(spawnPos, seaLevel + 4, 10, 30)).addRasterizer(new SolidRasterizer()).addPlugins().addRasterizer(new FloraRasterizer()).addRasterizer(new TreeRasterizer());
}
Also used : BiomeProvider(org.terasology.core.world.generator.facetProviders.BiomeProvider) PlateauProvider(org.terasology.core.world.generator.facetProviders.PlateauProvider) SolidRasterizer(org.terasology.core.world.generator.rasterizers.SolidRasterizer) PerlinBaseSurfaceProvider(org.terasology.core.world.generator.facetProviders.PerlinBaseSurfaceProvider) WorldBuilder(org.terasology.world.generation.WorldBuilder) SeaLevelProvider(org.terasology.core.world.generator.facetProviders.SeaLevelProvider) DefaultTreeProvider(org.terasology.core.world.generator.facetProviders.DefaultTreeProvider) PerlinSurfaceTemperatureProvider(org.terasology.core.world.generator.facetProviders.PerlinSurfaceTemperatureProvider) PerlinOceanProvider(org.terasology.core.world.generator.facetProviders.PerlinOceanProvider) SurfaceToDensityProvider(org.terasology.core.world.generator.facetProviders.SurfaceToDensityProvider) PerlinHumidityProvider(org.terasology.core.world.generator.facetProviders.PerlinHumidityProvider) ImmutableVector2i(org.terasology.math.geom.ImmutableVector2i) FloraRasterizer(org.terasology.core.world.generator.rasterizers.FloraRasterizer) DefaultFloraProvider(org.terasology.core.world.generator.facetProviders.DefaultFloraProvider) TreeRasterizer(org.terasology.core.world.generator.rasterizers.TreeRasterizer) PerlinHillsAndMountainsProvider(org.terasology.core.world.generator.facetProviders.PerlinHillsAndMountainsProvider) PerlinRiverProvider(org.terasology.core.world.generator.facetProviders.PerlinRiverProvider)

Aggregations

ImmutableVector2i (org.terasology.math.geom.ImmutableVector2i)2 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 DataBufferInt (java.awt.image.DataBufferInt)1 WritableRaster (java.awt.image.WritableRaster)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 BiomeProvider (org.terasology.core.world.generator.facetProviders.BiomeProvider)1 DefaultFloraProvider (org.terasology.core.world.generator.facetProviders.DefaultFloraProvider)1 DefaultTreeProvider (org.terasology.core.world.generator.facetProviders.DefaultTreeProvider)1 PerlinBaseSurfaceProvider (org.terasology.core.world.generator.facetProviders.PerlinBaseSurfaceProvider)1 PerlinHillsAndMountainsProvider (org.terasology.core.world.generator.facetProviders.PerlinHillsAndMountainsProvider)1 PerlinHumidityProvider (org.terasology.core.world.generator.facetProviders.PerlinHumidityProvider)1 PerlinOceanProvider (org.terasology.core.world.generator.facetProviders.PerlinOceanProvider)1 PerlinRiverProvider (org.terasology.core.world.generator.facetProviders.PerlinRiverProvider)1 PerlinSurfaceTemperatureProvider (org.terasology.core.world.generator.facetProviders.PerlinSurfaceTemperatureProvider)1 PlateauProvider (org.terasology.core.world.generator.facetProviders.PlateauProvider)1