WeatherEffectRenderer .java

net.minecraft.client.renderer.WeatherEffectRenderer (class): members: HALF_RAIN_TABLE_SIZE, INDICES_PER_COLUMN, RAIN_TABLE_SIZE, ColumnInstance, maxAlpha.

net.minecraft.client.rendererclass248 linesclient11.9 KB

Implements

API Surface (6)

void extractRenderState(final ClientLevel level, final float partialTicks, final Vec3 cameraPos, final WeatherRenderState renderState)
void prepare(final Vec3 cameraPos, final WeatherRenderState renderState)
void render(final WeatherRenderState renderState, final RenderPass renderPass)
void renderOit(final OitStage stage, final WeatherRenderState renderState, final RenderPass renderPass)
void close()
record ColumnInstance(int x, int z, int bottomY, int topY, float uOffset, float vOffset, int lightCoords)

Fields & Constants (20)

RAIN_LOCATION
SNOW_LOCATION
RAIN_TABLE_SIZE
HALF_RAIN_TABLE_SIZE
INDICES_PER_COLUMN
columnSizeX
columnSizeZ
gameRenderer
textureManager
rainTexture
snowTexture
vertexBuffer
primitiveTopology
indexCount
lightCoords
builder
columns
cameraPos
maxAlpha
radius

String literals (7)

"textures/environment/rain.png"
"textures/environment/snow.png"
"Sampler0"
"Weather Vertex Buffer"
"Weather"
"DynamicTransforms"
"Sampler2"

Full Source

WeatherEffectRenderer.java248 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer;
2
3
import com.mojang.blaze3d.systems.RenderSystem;
4
import com.mojang.blaze3d.vertex.BufferBuilder;
5
import com.mojang.blaze3d.vertex.ByteBufferBuilder;
6
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
7
import com.mojang.blaze3d.vertex.MeshData;
8
import com.mojang.blaze3d.vertex.VertexConsumer;
9
import com.mojang.renderpearl.api.buffers.GpuBuffer;
10
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
11
import com.mojang.renderpearl.api.commands.RenderPass;
12
import com.mojang.renderpearl.api.device.GpuDevice;
13
import com.mojang.renderpearl.api.pipeline.IndexType;
14
import com.mojang.renderpearl.api.pipeline.PrimitiveTopology;
15
import com.mojang.renderpearl.api.pipeline.RenderPipeline;
16
import com.mojang.renderpearl.api.textures.FilterMode;
17
import java.nio.ByteBuffer;
18
import java.util.List;
19
import net.fabricmc.api.EnvType;
20
import net.fabricmc.api.Environment;
21
import net.minecraft.client.Minecraft;
22
import net.minecraft.client.multiplayer.ClientLevel;
23
import net.minecraft.client.renderer.oit.OitStage;
24
import net.minecraft.client.renderer.state.level.WeatherRenderState;
25
import net.minecraft.client.renderer.texture.AbstractTexture;
26
import net.minecraft.client.renderer.texture.TextureManager;
27
import net.minecraft.core.BlockPos.MutableBlockPos;
28
import net.minecraft.resources.Identifier;
29
import net.minecraft.util.ARGB;
30
import net.minecraft.util.LightCoordsUtil;
31
import net.minecraft.util.Mth;
32
import net.minecraft.util.RandomSource;
33
import net.minecraft.world.level.biome.Biome.Precipitation;
34
import net.minecraft.world.level.levelgen.Heightmap.Types;
35
import net.minecraft.world.phys.Vec3;
36
import org.jspecify.annotations.Nullable;
37
38
@Environment(EnvType.CLIENT)
39
public class WeatherEffectRenderer implements AutoCloseable {
40
	private static final Identifier RAIN_LOCATION = Identifier.withDefaultNamespace("textures/environment/rain.png");
41
	private static final Identifier SNOW_LOCATION = Identifier.withDefaultNamespace("textures/environment/snow.png");
42
	private static final int RAIN_TABLE_SIZE = 32;
43
	private static final int HALF_RAIN_TABLE_SIZE = 16;
44
	private static final int INDICES_PER_COLUMN = 6;
45
	private final float[] columnSizeX = new float[1024];
46
	private final float[] columnSizeZ = new float[1024];
47
	private final GameRenderer gameRenderer;
48
	private final TextureManager textureManager;
49
	private @Nullable AbstractTexture rainTexture;
50
	private @Nullable AbstractTexture snowTexture;
51
	private @Nullable GpuBuffer vertexBuffer;
52
	private @Nullable PrimitiveTopology primitiveTopology;
53
	private int indexCount;
54
55
	public WeatherEffectRenderer() {
56
		for (int z = 0; z < 32; z++) {
57
			for (int x = 0; x < 32; x++) {
58
				float deltaX = x - 16;
59
				float deltaZ = z - 16;
60
				float distance = Mth.length(deltaX, deltaZ);
61
				this.columnSizeX[z * 32 + x] = -deltaZ / distance;
62
				this.columnSizeZ[z * 32 + x] = deltaX / distance;
63
			}
64
		}
65
66
		Minecraft minecraft = Minecraft.getInstance();
67
		this.gameRenderer = minecraft.gameRenderer;
68
		this.textureManager = minecraft.getTextureManager();
69
	}
70
71
	public void extractRenderState(final ClientLevel level, final float partialTicks, final Vec3 cameraPos, final WeatherRenderState renderState) {
72
		renderState.intensity = level.getRainLevel(partialTicks);
73
		if (!(renderState.intensity <= 0.0F)) {
74
			renderState.radius = Minecraft.getInstance().options.weatherRadius().get();
75
			int cameraBlockX = Mth.floor(cameraPos.x);
76
			int cameraBlockY = Mth.floor(cameraPos.y);
77
			int cameraBlockZ = Mth.floor(cameraPos.z);
78
			MutableBlockPos mutablePos = new MutableBlockPos();
79
			RandomSource random = RandomSource.createThreadLocalInstance();
80
81
			for (int z = cameraBlockZ - renderState.radius; z <= cameraBlockZ + renderState.radius; z++) {
82
				for (int x = cameraBlockX - renderState.radius; x <= cameraBlockX + renderState.radius; x++) {
83
					int terrainHeight = level.getHeight(Types.MOTION_BLOCKING, x, z);
84
					int y0 = Math.max(cameraBlockY - renderState.radius, terrainHeight);
85
					int y1 = Math.max(cameraBlockY + renderState.radius, terrainHeight);
86
					if (y1 - y0 != 0) {
87
						Precipitation precipitation = level.getPrecipitationAt(mutablePos.set(x, cameraBlockY, z));
88
						if (precipitation != Precipitation.NONE) {
89
							int seed = x * x * 3121 + x * 45238971 ^ z * z * 418711 + z * 13761;
90
							random.setSeed(seed);
91
							int lightSampleY = Math.max(cameraBlockY, terrainHeight);
92
							int lightCoords = LightCoordsUtil.getLightCoords(level, mutablePos.set(x, lightSampleY, z));
93
							if (precipitation == Precipitation.RAIN) {
94
								renderState.rainColumns.add(this.createRainColumnInstance(random, level.getGameTime(), x, y0, y1, z, lightCoords, partialTicks));
95
							} else if (precipitation == Precipitation.SNOW) {
96
								renderState.snowColumns.add(this.createSnowColumnInstance(random, level.getGameTime(), x, y0, y1, z, lightCoords, partialTicks));
97
							}
98
						}
99
					}
100
				}
101
			}
102
		}
103
	}
104
105
	private void renderWeather(final RenderPass renderPass, final AbstractTexture texture, final int startColumn, final int columnCount) {
106
		renderPass.setUniform("Sampler0", texture.getTextureView(), texture.getSampler());
107
		renderPass.drawIndexed(columnCount * 6, 1, startColumn * 6, 0, 0);
108
	}
109
110
	private void uploadVertexBuffer(final ByteBuffer buffer) {
111
		GpuDevice device = RenderSystem.getDevice();
112
		if (this.vertexBuffer == null || this.vertexBuffer.size() < buffer.remaining()) {
113
			if (this.vertexBuffer != null) {
114
				this.vertexBuffer.close();
115
			}
116
117
			this.vertexBuffer = device.createBuffer(() -> "Weather Vertex Buffer", 40, buffer.remaining());
118
		}
119
120
		device.createCommandEncoder().writeToBuffer(this.vertexBuffer.slice(), buffer);
121
	}
122
123
	public void prepare(final Vec3 cameraPos, final WeatherRenderState renderState) {
124
		int columnCount = renderState.rainColumns.size() + renderState.snowColumns.size();
125
		if (columnCount != 0) {
126
			try (ByteBufferBuilder builder = ByteBufferBuilder.exactlySized(columnCount * DefaultVertexFormat.PARTICLE.getVertexSize() * 4)) {
127
				BufferBuilder bufferBuilder = new BufferBuilder(builder, PrimitiveTopology.QUADS, DefaultVertexFormat.PARTICLE);
128
				this.prepareInstances(bufferBuilder, renderState.rainColumns, cameraPos, 1.0F, renderState.radius, renderState.intensity);
129
				this.prepareInstances(bufferBuilder, renderState.snowColumns, cameraPos, 0.8F, renderState.radius, renderState.intensity);
130
131
				try (MeshData mesh = bufferBuilder.buildOrThrow()) {
132
					this.uploadVertexBuffer(mesh.vertexBuffer());
133
					this.primitiveTopology = mesh.drawState().primitiveTopology();
134
					this.indexCount = mesh.drawState().indexCount();
135
				}
136
			}
137
138
			RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(this.primitiveTopology);
139
			autoIndices.requestIndexCount(this.indexCount);
140
			this.rainTexture = this.textureManager.getTexture(RAIN_LOCATION);
141
			this.snowTexture = this.textureManager.getTexture(SNOW_LOCATION);
142
		}
143
	}
144
145
	public void render(final WeatherRenderState renderState, final RenderPass renderPass) {
146
		RenderPipeline renderPipeline = RenderPipelines.WEATHER;
147
		this.render(renderState, renderPass, renderPipeline);
148
	}
149
150
	public void renderOit(final OitStage stage, final WeatherRenderState renderState, final RenderPass renderPass) {
151
		RenderPipeline renderPipeline = RenderPipelines.OIT_WEATHER.getPipeline(stage);
152
		this.render(renderState, renderPass, renderPipeline);
153
	}
154
155
	private void render(final WeatherRenderState renderState, final RenderPass renderPass, final RenderPipeline renderPipeline) {
156
		int columnCount = renderState.rainColumns.size() + renderState.snowColumns.size();
157
		if (this.vertexBuffer != null && columnCount != 0) {
158
			renderPass.pushDebugGroup(() -> "Weather");
159
			GpuBufferSlice dynamicTransforms = RenderSystem.getDynamicUniforms().writeTransform(RenderSystem.getModelViewMatrixCopy());
160
			RenderSystem.AutoStorageIndexBuffer autoIndices = RenderSystem.getSequentialBuffer(this.primitiveTopology);
161
			GpuBuffer indexBuffer = autoIndices.getBuffer();
162
			IndexType indexType = autoIndices.type();
163
			renderPass.setPipeline(RenderSystem.getCompiledPipeline(renderPipeline));
164
			RenderSystem.bindDefaultUniforms(renderPass);
165
			renderPass.setUniform("DynamicTransforms", dynamicTransforms);
166
			renderPass.setUniform("Sampler2", this.gameRenderer.lightmap(), RenderSystem.getSamplerCache().getClampToEdge(FilterMode.LINEAR));
167
			renderPass.setIndexBuffer(indexBuffer, indexType);
168
			renderPass.setVertexBuffer(0, this.vertexBuffer.slice());
169
			this.renderWeather(renderPass, this.rainTexture, 0, renderState.rainColumns.size());
170
			this.renderWeather(renderPass, this.snowTexture, renderState.rainColumns.size(), renderState.snowColumns.size());
171
			renderPass.popDebugGroup();
172
		}
173
	}
174
175
	private WeatherEffectRenderer.ColumnInstance createRainColumnInstance(
176
		final RandomSource random, final long ticks, final int x, final int bottomY, final int topY, final int z, final int lightCoords, final float partialTicks
177
	) {
178
		int wrappedTicks = (int)(ticks & 131071L);
179
		int tickOffset = x * x * 3121 + x * 45238971 + z * z * 418711 + z * 13761 & 0xFF;
180
		float blockPosRainSpeed = 3.0F + random.nextFloat();
181
		float textureOffset = -(wrappedTicks + tickOffset + partialTicks) / 32.0F * blockPosRainSpeed;
182
		float wrappedTextureOffset = textureOffset % 32.0F;
183
		return new WeatherEffectRenderer.ColumnInstance(x, z, bottomY, topY, 0.0F, wrappedTextureOffset, lightCoords);
184
	}
185
186
	private WeatherEffectRenderer.ColumnInstance createSnowColumnInstance(
187
		final RandomSource random, final long ticks, final int x, final int bottomY, final int topY, final int z, final int lightCoords, final float partialTicks
188
	) {
189
		int wrappedTicks = (int)(ticks & 131071L);
190
		float time = wrappedTicks + partialTicks;
191
		float u = (float)(random.nextDouble() + time * 0.01F * (float)random.nextGaussian());
192
		float v = (float)(random.nextDouble() + time * (float)random.nextGaussian() * 0.001F);
193
		float vOffset = -((float)(ticks & 511L) + partialTicks) / 512.0F;
194
		int brightenedLightCoords = LightCoordsUtil.pack((LightCoordsUtil.block(lightCoords) * 3 + 15) / 4, (LightCoordsUtil.sky(lightCoords) * 3 + 15) / 4);
195
		return new WeatherEffectRenderer.ColumnInstance(x, z, bottomY, topY, u, vOffset + v, brightenedLightCoords);
196
	}
197
198
	private void prepareInstances(
199
		final VertexConsumer builder,
200
		final List<WeatherEffectRenderer.ColumnInstance> columns,
201
		final Vec3 cameraPos,
202
		final float maxAlpha,
203
		final int radius,
204
		final float intensity
205
	) {
206
		if (!columns.isEmpty()) {
207
			float radiusSq = radius * radius;
208
209
			for (WeatherEffectRenderer.ColumnInstance column : columns) {
210
				float relativeX = (float)(column.x + 0.5 - cameraPos.x);
211
				float relativeZ = (float)(column.z + 0.5 - cameraPos.z);
212
				float distanceSq = Mth.lengthSquared(relativeX, relativeZ);
213
				float alpha = Mth.lerp(Math.min(distanceSq / radiusSq, 1.0F), maxAlpha, 0.5F) * intensity;
214
				int color = ARGB.white(alpha);
215
				int index = (column.z - Mth.floor(cameraPos.z) + 16) * 32 + column.x - Mth.floor(cameraPos.x) + 16;
216
				float halfSizeX = this.columnSizeX[index] / 2.0F;
217
				float halfSizeZ = this.columnSizeZ[index] / 2.0F;
218
				float x0 = relativeX - halfSizeX;
219
				float x1 = relativeX + halfSizeX;
220
				float y1 = (float)(column.topY - cameraPos.y);
221
				float y0 = (float)(column.bottomY - cameraPos.y);
222
				float z0 = relativeZ - halfSizeZ;
223
				float z1 = relativeZ + halfSizeZ;
224
				float u0 = column.uOffset + 0.0F;
225
				float u1 = column.uOffset + 1.0F;
226
				float v0 = column.bottomY * 0.25F + column.vOffset;
227
				float v1 = column.topY * 0.25F + column.vOffset;
228
				builder.addVertex(x0, y1, z0).setUv(u0, v0).setColor(color).setLight(column.lightCoords);
229
				builder.addVertex(x1, y1, z1).setUv(u1, v0).setColor(color).setLight(column.lightCoords);
230
				builder.addVertex(x1, y0, z1).setUv(u1, v1).setColor(color).setLight(column.lightCoords);
231
				builder.addVertex(x0, y0, z0).setUv(u0, v1).setColor(color).setLight(column.lightCoords);
232
			}
233
		}
234
	}
235
236
	@Override
237
	public void close() {
238
		if (this.vertexBuffer != null) {
239
			this.vertexBuffer.close();
240
			this.vertexBuffer = null;
241
		}
242
	}
243
244
	@Environment(EnvType.CLIENT)
245
	public record ColumnInstance(int x, int z, int bottomY, int topY, float uOffset, float vOffset, int lightCoords) {
246
	}
247
}
248

Decompiled Minecraft 26.3 source of WeatherEffectRenderer (net/minecraft/client/renderer/WeatherEffectRenderer.java) with Mojang official mappings, stored verbatim on the site.