|
| 1 | +package com.simibubi.create.content.fluids; |
| 2 | + |
| 3 | +import org.joml.Vector4f; |
| 4 | +import org.joml.Vector4fc; |
| 5 | + |
| 6 | +import dev.engine_room.flywheel.api.material.CardinalLightingMode; |
| 7 | +import dev.engine_room.flywheel.api.material.Transparency; |
| 8 | +import dev.engine_room.flywheel.api.model.Model; |
| 9 | +import dev.engine_room.flywheel.api.vertex.MutableVertexList; |
| 10 | +import dev.engine_room.flywheel.lib.material.SimpleMaterial; |
| 11 | +import dev.engine_room.flywheel.lib.model.QuadMesh; |
| 12 | +import dev.engine_room.flywheel.lib.model.SingleMeshModel; |
| 13 | +import dev.engine_room.flywheel.lib.util.RendererReloadCache; |
| 14 | +import net.createmod.catnip.data.Iterate; |
| 15 | +import net.minecraft.client.renderer.texture.OverlayTexture; |
| 16 | +import net.minecraft.client.renderer.texture.TextureAtlasSprite; |
| 17 | +import net.minecraft.core.Direction; |
| 18 | +import net.minecraft.util.Mth; |
| 19 | + |
| 20 | +public class FluidMesh { |
| 21 | + private static final RendererReloadCache<TextureAtlasSprite, Model> STREAM = new RendererReloadCache<>(sprite -> new SingleMeshModel(new FluidStreamMesh(sprite), material(sprite))); |
| 22 | + |
| 23 | + private static final RendererReloadCache<SurfaceKey, Model> SURFACE = new RendererReloadCache<>(sprite -> new SingleMeshModel(new FluidSurfaceMesh(sprite.texture(), sprite.width()), material(sprite.texture()))); |
| 24 | + public static final float PIPE_RADIUS = 3f / 16f; |
| 25 | + |
| 26 | + // TODO: width parameter here too |
| 27 | + public static Model stream(TextureAtlasSprite sprite) { |
| 28 | + return STREAM.get(sprite); |
| 29 | + } |
| 30 | + |
| 31 | + public static Model surface(TextureAtlasSprite sprite, float width) { |
| 32 | + return SURFACE.get(new SurfaceKey(sprite, width)); |
| 33 | + } |
| 34 | + |
| 35 | + private static SimpleMaterial material(TextureAtlasSprite sprite) { |
| 36 | + return SimpleMaterial.builder() |
| 37 | + .cardinalLightingMode(CardinalLightingMode.OFF) |
| 38 | + .texture(sprite.atlasLocation()) |
| 39 | + .transparency(Transparency.ORDER_INDEPENDENT) |
| 40 | + .build(); |
| 41 | + } |
| 42 | + |
| 43 | + private record SurfaceKey(TextureAtlasSprite texture, float width) { |
| 44 | + } |
| 45 | + |
| 46 | + public record FluidSurfaceMesh(TextureAtlasSprite texture, float width) implements QuadMesh { |
| 47 | + @Override |
| 48 | + public int vertexCount() { |
| 49 | + int quadWidth = Mth.ceil(width) - Mth.floor(-width); |
| 50 | + return 4 * quadWidth * quadWidth; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void write(MutableVertexList vertexList) { |
| 55 | + for (int i = 0; i < vertexCount(); i++) { |
| 56 | + vertexList.r(i, 1); |
| 57 | + vertexList.g(i, 1); |
| 58 | + vertexList.b(i, 1); |
| 59 | + vertexList.a(i, 1); |
| 60 | + vertexList.light(i, 0); |
| 61 | + vertexList.overlay(i, OverlayTexture.NO_OVERLAY); |
| 62 | + |
| 63 | + vertexList.normalX(i, 0); |
| 64 | + vertexList.normalY(i, 1); |
| 65 | + vertexList.normalZ(i, 0); |
| 66 | + |
| 67 | + vertexList.y(i, 0); |
| 68 | + } |
| 69 | + |
| 70 | + float textureScale = 1; |
| 71 | + |
| 72 | + float left = -width; |
| 73 | + float right = width; |
| 74 | + float down = -width; |
| 75 | + float up = width; |
| 76 | + |
| 77 | + int vertex = 0; |
| 78 | + |
| 79 | + float shrink = texture.uvShrinkRatio() * 0.25f * textureScale; |
| 80 | + float centerU = texture.getU0() + (texture.getU1() - texture.getU0()) * 0.5f; |
| 81 | + float centerV = texture.getV0() + (texture.getV1() - texture.getV0()) * 0.5f; |
| 82 | + |
| 83 | + float x2; |
| 84 | + float y2; |
| 85 | + for (float x1 = left; x1 < right; x1 = x2) { |
| 86 | + float x1floor = Mth.floor(x1); |
| 87 | + x2 = Math.min(x1floor + 1, right); |
| 88 | + float u1 = texture.getU((x1 - x1floor) * 16 * textureScale); |
| 89 | + float u2 = texture.getU((x2 - x1floor) * 16 * textureScale); |
| 90 | + u1 = Mth.lerp(shrink, u1, centerU); |
| 91 | + u2 = Mth.lerp(shrink, u2, centerU); |
| 92 | + for (float y1 = down; y1 < up; y1 = y2) { |
| 93 | + float y1floor = Mth.floor(y1); |
| 94 | + y2 = Math.min(y1floor + 1, up); |
| 95 | + float v1 = texture.getV((y1 - y1floor) * 16 * textureScale); |
| 96 | + float v2 = texture.getV((y2 - y1floor) * 16 * textureScale); |
| 97 | + v1 = Mth.lerp(shrink, v1, centerV); |
| 98 | + v2 = Mth.lerp(shrink, v2, centerV); |
| 99 | + |
| 100 | + vertexList.x(vertex, x1); |
| 101 | + vertexList.z(vertex, y1); |
| 102 | + vertexList.u(vertex, u1); |
| 103 | + vertexList.v(vertex, v1); |
| 104 | + |
| 105 | + vertexList.x(vertex + 1, x1); |
| 106 | + vertexList.z(vertex + 1, y2); |
| 107 | + vertexList.u(vertex + 1, u1); |
| 108 | + vertexList.v(vertex + 1, v2); |
| 109 | + |
| 110 | + vertexList.x(vertex + 2, x2); |
| 111 | + vertexList.z(vertex + 2, y2); |
| 112 | + vertexList.u(vertex + 2, u2); |
| 113 | + vertexList.v(vertex + 2, v2); |
| 114 | + |
| 115 | + vertexList.x(vertex + 3, x2); |
| 116 | + vertexList.z(vertex + 3, y1); |
| 117 | + vertexList.u(vertex + 3, u2); |
| 118 | + vertexList.v(vertex + 3, v1); |
| 119 | + vertex += 4; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Vector4fc boundingSphere() { |
| 126 | + return new Vector4f(0, 0, 0, width / Mth.SQRT_OF_TWO); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public record FluidStreamMesh(TextureAtlasSprite texture) implements QuadMesh { |
| 131 | + @Override |
| 132 | + public int vertexCount() { |
| 133 | + return 4 * 2 * 4; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void write(MutableVertexList vertexList) { |
| 138 | + for (int i = 0; i < vertexCount(); i++) { |
| 139 | + vertexList.r(i, 1); |
| 140 | + vertexList.g(i, 1); |
| 141 | + vertexList.b(i, 1); |
| 142 | + vertexList.a(i, 1); |
| 143 | + vertexList.light(i, 0); |
| 144 | + vertexList.overlay(i, OverlayTexture.NO_OVERLAY); |
| 145 | + |
| 146 | + vertexList.v(i, 0); |
| 147 | + } |
| 148 | + |
| 149 | + float textureScale = 0.5f; |
| 150 | + |
| 151 | + float shrink = texture.uvShrinkRatio() * 0.25f * textureScale; |
| 152 | + float centerU = texture.getU0() + (texture.getU1() - texture.getU0()) * 0.5f; |
| 153 | + |
| 154 | + float radius = PIPE_RADIUS; |
| 155 | + float left = -radius; |
| 156 | + float right = radius; |
| 157 | + |
| 158 | + int vertex = 0; |
| 159 | + |
| 160 | + for (var horizontalDirection : Iterate.horizontalDirections) { |
| 161 | + float x2; |
| 162 | + for (float x1 = left; x1 < right; x1 = x2) { |
| 163 | + float x1floor = Mth.floor(x1); |
| 164 | + x2 = Math.min(x1floor + 1, right); |
| 165 | + float u1 = texture.getU((x1 - x1floor) * 16 * textureScale); |
| 166 | + float u2 = texture.getU((x2 - x1floor) * 16 * textureScale); |
| 167 | + u1 = Mth.lerp(shrink, u1, centerU); |
| 168 | + u2 = Mth.lerp(shrink, u2, centerU); |
| 169 | + |
| 170 | + putQuad(vertexList, vertex, horizontalDirection, radius, x1, x2, u1, u2); |
| 171 | + vertex += 4; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static void putQuad(MutableVertexList vertexList, int i, Direction horizontal, float radius, float p0, float p1, float u0, float u1) { |
| 177 | + float xStart; |
| 178 | + float xEnd; |
| 179 | + float zStart; |
| 180 | + float zEnd; |
| 181 | + |
| 182 | + switch (horizontal) { |
| 183 | + case NORTH: |
| 184 | + xStart = p1; |
| 185 | + xEnd = p0; |
| 186 | + zStart = zEnd = -radius; |
| 187 | + break; |
| 188 | + case SOUTH: |
| 189 | + xStart = p0; |
| 190 | + xEnd = p1; |
| 191 | + zStart = zEnd = radius; |
| 192 | + break; |
| 193 | + case WEST: |
| 194 | + zStart = p0; |
| 195 | + zEnd = p1; |
| 196 | + xStart = xEnd = -radius; |
| 197 | + break; |
| 198 | + case EAST: |
| 199 | + zStart = p1; |
| 200 | + zEnd = p0; |
| 201 | + xStart = xEnd = radius; |
| 202 | + break; |
| 203 | + default: |
| 204 | + throw new IllegalStateException("Unexpected value: " + horizontal); |
| 205 | + } |
| 206 | + |
| 207 | + vertexList.x(i, xStart); |
| 208 | + vertexList.y(i, 1); |
| 209 | + vertexList.z(i, zStart); |
| 210 | + vertexList.u(i, u0); |
| 211 | + |
| 212 | + vertexList.x(i + 1, xStart); |
| 213 | + vertexList.y(i + 1, 0); |
| 214 | + vertexList.z(i + 1, zStart); |
| 215 | + vertexList.u(i + 1, u0); |
| 216 | + |
| 217 | + vertexList.x(i + 2, xEnd); |
| 218 | + vertexList.y(i + 2, 0); |
| 219 | + vertexList.z(i + 2, zEnd); |
| 220 | + vertexList.u(i + 2, u1); |
| 221 | + |
| 222 | + vertexList.x(i + 3, xEnd); |
| 223 | + vertexList.y(i + 3, 1); |
| 224 | + vertexList.z(i + 3, zEnd); |
| 225 | + vertexList.u(i + 3, u1); |
| 226 | + |
| 227 | + for (int j = 0; j < 4; j++) { |
| 228 | + vertexList.normalX(i + j, horizontal.getStepX()); |
| 229 | + vertexList.normalY(i + j, horizontal.getStepY()); |
| 230 | + vertexList.normalZ(i + j, horizontal.getStepZ()); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public Vector4fc boundingSphere() { |
| 236 | + return new Vector4f(0, 0.5f, 0, 1); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments