Skip to content

Commit

Permalink
light kinda works
Browse files Browse the repository at this point in the history
still some visual artifacts
need to remove light element of vertex builder i think
  • Loading branch information
Goby56 committed Aug 10, 2024
1 parent 6960423 commit abdeab9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/goby56/wakes/config/WakesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class WakesConfig {
public boolean showDebugInfo = false;
public RenderType renderType = RenderType.AUTO;
public boolean useLODs = true;
public int lightInfluence = 127;

// Appearance
public Resolution wakeResolution = Resolution.SIXTEEN;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/goby56/wakes/config/YACLIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public static Screen createScreen(Screen parent) {
.option(booleanOption("use_lods", false)
.binding(false, () -> config.useLODs, val -> config.useLODs = val)
.build())
.option(optionOf(Integer.class, "light_influence", false)
.binding(127, () -> config.lightInfluence, val -> config.lightInfluence = val)
.controller(opt -> integerSlider(opt, 0, 255))
.build())
.group(intervalGroup(0, WakeColor.TRANSPARENT, -50, -45))
.group(intervalGroup(1, WakeColor.DARK_GRAY, -45, -35))
.group(intervalGroup(2, WakeColor.GRAY, -35, -30))
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/goby56/wakes/render/WakeTexture.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,30 @@ public void render(Matrix4f matrix, Camera camera, Brick brick) {
buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL);

Vector3f pos = brick.pos.add(camera.getPos().negate()).toVector3f();
int light = LightmapTextureManager.MAX_LIGHT_COORDINATE;
buffer.vertex(matrix, pos.x, pos.y, pos.z)
.color(1f, 1f, 1f, 1f)
.texture(0, 0)
.overlay(OverlayTexture.DEFAULT_UV)
.light(14680064)
.light(light)
.normal(0f, 1f, 0f).next();
buffer.vertex(matrix, pos.x, pos.y, pos.z + brick.dim)
.color(1f, 1f, 1f, 1f)
.texture(0, 1)
.overlay(OverlayTexture.DEFAULT_UV)
.light(14680064)
.light(light)
.normal(0f, 1f, 0f).next();
buffer.vertex(matrix, pos.x + brick.dim, pos.y, pos.z + brick.dim)
.color(1f, 1f, 1f, 1f)
.texture(1, 1)
.overlay(OverlayTexture.DEFAULT_UV)
.light(14680064)
.light(light)
.normal(0f, 1f, 0f).next();
buffer.vertex(matrix, pos.x + brick.dim, pos.y, pos.z)
.color(1f, 1f, 1f, 1f)
.texture(1, 0)
.overlay(OverlayTexture.DEFAULT_UV)
.light(14680064)
.light(light)
.normal(0f, 1f, 0f).next();

Tessellator.getInstance().draw();
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/goby56/wakes/render/enums/WakeColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.goby56.wakes.WakesClient;
import com.goby56.wakes.config.WakesConfig;
import net.minecraft.util.StringIdentifiable;
import net.minecraft.util.math.ColorHelper;

public enum WakeColor implements StringIdentifiable {
TRANSPARENT(0, 0, 0, 0),
Expand All @@ -26,7 +27,7 @@ public enum WakeColor implements StringIdentifiable {
this.abgr = alpha << 24 | blue << 16 | green << 8 | red;
}

private int blend(int waterColor, float opacity, boolean isWhite) {
private int blend(int waterColor, int lightColor, float opacity, boolean isWhite) {
float srcA = (this.abgr >>> 24 & 0xFF) / 255f;
int a = (int) (opacity * 255 * srcA);
int b = 255, g = 255, r = 255;
Expand All @@ -35,15 +36,19 @@ private int blend(int waterColor, float opacity, boolean isWhite) {
g = (int) ((this.abgr >> 8 & 0xFF) * (1 - srcA) + (waterColor >> 8 & 0xFF) * (srcA));
r = (int) ((this.abgr & 0xFF) * (1 - srcA) + (waterColor >> 16 & 0xFF) * (srcA));
}
float lightA = WakesClient.CONFIG_INSTANCE.lightInfluence / 255f;
b = (int) ((b * (1-lightA) + (lightColor >> 16 & 0xFF) * lightA));
g = (int) ((g * (1-lightA) + (lightColor >> 8 & 0xFF) * lightA));
r = (int) ((r * (1-lightA) + (lightColor & 0xFF) * lightA));

return a << 24 | b << 16 | g << 8 | r;
}

public static int getColor(float waveEqAvg, int waterColor, float opacity) {
public static int getColor(float waveEqAvg, int waterColor, int lightColor, float opacity) {
double clampedRange = 100 / (1 + Math.exp(-0.1 * waveEqAvg)) - 50;
for (WakesConfig.ColorInterval interval : WakesClient.CONFIG_INSTANCE.colorIntervals) {
if (interval.lower <= clampedRange && clampedRange <= interval.upper) {
return interval.color.blend(waterColor, opacity, interval.color == WakeColor.WHITE);
return interval.color.blend(waterColor, lightColor, opacity, interval.color == WakeColor.WHITE);
}
}
return WakeColor.TRANSPARENT.abgr;
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/com/goby56/wakes/simulation/Brick.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import com.goby56.wakes.WakesClient;
import com.goby56.wakes.render.enums.WakeColor;
import com.goby56.wakes.debug.WakesDebugInfo;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.RunArgs;
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.client.render.Frustum;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.LightmapTextureManager;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.lwjgl.system.MemoryUtil;

import java.util.ArrayList;
Expand Down Expand Up @@ -159,24 +167,33 @@ public void updateAdjacency(Brick brick) {
}

public void populatePixels() {
World world = MinecraftClient.getInstance().world;
for (int z = 0; z < dim; z++) {
for (int x = 0; x < dim; x++) {
WakeNode node = this.get(x, z);

int waterCol = node != null ? node.waterColor : 0;
float opacity = node != null ? (float) ((-Math.pow(node.t, 2) + 1) * WakesClient.CONFIG_INSTANCE.wakeOpacity) : 0;
int lightCol = LightmapTextureManager.MAX_LIGHT_COORDINATE;
int waterCol = 0;
float opacity = 0;
if (node != null) {
waterCol = BiomeColors.getWaterColor(world, node.blockPos());
int lightCoordinate = WorldRenderer.getLightmapCoordinates(world, node.blockPos());
lightCol = MinecraftClient.getInstance().gameRenderer.getLightmapTextureManager().image.getColor(
LightmapTextureManager.getBlockLightCoordinates(lightCoordinate),
LightmapTextureManager.getSkyLightCoordinates(lightCoordinate)
);
opacity = (float) ((-Math.pow(node.t, 2) + 1) * WakesClient.CONFIG_INSTANCE.wakeOpacity);
}

long nodeOffset = texRes * 4L * (((long) z * dim * texRes) + (long) x);
for (int r = 0; r < texRes; r++) {
for (int c = 0; c < texRes; c++) {
float avg = 0;
int color = 0;
if (node != null) {
avg += (node.u[0][r + 1][c + 1] + node.u[1][r + 1][c + 1] + node.u[2][r + 1][c + 1]) / 3;
float avg = (node.u[0][r + 1][c + 1] + node.u[1][r + 1][c + 1] + node.u[2][r + 1][c + 1]) / 3;
color = WakeColor.getColor(avg, waterCol, lightCol, opacity);
}
int color = waterCol != 0 ? WakeColor.getColor(avg, waterCol, opacity) : 0;

long pixelOffset = 4L * (((long) r * dim * texRes) + c);

MemoryUtil.memPutInt(imgPtr + nodeOffset + pixelOffset, color);
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/goby56/wakes/simulation/WakeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ public class WakeNode implements Position<WakeNode>, Age<WakeNode> {
public float t = 0;
public int floodLevel;

public int waterColor = 0;
public int lightCoordinate = 0;

// public WakeNode(int x, int z) {
// this.x = x;
// this.z = z;
// }

//TODO MORE GENERALIZED CONSTRUCTOR
public WakeNode(Vec3d position, int initialStrength) {
this.initValues();
Expand Down Expand Up @@ -145,8 +137,6 @@ public boolean tick() {
this.u[0][z][x] *= beta;
}
}
waterColor = BiomeColors.getWaterColor(MinecraftClient.getInstance().world, this.blockPos());
lightCoordinate = WorldRenderer.getLightmapCoordinates(MinecraftClient.getInstance().world, this.blockPos());
floodFill();
return true;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/wakes.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ accessible field net/minecraft/entity/vehicle/BoatEntity paddlePhases [F

accessible field net/minecraft/entity/vehicle/BoatEntity NEXT_PADDLE_PHASE F

accessible field net/minecraft/client/model/ModelPart$Cuboid sides [Lnet/minecraft/client/model/ModelPart$Quad;
accessible field net/minecraft/client/model/ModelPart$Cuboid sides [Lnet/minecraft/client/model/ModelPart$Quad;

accessible field net/minecraft/client/render/LightmapTextureManager image Lnet/minecraft/client/texture/NativeImage;

0 comments on commit abdeab9

Please sign in to comment.