Skip to content

Commit

Permalink
Several changes, see commit body
Browse files Browse the repository at this point in the history
- Move over to satin for shader management
- Move over to tabs instead of 4 spaces
- Create FastMStack for a faster MatrixStack implementation
  - Use new implementation per default in RendererUtils
- Switch hud render event over to DrawContext instead of pure MatrixStack
  • Loading branch information
0x3C50 committed Jul 4, 2023
1 parent a8da3c3 commit e733e65
Show file tree
Hide file tree
Showing 38 changed files with 2,718 additions and 2,538 deletions.
13 changes: 13 additions & 0 deletions TestMod/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ configurations {
libImpl
}

repositories {
maven {
name = 'Ladysnake Mods'
url = 'https://maven.ladysnake.org/releases'
content {
includeGroup 'io.github.ladysnake'
includeGroup 'org.ladysnake'
includeGroupByRegex 'dev\\.onyxstudios.*'
}
}
}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
Expand All @@ -30,6 +42,7 @@ dependencies {
exclude group: "commons-io", module: "commons-io"
}
api(group: "de.javagl", name: "obj", version: "0.4.0")
modImplementation "io.github.ladysnake:satin:1.13.0"
// modImpl(files("rt-mods/render-explorer-1.0.0.jar"))

configurations.modImpl.dependencies.each {
Expand Down
168 changes: 168 additions & 0 deletions TestMod/src/main/java/me/x150/testmod/Animation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package me.x150.testmod;

import it.unimi.dsi.fastutil.doubles.Double2DoubleFunction;

import java.time.Duration;

public class Animation {
private float start;
private float end;
private long startTime;
private Duration dur;
private Easing easing = Easing.LINEAR;
private boolean forward = true;
public Animation(float start, float end, Duration duration) {
this.start = start;
this.end = end;
this.dur = duration;
this.startTime = System.nanoTime();
}

public Animation(float start, float end, Duration duration, Easing easing) {
this(start, end, duration);
this.easing = easing;
}

public void setStart(float start) {
this.start = start;
}

public void setEnd(float end) {
this.end = end;
}

public void setDuration(Duration dur) {
this.dur = dur;
}

public void reset() {
this.startTime = System.nanoTime();
}

public void setEasing(Easing easing) {
this.easing = easing;
}

public boolean isForward() {
return forward;
}

public void setForward(boolean forward) {
this.forward = forward;
}

public float get() {
long currentTime = System.nanoTime();
long delta = currentTime - startTime;
long nanoDuration = dur.toNanos();
float animDelta = (float) delta / nanoDuration;
animDelta = Math.max(0, Math.min(1, animDelta));
if (!forward) {
animDelta = 1 - animDelta;
}
animDelta = easing.apply(animDelta);
return start + (end - start) * animDelta;
}

public boolean isDone() {
return System.nanoTime() - startTime >= dur.toNanos();
}

enum Easing {
LINEAR(x -> x),
SINE_IN(x -> 1 - Math.cos(x * Math.PI / 2)),
SINE_OUT(x -> Math.sin(x * Math.PI / 2)),
SINE_IN_OUT(x -> -(Math.cos(Math.PI * x) - 1) / 2),

CUBIC_IN(x -> Math.pow(x, 3)),
CUBIC_OUT(x -> 1 - Math.pow(1 - x, 3)),
CUBIC_IN_OUT(x -> x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2),

QUINT_IN(x -> Math.pow(x, 5)),
QUINT_OUT(x -> 1 - Math.pow(1 - x, 5)),
QUINT_IN_OUT(x -> x < 0.5 ? 16 * x * x * x * x * x : 1 - Math.pow(-2 * x + 2, 5) / 2),

CIRC_IN(x -> 1 - Math.sqrt(1 - Math.pow(x, 2))),
CIRC_OUT(x -> Math.sqrt(1 - Math.pow(x - 1, 2))),
CIRC_IN_OUT(x -> x < 0.5 ? (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2 : (Math.sqrt(
1 - Math.pow(-2 * x + 2, 2)) + 1) / 2),

ELASTIC_IN(x -> {
double c4 = 2 * Math.PI / 3;

return x == 0 ? 0 : x == 1 ? 1 : -Math.pow(2, 10 * x - 10) * Math.sin((x * 10 - 10.75) * c4);
}),
ELASTIC_OUT(x -> {
double c4 = 2 * Math.PI / 3;

return x == 0 ? 0 : x == 1 ? 1 : Math.pow(2, -10 * x) * Math.sin((x * 10 - 0.75) * c4) + 1;
}),
ELASTIC_IN_OUT(x -> {
double c5 = 2 * Math.PI / 4.5;

double sin = Math.sin((20 * x - 11.125) * c5);
return x == 0 ? 0 : x == 1 ? 1 : x < 0.5 ? -(Math.pow(2, 20 * x - 10) * sin) / 2 : Math.pow(2,
-20 * x + 10) * sin / 2 + 1;
}),

QUAD_IN(x -> x * x),
QUAD_OUT(x -> 1 - (1 - x) * (1 - x)),
QUAD_IN_OUT(x -> x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2),

QUART_IN(x -> x * x * x * x),
QUART_OUT(x -> 1 - Math.pow(1 - x, 4)),
QUART_IN_OUT(x -> x < 0.5 ? 8 * x * x * x * x : 1 - Math.pow(-2 * x + 2, 4) / 2),

EXPO_IN(x -> x == 0 ? 0 : Math.pow(2, 10 * x - 10)),
EXPO_OUT(x -> x == 1 ? 1 : 1 - Math.pow(2, -10 * x)),
EXPO_IN_OUT(x -> x == 0 ? 0 : x == 1 ? 1 : x < 0.5 ? Math.pow(2, 20 * x - 10) / 2 : (2 - Math.pow(2,
-20 * x + 10)) / 2),

BACK_IN(x -> {
double c1 = 1.70158;
double c3 = c1 + 1;

return c3 * x * x * x - c1 * x * x;
}),
BACK_OUT(x -> {
double c1 = 1.70158;
double c3 = c1 + 1;

return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);
}),
BACK_IN_OUT(x -> {
double c1 = 1.70158;
double c2 = c1 * 1.525;

return x < 0.5 ? Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2) / 2 : (Math.pow(2 * x - 2,
2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
}),

BOUNCE_OUT(x -> {
double n1 = 7.5625;
double d1 = 2.75;

if (x < 1 / d1) {
return n1 * x * x;
} else if (x < 2 / d1) {
return n1 * (x -= 1.5 / d1) * x + 0.75;
} else if (x < 2.5 / d1) {
return n1 * (x -= 2.25 / d1) * x + 0.9375;
} else {
return n1 * (x -= 2.625 / d1) * x + 0.984375;
}
}),
BOUNCE_IN(x -> 1 - Easing.BOUNCE_OUT.apply(x)),
BOUNCE_IN_OUT(x -> x < 0.5 ? (1 - BOUNCE_OUT.apply(1 - 2 * x)) / 2 : (1 + BOUNCE_OUT.apply(2 * x - 1)) / 2);

final Double2DoubleFunction floatFunction;

Easing(Double2DoubleFunction function) {
floatFunction = function;
}

public float apply(double f) {
return (float) floatFunction.get(f);
}
}
}
72 changes: 42 additions & 30 deletions TestMod/src/main/java/me/x150/testmod/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,59 @@
import lombok.SneakyThrows;
import me.x150.renderer.font.FontRenderer;
import me.x150.renderer.objfile.ObjFile;
import me.x150.renderer.render.MSAAFramebuffer;
import me.x150.renderer.render.OutlineFramebuffer;
import me.x150.renderer.render.Renderer2d;
import me.x150.renderer.render.Renderer3d;
import me.x150.renderer.util.RendererUtils;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import org.joml.Matrix4f;
import org.lwjgl.opengl.GL30;

import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.nio.file.Path;

public class Handler {
static FontRenderer fr;
static FontRenderer fr1;
private static ObjFile ob;

static FontRenderer fr;
static FontRenderer fr1;
private static ObjFile ob;
@SneakyThrows
public static void world(MatrixStack stack) {
if (ob == null) {
ob = new ObjFile("untitled.obj",
ObjFile.ResourceProvider.ofPath(Path.of("/media/x150/stuff/Dev/Java/RenderLib2/run/")));
}
ob.draw(stack, new Matrix4f(), new Vec3d(0, 400, 0));
OutlineFramebuffer.useAndDraw(() -> Renderer3d.renderFilled(stack, Color.WHITE, new Vec3d(0, 300, 0), new Vec3d(5, 5, 5)), 1f, Color.GREEN, Color.BLACK);
}

@SneakyThrows
public static void world(MatrixStack stack) {
if (ob == null) {
ob = new ObjFile("untitled.obj", ObjFile.ResourceProvider.ofPath(Path.of("/media/x150/stuff/Dev/Java/RenderLib2/run/")));
}
OutlineFramebuffer.useAndDraw(() -> {
ob.draw(stack, new Matrix4f(), new Vec3d(0, 400, 0));
}, 1f, Color.RED, Color.BLUE);
OutlineFramebuffer.useAndDraw(() -> {
Renderer3d.renderFilled(stack, Color.WHITE, new Vec3d(0, 300, 0), new Vec3d(5, 5, 5));
}, 1f, Color.GREEN, Color.BLACK);
}

public static void hud(MatrixStack stack) {
if (fr == null) {
fr = new FontRenderer(new Font[] {
new Font("Ubuntu", Font.PLAIN, 8)
}, 9f);
fr1 = new FontRenderer(new Font[] {
new Font("Ubuntu", Font.BOLD, 8)
}, 9f*3);
}
}
public static void hud(DrawContext matrices) {
if (fr == null) {
fr = new FontRenderer(new Font[]{
new Font("Ubuntu", Font.PLAIN, 8)
}, 9f);
fr1 = new FontRenderer(new Font[]{
new Font("Ubuntu", Font.BOLD, 8)
}, 9f * 3);
}
MatrixStack fs = RendererUtils.getEmptyMatrixStack();
fs.push();
String n = """
This is a rendering library.
It supports TTF font rendering.
I can type äöü, it will render it.
It also supports newlines.
""".trim();
float stringWidth = fr.getStringWidth(n);
float stringHeight = fr.getStringHeight(n);
fs.multiply(RotationAxis.NEGATIVE_Z.rotationDegrees((System.currentTimeMillis() % 5000) / 5000f * 360f),
30 + (stringWidth + 5) / 2, 30 + (stringHeight + 5) / 2, 0);
Renderer2d.renderRoundedQuad(fs, Color.BLACK, 30 - 5, 30 - 5, 30 + stringWidth + 5, 30 + stringHeight + 5, 5,
5);
fr.drawString(fs, n, 30, 30, 1f, 1f, 1f, 1f);
fs.pop();
}
}
12 changes: 6 additions & 6 deletions TestMod/src/main/java/me/x150/testmod/TestMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import net.fabricmc.api.ModInitializer;

public class TestMod implements ModInitializer {
/**
* Runs the mod initializer.
*/
@Override
public void onInitialize() {
/**
* Runs the mod initializer.
*/
@Override
public void onInitialize() {

}
}
}
16 changes: 8 additions & 8 deletions TestMod/src/main/java/me/x150/testmod/client/TestModClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import net.fabricmc.api.ClientModInitializer;

public class TestModClient implements ClientModInitializer {
/**
* Runs the mod initializer on the client environment.
*/
@Override
public void onInitializeClient() {
RenderEvents.HUD.register(Handler::hud);
RenderEvents.WORLD.register(Handler::world);
}
/**
* Runs the mod initializer on the client environment.
*/
@Override
public void onInitializeClient() {
RenderEvents.HUD.register(Handler::hud);
RenderEvents.WORLD.register(Handler::world);
}
}
24 changes: 0 additions & 24 deletions TestMod/src/main/java/me/x150/testmod/mixin/ChatScreenMixin.java

This file was deleted.

2 changes: 1 addition & 1 deletion TestMod/src/main/resources/TestMod.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"mixins": [
],
"client": [
"ChatScreenMixin"

],
"injectors": {
"defaultRequire": 1
Expand Down
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ repositories {
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven {
name = 'Ladysnake Mods'
url = 'https://maven.ladysnake.org/releases'
content {
includeGroup 'io.github.ladysnake'
includeGroup 'org.ladysnake'
includeGroupByRegex 'dev\\.onyxstudios.*'
}
}
}

configurations {
Expand Down Expand Up @@ -97,6 +106,8 @@ dependencies {
}
api(group: "de.javagl", name: "obj", version: "0.4.0")

modImplementation "io.github.ladysnake:satin:1.13.0"

configurations.modImpl.dependencies.each {
modImplementation(it)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/x150/renderer/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.fabricmc.api.ModInitializer;

public class Renderer implements ModInitializer {
@Override
public void onInitialize() {
@Override
public void onInitialize() {

}
}
}
Loading

0 comments on commit e733e65

Please sign in to comment.