Search in sources :

Example 11 with Shader

use of io.xol.chunkstories.api.rendering.shader.Shader in project chunkstories by Hugobros3.

the class DefaultWeatherEffectsRenderer method renderRain.

// int vboId = -1;
private void renderRain(RenderingInterface renderingContext, float rainPresence) {
    Shader weatherShader = renderingContext.useShader("weather");
    if ((System.currentTimeMillis() - lastRender) >= 1000 || Math.abs(viewX - lastX) > 10 || Math.abs(viewZ - lastZ) > 10) {
        generateRainForOneSecond(renderingContext, rainPresence);
        lastRender = System.currentTimeMillis();
    }
    renderingContext.setCullingMode(CullingMode.DISABLED);
    renderingContext.getCamera().setupShader(weatherShader);
    // Time since gen in ms
    weatherShader.setUniform1f("time", (System.currentTimeMillis() - lastRender) / 1000f);
    renderingContext.bindTexture2D("sunlightCycle", renderingContext.textures().getTexture("./textures/environement/lightcolors.png"));
    renderingContext.textures().getTexture("./textures/environement/lightcolors.png").setTextureWrapping(true);
    // TODO check on this
    this.worldRenderer.getSkyRenderer().setupShader(weatherShader);
    weatherShader.setUniform1f("sunTime", world.getTime() % 10000);
    renderingContext.bindAttribute("vertexIn", rainVerticesBuffer.asAttributeSource(VertexFormat.FLOAT, 4));
    float rainIntensity = Math.min(Math.max(0.0f, rainPresence - 0.5f) / 0.3f, 1.0f);
    renderingContext.draw(Primitive.TRIANGLE, 0, 20000 + (int) (90000 * rainIntensity));
    renderingContext.setCullingMode(CullingMode.COUNTERCLOCKWISE);
}
Also used : Shader(io.xol.chunkstories.api.rendering.shader.Shader)

Example 12 with Shader

use of io.xol.chunkstories.api.rendering.shader.Shader in project chunkstories by Hugobros3.

the class BonedRenderer method internalRenderer.

private void internalRenderer(RenderingInterface renderingContext, SkeletonAnimator skeleton, double animationTime) {
    prepareDraw(renderingContext);
    Shader shader = renderingContext.currentShader();
    if (skeleton != null) {
        for (int i = 0; i < boneName.length; i++) {
            if (skeleton.shouldHideBone(renderingContext, boneName[i])) {
                Matrix4f boneMatrix = new Matrix4f();
                boneMatrix.translate(50000, 50000, 50000);
                shader.setUniformMatrix4f("bones[" + i + "]", boneMatrix);
            } else {
                Matrix4fc boneMatrix = skeleton.getBoneHierarchyTransformationMatrixWithOffset(boneName[i], animationTime < 0 ? 0 : animationTime);
                shader.setUniformMatrix4f("bones[" + i + "]", boneMatrix);
            }
        }
    }
    renderingContext.draw(Primitive.TRIANGLE, 0, verticesCount);
}
Also used : Matrix4fc(org.joml.Matrix4fc) Matrix4f(org.joml.Matrix4f) Shader(io.xol.chunkstories.api.rendering.shader.Shader)

Example 13 with Shader

use of io.xol.chunkstories.api.rendering.shader.Shader in project chunkstories-api by Hugobros3.

the class EntityRenderer method renderEntities.

/**
 * Override this to implement your own entity render
 */
public int renderEntities(RenderingInterface renderer, RenderingIterator<E> renderableEntitiesIterator) {
    // The new and improved base 'entities' shader supports
    Shader shader = renderer.useShader("entities");
    int c = 0;
    for (E entity : renderableEntitiesIterator.getElementsInFrustrumOnly()) {
        c++;
    // do your rendering here
    }
    return c;
}
Also used : Shader(io.xol.chunkstories.api.rendering.shader.Shader)

Example 14 with Shader

use of io.xol.chunkstories.api.rendering.shader.Shader 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 15 with Shader

use of io.xol.chunkstories.api.rendering.shader.Shader in project chunkstories by Hugobros3.

the class FarTerrainGSMeshRenderer method renderTerrain.

@Override
public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
    Shader terrainShader = renderer.useShader("terrain_blocky");
    renderer.setBlendMode(BlendMode.DISABLED);
    renderer.getCamera().setupShader(terrainShader);
    worldRenderer.getSkyRenderer().setupShader(terrainShader);
    terrainShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
    Texture2D waterTexture = renderer.textures().getTexture("./textures/water/shallow.png");
    waterTexture.setLinearFiltering(true);
    waterTexture.setMipMapping(true);
    Texture2D waterTexture2 = renderer.textures().getTexture("./textures/water/deep.png");
    waterTexture2.setLinearFiltering(true);
    waterTexture2.setMipMapping(true);
    // renderer.bindCubemap("environmentCubemap", worldRenderer.renderBuffers.rbEnvironmentMap);
    renderer.bindTexture2D("blockLightmap", TexturesHandler.getTexture("./textures/environement/light.png"));
    Texture2D lightColors = TexturesHandler.getTexture("./textures/environement/lightcolors.png");
    renderer.bindTexture2D("lightColors", lightColors);
    renderer.bindTexture2D("waterNormalShallow", waterTexture);
    renderer.bindTexture2D("waterNormalDeep", waterTexture2);
    world.getGenerator().getEnvironment().setupShadowColors(renderer, terrainShader);
    renderer.bindTexture2D("vegetationColorTexture", world.getGenerator().getEnvironment().getGrassTexture(renderer));
    terrainShader.setUniform1f("mapSize", worldRenderer.getWorld().getSizeInChunks() * 32);
    terrainShader.setUniform1f("animationTimer", worldRenderer.getAnimationTimer());
    renderer.bindArrayTexture("heights", worldRenderer.getSummariesTexturesHolder().getHeightsArrayTexture());
    renderer.bindArrayTexture("topVoxels", worldRenderer.getSummariesTexturesHolder().getTopVoxelsArrayTexture());
    renderer.bindTexture1D("blocksTexturesSummary", colours.get());
    if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
    if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed) {
        renderer.setCullingMode(CullingMode.DISABLED);
        // renderer.setCullingMode(CullingMode.COUNTERCLOCKWISE);
        renderer.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
        Player player = Client.getInstance().getPlayer();
        Location playerPosition = player.getLocation();
        if (playerPosition == null)
            // We won't do shit with that going on
            return;
        Vector2d playerCenter = new Vector2d(playerPosition.x, playerPosition.z);
        int chunkX = (int) Math.floor(playerPosition.x / 32.0);
        int chunkZ = (int) Math.floor(playerPosition.z / 32.0);
        int regionX = chunkX / 8;
        int regionZ = chunkZ / 8;
        int[] lodInstanceCount = new int[detailLevels.length];
        ByteBuffer[] lodByteBuffer = new ByteBuffer[detailLevels.length];
        // ByteBuffer summariesAttributes = MemoryUtil.memAlloc(9 * 9 * (4 + 2 * 4));
        // MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
        // int count = 0;
        double lodBias = -0.2;
        double lodExponent = 0.35;
        if (!world.getClient().getConfiguration().getBooleanOption("client.rendering.hqTerrain")) {
            lodBias = 0.3;
            lodExponent = 0.35;
        } else {
            lodBias = 0.0;
            lodExponent = 0.45;
        }
        if (mask == null) {
            lodExponent = 1.0;
            lodBias = 0.6;
        }
        Vector2d center = new Vector2d();
        for (int i = -4; i <= 4; i++) for (int j = -4; j <= 4; j++) {
            int regionI = regionX + i;
            int regionJ = regionZ + j;
            int index = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ);
            if (index == -1)
                continue;
            // For the extra row of triangles to mask the seams
            int index10 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ);
            int index01 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ + 1);
            int index11 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ + 1);
            if (index10 == -1 || index01 == -1 || index11 == -1)
                continue;
            HeightmapImplementation sum = (HeightmapImplementation) world.getRegionsSummariesHolder().getHeightmap(regionI, regionJ);
            // Early out
            if (sum == null || !sum.isLoaded())
                continue;
            if (!renderer.getCamera().isBoxInFrustrum(new CollisionBox(regionI * 256, 0, regionJ * 256, 256, 1024, /*+ sum.getHeightMipmapped(0, 0, 9)*/
            256)))
                continue;
            for (int l = 0; l < 8; l++) for (int m = 0; m < 8; m++) {
                if (mask != null) {
                    if (mask.shouldMaskSlab(regionI * 8 + l, regionJ * 8 + m, sum.min[l][m], sum.max[l][m]))
                        continue;
                }
                center.set(regionI * 256 + l * 32 + 16, regionJ * 256 + m * 32 + 16);
                int lod = detailLevels.length - (int) ((lodBias + Math.pow(Math.min(1024, center.distance(playerCenter)) / 1024, lodExponent)) * detailLevels.length);
                // System.out.println(center.distance(playerCenter));
                if (lod <= 0)
                    lod = 0;
                if (lod >= detailLevels.length)
                    lod = detailLevels.length - 1;
                // lod = 2;
                // lod = 2;
                // System.out.println("lod:"+lod);
                // lod = Math.random() > 0.5 ? 1 : 2;
                ByteBuffer summariesAttributes = lodByteBuffer[lod];
                if (summariesAttributes == null) {
                    summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                    lodByteBuffer[lod] = summariesAttributes;
                }
                summariesAttributes.putFloat(regionI * 256 + l * 32);
                summariesAttributes.putFloat(regionJ * 256 + m * 32);
                summariesAttributes.put((byte) index);
                summariesAttributes.put((byte) index10);
                summariesAttributes.put((byte) index01);
                summariesAttributes.put((byte) index11);
                lodInstanceCount[lod]++;
            // Always add both so lod 1 is drew under
            /*if(lod != 0) {
								lod = 0;
								
								summariesAttributes = lodByteBuffer[lod];
								
								if(summariesAttributes == null) {
									summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
									lodByteBuffer[lod] = summariesAttributes;
								}
								
								summariesAttributes.putFloat(regionI * 256 + l * 32);
								summariesAttributes.putFloat(regionJ * 256 + m * 32);
								summariesAttributes.put((byte)index);
								summariesAttributes.put((byte)index10);
								summariesAttributes.put((byte)index01);
								summariesAttributes.put((byte)index11);
								lodInstanceCount[lod]++;
							}*/
            }
        }
        // for(int lod = 0; lod < detailLevels.length; lod++) {
        for (int lod = detailLevels.length - 1; lod >= 0; lod--) {
            // Check if anything was supposed to be drew at this lod
            ByteBuffer summariesAttributes = lodByteBuffer[lod];
            if (summariesAttributes == null)
                continue;
            if (lod < 0) {
                MemoryUtil.memFree(summariesAttributes);
                continue;
            }
            // Flip buffer, box it for autodeletion, upload it
            summariesAttributes.flip();
            MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
            gridAttributes.uploadData(auto_free_summariesAttributes);
            terrainShader.setUniform1i("lodLevel", lod);
            terrainShader.setUniform1f("textureLodLevel", lod - 5);
            terrainShader.setUniform1i("maskPresence", mask == null ? 0 : 1);
            renderer.bindAttribute("vertexIn", grids[lod].asAttributeSource(VertexFormat.FLOAT, 3, 0, 0L));
            renderer.bindAttribute("displacementIn", gridAttributes.asAttributeSource(VertexFormat.FLOAT, 2, (4 + 2 * 4), 0L, 1));
            renderer.bindAttribute("indexIn", gridAttributes.asIntegerAttributeSource(VertexFormat.BYTE, 4, (4 + 2 * 4), 8L, 1));
            renderer.draw(Primitive.POINT, 0, (detailLevels[lod] + 1) * (detailLevels[lod] + 1) * 1, lodInstanceCount[lod]);
        }
    }
    renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
Also used : Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Player(io.xol.chunkstories.api.player.Player) Shader(io.xol.chunkstories.api.rendering.shader.Shader) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) Vector2d(org.joml.Vector2d) Location(io.xol.chunkstories.api.Location) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Aggregations

Shader (io.xol.chunkstories.api.rendering.shader.Shader)27 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)9 Vector4f (org.joml.Vector4f)5 ByteBuffer (java.nio.ByteBuffer)4 Matrix4f (org.joml.Matrix4f)4 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)3 GameWindow (io.xol.chunkstories.api.rendering.GameWindow)3 Vector3f (org.joml.Vector3f)3 Location (io.xol.chunkstories.api.Location)2 Player (io.xol.chunkstories.api.player.Player)2 Voxel (io.xol.chunkstories.api.voxel.Voxel)2 CellData (io.xol.chunkstories.api.world.cell.CellData)2 MemFreeByteBuffer (io.xol.chunkstories.client.util.MemFreeByteBuffer)2 HeightmapImplementation (io.xol.chunkstories.world.summary.HeightmapImplementation)2 Vector2d (org.joml.Vector2d)2 Vector3dc (org.joml.Vector3dc)2 Layer (io.xol.chunkstories.api.gui.Layer)1 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)1 RenderPass (io.xol.chunkstories.api.rendering.pass.RenderPass)1 DummyCell (io.xol.chunkstories.api.world.cell.DummyCell)1