|
| 1 | +package com.modularmods.mcgltf.example; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.lwjgl.opengl.GL11; |
| 6 | +import org.lwjgl.opengl.GL30; |
| 7 | + |
| 8 | +import com.modularmods.mcgltf.MCglTF; |
| 9 | +import com.modularmods.mcgltf.RenderedGltfModel; |
| 10 | +import com.modularmods.mcgltf.animation.InterpolatedChannel; |
| 11 | +import com.modularmods.mcgltf.iris.IrisRenderingHook; |
| 12 | +import com.mojang.blaze3d.platform.GlStateManager; |
| 13 | +import com.mojang.blaze3d.vertex.PoseStack; |
| 14 | +import com.mojang.math.Matrix3f; |
| 15 | +import com.mojang.math.Matrix4f; |
| 16 | +import com.mojang.math.Quaternion; |
| 17 | + |
| 18 | +import net.minecraft.client.renderer.MultiBufferSource; |
| 19 | +import net.minecraft.client.renderer.RenderType; |
| 20 | +import net.minecraft.client.renderer.texture.TextureAtlas; |
| 21 | +import net.minecraft.core.Direction; |
| 22 | +import net.minecraft.world.level.Level; |
| 23 | +import net.minecraft.world.level.block.HorizontalDirectionalBlock; |
| 24 | + |
| 25 | +public class ExampleBlockEntityRendererIris extends ExampleBlockEntityRenderer { |
| 26 | + |
| 27 | + @Override |
| 28 | + public void render(ExampleBlockEntity blockEntity, float tickDelta, PoseStack matrices, MultiBufferSource vertexConsumers, int light, int overlay) { |
| 29 | + RenderType renderType = RenderType.entitySolid(TextureAtlas.LOCATION_BLOCKS); |
| 30 | + vertexConsumers.getBuffer(renderType); //Put renderType into MultiBufferSource to ensure command submit to IrisRenderingHook will be run in Iris batched entity rendering. |
| 31 | + Matrix4f modelViewMatrix = matrices.last().pose().copy(); |
| 32 | + |
| 33 | + if(MCglTF.getInstance().isShaderModActive()) { |
| 34 | + IrisRenderingHook.submitCommandForIrisRenderingByPhaseName("BLOCK_ENTITIES", renderType, () -> { |
| 35 | + Level level = blockEntity.getLevel(); |
| 36 | + if(level != null) { |
| 37 | + float time = (level.getGameTime() + tickDelta) / 20; |
| 38 | + //Play every animation clips simultaneously |
| 39 | + for(List<InterpolatedChannel> animation : animations) { |
| 40 | + animation.parallelStream().forEach((channel) -> { |
| 41 | + float[] keys = channel.getKeys(); |
| 42 | + channel.update(time % keys[keys.length - 1]); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + modelViewMatrix.multiplyWithTranslation(0.5F, 0.5F, 0.5F); //Make sure it is in the center of the block |
| 47 | + switch(level.getBlockState(blockEntity.getBlockPos()).getOptionalValue(HorizontalDirectionalBlock.FACING).orElse(Direction.NORTH)) { |
| 48 | + case DOWN: |
| 49 | + break; |
| 50 | + case UP: |
| 51 | + break; |
| 52 | + case NORTH: |
| 53 | + break; |
| 54 | + case SOUTH: |
| 55 | + modelViewMatrix.multiply(new Quaternion(0.0F, 1.0F, 0.0F, 0.0F)); |
| 56 | + break; |
| 57 | + case WEST: |
| 58 | + modelViewMatrix.multiply(new Quaternion(0.0F, 0.7071068F, 0.0F, 0.7071068F)); |
| 59 | + break; |
| 60 | + case EAST: |
| 61 | + modelViewMatrix.multiply(new Quaternion(0.0F, -0.7071068F, 0.0F, 0.7071068F)); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + RenderedGltfModel.CURRENT_POSE = modelViewMatrix; |
| 67 | + |
| 68 | + boolean currentBlend = GL11.glGetBoolean(GL11.GL_BLEND); |
| 69 | + GL11.glEnable(GL11.GL_BLEND); //Since the renderType is entity solid, we need to turn on blending manually. |
| 70 | + GlStateManager._blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); |
| 71 | + |
| 72 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV1, overlay & '\uffff', overlay >> 16 & '\uffff'); |
| 73 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV2, light & '\uffff', light >> 16 & '\uffff'); |
| 74 | + |
| 75 | + renderedScene.renderForShaderMod(); |
| 76 | + |
| 77 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV1, 0, 0); |
| 78 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV2, 0, 0); |
| 79 | + |
| 80 | + if(!currentBlend) GL11.glDisable(GL11.GL_BLEND); |
| 81 | + }); |
| 82 | + } |
| 83 | + else { |
| 84 | + Matrix3f normalMatrix = matrices.last().normal().copy(); |
| 85 | + IrisRenderingHook.submitCommandForIrisRenderingByPhaseName("NONE", renderType, () -> { |
| 86 | + Level level = blockEntity.getLevel(); |
| 87 | + if(level != null) { |
| 88 | + float time = (level.getGameTime() + tickDelta) / 20; |
| 89 | + for(List<InterpolatedChannel> animation : animations) { |
| 90 | + animation.parallelStream().forEach((channel) -> { |
| 91 | + float[] keys = channel.getKeys(); |
| 92 | + channel.update(time % keys[keys.length - 1]); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + modelViewMatrix.multiplyWithTranslation(0.5F, 0.5F, 0.5F); |
| 97 | + switch(level.getBlockState(blockEntity.getBlockPos()).getOptionalValue(HorizontalDirectionalBlock.FACING).orElse(Direction.NORTH)) { |
| 98 | + case DOWN: |
| 99 | + break; |
| 100 | + case UP: |
| 101 | + break; |
| 102 | + case NORTH: |
| 103 | + break; |
| 104 | + case SOUTH: |
| 105 | + Quaternion rotation = new Quaternion(0.0F, 1.0F, 0.0F, 0.0F); |
| 106 | + modelViewMatrix.multiply(rotation); |
| 107 | + normalMatrix.mul(rotation); |
| 108 | + break; |
| 109 | + case WEST: |
| 110 | + rotation = new Quaternion(0.0F, 0.7071068F, 0.0F, 0.7071068F); |
| 111 | + modelViewMatrix.multiply(rotation); |
| 112 | + normalMatrix.mul(rotation); |
| 113 | + break; |
| 114 | + case EAST: |
| 115 | + rotation = new Quaternion(0.0F, -0.7071068F, 0.0F, 0.7071068F); |
| 116 | + modelViewMatrix.multiply(rotation); |
| 117 | + normalMatrix.mul(rotation); |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + RenderedGltfModel.CURRENT_POSE = modelViewMatrix; |
| 123 | + RenderedGltfModel.CURRENT_NORMAL = normalMatrix; |
| 124 | + |
| 125 | + boolean currentBlend = GL11.glGetBoolean(GL11.GL_BLEND); |
| 126 | + GL11.glEnable(GL11.GL_BLEND); |
| 127 | + GlStateManager._blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); |
| 128 | + |
| 129 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV1, overlay & '\uffff', overlay >> 16 & '\uffff'); |
| 130 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV2, light & '\uffff', light >> 16 & '\uffff'); |
| 131 | + |
| 132 | + renderedScene.renderForVanilla(); |
| 133 | + |
| 134 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV1, 0, 0); |
| 135 | + GL30.glVertexAttribI2i(RenderedGltfModel.vaUV2, 0, 0); |
| 136 | + |
| 137 | + if(!currentBlend) GL11.glDisable(GL11.GL_BLEND); |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments