Search in sources :

Example 1 with LevelRenderer

use of net.minecraft.client.renderer.LevelRenderer in project Iris by IrisShaders.

the class ShadowRenderer method renderShadows.

@Override
public void renderShadows(LevelRendererAccessor levelRenderer, Camera playerCamera) {
    // vanilla ShaderInstances.
    if (blendModeOverride != null) {
        blendModeOverride.apply();
    } else {
        BlendModeOverride.restore();
    }
    Minecraft client = Minecraft.getInstance();
    profiler.popPush("shadows");
    ACTIVE = true;
    // NB: We store the previous player buffers in order to be able to allow mods rendering entities in the shadow pass (Flywheel) to use the shadow buffers instead.
    RenderBuffers playerBuffers = levelRenderer.getRenderBuffers();
    levelRenderer.setRenderBuffers(buffers);
    visibleBlockEntities = new ArrayList<>();
    // Create our camera
    PoseStack modelView = createShadowModelView(this.sunPathRotation, this.intervalSize);
    MODELVIEW = modelView.last().pose().copy();
    float[] projMatrix;
    if (this.fov != null) {
        // If FOV is not null, the pack wants a perspective based projection matrix. (This is to support legacy packs)
        projMatrix = ShadowMatrices.createPerspectiveMatrix(this.fov);
    } else {
        projMatrix = ShadowMatrices.createOrthoMatrix(halfPlaneLength);
    }
    PROJECTION = new Matrix4f();
    ((Matrix4fAccess) (Object) PROJECTION).copyFromArray(projMatrix);
    profiler.push("terrain_setup");
    if (levelRenderer instanceof CullingDataCache) {
        ((CullingDataCache) levelRenderer).saveState();
    }
    profiler.push("initialize frustum");
    terrainFrustumHolder = createShadowFrustum(renderDistanceMultiplier, terrainFrustumHolder);
    // Determine the player camera position
    Vector3d cameraPos = CameraUniforms.getUnshiftedCameraPosition();
    double cameraX = cameraPos.x();
    double cameraY = cameraPos.y();
    double cameraZ = cameraPos.z();
    // Center the frustum on the player camera position
    terrainFrustumHolder.getFrustum().prepare(cameraX, cameraY, cameraZ);
    profiler.pop();
    // Disable chunk occlusion culling - it's a bit complex to get this properly working with shadow rendering
    // as-is, however in the future it will be good to work on restoring it for a nice performance boost.
    // 
    // TODO: Get chunk occlusion working with shadows
    boolean wasChunkCullingEnabled = client.smartCull;
    client.smartCull = false;
    // Always schedule a terrain update
    // TODO: Only schedule a terrain update if the sun / moon is moving, or the shadow map camera moved.
    ((LevelRenderer) levelRenderer).needsUpdate();
    // Execute the vanilla terrain setup / culling routines using our shadow frustum.
    levelRenderer.invokeSetupRender(playerCamera, terrainFrustumHolder.getFrustum(), false, levelRenderer.getFrameId(), false);
    // Don't forget to increment the frame counter! This variable is arbitrary and only used in terrain setup,
    // and if it's not incremented, the vanilla culling code will get confused and think that it's already seen
    // chunks during traversal, and break rendering in concerning ways.
    levelRenderer.setFrameId(levelRenderer.getFrameId() + 1);
    client.smartCull = wasChunkCullingEnabled;
    profiler.popPush("terrain");
    pipeline.pushProgram(GbufferProgram.NONE);
    pipeline.beginShadowRender();
    setupGlState(projMatrix);
    // Render all opaque terrain unless pack requests not to
    if (shouldRenderTerrain) {
        levelRenderer.invokeRenderChunkLayer(RenderType.solid(), modelView, cameraX, cameraY, cameraZ);
        levelRenderer.invokeRenderChunkLayer(RenderType.cutout(), modelView, cameraX, cameraY, cameraZ);
        levelRenderer.invokeRenderChunkLayer(RenderType.cutoutMipped(), modelView, cameraX, cameraY, cameraZ);
    }
    // Reset our shader program in case Sodium overrode it.
    // 
    // If we forget to do this entities will be very small on most shaderpacks since they're being rendered
    // without shaders, which doesn't integrate with their shadow distortion code.
    setupShadowProgram();
    profiler.popPush("entities");
    // Get the current tick delta. Normally this is the same as client.getTickDelta(), but when the game is paused,
    // it is set to a fixed value.
    final float tickDelta = CapturedRenderingState.INSTANCE.getTickDelta();
    // Create a constrained shadow frustum for entities to avoid rendering faraway entities in the shadow pass
    // TODO: Make this configurable and disable-able
    boolean hasEntityFrustum = false;
    if (entityShadowDistanceMultiplier == 1.0F || entityShadowDistanceMultiplier < 0.0F) {
        entityFrustumHolder.setInfo(terrainFrustumHolder.getFrustum(), terrainFrustumHolder.getDistanceInfo(), terrainFrustumHolder.getCullingInfo());
    } else {
        hasEntityFrustum = true;
        entityFrustumHolder = createShadowFrustum(renderDistanceMultiplier * entityShadowDistanceMultiplier, entityFrustumHolder);
    }
    Frustum entityShadowFrustum = entityFrustumHolder.getFrustum();
    entityShadowFrustum.prepare(cameraX, cameraY, cameraZ);
    // rendering.
    if (renderBuffersExt != null) {
        renderBuffersExt.beginLevelRendering();
    }
    if (buffers instanceof DrawCallTrackingRenderBuffers) {
        ((DrawCallTrackingRenderBuffers) buffers).resetDrawCounts();
    }
    MultiBufferSource.BufferSource bufferSource = buffers.bufferSource();
    if (shouldRenderEntities) {
        renderEntities(levelRenderer, entityShadowFrustum, bufferSource, modelView, cameraX, cameraY, cameraZ, tickDelta);
    }
    if (shouldRenderBlockEntities) {
        renderBlockEntities(bufferSource, modelView, cameraX, cameraY, cameraZ, tickDelta, hasEntityFrustum);
    }
    profiler.popPush("draw entities");
    // NB: Don't try to draw the translucent parts of entities afterwards. It'll cause problems since some
    // shader packs assume that everything drawn afterwards is actually translucent and should cast a colored
    // shadow...
    bufferSource.endBatch();
    copyPreTranslucentDepth();
    profiler.popPush("translucent terrain");
    // Just something to watch out for, however...
    if (shouldRenderTranslucent) {
        levelRenderer.invokeRenderChunkLayer(RenderType.translucent(), modelView, cameraX, cameraY, cameraZ);
    }
    if (renderBuffersExt != null) {
        renderBuffersExt.endLevelRendering();
    }
    debugStringTerrain = ((LevelRenderer) levelRenderer).getChunkStatistics();
    profiler.popPush("generate mipmaps");
    generateMipmaps();
    profiler.popPush("restore gl state");
    restoreGlState(client);
    pipeline.endShadowRender();
    // Note: This MUST happen before popProgram, otherwise we'll mess up the blend mode override of the program that
    // is being restored.
    BlendModeOverride.restore();
    // Note: This unbinds the shadow framebuffer
    pipeline.popProgram(GbufferProgram.NONE);
    if (levelRenderer instanceof CullingDataCache) {
        ((CullingDataCache) levelRenderer).restoreState();
    }
    levelRenderer.setRenderBuffers(playerBuffers);
    ACTIVE = false;
    profiler.popPush("updatechunks");
}
Also used : AdvancedShadowCullingFrustum(net.coderbot.iris.shadows.frustum.advanced.AdvancedShadowCullingFrustum) Frustum(net.minecraft.client.renderer.culling.Frustum) BoxCullingFrustum(net.coderbot.iris.shadows.frustum.fallback.BoxCullingFrustum) NonCullingFrustum(net.coderbot.iris.shadows.frustum.fallback.NonCullingFrustum) CullEverythingFrustum(net.coderbot.iris.shadows.frustum.CullEverythingFrustum) PoseStack(com.mojang.blaze3d.vertex.PoseStack) RenderBuffers(net.minecraft.client.renderer.RenderBuffers) DrawCallTrackingRenderBuffers(net.coderbot.batchedentityrendering.impl.DrawCallTrackingRenderBuffers) MultiBufferSource(net.minecraft.client.renderer.MultiBufferSource) Minecraft(net.minecraft.client.Minecraft) DrawCallTrackingRenderBuffers(net.coderbot.batchedentityrendering.impl.DrawCallTrackingRenderBuffers) Matrix4f(com.mojang.math.Matrix4f) Vector3d(net.coderbot.iris.vendored.joml.Vector3d) CullingDataCache(net.coderbot.iris.shadows.CullingDataCache) Matrix4fAccess(net.coderbot.iris.shadows.Matrix4fAccess) LevelRenderer(net.minecraft.client.renderer.LevelRenderer) BlendModeOverride(net.coderbot.iris.gl.blending.BlendModeOverride)

Example 2 with LevelRenderer

use of net.minecraft.client.renderer.LevelRenderer in project Iris by IrisShaders.

the class ShadowRenderer method renderEntities.

private void renderEntities(LevelRendererAccessor levelRenderer, Frustum frustum, MultiBufferSource.BufferSource bufferSource, PoseStack modelView, double cameraX, double cameraY, double cameraZ, float tickDelta) {
    EntityRenderDispatcher dispatcher = levelRenderer.getEntityRenderDispatcher();
    int shadowEntities = 0;
    profiler.push("cull");
    List<Entity> renderedEntities = new ArrayList<>(32);
    // TODO: I'm sure that this can be improved / optimized.
    for (Entity entity : getLevel().entitiesForRendering()) {
        if (!dispatcher.shouldRender(entity, frustum, cameraX, cameraY, cameraZ) || entity.isSpectator()) {
            continue;
        }
        renderedEntities.add(entity);
    }
    profiler.popPush("sort");
    // Sort the entities by type first in order to allow vanilla's entity batching system to work better.
    renderedEntities.sort(Comparator.comparingInt(entity -> entity.getType().hashCode()));
    profiler.popPush("build geometry");
    for (Entity entity : renderedEntities) {
        levelRenderer.invokeRenderEntity(entity, cameraX, cameraY, cameraZ, tickDelta, modelView, bufferSource);
        shadowEntities++;
    }
    renderedShadowEntities = shadowEntities;
}
Also used : Camera(net.minecraft.client.Camera) ShadowMatrices(net.coderbot.iris.shadow.ShadowMatrices) Object2ObjectMap(it.unimi.dsi.fastutil.objects.Object2ObjectMap) Matrix4f(com.mojang.math.Matrix4f) Vector3d(net.coderbot.iris.vendored.joml.Vector3d) IrisRenderSystem(net.coderbot.iris.gl.IrisRenderSystem) ProgramSamplers(net.coderbot.iris.gl.program.ProgramSamplers) GL20C(org.lwjgl.opengl.GL20C) ProgramManager(com.mojang.blaze3d.shaders.ProgramManager) FrustumHolder(net.coderbot.iris.shadows.frustum.FrustumHolder) EntityRenderDispatcher(net.minecraft.client.renderer.entity.EntityRenderDispatcher) OptionalBoolean(net.coderbot.iris.shaderpack.OptionalBoolean) ImmutableSet(com.google.common.collect.ImmutableSet) BatchingDebugMessageHelper(net.coderbot.batchedentityrendering.impl.BatchingDebugMessageHelper) ProfilerFiller(net.minecraft.util.profiling.ProfilerFiller) AdvancedShadowCullingFrustum(net.coderbot.iris.shadows.frustum.advanced.AdvancedShadowCullingFrustum) LevelRenderer(net.minecraft.client.renderer.LevelRenderer) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) ProgramSet(net.coderbot.iris.shaderpack.ProgramSet) Objects(java.util.Objects) Matrix4fAccess(net.coderbot.iris.shadows.Matrix4fAccess) Nullable(org.jetbrains.annotations.Nullable) AbstractTexture(net.minecraft.client.renderer.texture.AbstractTexture) List(java.util.List) IrisImages(net.coderbot.iris.samplers.IrisImages) RenderBuffersExt(net.coderbot.batchedentityrendering.impl.RenderBuffersExt) ProgramSource(net.coderbot.iris.shaderpack.ProgramSource) BlockPos(net.minecraft.core.BlockPos) LevelRendererAccessor(net.coderbot.iris.mixin.LevelRendererAccessor) MultiBufferSource(net.minecraft.client.renderer.MultiBufferSource) BlockEntityRenderDispatcher(net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher) CommonUniforms(net.coderbot.iris.uniforms.CommonUniforms) RenderBuffers(net.minecraft.client.renderer.RenderBuffers) Frustum(net.minecraft.client.renderer.culling.Frustum) BoxCullingFrustum(net.coderbot.iris.shadows.frustum.fallback.BoxCullingFrustum) ShadowRenderTargets(net.coderbot.iris.shadows.ShadowRenderTargets) ClientLevel(net.minecraft.client.multiplayer.ClientLevel) ShadowMapRenderer(net.coderbot.iris.shadows.ShadowMapRenderer) GL30C(org.lwjgl.opengl.GL30C) Supplier(java.util.function.Supplier) PoseStack(com.mojang.blaze3d.vertex.PoseStack) Vector3f(com.mojang.math.Vector3f) CullingDataCache(net.coderbot.iris.shadows.CullingDataCache) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Minecraft(net.minecraft.client.Minecraft) InternalTextureFormat(net.coderbot.iris.gl.texture.InternalTextureFormat) PackDirectives(net.coderbot.iris.shaderpack.PackDirectives) PackShadowDirectives(net.coderbot.iris.shaderpack.PackShadowDirectives) NonCullingFrustum(net.coderbot.iris.shadows.frustum.fallback.NonCullingFrustum) IntSupplier(java.util.function.IntSupplier) DrawCallTrackingRenderBuffers(net.coderbot.batchedentityrendering.impl.DrawCallTrackingRenderBuffers) GbufferProgram(net.coderbot.iris.layer.GbufferProgram) CelestialUniforms(net.coderbot.iris.uniforms.CelestialUniforms) Vector4f(net.coderbot.iris.vendored.joml.Vector4f) GL11C(org.lwjgl.opengl.GL11C) RenderType(net.minecraft.client.renderer.RenderType) IrisSamplers(net.coderbot.iris.samplers.IrisSamplers) RenderTargets(net.coderbot.iris.rendertarget.RenderTargets) CapturedRenderingState(net.coderbot.iris.uniforms.CapturedRenderingState) Entity(net.minecraft.world.entity.Entity) BlendModeOverride(net.coderbot.iris.gl.blending.BlendModeOverride) Program(net.coderbot.iris.gl.program.Program) ProgramBuilder(net.coderbot.iris.gl.program.ProgramBuilder) RenderSystem(com.mojang.blaze3d.systems.RenderSystem) CameraUniforms(net.coderbot.iris.uniforms.CameraUniforms) BoxCuller(net.coderbot.iris.shadows.frustum.BoxCuller) CullEverythingFrustum(net.coderbot.iris.shadows.frustum.CullEverythingFrustum) Comparator(java.util.Comparator) IrisVideoSettings(net.coderbot.iris.gui.option.IrisVideoSettings) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Entity(net.minecraft.world.entity.Entity) ArrayList(java.util.ArrayList) EntityRenderDispatcher(net.minecraft.client.renderer.entity.EntityRenderDispatcher) BlockEntityRenderDispatcher(net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher)

Example 3 with LevelRenderer

use of net.minecraft.client.renderer.LevelRenderer in project TinkersConstruct by SlimeKnights.

the class ToolRenderEvents method renderBlockHighlights.

/**
 * Renders the outline on the extra blocks
 *
 * @param event the highlight event
 */
@SubscribeEvent
static void renderBlockHighlights(DrawSelectionEvent.HighlightBlock event) {
    Level world = Minecraft.getInstance().level;
    Player player = Minecraft.getInstance().player;
    if (world == null || player == null) {
        return;
    }
    // must have the right tags
    ItemStack stack = player.getMainHandItem();
    if (stack.isEmpty() || !TinkerTags.Items.HARVEST.contains(stack.getItem())) {
        return;
    }
    // must be targeting a block
    HitResult result = Minecraft.getInstance().hitResult;
    if (result == null || result.getType() != Type.BLOCK) {
        return;
    }
    // must not be broken, must be right interface
    ToolStack tool = ToolStack.from(stack);
    if (tool.isBroken()) {
        return;
    }
    BlockHitResult blockTrace = event.getTarget();
    BlockPos origin = blockTrace.getBlockPos();
    BlockState state = world.getBlockState(origin);
    if (!ToolHarvestLogic.isEffective(tool, state)) {
        return;
    }
    Iterator<BlockPos> extraBlocks = tool.getDefinition().getData().getAOE().getBlocks(tool, stack, player, world.getBlockState(origin), world, origin, blockTrace.getDirection(), IAreaOfEffectIterator.AOEMatchType.BREAKING).iterator();
    if (!extraBlocks.hasNext()) {
        return;
    }
    // set up renderer
    LevelRenderer worldRender = event.getLevelRenderer();
    PoseStack matrices = event.getPoseStack();
    MultiBufferSource.BufferSource buffers = worldRender.renderBuffers.bufferSource();
    VertexConsumer vertexBuilder = buffers.getBuffer(RenderType.lines());
    matrices.pushPose();
    // start drawing
    Camera renderInfo = Minecraft.getInstance().gameRenderer.getMainCamera();
    Entity viewEntity = renderInfo.getEntity();
    Vec3 vector3d = renderInfo.getPosition();
    double x = vector3d.x();
    double y = vector3d.y();
    double z = vector3d.z();
    int rendered = 0;
    do {
        BlockPos pos = extraBlocks.next();
        if (world.getWorldBorder().isWithinBounds(pos)) {
            rendered++;
            worldRender.renderHitOutline(matrices, vertexBuilder, viewEntity, x, y, z, pos, world.getBlockState(pos));
        }
    } while (rendered < MAX_BLOCKS && extraBlocks.hasNext());
    matrices.popPose();
    buffers.endBatch();
}
Also used : Entity(net.minecraft.world.entity.Entity) Player(net.minecraft.world.entity.player.Player) PoseStack(com.mojang.blaze3d.vertex.PoseStack) MultiBufferSource(net.minecraft.client.renderer.MultiBufferSource) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) HitResult(net.minecraft.world.phys.HitResult) BlockState(net.minecraft.world.level.block.state.BlockState) Vec3(net.minecraft.world.phys.Vec3) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) VertexConsumer(com.mojang.blaze3d.vertex.VertexConsumer) Camera(net.minecraft.client.Camera) ItemStack(net.minecraft.world.item.ItemStack) BlockHitResult(net.minecraft.world.phys.BlockHitResult) LevelRenderer(net.minecraft.client.renderer.LevelRenderer) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Aggregations

PoseStack (com.mojang.blaze3d.vertex.PoseStack)3 LevelRenderer (net.minecraft.client.renderer.LevelRenderer)3 MultiBufferSource (net.minecraft.client.renderer.MultiBufferSource)3 Matrix4f (com.mojang.math.Matrix4f)2 DrawCallTrackingRenderBuffers (net.coderbot.batchedentityrendering.impl.DrawCallTrackingRenderBuffers)2 BlendModeOverride (net.coderbot.iris.gl.blending.BlendModeOverride)2 Camera (net.minecraft.client.Camera)2 BlockPos (net.minecraft.core.BlockPos)2 Entity (net.minecraft.world.entity.Entity)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ProgramManager (com.mojang.blaze3d.shaders.ProgramManager)1 RenderSystem (com.mojang.blaze3d.systems.RenderSystem)1 VertexConsumer (com.mojang.blaze3d.vertex.VertexConsumer)1 Vector3f (com.mojang.math.Vector3f)1 Object2ObjectMap (it.unimi.dsi.fastutil.objects.Object2ObjectMap)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Objects (java.util.Objects)1