Skip to content

Commit

Permalink
without delaunay = worse
Browse files Browse the repository at this point in the history
  • Loading branch information
Goby56 committed Dec 27, 2023
1 parent d8f9b9c commit 17b0edf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 63 deletions.
141 changes: 78 additions & 63 deletions src/main/java/com/goby56/wakes/render/SplashPlaneRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,62 @@
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.color.world.BiomeColors;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.*;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.Pair;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix4f;
import org.joml.Vector3f;

import java.util.ArrayList;
import java.util.List;

public class SplashPlaneRenderer implements ClientLifecycleEvents.ClientStarted {

private static ArrayList<Vector2D> points;
private static List<Triangle2D> triangles;
private static ArrayList<Vec3d> vertices;
private static ArrayList<Vec3d> normals;
private static ArrayList<Vec2f> uvs;

private static ArrayList<ArrayList<Vertex>> vertices;
private static Color renderingColor = new Color();
private static final double SQRT_8 = Math.sqrt(8);

public static <T extends Entity> void render(T entity, float yaw, float tickDelta, MatrixStack matrices, int light) {
private static boolean DEBUG = false;

private static class Vertex {
public final Vector3f pos;
public final Vector3f normal;

public Vertex(double x, double y) {
this.pos = new Vector3f((float) x, (float) y, (float) height(x, y));
this.normal = normal(x, y);
}
}

private static class Color {
public float r;
public float g;
public float b;

public Color(int color) {
this.r = (float) (color >> 16 & 0xFF) / 255f;
this.g = (float) (color >> 8 & 0xFF) / 255f;
this.b = (float) (color & 0xFF) / 255f;
}

public Color() {
this.reset();
}

public void reset() {
this.r = 1f; this.g = 1f; this.b = 1f;
}
}

public static <T extends Entity> void render(T entity, float yaw, float tickDelta, MatrixStack matrices, int light) {
RenderSystem.setShader(RenderType.getProgram());
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
RenderSystem.enableBlend();
// RenderSystem.setShaderTexture(0, new Identifier("minecraft", "textures/block/water_flow.png"));

matrices.push();
float width = entity.getWidth();
Expand All @@ -45,46 +73,39 @@ public static <T extends Entity> void render(T entity, float yaw, float tickDelt
matrices.scale(scalar, scalar, scalar);
Matrix4f matrix = matrices.peek().getPositionMatrix();

float r, g, b;
int waterCol = BiomeColors.getWaterColor(entity.getWorld(), entity.getBlockPos());
if (WakesClient.CONFIG_INSTANCE.useWaterBlending) {
r = (float) (waterCol >> 16 & 0xFF) / 255f;
g = (float) (waterCol >> 8 & 0xFF) / 255f;
b = (float) (waterCol & 0xFF) / 255f;
renderingColor = new Color(BiomeColors.getWaterColor(entity.getWorld(), entity.getBlockPos()));
} else {
r = 1f;
g = 1f;
b = 1f;
renderingColor.reset();
}

RenderSystem.setShaderTexture(0, new Identifier("wakes", "textures/splash_plane.png"));
renderSurface(matrix, r, g, b, light);
RenderSystem.setShaderTexture(0, new Identifier("wakes", "textures/splash_plane_outline.png"));
renderSurface(matrix, 1f, 1f, 1f, light);
renderSurface(matrix, 2 * velocity, light);
// renderingColor.reset();
// RenderSystem.setShaderTexture(0, new Identifier("wakes", "textures/splash_plane_outline.png"));
// renderSurface(matrix, velocity, light);

matrices.pop();
}

private static void renderSurface(Matrix4f matrix, float r, float g, float b, int light) {
private static void renderSurface(Matrix4f matrix, float progress, int light) {
BufferBuilder buffer = Tessellator.getInstance().getBuffer();
buffer.begin(VertexFormat.DrawMode.TRIANGLES, VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL);
buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL);

// https://streamable.com/tz0gp
for (int s = -1; s < 2; s++) {
if (s == 0) continue;
for (int i = 0; i < vertices.size(); i++) {
Vec3d vertex = vertices.get(i);
Vec3d normal = normals.get(i);
buffer.vertex(matrix,
(float) (s * vertex.x * WakesClient.CONFIG_INSTANCE.splashPlaneWidth),
(float) (vertex.z * WakesClient.CONFIG_INSTANCE.splashPlaneHeight),
(float) (vertex.y * WakesClient.CONFIG_INSTANCE.splashPlaneDepth))
.color(r, g, b, 1f)
.texture((float) vertex.x, (float) vertex.y)
.overlay(OverlayTexture.DEFAULT_UV)
.light(light)
.normal((float) normal.x, (float) normal.y, (float) normal.z)
.next();
for (int i = 0; i < vertices.size() - 1; i++) {
ArrayList<Vertex> column1 = vertices.get(i);
ArrayList<Vertex> column2 = vertices.get(i+1);
for (int j = 0; j < Math.min(column1.size(), column2.size()) - 1; j++) {
// for (int j = 0; j < column1.size(); j++) {
// addVertex(buffer, matrix, column1.get(j), s, light, 0, 0);
addVertex(buffer, matrix, column1.get(j), s, light, 0, 1);
addVertex(buffer, matrix, column1.get(j + 1), s, light, 0, 0);
addVertex(buffer, matrix, column2.get(j + 1), s, light, 1, 0);
addVertex(buffer, matrix, column2.get(j), s, light, 1, 1);
}
}
}

Expand All @@ -93,6 +114,19 @@ private static void renderSurface(Matrix4f matrix, float r, float g, float b, in
RenderSystem.enableCull();
}

private static void addVertex(BufferBuilder buffer, Matrix4f matrix, Vertex vertex, int sign, int light, float u, float v) {
buffer.vertex(matrix,
sign * vertex.pos.x * WakesClient.CONFIG_INSTANCE.splashPlaneWidth,
vertex.pos.z * WakesClient.CONFIG_INSTANCE.splashPlaneHeight,
vertex.pos.y * WakesClient.CONFIG_INSTANCE.splashPlaneDepth)
.color(renderingColor.r, renderingColor.g, renderingColor.b, 1f)
.texture(vertex.pos.x, vertex.pos.y)
.overlay(OverlayTexture.DEFAULT_UV)
.light(light)
.normal(vertex.normal.x, vertex.normal.y, vertex.normal.z)
.next();
}

private static double upperBound(double x) {
return - 2 * x * x + SQRT_8 * x;
}
Expand All @@ -102,54 +136,35 @@ private static double lowerBound(double x) {
}

private static double height(double x, double y) {
return 4 * (x * (SQRT_8 - x) -y - x * x) / SQRT_8;
float res = WakesClient.CONFIG_INSTANCE.splashPlaneResolution;
return Math.floor(res * 4 * (x * (SQRT_8 - x) - y - x * x) / SQRT_8) / res;
}

private static Vec3d normal(double x, double y) {
private static Vector3f normal(double x, double y) {
double nx = SQRT_8 / (4 * (4 * x + y - SQRT_8));
double ny = SQRT_8 / (4 * (2 * x * x - SQRT_8 + 1));
return Vec3d.fromPolar((float) Math.tan(nx), (float) Math.tan(ny));
return Vec3d.fromPolar((float) Math.tan(nx), (float) Math.tan(ny)).toVector3f();
}

private static void distributePoints() {
int res = WakesClient.CONFIG_INSTANCE.splashPlaneResolution;
points = new ArrayList<>();
vertices = new ArrayList<>();

for (float i = 0; i < res; i++) {
vertices.add(new ArrayList<>());
double x = i / (res - 1);
double h = upperBound(x) - lowerBound(x);
int n_points = (int) Math.max(1, Math.floor(h * res));
for (float j = 0; j < n_points + 1; j++) {
if (n_points == 1) continue;
for (float j = n_points; j >= 0; j--) {
float y = (float) ((j / n_points) * h + lowerBound(x));
points.add(new Vector2D(x, y));
}
}
}

private static void generateMesh() {
vertices = new ArrayList<>();
normals = new ArrayList<>();
uvs = new ArrayList<>();
try {
DelaunayTriangulator delaunay = new DelaunayTriangulator(points);
delaunay.triangulate();
triangles = delaunay.getTriangles();
} catch (NotEnoughPointsException e) {
e.printStackTrace();
}
for (Triangle2D tri : triangles) {
for (Vector2D vec : new Vector2D[] {tri.a, tri.b, tri.c}) {
double x = vec.x, y = vec.y;
vertices.add(new Vec3d(x, y, height(x, y)));
normals.add(normal(x, y));
uvs.add(new Vec2f((float) x, x == 0 ? 0 : (float) ((y - lowerBound(x)) / (upperBound(x) - lowerBound(x)))));
vertices.get((int) i).add(new Vertex(x, y));
}
}
}

public static void initSplashPlane() {
distributePoints();
generateMesh();
}

@Override
Expand Down
Binary file modified src/main/resources/assets/wakes/textures/splash_plane.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 17b0edf

Please sign in to comment.