少女祈祷中...

为了得到一个彩色弹幕,首先,我们为贴图准备一个二维平面

在这段代码中,我们传入了一个VertexConsumer对象,并为其定义了四个在边长为size的正方形的顶点。传入了一个color值,通过Color对象拿到r,g,b三个分量并赋予这个平面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
private static void renderSquare(VertexConsumer consumer, Matrix4f matrix4f, Matrix3f matrix3f, int i, float size,int rgb) {
float x1 = -size;
float y1 = -size;
Color color = new Color(rgb);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int alpha = 255;
consumer.vertex(matrix4f, x1, y1, 0.0F)
.color(red, green, blue, alpha)
.uv(0.0F, 0.0F)
.overlayCoords(OverlayTexture.NO_OVERLAY)
.uv2(i)
.normal(matrix3f, 0.0F, 1.0F, 0.0F)
.endVertex();

consumer.vertex(matrix4f, size, y1, 0.0F)
.color(red, green, blue, alpha)
.uv(1.0F, 0.0F)
.overlayCoords(OverlayTexture.NO_OVERLAY)
.uv2(i)
.normal(matrix3f, 0.0F, 1.0F, 0.0F)
.endVertex();

consumer.vertex(matrix4f, size, size, 0.0F)
.color(red, green, blue, alpha)
.uv(1.0F, 1.0F)
.overlayCoords(OverlayTexture.NO_OVERLAY)
.uv2(i)
.normal(matrix3f, 0.0F, 1.0F, 0.0F)
.endVertex();

consumer.vertex(matrix4f, x1, size, 0.0F)
.color(red, green, blue, alpha)
.uv(0.0F, 1.0F)
.overlayCoords(OverlayTexture.NO_OVERLAY)
.uv2(i)
.normal(matrix3f, 0.0F, 1.0F, 0.0F)
.endVertex();
}

接下来,一个彩色弹幕,有外围色彩部分和中心白色部分,所以我们需要有两个白色贴图和对应的RenderType对象

1
2
3
4
5
6
public static final ResourceLocation TEXTURE_IN = new ResourceLocation("lively_danmaku", "textures/entity/danmaku_in.png");
public static final ResourceLocation TEXTURE_OUT = new ResourceLocation("lively_danmaku", "textures/entity/danmaku_out.png");

private static final RenderType RENDER_TYPE_IN = RenderType.entityCutoutNoCull(TEXTURE_IN);
private static final RenderType RENDER_TYPE_OUT = RenderType.entityCutoutNoCull(TEXTURE_OUT);

在render方法中,我们需要对这个平面进行旋转以正对玩家

1
2
poseStack.mulPose(this.entityRenderDispatcher.cameraOrientation());
poseStack.mulPose(Axis.YP.rotationDegrees(180.0F));

最后把一个外环绘制在特定颜色遮罩的平面上来实现彩色环,把中心白色部分绘制在白色遮罩上。

1
2
3
4
VertexConsumer vertexConsumer1 = pBuffer.getBuffer(RENDER_TYPE_IN);
renderSquare(vertexConsumer1, matrix4f, matrix3f, pPackedLight, danmaku.getSize(), Color.white.getRGB());
VertexConsumer vertexConsumer2 = pBuffer.getBuffer(RENDER_TYPE_OUT);
renderSquare(vertexConsumer2, matrix4f, matrix3f, pPackedLight, danmaku.getSize(), danmaku.getColor());

https://github.com/Crystal1921/LivelyDanmaku/blob/main/src/main/java/com/tutorial/lively_danmaku/entity/render/DanmakuRender.java