| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.blaze3d.vertex.VertexConsumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 8 | import net.minecraft.client.renderer.entity.state.LightningBoltRenderState;
|
| 9 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 10 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.world.entity.LightningBolt;
|
| 13 | import org.joml.Matrix4fc;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class LightningBoltRenderer extends EntityRenderer<LightningBolt, LightningBoltRenderState> {
|
| 17 | private static final float BOLT_RED = 0.45F;
|
| 18 | private static final float BOLT_GREEN = 0.45F;
|
| 19 | private static final float BOLT_BLUE = 0.5F;
|
| 20 | private static final int SEGMENT_COUNT = 8;
|
| 21 | private static final int LAYER_COUNT = 4;
|
| 22 | private static final int BRANCH_COUNT = 3;
|
| 23 | private static final int BRANCH_SEGMENT_COUNT = 3;
|
| 24 |
|
| 25 | public LightningBoltRenderer(final EntityRendererProvider.Context context) {
|
| 26 | super(context);
|
| 27 | }
|
| 28 |
|
| 29 | public void submit(
|
| 30 | final LightningBoltRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera
|
| 31 | ) {
|
| 32 | float[] xOffsets = new float[8];
|
| 33 | float[] zOffsets = new float[8];
|
| 34 | float xOffset = 0.0F;
|
| 35 | float zOffset = 0.0F;
|
| 36 | RandomSource random = RandomSource.createThreadLocalInstance(state.seed);
|
| 37 |
|
| 38 | for (int heightSegmentIndex = 7; heightSegmentIndex >= 0; heightSegmentIndex--) {
|
| 39 | xOffsets[heightSegmentIndex] = xOffset;
|
| 40 | zOffsets[heightSegmentIndex] = zOffset;
|
| 41 | xOffset += random.nextInt(11) - 5;
|
| 42 | zOffset += random.nextInt(11) - 5;
|
| 43 | }
|
| 44 |
|
| 45 | float finalXOff = xOffset;
|
| 46 | float finalZOff = zOffset;
|
| 47 | submitNodeCollector.submitCustomGeometry(poseStack, RenderTypes.lightning(), (pose, buffer) -> {
|
| 48 | Matrix4fc poseMatrix = pose.pose();
|
| 49 |
|
| 50 | for (int layer = 0; layer < 4; layer++) {
|
| 51 | RandomSource randomx = RandomSource.createThreadLocalInstance(state.seed);
|
| 52 |
|
| 53 | for (int branchNumber = 0; branchNumber < 3; branchNumber++) {
|
| 54 | boolean isTrunkBranch = branchNumber == 0;
|
| 55 | int branchStartSegment = 7 - branchNumber;
|
| 56 | int branchEndSegment = isTrunkBranch ? 0 : branchStartSegment - 3 + 1;
|
| 57 | float segmentStartX = xOffsets[branchStartSegment] - finalXOff;
|
| 58 | float segmentStartZ = zOffsets[branchStartSegment] - finalZOff;
|
| 59 |
|
| 60 | for (int currentSegment = branchStartSegment; currentSegment >= branchEndSegment; currentSegment--) {
|
| 61 | float segmentEndX = segmentStartX;
|
| 62 | float segmentEndZ = segmentStartZ;
|
| 63 | if (isTrunkBranch) {
|
| 64 | segmentStartX += randomx.nextInt(11) - 5;
|
| 65 | segmentStartZ += randomx.nextInt(11) - 5;
|
| 66 | } else {
|
| 67 | segmentStartX += randomx.nextInt(31) - 15;
|
| 68 | segmentStartZ += randomx.nextInt(31) - 15;
|
| 69 | }
|
| 70 |
|
| 71 | float topRadius = 0.1F + layer * 0.2F;
|
| 72 | float bottomRadius = topRadius;
|
| 73 | if (isTrunkBranch) {
|
| 74 | topRadius *= currentSegment * 0.1F + 1.0F;
|
| 75 | bottomRadius *= (currentSegment - 1) * 0.1F + 1.0F;
|
| 76 | }
|
| 77 |
|
| 78 | quad(poseMatrix, buffer, segmentStartX, segmentStartZ, segmentEndX, segmentEndZ, currentSegment, topRadius, bottomRadius, false, false, true, false);
|
| 79 | quad(poseMatrix, buffer, segmentStartX, segmentStartZ, segmentEndX, segmentEndZ, currentSegment, topRadius, bottomRadius, true, false, true, true);
|
| 80 | quad(poseMatrix, buffer, segmentStartX, segmentStartZ, segmentEndX, segmentEndZ, currentSegment, topRadius, bottomRadius, true, true, false, true);
|
| 81 | quad(poseMatrix, buffer, segmentStartX, segmentStartZ, segmentEndX, segmentEndZ, currentSegment, topRadius, bottomRadius, false, true, false, false);
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 | });
|
| 86 | }
|
| 87 |
|
| 88 | private static void quad(
|
| 89 | final Matrix4fc pose,
|
| 90 | final VertexConsumer buffer,
|
| 91 | final float segmentStartX,
|
| 92 | final float segmentStartZ,
|
| 93 | final float segmentEndX,
|
| 94 | final float segmentEndZ,
|
| 95 | final int currentSegment,
|
| 96 | final float topRadius,
|
| 97 | final float bottomRadius,
|
| 98 | final boolean rightXPositive,
|
| 99 | final boolean rightZPositive,
|
| 100 | final boolean leftXPositive,
|
| 101 | final boolean leftZPositive
|
| 102 | ) {
|
| 103 | buffer.addVertex(
|
| 104 | pose, segmentStartX + (rightXPositive ? bottomRadius : -bottomRadius), currentSegment * 16, segmentStartZ + (rightZPositive ? bottomRadius : -bottomRadius)
|
| 105 | )
|
| 106 | .setColor(0.45F, 0.45F, 0.5F, 0.3F);
|
| 107 | buffer.addVertex(
|
| 108 | pose, segmentEndX + (rightXPositive ? topRadius : -topRadius), (currentSegment + 1) * 16, segmentEndZ + (rightZPositive ? topRadius : -topRadius)
|
| 109 | )
|
| 110 | .setColor(0.45F, 0.45F, 0.5F, 0.3F);
|
| 111 | buffer.addVertex(
|
| 112 | pose, segmentEndX + (leftXPositive ? topRadius : -topRadius), (currentSegment + 1) * 16, segmentEndZ + (leftZPositive ? topRadius : -topRadius)
|
| 113 | )
|
| 114 | .setColor(0.45F, 0.45F, 0.5F, 0.3F);
|
| 115 | buffer.addVertex(
|
| 116 | pose, segmentStartX + (leftXPositive ? bottomRadius : -bottomRadius), currentSegment * 16, segmentStartZ + (leftZPositive ? bottomRadius : -bottomRadius)
|
| 117 | )
|
| 118 | .setColor(0.45F, 0.45F, 0.5F, 0.3F);
|
| 119 | }
|
| 120 |
|
| 121 | public LightningBoltRenderState createRenderState() {
|
| 122 | return new LightningBoltRenderState();
|
| 123 | }
|
| 124 |
|
| 125 | public void extractRenderState(final LightningBolt entity, final LightningBoltRenderState state, final float partialTicks) {
|
| 126 | super.extractRenderState(entity, state, partialTicks);
|
| 127 | state.seed = entity.seed;
|
| 128 | }
|
| 129 |
|
| 130 | protected boolean affectedByCulling(final LightningBolt entity) {
|
| 131 | return false;
|
| 132 | }
|
| 133 | }
|
| 134 |
|