Lighting .java

com.mojang.blaze3d.platform.Lighting (class, 89 lines): members: INVENTORY_DIFFUSE_LIGHT_0, INVENTORY_DIFFUSE_LIGHT_1, NETHER_DIFFUSE_LIGHT_0, updateLevel.

com.mojang.blaze3d.platformclass89 linesclient3.7 KB

Implements

API Surface (3)

void updateLevel(final Type type)
void setupFor(final Lighting.Entry entry)
void close()

Fields & Constants (9)

DIFFUSE_LIGHT_0
DIFFUSE_LIGHT_1
NETHER_DIFFUSE_LIGHT_0
NETHER_DIFFUSE_LIGHT_1
INVENTORY_DIFFUSE_LIGHT_0
INVENTORY_DIFFUSE_LIGHT_1
UBO_SIZE
buffer
paddedSize

String literals (1)

"Lighting UBO"

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Lighting.java89 lines · Minecraft 26.3 · verbatim
1
package com.mojang.blaze3d.platform;
2
3
import com.mojang.blaze3d.buffers.Std140Builder;
4
import com.mojang.blaze3d.buffers.Std140SizeCalculator;
5
import com.mojang.blaze3d.systems.RenderSystem;
6
import com.mojang.renderpearl.api.buffers.GpuBuffer;
7
import com.mojang.renderpearl.api.device.GpuDevice;
8
import java.nio.ByteBuffer;
9
import net.fabricmc.api.EnvType;
10
import net.fabricmc.api.Environment;
11
import net.minecraft.util.Mth;
12
import net.minecraft.world.level.CardinalLighting.Type;
13
import org.joml.Matrix4f;
14
import org.joml.Vector3f;
15
import org.joml.Vector3fc;
16
import org.lwjgl.system.MemoryStack;
17
18
@Environment(EnvType.CLIENT)
19
public class Lighting implements AutoCloseable {
20
	private static final Vector3fc DIFFUSE_LIGHT_0 = new Vector3f(0.2F, 1.0F, -0.7F).normalize();
21
	private static final Vector3fc DIFFUSE_LIGHT_1 = new Vector3f(-0.2F, 1.0F, 0.7F).normalize();
22
	private static final Vector3fc NETHER_DIFFUSE_LIGHT_0 = new Vector3f(0.2F, 1.0F, -0.7F).normalize();
23
	private static final Vector3fc NETHER_DIFFUSE_LIGHT_1 = new Vector3f(-0.2F, -1.0F, 0.7F).normalize();
24
	private static final Vector3fc INVENTORY_DIFFUSE_LIGHT_0 = new Vector3f(0.2F, -1.0F, 1.0F).normalize();
25
	private static final Vector3fc INVENTORY_DIFFUSE_LIGHT_1 = new Vector3f(-0.2F, -1.0F, 0.0F).normalize();
26
	public static final int UBO_SIZE = new Std140SizeCalculator().putVec3().putVec3().get();
27
	private final GpuBuffer buffer;
28
	private final long paddedSize;
29
30
	public Lighting() {
31
		GpuDevice device = RenderSystem.getDevice();
32
		this.paddedSize = Mth.roundToward(UBO_SIZE, device.getDeviceInfo().limits().minUniformOffsetAlignment());
33
		this.buffer = device.createBuffer(() -> "Lighting UBO", 136, this.paddedSize * Lighting.Entry.values().length);
34
		Matrix4f flatPose = new Matrix4f().rotationY((float) (-Math.PI / 8)).rotateX((float) (Math.PI * 3.0 / 4.0));
35
		this.updateBuffer(
36
			Lighting.Entry.ITEMS_FLAT, flatPose.transformDirection(DIFFUSE_LIGHT_0, new Vector3f()), flatPose.transformDirection(DIFFUSE_LIGHT_1, new Vector3f())
37
		);
38
		Matrix4f item3DPose = new Matrix4f()
39
			.scaling(1.0F, -1.0F, 1.0F)
40
			.rotateYXZ(1.0821041F, 3.2375858F, 0.0F)
41
			.rotateYXZ((float) (-Math.PI / 8), (float) (Math.PI * 3.0 / 4.0), 0.0F);
42
		this.updateBuffer(
43
			Lighting.Entry.ITEMS_3D, item3DPose.transformDirection(DIFFUSE_LIGHT_0, new Vector3f()), item3DPose.transformDirection(DIFFUSE_LIGHT_1, new Vector3f())
44
		);
45
		this.updateBuffer(Lighting.Entry.ENTITY_IN_UI, INVENTORY_DIFFUSE_LIGHT_0, INVENTORY_DIFFUSE_LIGHT_1);
46
		Matrix4f playerSkinPose = new Matrix4f();
47
		this.updateBuffer(
48
			Lighting.Entry.PLAYER_SKIN,
49
			playerSkinPose.transformDirection(INVENTORY_DIFFUSE_LIGHT_0, new Vector3f()),
50
			playerSkinPose.transformDirection(INVENTORY_DIFFUSE_LIGHT_1, new Vector3f())
51
		);
52
	}
53
54
	public void updateLevel(final Type type) {
55
		switch (type) {
56
			case DEFAULT:
57
				this.updateBuffer(Lighting.Entry.LEVEL, DIFFUSE_LIGHT_0, DIFFUSE_LIGHT_1);
58
				break;
59
			case NETHER:
60
				this.updateBuffer(Lighting.Entry.LEVEL, NETHER_DIFFUSE_LIGHT_0, NETHER_DIFFUSE_LIGHT_1);
61
		}
62
	}
63
64
	private void updateBuffer(final Lighting.Entry entry, final Vector3fc light0, final Vector3fc light1) {
65
		try (MemoryStack stack = MemoryStack.stackPush()) {
66
			ByteBuffer byteBuffer = Std140Builder.onStack(stack, UBO_SIZE).putVec3(light0).putVec3(light1).get();
67
			RenderSystem.getDevice().createCommandEncoder().writeToBuffer(this.buffer.slice(entry.ordinal() * this.paddedSize, this.paddedSize), byteBuffer);
68
		}
69
	}
70
71
	public void setupFor(final Lighting.Entry entry) {
72
		RenderSystem.setShaderLights(this.buffer.slice(entry.ordinal() * this.paddedSize, UBO_SIZE));
73
	}
74
75
	@Override
76
	public void close() {
77
		this.buffer.close();
78
	}
79
80
	@Environment(EnvType.CLIENT)
81
	public enum Entry {
82
		LEVEL,
83
		ITEMS_FLAT,
84
		ITEMS_3D,
85
		ENTITY_IN_UI,
86
		PLAYER_SKIN;
87
	}
88
}
89

Decompiled Minecraft 26.3 source of Lighting (com/mojang/blaze3d/platform/Lighting.java) with Mojang official mappings, stored verbatim on the site.