getTexture(LaserRayEntity)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
package de.pewpewproject.lasertag.entity.render;
2
3
import de.pewpewproject.lasertag.entity.LaserRayEntity;
4
import net.minecraft.client.render.*;
5
import net.minecraft.client.render.entity.EntityRenderer;
6
import net.minecraft.client.render.entity.EntityRendererFactory.Context;
7
import net.minecraft.client.util.math.MatrixStack;
8
import net.minecraft.util.Identifier;
9
import net.minecraft.util.math.*;
10
11
/**
12
 * A custom renderer to render the laser ray entity as a smaller beacon beam
13
 *
14
 * @author Étienne Muser
15
 */
16
public class LaserRayEntityRenderer extends EntityRenderer<LaserRayEntity> {
17
    public static final Identifier BEAM_TEXTURE = new Identifier("textures/entity/beacon_beam.png");
18
19
    public LaserRayEntityRenderer(Context ctx) {
20
        super(ctx);
21
    }
22
23
    @Override
24
    public Identifier getTexture(LaserRayEntity var1) {
25
        return BEAM_TEXTURE;
26
    }
27
28
    /**
29
     * Renders the entity
30
     */
31
    @Override
32
    public void render(LaserRayEntity laserRayEntity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light) {
33
        long worldTime = laserRayEntity.getWorld().getTime();
34
        int color = laserRayEntity.getColor();
35
        float[] colorArr = new float[] {
36
                ((color >> 16) & 0xFF) / 255.0F,
37
                ((color >> 8) & 0xFF) / 255.0F,
38
                ((color) & 0xFF) / 255.0F
39
        };
40
        Vec3d end = laserRayEntity.getEnd().subtract(laserRayEntity.getStart());
41
        renderBeam(matrices, vertexConsumers, tickDelta, worldTime, colorArr, end);
42
    }
43
44
    /**
45
     * Render a beacon beam
46
     *
47
     * @param matrices The matrix stack
48
     * @param vertexConsumers The vertex consumer provider
49
     * @param color the color float array
50
     * @param end The end position of the ray relative to its start position
51
     */
52
    private static void renderBeam(MatrixStack matrices, VertexConsumerProvider vertexConsumers, float tickDelta, long worldTime, float[] color, Vec3d end) {
53
        renderBeam(matrices, vertexConsumers, BEAM_TEXTURE, tickDelta, worldTime, end, color, 0.0125f, 0.015625f);
54
    }
55
56
    /**
57
     * Render a beacon beam
58
     *
59
     * @param matrices The matrix stack
60
     * @param vertexConsumers The vertex consumer provider
61
     */
62
    public static void renderBeam(MatrixStack matrices, VertexConsumerProvider vertexConsumers, Identifier textureId, float tickDelta, long worldTime, Vec3d end, float[] color, float innerRadius, float outerRadius) {
63
        double length = end.length();
64
        Quaternion rot = getRot(end);
65
        matrices.push();
66
67
        float beamSpinProgress = (float) Math.floorMod(worldTime, 40) + tickDelta;
68
        float h = MathHelper.fractionalPart(beamSpinProgress * 0.2f - (float) MathHelper.floor(beamSpinProgress * 0.1f));
69
        float colorR = color[0];
70
        float colorG = color[1];
71
        float colorB = color[2];
72
        matrices.push();
73
        matrices.multiply(rot);
74
        float m;
75
        float n = innerRadius;
76
        float o = innerRadius;
77
        float p;
78
        float q = -innerRadius;
79
        float r;
80
        float s;
81
        float t = -innerRadius;
82
        float w = -1.0f + h;
83
        float x = (float) length * (0.5f / innerRadius) + w;
84
        renderBeamLayer(matrices, vertexConsumers.getBuffer(RenderLayer.getBeaconBeam(textureId, false)), colorR, colorG, colorB, 1.0f, length, 0.0f, n, o, 0.0f, q, 0.0f, 0.0f, t, x, w);
85
        matrices.pop();
86
        matrices.multiply(rot);
87
        m = -outerRadius;
88
        n = -outerRadius;
89
        o = outerRadius;
90
        p = -outerRadius;
91
        q = -outerRadius;
92
        r = outerRadius;
93
        s = outerRadius;
94
        t = outerRadius;
95
        w = -1.0f + h;
96
        x = (float) length + w;
97
        renderBeamLayer(matrices, vertexConsumers.getBuffer(RenderLayer.getBeaconBeam(textureId, true)), colorR, colorG, colorB, 0.125f, length, m, n, o, p, q, r, s, t, x, w);
98
        matrices.pop();
99
    }
100
101
    /**
102
     * Renders a single layer of a beacon
103
     */
104
    private static void renderBeamLayer(MatrixStack matrices, VertexConsumer vertices, float red, float green, float blue, float alpha, double length, float x1, float z1, float x2, float z2, float x3, float z3, float x4, float z4, float v1, float v2) {
105
        MatrixStack.Entry entry = matrices.peek();
106
        Matrix4f matrix4f = entry.getPositionMatrix();
107
        Matrix3f matrix3f = entry.getNormalMatrix();
108
        renderBeamFace(matrix4f, matrix3f, vertices, red, green, blue, alpha, length, x1, z1, x2, z2, v1, v2);
109
        renderBeamFace(matrix4f, matrix3f, vertices, red, green, blue, alpha, length, x4, z4, x3, z3, v1, v2);
110
        renderBeamFace(matrix4f, matrix3f, vertices, red, green, blue, alpha, length, x2, z2, x4, z4, v1, v2);
111
        renderBeamFace(matrix4f, matrix3f, vertices, red, green, blue, alpha, length, x3, z3, x1, z1, v1, v2);
112
    }
113
114
    private static void renderBeamFace(Matrix4f positionMatrix, Matrix3f normalMatrix, VertexConsumer vertices, float red, float green, float blue, float alpha, double length, float x1, float z1, float x2, float z2, float v1, float v2) {
115
        renderBeamVertex(positionMatrix, normalMatrix, vertices, red, green, blue, alpha, length, x1, z1, (float) 1.0, v1);
116
        renderBeamVertex(positionMatrix, normalMatrix, vertices, red, green, blue, alpha, 0, x1, z1, (float) 1.0, v2);
117
        renderBeamVertex(positionMatrix, normalMatrix, vertices, red, green, blue, alpha, 0, x2, z2, (float) 0.0, v2);
118
        renderBeamVertex(positionMatrix, normalMatrix, vertices, red, green, blue, alpha, length, x2, z2, (float) 0.0, v1);
119
    }
120
121
    /**
122
     * @param v the top-most coordinate of the texture region
123
     * @param u the left-most coordinate of the texture region
124
     */
125
    private static void renderBeamVertex(Matrix4f positionMatrix, Matrix3f normalMatrix, VertexConsumer vertices, float red, float green, float blue, float alpha, double y, float x, float z, float u, float v) {
126
        vertices.vertex(positionMatrix, x, (float) y, z).color(red, green, blue, alpha).texture(u, v).overlay(OverlayTexture.DEFAULT_UV).light(LightmapTextureManager.MAX_LIGHT_COORDINATE).normal(normalMatrix, 0.0f, 1.0f, 0.0f).next();
127
    }
128
129
    /**
130
     * Helper method to get the rotation quaternion to rotate the up-vector to the direction vector
131
     *
132
     * @param direction The direction vector
133
     * @return The rotation quaternion
134
     */
135
    private static Quaternion getRot(Vec3d direction) {
136
        Vec3d up = new Vec3d(0, 1, 0);
137
138
        Vec3d cross = up.crossProduct(direction);
139
        double dot = up.dotProduct(direction);
140
141
        Quaternion q;
142
        if (direction.y < 0 && Math.abs(direction.x) < 0.001F && Math.abs(direction.z) < 0.001F) {
143
            q = new Quaternion(1.0F, 0.0F, 0.0F, (float) Math.cos(0.5 * Math.PI));
144
        } else {
145
            q = new Quaternion((float) cross.x, (float) cross.y, (float) cross.z,
146
                    (float) direction.length() + (float) dot);
147
        }
148
149
        q.normalize();
150
        return q;
151
    }
152
}
153