Skip to content

Commit

Permalink
1.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-makes committed Jun 12, 2024
1 parent a6cf2d3 commit d510583
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 165 deletions.
7 changes: 6 additions & 1 deletion common/src/main/java/dev/schmarrn/lighty/api/LightyMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package dev.schmarrn.lighty.api;

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -30,7 +33,9 @@ public abstract class LightyMode {
* - a bunch of `compute()` calls<br/>
* - exactly one call to `afterCompute()`
*/
public void beforeCompute(BufferBuilder builder) {}
public BufferBuilder beforeCompute(Tesselator builder) {
return builder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK);
}

public void beforeRendering() {}
public void afterRendering() {}
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/dev/schmarrn/lighty/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Config {
private static boolean initStage = true;

// all the different config values
public static final ResourceLocationConfig LAST_USED_MODE = new ResourceLocationConfig("lighty.last_used_mode", new ResourceLocation("lighty:carpet_mode"));
public static final ResourceLocationConfig LAST_USED_MODE = new ResourceLocationConfig("lighty.last_used_mode", ResourceLocation.parse("lighty:carpet_mode"));

public static final IntegerConfig SKY_THRESHOLD = new IntegerConfig("lighty.sky_threshold", 0, 0, 15);
public static final IntegerConfig BLOCK_THRESHOLD = new IntegerConfig("lighty.block_threshold", 0, 0, 15);
Expand All @@ -48,11 +48,11 @@ public class Config {
public static final ColorConfig OVERLAY_ORANGE = new ColorConfig("lighty.overlay_orange", 0xFF6600);
public static final ColorConfig OVERLAY_RED = new ColorConfig("lighty.overlay_red", 0xFF0000);

public static final ResourceLocationConfig AUTO_ON_ITEM = new ResourceLocationConfig("lighty.auto_on.item", new ResourceLocation("minecraft:torch"));
public static final ResourceLocationConfig AUTO_ON_ITEM = new ResourceLocationConfig("lighty.auto_on.item", ResourceLocation.parse("minecraft:torch"));
public static final BooleanConfig SHOULD_AUTO_ON = new BooleanConfig("lighty.auto_on", false);

public static final ResourceLocationConfig CARPET_TEXTURE = new ResourceLocationConfig("lighty.mode.carpet.texture", new ResourceLocation(Lighty.MOD_ID, "textures/block/transparent.png"));
public static final ResourceLocationConfig CROSS_TEXTURE = new ResourceLocationConfig("lighty.mode.cross.texture", new ResourceLocation(Lighty.MOD_ID, "textures/block/cross.png"));
public static final ResourceLocationConfig CARPET_TEXTURE = new ResourceLocationConfig("lighty.mode.carpet.texture", ResourceLocation.fromNamespaceAndPath(Lighty.MOD_ID, "textures/block/transparent.png"));
public static final ResourceLocationConfig CROSS_TEXTURE = new ResourceLocationConfig("lighty.mode.cross.texture", ResourceLocation.fromNamespaceAndPath(Lighty.MOD_ID, "textures/block/cross.png"));

private static void loadFromFile(String key, ConfigSerDe type) {
// If the file contains the config value, get the configured value...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ String serialize() {

@Override
void deserialize(String value) {
setValue(new ResourceLocation(value));
setValue(ResourceLocation.parse(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void migrate() {
properties.putIfAbsent(OVERLAY_RED, Integer.toHexString(0xFF0000));

// Set the new stuff
Config.LAST_USED_MODE.setValue(new ResourceLocation(properties.getProperty(LAST_USED_MODE)));
Config.LAST_USED_MODE.setValue(ResourceLocation.parse(properties.getProperty(LAST_USED_MODE)));
Config.SKY_THRESHOLD.setValue(Integer.valueOf(properties.getProperty(SKY_THRESHOLD)));
Config.BLOCK_THRESHOLD.setValue(Integer.valueOf(properties.getProperty(BLOCK_THRESHOLD)));
Config.FARM_GROWTH_THRESHOLD.setValue(Integer.valueOf(properties.getProperty(FARM_GROWTH_THRESHOLD)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package dev.schmarrn.lighty.event;

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.MeshData;
import com.mojang.blaze3d.vertex.VertexBuffer;
import net.minecraft.client.renderer.ShaderInstance;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -42,15 +43,14 @@ void close() {
}
}

void upload(BufferBuilder.RenderedBuffer buffer) {
void upload(MeshData buffer) {
if (vertexBuffer != null) {
vertexBuffer.close();
}
if (buffer.isEmpty()) {
if (buffer == null) {
// Don't upload
isEmpty = true;
vertexBuffer = null;
buffer.release();
} else {
isEmpty = false;
vertexBuffer = new VertexBuffer(VertexBuffer.Usage.DYNAMIC);
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/dev/schmarrn/lighty/event/Compute.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static void updateSubChunk(SectionPos pos) {
toBeUpdated.add(pos);
}

private static BufferHolder buildChunk(LightyMode mode, SectionPos chunkPos, BufferBuilder builder, ClientLevel world) {
mode.beforeCompute(builder);
private static BufferHolder buildChunk(LightyMode mode, SectionPos chunkPos, Tesselator tesselator, ClientLevel world) {
BufferBuilder builder = mode.beforeCompute(tesselator);
for (int x = 0; x < 16; ++x) {
for (int y = 0; y < 16; ++y) {
for (int z = 0; z < 16; ++z) {
Expand All @@ -102,7 +102,7 @@ private static BufferHolder buildChunk(LightyMode mode, SectionPos chunkPos, Buf
if (buffer == null) {
buffer = new BufferHolder();
}
buffer.upload(builder.end());
buffer.upload(builder.build());

mode.afterCompute();
return buffer;
Expand Down Expand Up @@ -155,7 +155,7 @@ public static void computeCache(Minecraft client) {
if (vertexBuffer != null) {
vertexBuffer.close();
}
return buildChunk(mode, pos, Tesselator.getInstance().getBuilder(), world);
return buildChunk(mode, pos, Tesselator.getInstance(), world);
});
}
}
Expand Down
16 changes: 6 additions & 10 deletions common/src/main/java/dev/schmarrn/lighty/mode/BoringCrossMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import dev.schmarrn.lighty.Lighty;
import dev.schmarrn.lighty.api.LightyColors;
Expand All @@ -33,11 +34,6 @@
import net.minecraft.world.level.block.state.BlockState;

public class BoringCrossMode extends LightyMode {
@Override
public void beforeCompute(BufferBuilder builder) {
builder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK);
}

@Override
public void compute(ClientLevel world, BlockPos pos, BufferBuilder builder) {
BlockState blockState = world.getBlockState(pos);
Expand Down Expand Up @@ -65,10 +61,10 @@ public void compute(ClientLevel world, BlockPos pos, BufferBuilder builder) {
int overlayBrightness = Config.OVERLAY_BRIGHTNESS.getValue();
int lightmap = LightTexture.pack(overlayBrightness, overlayBrightness);

builder.vertex(x1, y, z1).color(color).uv(0, 0).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.vertex(x1, y, z2).color(color).uv(0, 1).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.vertex(x2, y, z2).color(color).uv(1, 1).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.vertex(x2, y, z1).color(color).uv(1, 0).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.addVertex(x1, y, z1).setColor(color).setUv(0, 0).setLight(lightmap).setNormal(0f, 1f, 0f);
builder.addVertex(x1, y, z2).setColor(color).setUv(0, 1).setLight(lightmap).setNormal(0f, 1f, 0f);
builder.addVertex(x2, y, z2).setColor(color).setUv(1, 1).setLight(lightmap).setNormal(0f, 1f, 0f);
builder.addVertex(x2, y, z1).setColor(color).setUv(1, 0).setLight(lightmap).setNormal(0f, 1f, 0f);
}
}

Expand All @@ -86,7 +82,7 @@ public void afterRendering() {

@Override
public ResourceLocation getResourceLocation() {
return new ResourceLocation(Lighty.MOD_ID, "boring_cross_mode");
return ResourceLocation.fromNamespaceAndPath(Lighty.MOD_ID, "boring_cross_mode");
}

public static void init() {
Expand Down
90 changes: 45 additions & 45 deletions common/src/main/java/dev/schmarrn/lighty/mode/CarpetMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import dev.schmarrn.lighty.Lighty;
import dev.schmarrn.lighty.api.LightyColors;
Expand All @@ -36,11 +37,6 @@
import net.minecraft.world.level.block.state.BlockState;

public class CarpetMode extends LightyMode {
@Override
public void beforeCompute(BufferBuilder builder) {
builder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.BLOCK);
}

@Override
public void compute(ClientLevel world, BlockPos pos, BufferBuilder builder) {
BlockPos posUp = pos.above();
Expand All @@ -59,53 +55,57 @@ public void compute(ClientLevel world, BlockPos pos, BufferBuilder builder) {

int color = LightyColors.getARGB(blockLightLevel, skyLightLevel);

double offset = LightyHelper.getOffset(blockState, pos, world);
float offset = LightyHelper.getOffset(blockState, pos, world);
if (offset == -1f) {
return;
}

double x = pos.getX();
double y = pos.getY() + 1 + offset;
double z = pos.getZ();
float x = pos.getX();
float y = pos.getY() + 1 + offset;
float z = pos.getZ();
int overlayBrightness = Config.OVERLAY_BRIGHTNESS.getValue();
// the first parameter corresponds to the blockLightLevel, the second to the skyLightLevel
int lightmap = LightTexture.pack(overlayBrightness, overlayBrightness);
//TOP
builder.vertex(x, y + 1 / 16f, z).color(color).uv(0, 0).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.vertex(x, y + 1 / 16f, z + 1).color(color).uv(0, 1).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.vertex(x + 1, y + 1 / 16f, z + 1).color(color).uv(1, 1).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
builder.vertex(x + 1, y + 1 / 16f, z).color(color).uv(1, 0).uv2(lightmap).normal(0f, 1f, 0f).endVertex();
if (offset > 0.001f) {
//if it renders above it should check if the block above culls the faces
pos = pos.above();
}
//NORTH
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.SOUTH, pos.relative(Direction.SOUTH))) {
builder.vertex(x, y + 1 / 16f, z + 1).color(color).uv(0, 1f / 16).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
builder.vertex(x, y, z + 1).color(color).uv(0, 0).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
builder.vertex(x + 1, y, z + 1).color(color).uv(1, 0).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
builder.vertex(x + 1, y + 1 / 16f, z + 1).color(color).uv(1, 1f / 16).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
}
//EAST
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.WEST, pos.relative(Direction.WEST))) {
builder.vertex(x, y + 1/16f, z).color(color).uv(0,1f/16).uv2(lightmap).normal(-1f, 0f, 0f).endVertex();
builder.vertex(x, y, z).color(color).uv(0, 0).uv2(lightmap).normal(-1f, 0f, 0f).endVertex();
builder.vertex(x, y, z + 1).color(color).uv(1, 0).uv2(lightmap).normal(-1f, 0f, 0f).endVertex();
builder.vertex(x, y + 1/16f, z + 1).color(color).uv(1, 1f/16).uv2(lightmap).normal(-1f, 0f, 0f).endVertex();
}
//SOUTH
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.NORTH, pos.relative(Direction.NORTH))) {
builder.vertex(x+1, y + 1/16f, z).color(color).uv(0,1f/16).uv2(lightmap).normal(0f, 0f, 1f).endVertex();
builder.vertex(x+1, y, z).color(color).uv(0, 0).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
builder.vertex(x, y, z).color(color).uv(1, 0).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
builder.vertex(x, y + 1/16f, z).color(color).uv(1, 1f/16).uv2(lightmap).normal(0f, 0f, -1f).endVertex();
}
//WEST
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.EAST, pos.relative(Direction.EAST))) {
builder.vertex(x+1, y + 1/16f, z+1).color(color).uv(0,1f/16).uv2(lightmap).normal(1f, 0f, 0f).endVertex();
builder.vertex(x+1, y, z+1).color(color).uv(0, 0).uv2(lightmap).normal(1f, 0f, 0f).endVertex();
builder.vertex(x+1, y, z).color(color).uv(1, 0).uv2(lightmap).normal(1f, 0f, 0f).endVertex();
builder.vertex(x+1, y + 1/16f, z).color(color).uv(1, 1f/16).uv2(lightmap).normal(1f, 0f, 0f).endVertex();
try {
builder.addVertex(x, y + 1 / 16f, z).setColor(color).setUv(0, 0).setLight(lightmap).setNormal(0f, 1f, 0f);
builder.addVertex(x, y + 1 / 16f, z + 1).setColor(color).setUv(0, 1).setLight(lightmap).setNormal(0f, 1f, 0f);
builder.addVertex(x + 1, y + 1 / 16f, z + 1).setColor(color).setUv(1, 1).setLight(lightmap).setNormal(0f, 1f, 0f);
builder.addVertex(x + 1, y + 1 / 16f, z).setColor(color).setUv(1, 0).setLight(lightmap).setNormal(0f, 1f, 0f);
if (offset > 0.001f) {
//if it renders above it should check if the block above culls the faces
pos = pos.above();
}
//NORTH
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.SOUTH, pos.relative(Direction.SOUTH))) {
builder.addVertex(x, y + 1 / 16f, z + 1).setColor(color).setUv(0, 1f / 16).setLight(lightmap).setNormal(0f, 0f, -1f);
builder.addVertex(x, y, z + 1).setColor(color).setUv(0, 0).setLight(lightmap).setNormal(0f, 0f, -1f);
builder.addVertex(x + 1, y, z + 1).setColor(color).setUv(1, 0).setLight(lightmap).setNormal(0f, 0f, -1f);
builder.addVertex(x + 1, y + 1 / 16f, z + 1).setColor(color).setUv(1, 1f / 16).setLight(lightmap).setNormal(0f, 0f, -1f);
}
//EAST
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.WEST, pos.relative(Direction.WEST))) {
builder.addVertex(x, y + 1/16f, z).setColor(color).setUv(0,1f/16).setLight(lightmap).setNormal(-1f, 0f, 0f);
builder.addVertex(x, y, z).setColor(color).setUv(0, 0).setLight(lightmap).setNormal(-1f, 0f, 0f);
builder.addVertex(x, y, z + 1).setColor(color).setUv(1, 0).setLight(lightmap).setNormal(-1f, 0f, 0f);
builder.addVertex(x, y + 1/16f, z + 1).setColor(color).setUv(1, 1f/16).setLight(lightmap).setNormal(-1f, 0f, 0f);
}
//SOUTH
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.NORTH, pos.relative(Direction.NORTH))) {
builder.addVertex(x+1, y + 1/16f, z).setColor(color).setUv(0,1f/16).setLight(lightmap).setNormal(0f, 0f, 1f);
builder.addVertex(x+1, y, z).setColor(color).setUv(0, 0).setLight(lightmap).setNormal(0f, 0f, -1f);
builder.addVertex(x, y, z).setColor(color).setUv(1, 0).setLight(lightmap).setNormal(0f, 0f, -1f);
builder.addVertex(x, y + 1/16f, z).setColor(color).setUv(1, 1f/16).setLight(lightmap).setNormal(0f, 0f, -1f);
}
//WEST
if (Block.shouldRenderFace(Blocks.STONE.defaultBlockState(), world, pos, Direction.EAST, pos.relative(Direction.EAST))) {
builder.addVertex(x+1, y + 1/16f, z+1).setColor(color).setUv(0,1f/16).setLight(lightmap).setNormal(1f, 0f, 0f);
builder.addVertex(x+1, y, z+1).setColor(color).setUv(0, 0).setLight(lightmap).setNormal(1f, 0f, 0f);
builder.addVertex(x+1, y, z).setColor(color).setUv(1, 0).setLight(lightmap).setNormal(1f, 0f, 0f);
builder.addVertex(x+1, y + 1/16f, z).setColor(color).setUv(1, 1f/16).setLight(lightmap).setNormal(1f, 0f, 0f);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

Expand All @@ -124,7 +124,7 @@ public void afterRendering() {

@Override
public ResourceLocation getResourceLocation() {
return new ResourceLocation(Lighty.MOD_ID, "carpet_mode");
return ResourceLocation.fromNamespaceAndPath(Lighty.MOD_ID, "carpet_mode");
}

public static void init() {
Expand Down
Loading

0 comments on commit d510583

Please sign in to comment.