Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ repositories {
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
mavenCentral()
maven {
name = 'ParchmentMC'
url = 'https://maven.parchmentmc.org'
}
//----for test---
}

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings loom.officialMojangMappings()
mappings loom.layered() {
officialMojangMappings()
parchment("org.parchmentmc.data:parchment-1.20.2:2023.10.08@zip")
}
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

// Fabric API. This is technically optional, but you probably want it anyway.
Expand Down Expand Up @@ -78,4 +85,7 @@ publishing {
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
}
}
loom {
accessWidenerPath = file("src/main/resources/ryansRenderingKit.accesswidener")
}
93 changes: 93 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- ==== Maven Coordinates ==== -->
<groupId>io.github.hotpad100c</groupId>
<artifactId>ryansrenderingkit</artifactId>
<version>1.0.0</version>
<name>Ryan's Rendering Kit</name>
<description>A Fabric-based rendering toolkit for Minecraft</description>
<url>https://github.com/hotpad100c/ryansrenderingkit</url>

<!-- ==== Java 21 ==== -->
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- prevent "duplicate classes" in modding environment -->
<maven.jar.skipIfEmpty>true</maven.jar.skipIfEmpty>
</properties>

<!-- ==== GitHub Packages ==== -->
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/hotpad100c/ryansrenderingkit</url>
</repository>
</distributionManagement>

<!-- ==== Dependencies (Fabric API) ====
<dependencies>

<dependency>
<groupId>net.fabricmc</groupId>
<artifactId>fabric-api</artifactId>
<version>0.119.4+1.21.4</version>
<scope>provided</scope>
</dependency>

</dependencies>
-->

<!-- ==== Build configuration ==== -->
<build>
<plugins>

<!-- Source JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Javadoc JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>21</release>
</configuration>
</plugin>

</plugins>
</build>

</project>
126 changes: 126 additions & 0 deletions src/main/java/mypals/ml/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package mypals.ml;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Axis;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ShapeRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.SignText;
import net.minecraft.world.phys.Vec3;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.joml.Vector4f;

import static mypals.ml.RyansRenderingKit.MOD_ID;

Expand All @@ -11,4 +26,115 @@ public static ResourceLocation generateUniqueId(String prefix) {
int randomNum = ThreadLocalRandom.current().nextInt(10000);
return ResourceLocation.fromNamespaceAndPath(MOD_ID,prefix.toLowerCase() +"_"+ timestamp + "_" + randomNum);
}
public static Vec3 max(Vec3 a, Vec3 b) {
return new Vec3(
Math.max(a.x, b.x),
Math.max(a.y, b.y),
Math.max(a.z, b.z)
);
}

public static Vec3 min(Vec3 a, Vec3 b) {
return new Vec3(
Math.min(a.x, b.x),
Math.min(a.y, b.y),
Math.min(a.z, b.z)
);
}
public static Vec3 calculateCentroid(List<Vec3> vertices) {
double sumX = 0, sumY = 0, sumZ = 0;
for (Vec3 v : vertices) {
sumX += v.x;
sumY += v.y;
sumZ += v.z;
}
double n = vertices.size();
return new Vec3(sumX / n, sumY / n, sumZ / n);
}
public static Matrix4f createViewMatrix(Camera camera) {
Matrix4f view = new Matrix4f();

Quaternionf camRot = camera.rotation();
Quaternionf camRotInv = new Quaternionf(camRot).conjugate();
view.rotation(camRotInv);

Vec3 camPos = camera.getPosition();
view.translate(new Vector3f(
(float) -camPos.x,
(float) -camPos.y,
(float) -camPos.z
));

return view;
}
public static boolean isVertexInFrustum(Vec3 v, Matrix4f mvp) {
Vector4f clip = new Vector4f((float)v.x, (float)v.y, (float)v.z, 1f);
clip.mul(mvp);
if (clip.w <= 0) return false;
float ndcX = clip.x / clip.w;
float ndcY = clip.y / clip.w;
float ndcZ = clip.z / clip.w;

return ndcX >= -1 && ndcX <= 1 &&
ndcY >= -1 && ndcY <= 1 &&
ndcZ >= -1 && ndcZ <= 1;
}
public static int multiplyRGB(int color, float shade) {
int alpha = color >>> 24 & 255;
int red = (int)((float)(color >>> 16 & 255) * shade);
int green = (int)((float)(color >>> 8 & 255) * shade);
int blue = (int)((float)(color & 255) * shade);
return alpha << 24 | red << 16 | green << 8 | blue;
}
public static void renderLineBox(PoseStack poseStack, VertexConsumer consumer,
Vec3 center, float size,
float red, float green, float blue, float alpha) {

double half = size / 2.0;

ShapeRenderer.renderLineBox(
poseStack, consumer,
center.x - half, center.y - half, center.z - half,
center.x + half, center.y + half, center.z + half,
red, green, blue,
alpha, red, green,
blue
);
}
public static void renderBillboardFrame(
PoseStack poseStack,
VertexConsumer vc,
Vec3 vec3,
float size,
float r, float g, float b, float a
) {
poseStack.pushPose();
poseStack.translate(vec3);

Camera camera = Minecraft.getInstance().gameRenderer.getMainCamera();

poseStack.mulPose(Axis.YP.rotationDegrees(-camera.getYRot()));
poseStack.mulPose(Axis.XP.rotationDegrees(camera.getXRot()));

PoseStack.Pose pose = poseStack.last();

float s = size / 2f;

Vec3 v1 = new Vec3(-s, -s, 0);
Vec3 v2 = new Vec3(s, -s, 0);
Vec3 v3 = new Vec3(s, s, 0);
Vec3 v4 = new Vec3(-s, s, 0);

Vec3 n = new Vec3(0, 0, -1);

addLine(pose, vc, v1, v2, r, g, b, a, n);
addLine(pose, vc, v2, v3, r, g, b, a, n);
addLine(pose, vc, v3, v4, r, g, b, a, n);
addLine(pose, vc, v4, v1, r, g, b, a, n);
poseStack.popPose();
}
private static void addLine(PoseStack.Pose pose, VertexConsumer vc, Vec3 a, Vec3 b, float r, float g, float b2, float a2, Vec3 normal) {
vc.addVertex(pose, (float)a.x, (float)a.y, (float)a.z).setColor(r, g, b2, a2).setNormal(pose, (float)normal.x, (float)normal.y, (float)normal.z);
vc.addVertex(pose, (float)b.x, (float)b.y, (float)b.z).setColor(r, g, b2, a2).setNormal(pose, (float)normal.x, (float)normal.y, (float)normal.z);
}
}
22 changes: 6 additions & 16 deletions src/main/java/mypals/ml/RyansRenderingKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,33 @@
import mypals.ml.test.Tester;
import net.fabricmc.api.ModInitializer;

import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static mypals.ml.test.Tester.registerDebugCommands;

public class RyansRenderingKit implements ModInitializer {
public static final String MOD_ID = "ryansrenderingkit";

// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
BuilderManagers.init();
ShapeManagers.init();
VertexBuilderGetter.init();
WorldRenderEvents.AFTER_TRANSLUCENT.register(this::handleRenderFabulous);
WorldRenderEvents.LAST.register(this::handleRenderLast);
ClientTickEvents.END_WORLD_TICK.register(c-> ShapeManagers.syncShapeTransform());

ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> {
registerDebugCommands(dispatcher);
});
Tester.init();

LOGGER.info("Hello Fabric world!");
}
private void handleRenderFabulous(WorldRenderContext ctx) {
//if (!ctx.advancedTranslucency()) return;
//InformationRender.render(ctx.matrixStack(), ctx.camera(),ctx.tickCounter().getGameTimeDeltaPartialTick(true));
}

private void handleRenderLast(WorldRenderContext ctx) {
//if (ctx.advancedTranslucency()) return;
InformationRender.render(ctx.matrixStack(), ctx.camera(),ctx.tickCounter().getGameTimeDeltaPartialTick(true));
}
}
7 changes: 4 additions & 3 deletions src/main/java/mypals/ml/builderManager/BuilderManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mypals.ml.builderManager;

import com.mojang.blaze3d.vertex.PoseStack;
import mypals.ml.builders.vertexBuilders.BatchVertexBuilder;
import mypals.ml.builders.vertexBuilders.BufferedVertexBuilder;
import mypals.ml.builders.vertexBuilders.ImmediateVertexBuilder;
Expand Down Expand Up @@ -38,7 +39,7 @@ public BuilderGroup(Matrix4f matrix,boolean seeThrough,RenderMethod renderMethod
public void drawBatch(Consumer<BatchVertexBuilder> builder, RenderMethod renderMethod){
batchVertexBuilder.beginBatch(renderMethod);
builder.accept(batchVertexBuilder);
batchVertexBuilder.drawBatch();
batchVertexBuilder.drawBatch(renderMethod);
}
public void drawImmediate(Shape shape, Consumer<VertexBuilder> builder, RenderMethod renderMethod){
immediateShapeBuilder.draw(shape,builder,renderMethod);
Expand Down Expand Up @@ -73,7 +74,7 @@ public void rebuildVBO(Collection<Shape> shapeList, boolean seeThrough){
sortedShapes.sort(SHAPE_ORDER_COMPARATOR);

for(Shape shape: sortedShapes){
shape.draw(builder);
shape.draw(false, builder,new PoseStack(),1);
}
});
else normalBuilderGroup.bufferedVertexBuilder.rebuild(renderMethod, builder->{
Expand All @@ -82,7 +83,7 @@ public void rebuildVBO(Collection<Shape> shapeList, boolean seeThrough){
sortedShapes.sort(SHAPE_ORDER_COMPARATOR);

for(Shape shape: sortedShapes){
shape.draw(builder);
shape.draw(false, builder,new PoseStack(),1);
}
});
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/mypals/ml/builderManager/BuilderManagers.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
package mypals.ml.builderManager;

import mypals.ml.render.RenderMethod;
import org.joml.Matrix4f;

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

public class BuilderManagers {
public static BuilderManager QUADS_BUILDER_MANAGER = null;
public static BuilderManager LINES_BUILDER_MANAGER = null;
public static BuilderManager LINE_STRIP_BUILDER_MANAGER = null;
public static BuilderManager TRIANGLES_BUILDER_MANAGER = null;
public static BuilderManager TRIANGLES_STRIP_BUILDER_MANAGER = null;
public static BuilderManager TRIANGLES_FAN_BUILDER_MANAGER = null;
public static BuilderManager TEXT = null;
public static EmptyBuilderManager NON_SHAPE_OBJECTS = null;
public static List<BuilderManager> builders = new ArrayList<>();
public static List<EmptyBuilderManager> emptyBuilders = new ArrayList<>();
public static void init() {
Matrix4f matrix4f = new Matrix4f();
QUADS_BUILDER_MANAGER = register(matrix4f,RenderMethod.QUADS,"quads_builder_manager");
LINES_BUILDER_MANAGER = register(matrix4f, RenderMethod.LINES,"lines_builder_manager");
LINE_STRIP_BUILDER_MANAGER = register(matrix4f,RenderMethod.LINE_STRIP,"line_strip_builder_manager");
TRIANGLES_BUILDER_MANAGER = register(matrix4f,RenderMethod.TRIANGLES,"triangles_builder_manager");
TRIANGLES_STRIP_BUILDER_MANAGER = register(matrix4f,RenderMethod.TRIANGLES_STRIP,"triangles_strip_builder_manager");
TRIANGLES_FAN_BUILDER_MANAGER = register(matrix4f,RenderMethod.TRIANGLES_FAN,"triangles_fan_builder_manager");
TEXT = register(matrix4f,RenderMethod.TEXT,"text_manager");
NON_SHAPE_OBJECTS = registerEmpty(matrix4f,"empty");
}
public static void updateMatrix(Matrix4f matrix4f){
for(BuilderManager builderManager : builders){
builderManager.updateMatrix(matrix4f);
}
for(EmptyBuilderManager builderManager : emptyBuilders){
builderManager.updateMatrix(matrix4f);
}
}

public static BuilderManager register(Matrix4f matrix4f, RenderMethod renderMethod,String id){
BuilderManager builderManager = new BuilderManager(matrix4f,renderMethod,id);
builders.add(builderManager);
return builderManager;
}
public static EmptyBuilderManager registerEmpty(Matrix4f matrix4f, String id){
EmptyBuilderManager builderManager = new EmptyBuilderManager(matrix4f,id);
emptyBuilders.add(builderManager);
return builderManager;
}
}
Loading