Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Remake animations using shaders #22

Open
wants to merge 2 commits into
base: 1.19.2
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ public ConfigScreenFactory<?> getModConfigScreenFactory() {
}

public AnimatedChunks() {
var eases = new Manager<>(new Descriptor<Ease>(x -> 1, "default")
var eases = new Manager<>(new Descriptor<Ease>(() -> "t = 1;", "default")
.author("TopchetoEU")
.description("Ends the animation as soon as it has started.")
.displayName("No animation")
);
var animations = new Manager<>(new Descriptor<Animation>((a, b, c, d, e, f, g, h) -> {}, "default")
var animations = new Manager<>(new Descriptor<Animation>(() -> ";", "default")
.author("TopchetoEU")
.description("Does nothing.")
.displayName("No animation")
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/me/topchetoeu/animatedchunks/StatementFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.topchetoeu.animatedchunks;

import java.util.Map;

public interface StatementFactory {
/**
* Returns a GLSL statement, with access to at least the following variables:
* <ul>
* <li>playerPos - vec3, the position of the player</li>
* <li>chunkPos - vec3, the position of the current chunk</li>
* <li>pos - vec3, the position of the current vertex</li>
* <li>x - the raw stage of the animation, from 0 to 1</li>
* <li>t - the eased stage of the animation, from 0 to 1</li>
* <li>tmp0 - tmp7 - temporary float variables</li>
* <li>tmp8 - tmp11 - temporary vec3 variables</li>
* <li>tmp12 - tmp15 - temporary vec4 variables</li>
* </ul>
*/
String statement();
/**
* Returns all uniforms that are used by the statement
*/
public default Map<String, Float> uniforms() {
return Map.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
package me.topchetoeu.animatedchunks.animation;

import net.minecraft.client.util.math.MatrixStack;
import me.topchetoeu.animatedchunks.StatementFactory;

public interface Animation {
/**
* Animations using the currently set ease
* @param progress The point at which the animation currently is (a value between 0 and 1)
* @param matrices The matrix stack used for rendering
* @param chunkX The current chunk's x (in blocks) which is animated
* @param chunkY The current chunk's y (in blocks) which is animated
* @param chunkZ The current chunk's z (in blocks) which is animated
* @param playerX The player's x (in blocks)
* @param playerY The player's y (in blocks)
* @param playerZ The player's z (in blocks)
*/
void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ);
}
public interface Animation extends StatementFactory { }
32 changes: 0 additions & 32 deletions src/main/java/me/topchetoeu/animatedchunks/animation/Animator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

import me.topchetoeu.animatedchunks.Manager;
import me.topchetoeu.animatedchunks.easing.Ease;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3d;
import net.minecraft.util.math.BlockPos;

public final class Animator {
public static class ChunkLoc {
Expand Down Expand Up @@ -152,33 +149,4 @@ public float getChunkProgress(int x, int y, int z) {
if (!chunkToStage.containsKey(new ChunkLoc(x, y, z))) return 1f;
return chunkToStage.get(new ChunkLoc(x, y, z));
}

public void animate(MatrixStack matrices, float progress, BlockPos chunkPos, Vector3d playerPos) {
if (progress < 0) progress = 0;
if (progress > 1) progress = 1;
if (progress < 0.999) {
float _progress = EASES.getValue().ease(progress);
ANIMATIONS.getValue().animate(
_progress, matrices,
chunkPos.getX() * 16, chunkPos.getY() * 16, chunkPos.getZ() * 16,
(float)playerPos.x, (float)playerPos.y, (float)playerPos.z
);
// matrices.translate(0, 0, 16);
}
}
public void animate(MatrixStack matrices, BlockPos chunkPos, Vector3d playerPos) {
if (!isChunkLoaded(chunkPos.getX(), chunkPos.getY(), chunkPos.getZ())) {
matrices.scale(0, 0, 0);
}
else {
animate(matrices, getChunkProgress(chunkPos.getX(), chunkPos.getY(), chunkPos.getZ()), chunkPos, playerPos);
}
}

public void animate(MatrixStack matrices, BlockPos chunkPos) {
animate(matrices, chunkPos, new Vector3d(0, 0, 0));
}
public void animate(MatrixStack matrices, float progress, BlockPos chunkPos) {
animate(matrices, progress, chunkPos, new Vector3d(0, 0, 0));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.topchetoeu.animatedchunks.animation;

import net.minecraft.client.util.math.MatrixStack;
import java.util.Map;

public class FallAnimation implements Animation {
private float offset;
Expand All @@ -12,12 +12,11 @@ public void setOffset(float offset) {
this.offset = offset;
}

@Override
public void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ) {
animate(progress, matrices);
public Map<String, Float> uniforms() {
return Map.of("animation_f", offset);
}
public void animate(float progress, MatrixStack matrices) {
matrices.translate(0, offset * (1 - progress), 0);
public String statement() {
return "pos += vec3(0, animation_f * (1 - t));";
}

public FallAnimation(float offset) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.topchetoeu.animatedchunks.animation;

import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.Vec2f;
import java.util.Map;

public class FlyInAnimation implements Animation {
private float offset;
Expand All @@ -13,14 +12,22 @@ public void setOffset(float offset) {
this.offset = offset;
}


public Map<String, Float> uniforms() {
return Map.of("animation_f", offset);
}
@Override
public void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ) {
Vec2f direction = new Vec2f(playerX, playerZ).add(new Vec2f(-chunkX, -chunkZ)).normalize().multiply(-offset);

matrices.translate(direction.x * (1 - progress), 0, direction.y * (1 - progress));
public String statement() {
return "tmp8 = normalize(chunkPos.xz - playerPos.xz) * animation_f; tmp8 *= (1 - t); pos += tmp8;";
}


// @Override
// public void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ) {
// Vec2f direction = new Vec2f(playerX, playerZ).add(new Vec2f(-chunkX, -chunkZ)).normalize().multiply(-offset);

// matrices.translate(direction.x * (1 - progress), 0, direction.y * (1 - progress));
// }

public FlyInAnimation() {
offset = 50;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.topchetoeu.animatedchunks.animation;

import net.minecraft.client.util.math.MatrixStack;
import java.util.Map;

public final class RiseAnimation implements Animation {
private float offset;
Expand All @@ -12,12 +12,11 @@ public void setOffset(float offset) {
this.offset = offset;
}

@Override
public void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ) {
animate(progress, matrices);
public Map<String, Float> uniforms() {
return Map.of("animation_f", offset);
}
public void animate(float progress, MatrixStack matrices) {
matrices.translate(0, offset * (progress - 1), 0);
public String statement() {
return "pos += vec3(0, animation_f * (t - 1));";
}

public RiseAnimation(float offset) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package me.topchetoeu.animatedchunks.animation;

import net.minecraft.client.util.math.MatrixStack;
import java.util.Map;

public class ScaleAnimation implements Animation {
public static float xOffset = 8, yOffset = 8, zOffset = 8;

private boolean scaleY = true;
private float xOffset = 8, yOffset = 8, zOffset = 8;

public boolean isScalingY() {
return scaleY;
Expand All @@ -13,38 +14,31 @@ public void setScalingY(boolean scaleY) {
this.scaleY = scaleY;
}

public float getXOffset() {
return xOffset;
}
public void setXOffset(float xOffset) {
this.xOffset = xOffset;
}

public float getYOffset() {
return yOffset;
}
public void setYOffset(float yOffset) {
this.yOffset = yOffset;
}

public float getZOffset() {
return zOffset;
}
public void setZOffset(float zOffset) {
this.zOffset = zOffset;
}

@Override
public void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ) {
float scaleX = progress;
float scaleZ = progress;

matrices.translate(xOffset, yOffset, zOffset);
matrices.scale(scaleX, 1, scaleZ);
matrices.translate(-xOffset, -yOffset, -zOffset);
public Map<String, Float> uniforms() {
return Map.of("animation_ox", xOffset, "animation_oy", yOffset, "animation_oz", zOffset, "animation_sy", scaleY ? 1f : 0f);
}

public ScaleAnimation() {

}
@Override
public String statement() {
return
"tmp8 = vec3(animation_ox, animation_oy, animation_oz);" +
"tmp9 = vec3(t);" +
"if (animation_sy < .5f) tmp9.y = 0;" +
"pos += tmp8;" +
"pos *= tmp9;" +
"pos -= tmp8;";
}

// @Override
// public void animate(float progress, MatrixStack matrices, int chunkX, int chunkY, int chunkZ, float playerX, float playerY, float playerZ) {
// float scaleX = progress;
// float scaleZ = progress;

// matrices.translate(xOffset, yOffset, zOffset);
// matrices.scale(scaleX, 1, scaleZ);
// matrices.translate(-xOffset, -yOffset, -zOffset);
// }

public ScaleAnimation() { }
}
36 changes: 16 additions & 20 deletions src/main/java/me/topchetoeu/animatedchunks/easing/Ease.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
package me.topchetoeu.animatedchunks.easing;

public interface Ease {
/**
* Converts the linear progress of an animation to an eased progress
* @param x The progress of the animation being eased
* @return The new, eased progress
*/
float ease(float x);
import me.topchetoeu.animatedchunks.StatementFactory;

public interface Ease extends StatementFactory {
/**
* Converts a function to an ease out version of itself.
* Converts a ease-in statement to an ease-out statement
* Mathematically, the function is being "rotated" 180 degrees
* @param func The function to convert
* @return The ease out version of the function
* @param func The ease statement to convert
*/
public static Ease easeOut(Ease func) {
return x -> 1 - func.ease(1 - x);
return () -> "x = 1 - x; " + func.statement() + "x = 1 - x; t = 1 - t;";
// return x -> 1 - func.ease(1 - x);
}
/**
* Converts a function to an ease in-out version of itself.
* Mathematically, the function is being split into two, where in the interval [0; 0.5], the ease-out function is being used, and in the other interval [0.5; 1], the ease-in function is being used
* @param func The function to convert
* @return The ease in-out version of the function
* Converts a ease statement to an ease in-out statement
* Mathematically, the function is being split into two, where in the interval [0; 0.5), the ease-in function is being used, and in the other interval [0.5; 1], the ease-out function is being used
* @param func The ease statement to convert
* @return The ease in-out statement
*/
public static Ease easeInOut(Ease func) {
return x -> {
float x2 = 2 * x;
return () -> "x *= 2; if (x < 1f) { " + easeOut(func) + "} else { x -= 1; " + func.statement() + " } x /= 2;";
// return x -> {
// float x2 = 2 * x;

if (x < 0.5f) return (1 - func.ease(1 - x2)) / 2;
else return (1 + func.ease(x2 - 1)) / 2;
};
// if (x < 0.5f) return (1 - func.ease(1 - x2)) / 2;
// else return (1 + func.ease(x2 - 1)) / 2;
// };
}
}
20 changes: 16 additions & 4 deletions src/main/java/me/topchetoeu/animatedchunks/easing/ElasticEase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.topchetoeu.animatedchunks.easing;

import java.util.Map;

public class ElasticEase implements Ease {
private float steepness = 1;
private int periods = 3;
Expand All @@ -19,11 +21,21 @@ public void setSteepness(float steepness) {
}

@Override
public float ease(float x) {
float amplitude = (float)Math.pow(2, -steepness * x) * (1 - x);
float wave = (float)Math.sin(2 * Math.PI * periods * x - Math.PI / 2);
public Map<String, Float> uniforms() {
return Map.of("ease_s", steepness, "ease_p", (float)periods);
}

@Override
public String statement() {
return
"tmp0 = pow(2, -ease_s * x) * (1 - x);" +
"tmp1 = sin(6.28 * ease_p * x - 1.57);" +
"t = tmp0 * tmp1 + 1;";

// float amplitude = (float)Math.pow(2, -steepness * x) * (1 - x);
// float wave = (float)Math.sin(2 * Math.PI * periods * x - Math.PI / 2);

return amplitude * wave + 1;
// return amplitude * wave + 1;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.topchetoeu.animatedchunks.easing;

public class LinearEase implements Ease {
public float ease(float x) {
return x;
public String statement() {
return "t = x;";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.animatedchunks.easing;

public class QuadraticEase implements Ease {
@Override
public float ease(float x) {
return x * x;
public String statement() {
return "t = x * x;";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package me.topchetoeu.animatedchunks.easing;

public class SineEase implements Ease {
@Override
public float ease(float x) {
return (float)Math.sin(x * Math.PI / 2);
public String statement() {
return "t = sin(x * 1.57);";
}
}
Loading