Search in sources :

Example 11 with Voxel

use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories-api by Hugobros3.

the class PacketVoxelUpdate method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    if (processor instanceof ClientPacketsProcessor) {
        ClientPacketsProcessor cpp = (ClientPacketsProcessor) processor;
        int x = in.readInt();
        int y = in.readInt();
        int z = in.readInt();
        int data = in.readInt();
        Voxel voxel = world.getContentTranslator().getVoxelForId(VoxelFormat.id(data));
        byte nextComponent = in.readByte();
        try {
            ChunkCell context = cpp.getWorld().getChunkWorldCoordinates(x, y, z).poke(x, y, z, voxel, VoxelFormat.sunlight(data), VoxelFormat.blocklight(data), VoxelFormat.meta(data), null);
            while (nextComponent != 0) {
                String componentName = in.readUTF();
                context.components().get(componentName).pull(sender, in);
                nextComponent = in.readByte();
            }
        } catch (WorldException e) {
        // Maybe the world wasn't ready ?
        // Edge case: what happens we receive an update for a chunk we haven't received the data yet ?
        // The best option would be to delay the process but this is complicated for a rare instance
        // Maybe one day
        }
    } else {
    // Fail hard
    }
}
Also used : ClientPacketsProcessor(io.xol.chunkstories.api.client.net.ClientPacketsProcessor) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) Voxel(io.xol.chunkstories.api.voxel.Voxel) ChunkCell(io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell)

Example 12 with Voxel

use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories-api by Hugobros3.

the class VoxelItemRenderer method renderItemInWorld.

@Override
public void renderItemInWorld(RenderingInterface renderer, ItemPile pile, World world, Location location, Matrix4f handTransformation) {
    /*if (((ItemVoxel) pile.getItem()).getVoxel() instanceof VoxelCustomIcon) {
			fallbackRenderer.renderItemInWorld(renderer, pile, world, location, handTransformation);
			return;
		}*/
    Voxel voxel = ((ItemVoxel) pile.getItem()).getVoxel();
    if (voxel == null)
        return;
    CellData fakeCell = new DummyCell(0, 0, 0, voxel, 0, 0, 0) {

        CellData air = new DummyCell(0, 1, 0, voxel.store().air(), 0, 0, 0);

        @Override
        public int getBlocklight() {
            return voxel.getEmittedLightLevel(this);
        }

        @Override
        public CellData getNeightbor(int side) {
            return air;
        }

        @Override
        public int getMetaData() {
            return ((ItemVoxel) pile.getItem()).getVoxelMeta();
        }
    };
    float s = 0.45f;
    handTransformation.scale(new Vector3f(s, s, s));
    handTransformation.translate(new Vector3f(-0.25f, -0.5f, -0.5f));
    renderer.setObjectMatrix(handTransformation);
    // Add a light only on the opaque pass
    if (fakeCell.getBlocklight() > 0 && renderer.getCurrentPass().name.contains("gBuffers")) {
        Vector4f lightposition = new Vector4f(0.0f, 0.0f, 0.0f, 1.0f);
        handTransformation.transform(lightposition);
        Light heldBlockLight = new Light(new Vector3f(0.6f, 0.50f, 0.4f).mul(0.5f), new Vector3f(lightposition.x(), lightposition.y(), lightposition.z()), 15f);
        renderer.getLightsRenderer().queueLight(heldBlockLight);
        // If we hold a light source, prepare the shader accordingly
        renderer.currentShader().setUniform2f("worldLightIn", Math.max(world.peekSafely(location).getBlocklight(), fakeCell.getBlocklight()), world.peekSafely(location).getSunlight());
    }
    Texture2D texture = content.voxels().textures().getDiffuseAtlasTexture();
    texture.setLinearFiltering(false);
    renderer.bindAlbedoTexture(texture);
    Texture2D normalTexture = content.voxels().textures().getNormalAtlasTexture();
    normalTexture.setLinearFiltering(false);
    renderer.bindNormalTexture(normalTexture);
    Texture2D materialTexture = content.voxels().textures().getMaterialAtlasTexture();
    materialTexture.setLinearFiltering(false);
    renderer.bindMaterialTexture(materialTexture);
    renderFakeVoxel(renderer, fakeCell);
}
Also used : ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Vector4f(org.joml.Vector4f) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) Light(io.xol.chunkstories.api.rendering.lightning.Light) Vector3f(org.joml.Vector3f) CellData(io.xol.chunkstories.api.world.cell.CellData) DummyCell(io.xol.chunkstories.api.world.cell.DummyCell)

Example 13 with Voxel

use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories-api by Hugobros3.

the class VoxelItemRenderer method renderItemInInventory.

@Override
public void renderItemInInventory(RenderingInterface renderer, ItemPile pile, float screenPositionX, float screenPositionY, int scaling) {
    /*if (((ItemVoxel) pile.getItem()).getVoxel() instanceof VoxelCustomIcon) {
			fallbackRenderer.renderItemInInventory(renderer, pile, screenPositionX, screenPositionY, scaling);
			return;
		}*/
    int slotSize = 24 * scaling;
    Shader program = renderer.useShader("inventory_blockmodel");
    renderer.setCullingMode(CullingMode.COUNTERCLOCKWISE);
    renderer.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
    program.setUniform2f("screenSize", renderer.getWindow().getWidth(), renderer.getWindow().getHeight());
    program.setUniform2f("dekal", screenPositionX + pile.getItem().getDefinition().getSlotsWidth() * slotSize / 2, screenPositionY + pile.getItem().getDefinition().getSlotsHeight() * slotSize / 2);
    program.setUniform1f("scaling", slotSize / 1.65f);
    transformation.identity();
    transformation.scale(new Vector3f(-1f, 1f, 1f));
    transformation.rotate(toRad(-22.5f), new Vector3f(1.0f, 0.0f, 0.0f));
    transformation.rotate(toRad(45f), new Vector3f(0.0f, 1.0f, 0.0f));
    transformation.translate(new Vector3f(-0.5f, -0.5f, -0.5f));
    program.setUniformMatrix4f("transformation", transformation);
    Voxel voxel = ((ItemVoxel) pile.getItem()).getVoxel();
    if (voxel == null) {
        int width = slotSize * pile.getItem().getDefinition().getSlotsWidth();
        int height = slotSize * pile.getItem().getDefinition().getSlotsHeight();
        renderer.getGuiRenderer().drawBoxWindowsSpaceWithSize(screenPositionX, screenPositionY, width, height, 0, 1, 1, 0, content.textures().getTexture("./items/icons/notex.png"), true, true, null);
        return;
    }
    Texture2D texture = content.voxels().textures().getDiffuseAtlasTexture();
    texture.setLinearFiltering(false);
    renderer.bindAlbedoTexture(texture);
    Texture2D normalTexture = content.voxels().textures().getNormalAtlasTexture();
    normalTexture.setLinearFiltering(false);
    renderer.bindNormalTexture(normalTexture);
    Texture2D materialTexture = content.voxels().textures().getMaterialAtlasTexture();
    materialTexture.setLinearFiltering(false);
    renderer.bindMaterialTexture(materialTexture);
    CellData fakeCell = new DummyCell(0, 0, 0, voxel, 0, 0, 0) {

        CellData air = new DummyCell(0, 1, 0, voxel.store().air(), 0, 0, 0);

        @Override
        public int getBlocklight() {
            return voxel.getEmittedLightLevel(this);
        }

        @Override
        public CellData getNeightbor(int side) {
            return air;
        }

        @Override
        public int getMetaData() {
            return ((ItemVoxel) pile.getItem()).getVoxelMeta();
        }
    };
    renderFakeVoxel(renderer, fakeCell);
}
Also used : ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) Vector3f(org.joml.Vector3f) Shader(io.xol.chunkstories.api.rendering.shader.Shader) CellData(io.xol.chunkstories.api.world.cell.CellData) DummyCell(io.xol.chunkstories.api.world.cell.DummyCell)

Example 14 with Voxel

use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories by Hugobros3.

the class TaskBakeChunk method task.

@Override
protected boolean task(TaskExecutor taskExecutor) {
    if (!(taskExecutor instanceof BakeChunkTaskExecutor))
        throw new UnexecutableTaskException(this, "This class requires to be executed by a BakeChunkTaskExecutor");
    this.cmd = ((BakeChunkTaskExecutor) taskExecutor).getBuffers();
    if (chunk == null) {
        throw new RuntimeException("Fuck off no");
    }
    Vector3dc camera = ((WorldClient) chunk.getWorld()).getWorldRenderer().getRenderingInterface().getCamera().getCameraPosition();
    // Check we aren't too far from the camera, and thus that our request hasn't been yet cancelled
    int vx = Math2.floor(camera.x() / 32);
    int vy = Math2.floor(camera.y() / 32);
    int vz = Math2.floor(camera.z() / 32);
    int dx = LoopingMathHelper.moduloDistance(chunk.getChunkX(), vx, chunk.getWorld().getSizeInChunks());
    int dz = LoopingMathHelper.moduloDistance(chunk.getChunkZ(), vz, chunk.getWorld().getSizeInChunks());
    int dy = Math.abs(chunk.getChunkY() - vy);
    int chunksViewDistance = (int) (world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance") / 32);
    if (dx > chunksViewDistance || dz > chunksViewDistance || dy > 2) {
        // logger.info("unscheduled chunk mesh render task for it being too far to be rendered anyway");
        return true;
    }
    // Require the chunk and nearby ones to be already loaded in the world
    ChunkRenderable chunkWithinWorld = (ChunkRenderable) world.getChunk(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ());
    if (chunkWithinWorld != null) {
        // Require the chunks ARROUND it to be already loaded in the world
        int nearChunks = 0;
        if (world.isChunkLoaded(chunk.getChunkX() + 1, chunk.getChunkY(), chunk.getChunkZ()))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX() - 1, chunk.getChunkY(), chunk.getChunkZ()))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ() + 1))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ() - 1))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY() + 1, chunk.getChunkZ()) || chunk.getChunkY() == world.getWorldInfo().getSize().heightInChunks - 1)
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY() - 1, chunk.getChunkZ()) || chunk.getChunkY() == 0)
            nearChunks++;
        if (nearChunks != 6) {
            // We wait until that's the case
            return false;
        }
    } else {
        // We wait until the chunk is loaded in the world ( or destroyed, then the task is cancelled )
        return false;
    }
    // If the chunk has pending light updates, wait until THOSE are done
    if (chunk.lightBaker.pendingUpdates() > 0) {
        chunk.lightBaker.spawnUpdateTaskIfNeeded();
        return false;
    }
    int updatesToConsider = chunk.chunkRenderData.unbakedUpdates.get();
    // Don't waste time rendering void chunks m8
    if (chunk.isAirChunk())
        i = 32;
    int cx = chunk.getChunkX();
    int cy = chunk.getChunkY();
    int cz = chunk.getChunkZ();
    // Fill chunk caches ( saves much time avoiding slow-ass world->regions hashmap->chunk holder access for each vert )
    for (int relx = -1; relx <= 1; relx++) for (int rely = -1; rely <= 1; rely++) for (int relz = -1; relz <= 1; relz++) {
        CubicChunk chunk2 = (CubicChunk) world.getChunk(cx + relx, cy + rely, cz + relz);
        if (chunk2 != null)
            cmd.cache[((relx + 1) * 3 + (rely + 1)) * 3 + (relz + 1)] = chunk2.chunkVoxelData;
        else
            cmd.cache[((relx + 1) * 3 + (rely + 1)) * 3 + (relz + 1)] = null;
    }
    // Make sure we clear each sub-buffer type.
    for (int i = 0; i < ChunkMeshDataSubtypes.VertexLayout.values().length; i++) {
        for (int j = 0; j < ChunkMeshDataSubtypes.LodLevel.values().length; j++) {
            for (int k = 0; k < ChunkMeshDataSubtypes.ShadingType.values().length; k++) {
                cmd.byteBuffers[i][j][k].clear();
            }
        }
    }
    // Creates wrapper/interfaces for all the elements
    ChunkRenderer chunkRendererOutput = new ChunkRenderer() {

        @Override
        public VoxelBakerHighPoly getHighpolyBakerFor(LodLevel lodLevel, ShadingType renderPass) {
            return (VoxelBakerHighPoly) cmd.byteBuffersWrappers[VertexLayout.INTRICATE.ordinal()][lodLevel.ordinal()][renderPass.ordinal()];
        }

        @Override
        public VoxelBakerCubic getLowpolyBakerFor(LodLevel lodLevel, ShadingType renderPass) {
            return (VoxelBakerCubic) cmd.byteBuffersWrappers[VertexLayout.WHOLE_BLOCKS.ordinal()][lodLevel.ordinal()][renderPass.ordinal()];
        }
    };
    ChunkBakerRenderContext chunkRenderingContext = new ChunkBakerRenderContext(chunk, cx, cy, cz);
    bakedBlockId = -1;
    Map<Voxel, DynamicallyRenderedVoxelType> dynamicVoxels = new HashMap<>();
    BakeChunkScratchCell cell = new BakeChunkScratchCell(world);
    // Render the fucking thing!
    for (i = 0; i < 32; i++) {
        for (j = 0; j < 32; j++) {
            for (k = 0; k < 32; k++) {
                peek(i, k, j, cell);
                if (cell.voxel.isAir())
                    continue;
                // Fill near-blocks info
                // chunkRenderingContext.prepareVoxelLight(); // lol nope
                VoxelRenderer voxelRenderer = cell.getVoxelRenderer();
                if (voxelRenderer == null)
                    voxelRenderer = world.getContent().voxels().getDefaultVoxelRenderer();
                // Run the VoxelRenderer
                voxelRenderer.bakeInto(chunkRendererOutput, chunkRenderingContext, chunk, cell);
                // We handle voxels with a dynamic renderer here too - we just add them to a list !
                if (voxelRenderer instanceof VoxelDynamicRenderer) {
                    DynamicallyRenderedVoxelType drvt = dynamicVoxels.get(cell.voxel);
                    if (drvt == null) {
                        drvt = new DynamicallyRenderedVoxelType((VoxelDynamicRenderer) voxelRenderer, cell.voxel);
                        dynamicVoxels.put(cell.voxel, drvt);
                    }
                    drvt.indexes.add(i * 1024 + k * 32 + j);
                }
                bakedBlockId++;
            }
        }
    }
    // Parse output neatly
    int[][][] sizes = new int[ChunkMeshDataSubtypes.VertexLayout.values().length][ChunkMeshDataSubtypes.LodLevel.values().length][ChunkMeshDataSubtypes.ShadingType.values().length];
    ;
    int[][][] offsets = new int[ChunkMeshDataSubtypes.VertexLayout.values().length][ChunkMeshDataSubtypes.LodLevel.values().length][ChunkMeshDataSubtypes.ShadingType.values().length];
    ;
    int currentOffset = 0;
    // Compute total size to create final bytebuffer
    int sizeInBytes = 0;
    for (VertexLayout vertexLayout : VertexLayout.values()) for (LodLevel lodLevel : LodLevel.values()) for (ShadingType renderPass : ShadingType.values()) {
        int vertexLayoutIndex = vertexLayout.ordinal();
        int lodLevelIndex = lodLevel.ordinal();
        int renderPassIndex = renderPass.ordinal();
        final ByteBuffer relevantByteBuffer = cmd.byteBuffers[vertexLayoutIndex][lodLevelIndex][renderPassIndex];
        // / vertexLayout.bytesPerVertex;
        sizeInBytes += relevantByteBuffer.position();
    }
    ByteBuffer finalData = MemoryUtil.memAlloc(sizeInBytes);
    MemFreeByteBuffer wrappedBuffer = new MemFreeByteBuffer(finalData);
    // For EACH section, make offset and shite
    for (VertexLayout vertexLayout : VertexLayout.values()) for (LodLevel lodLevel : LodLevel.values()) for (ShadingType renderPass : ShadingType.values()) {
        int vertexLayoutIndex = vertexLayout.ordinal();
        int lodLevelIndex = lodLevel.ordinal();
        int renderPassIndex = renderPass.ordinal();
        // Else it gets really long for no reason
        final ByteBuffer relevantByteBuffer = cmd.byteBuffers[vertexLayoutIndex][lodLevelIndex][renderPassIndex];
        offsets[vertexLayoutIndex][lodLevelIndex][renderPassIndex] = currentOffset;
        sizes[vertexLayoutIndex][lodLevelIndex][renderPassIndex] = relevantByteBuffer.position() / vertexLayout.bytesPerVertex;
        // Move the offset accordingly
        currentOffset += relevantByteBuffer.position();
        // Limit the temporary byte buffer and fill the main buffer with it
        relevantByteBuffer.limit(relevantByteBuffer.position());
        relevantByteBuffer.position(0);
        finalData.put(relevantByteBuffer);
    }
    finalData.flip();
    ChunkMeshDataSections newRenderData = new ChunkMeshDataSections(wrappedBuffer, sizes, offsets);
    DynamicallyRenderedVoxelType[] bakedDrvt = new DynamicallyRenderedVoxelType[dynamicVoxels.size()];
    Iterator<DynamicallyRenderedVoxelType> i = dynamicVoxels.values().iterator();
    for (int j = 0; j < dynamicVoxels.size(); j++) {
        if (i.hasNext())
            bakedDrvt[j] = i.next();
        else {
            logger.error("while baking dynamicVoxelTypes array the iterator returned less than dynamicVoxels.size() elements");
            logger.error("cancelling");
            bakedDrvt = null;
            break;
        }
    }
    newRenderData.dynamicVoxelTypes = bakedDrvt;
    chunk.getChunkRenderData().setData(newRenderData);
    chunk.chunkRenderData.unbakedUpdates.addAndGet(-updatesToConsider);
    return true;
}
Also used : ChunkRenderable(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable) HashMap(java.util.HashMap) VoxelBakerCubic(io.xol.chunkstories.api.rendering.voxel.VoxelBakerCubic) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) VoxelBakerHighPoly(io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) LodLevel(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.LodLevel) Voxel(io.xol.chunkstories.api.voxel.Voxel) VoxelDynamicRenderer(io.xol.chunkstories.api.rendering.voxel.VoxelDynamicRenderer) UnexecutableTaskException(io.xol.chunkstories.api.exceptions.tasks.UnexecutableTaskException) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) VertexLayout(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.VertexLayout) Vector3dc(org.joml.Vector3dc) ChunkRenderer(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderer) ShadingType(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.ShadingType) DynamicallyRenderedVoxelType(io.xol.chunkstories.renderer.chunks.ChunkMeshDataSections.DynamicallyRenderedVoxelType) VoxelRenderer(io.xol.chunkstories.api.rendering.voxel.VoxelRenderer)

Example 15 with Voxel

use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories by Hugobros3.

the class HeightmapArrayTexture method loadTopVoxels.

private void loadTopVoxels(HeightmapImplementation sum, ByteBuffer bb, int lod) {
    bb.clear();
    int[] data = sum.getVoxelData();
    ScratchCell cell = new ScratchCell(world);
    cell.sunlight = 15;
    for (int i = 0; i < size[lod] * size[lod]; i++) {
        int j = HeightmapImplementation.mainMimpmapOffsets[lod] + i;
        int raw_data = data[j];
        Voxel v = world.getContentTranslator().getVoxelForId(VoxelFormat.id(raw_data));
        cell.voxel = v;
        if (v.getDefinition().isLiquid())
            bb.putInt(512);
        else
            bb.putInt(((VoxelTextureAtlased) v.getVoxelTexture(VoxelSide.TOP, cell)).positionInColorIndex);
    }
    bb.flip();
}
Also used : VoxelTextureAtlased(io.xol.chunkstories.voxel.VoxelTextureAtlased) Voxel(io.xol.chunkstories.api.voxel.Voxel) ScratchCell(io.xol.chunkstories.world.cell.ScratchCell)

Aggregations

Voxel (io.xol.chunkstories.api.voxel.Voxel)26 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)6 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)5 Entity (io.xol.chunkstories.api.entity.Entity)4 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)4 CellData (io.xol.chunkstories.api.world.cell.CellData)4 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)3 World (io.xol.chunkstories.api.world.World)3 WorldClient (io.xol.chunkstories.api.world.WorldClient)3 Vector3d (org.joml.Vector3d)3 Vector3dc (org.joml.Vector3dc)3 Location (io.xol.chunkstories.api.Location)2 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)2 Shader (io.xol.chunkstories.api.rendering.shader.Shader)2 VoxelBakerCubic (io.xol.chunkstories.api.rendering.voxel.VoxelBakerCubic)2 VoxelBakerHighPoly (io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly)2 ChunkRenderable (io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable)2 DummyCell (io.xol.chunkstories.api.world.cell.DummyCell)2 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)2 Vector3f (org.joml.Vector3f)2