Skip to content

Commit

Permalink
修复 RecipeReg 使用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Oct 25, 2024
1 parent bd866eb commit a9ad196
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kasuga.lib.core.client.model.anim_json;

import com.mojang.math.Vector3f;
import kasuga.lib.core.client.render.texture.Vec2f;
import net.minecraft.world.phys.Vec2;

public class CatmullRomUtils {
public static final float DEFAULT_TAU = 0.5f;
Expand Down Expand Up @@ -121,4 +123,41 @@ public static Vector3f applyCRS(Vector3f[] points, float u) {
result.add(v3);
return result;
}

public static Vec2f[] first3PointsToCRSPoints(Vec2f p0, Vec2f p1, Vec2f p2) {
return genDefaultCRSPoints(p0, p1, p2, p2.scale(2).subtract(p1));
}

public static Vec2f[] last3PointsToCRSPoints(Vec2f p1, Vec2f p2, Vec2f p3) {
return genDefaultCRSPoints(p1.scale(2).subtract(p2), p1, p2, p3);
}

public static Vec2f[] genDefaultCRSPoints
(Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3) {
return genCRSPoints(DEFAULT_TAU, p0, p1, p2, p3);
}

public static Vec2f[] genCRSPoints
(float tau, Vec2f p0, Vec2f p1, Vec2f p2, Vec2f p3) {
Vec2f c0 = p0;
Vec2f c1 = p0.scale(-tau)
.add(p2.scale(tau));
Vec2f c2 = p0.scale(2 * tau)
.add(p1.scale(tau - 3))
.add(p2.scale(3 - 2 * tau))
.add(p3.scale(-tau));
Vec2f c3 = p0.scale(-tau)
.add(p1.scale(2 - tau))
.add(p2.scale(tau - 2))
.add(p3.scale(tau));

return new Vec2f[]{c0, c1, c2, c3};
}

public static Vec2f applyCRS(Vec2f[] points, float u) {
return points[0]
.add(points[1].scale(u))
.add(points[2].scale(u * u))
.add(points[3].scale(u * u * u));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package kasuga.lib.core.client.render.curve;

import com.google.common.collect.Lists;
import kasuga.lib.core.client.model.anim_json.CatmullRomUtils;
import kasuga.lib.core.client.render.texture.Vec2f;
import kasuga.lib.core.util.data_type.Pair;

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

public class CatmullromCurveTemplate implements CurveTemplate {

private final List<Pair<Vec2f, Vec2f[]>> anchors;
private final List<Vec2f> points;
private float left, right, step;
public CatmullromCurveTemplate(float left, float right, float step,
List<Vec2f> anchors) {
this.anchors = new ArrayList<>(anchors.isEmpty() ? 0 : anchors.size() - 1);
this.points = Lists.newArrayList();
this.left = left;
this.right = right;
this.step = step;
boolean flag = compileAnchors(anchors, false);
if (flag) compileCurve(); else compileLine();
}

public boolean compileAnchors(List<Vec2f> anchors, boolean shouldClear) {
if (shouldClear) this.anchors.clear();
if (anchors.size() < 3) return false;
for (int i = 0; i < anchors.size() - 1; i++) {
Vec2f p1 = anchors.get(i);
Vec2f p2 = anchors.get(i + 1);
Vec2f[] controllers;
if (i == 0) {
Vec2f p3 = anchors.get(i + 2);
controllers = CatmullRomUtils.last3PointsToCRSPoints(p1, p2, p3);
} else if (i == anchors.size() - 2) {
Vec2f p0 = anchors.get(i - 1);
controllers = CatmullRomUtils.first3PointsToCRSPoints(p0, p1, p2);
} else {
Vec2f p0 = anchors.get(i - 1);
Vec2f p3 = anchors.get(i + 2);
controllers = CatmullRomUtils.genDefaultCRSPoints(p0, p1, p2, p3);
}
this.anchors.add(Pair.of(p1, controllers));
}
return true;
}

@Override
public List<Vec2f> getPointList() {
return points;
}

public void compileCurve() {
points.clear();
float length = right - left;
if (length * step < 0) return;
Vec2f p0, p1;
for (int i = 0; i < anchors.size() - 1; i++) {
p0 = anchors.get(i).getFirst();
p1 = anchors.get(i + 1).getFirst();
float l = Math.min(left, right);
float r = Math.max(left, right);
if (p0.x() < l && p1.x() < l) continue;
if (p0.x() > r && p1.x() > r) continue;
float pl = Math.min(p0.x(), p1.x());
float pr = Math.max(p0.x(), p1.x());
}
}

public void compileLine() {

}

@Override
public Pair<Float, Float> getRange() {
return Pair.of(left, right);
}

@Override
public void setLeft(float left) {
this.left = left;
}

@Override
public void setRight(float right) {
this.right = right;
}

@Override
public float getStep() {
return step;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kasuga.lib.core.client.render.curve;

import kasuga.lib.core.client.render.texture.Vec2f;
import kasuga.lib.core.util.data_type.Pair;

import java.util.List;

public interface CurveTemplate {
List<Vec2f> getPointList();
Pair<Float, Float> getRange();

default Float getLeft() {
return getRange().getFirst();
}

default Float getRight() {
return getRange().getSecond();
}
void setLeft(float left);
void setRight(float right);

float getStep();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package kasuga.lib.core.client.render.curve;

import com.google.common.collect.Lists;
import interpreter.Code;
import interpreter.compute.data.Namespace;
import interpreter.compute.infrastructure.Formula;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.render.texture.Vec2f;
import kasuga.lib.core.util.data_type.Pair;
import org.jetbrains.annotations.NotNull;

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

public class FunctionCurveTemplate implements CurveTemplate {
private final Namespace namespace;
private Formula formula;
private @NotNull String independentVar;
private float left, right, step;
private final List<Vec2f> cache;
public FunctionCurveTemplate(String function, float left, float right, float step) {
this.namespace = new Namespace(Code.root());
formula = namespace.decodeFormula(function);
this.left = left;
this.right = right;
this.step = step;
this.cache = Lists.newArrayList();
independentVar = "x";
}

public void setIndependentVar(@NotNull String independentVar) {
this.independentVar = independentVar;
}

public void setLeft(float left) {
this.left = left;
}

public void setRight(float right) {
this.right = right;
}

public void setStep(float step) {
this.step = step;
}

public void setFunction(String formula) {
this.formula = namespace.decodeFormula(formula);
}

@Override
public List<Vec2f> getPointList() {
return cache;
}

public void compile() {
cache.clear();
float length = right - left;
if (length * step < 0) {
KasugaLib.MAIN_LOGGER.error("Predictions are not likely to be fulfilled.");
return;
}
boolean flag = length < 0;
for (float i = left; flag ? (i > this.right) : (i < right); i += step) {
namespace.assign(independentVar, i);
float result = formula.getResult();
getPointList().add(new Vec2f(i, result));
}
}

@Override
public float getStep() {
return step;
}

@Override
public Pair<Float, Float> getRange() {
return Pair.of(left, right);
}
}
13 changes: 3 additions & 10 deletions src/main/java/kasuga/lib/registrations/common/RecipeReg.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @param <F> the class of your recipe serializer.
*/
public class RecipeReg<T extends Recipe<?>, F extends RecipeSerializer<?>> extends Reg {
public T recipe;
public F recipeSerializer;
public RegistryObject<RecipeType<T>> recipeRegistryObject = null;
public RegistryObject<F> recipeSerializerRegistryObject = null;
Expand All @@ -29,12 +28,11 @@ public RecipeReg(String registrationKey) {
/**
* Create a recipe reg.
* @param registrationKey The registration key of your recipe.
* @param recipe The recipe instance.
* @param serializer The recipe serializer instance.
*/
public RecipeReg(String registrationKey, Recipe<?> recipe, RecipeSerializer<?> serializer) {
public RecipeReg(String registrationKey, RecipeSerializer<?> serializer) {
this(registrationKey);
withRecipeAndSerializer(recipe, serializer);
withSerializer(serializer);
}

/**
Expand All @@ -45,9 +43,6 @@ public RecipeReg(String registrationKey, Recipe<?> recipe, RecipeSerializer<?> s
@Override
@Mandatory
public RecipeReg<T, F> submit(SimpleRegistry registry) {
if (recipe == null) {
crashOnNotPresent(Recipe.class, "recipe", "submit");
}
if (recipeSerializer == null) {
crashOnNotPresent(RecipeSerializer.class, "recipeSerializer", "submit");
}
Expand All @@ -67,9 +62,7 @@ public RecipeType<T> getRecipeType() {
return recipeRegistryObject == null ? null : recipeRegistryObject.get();
}

@Inner
private RecipeReg<T, F> withRecipeAndSerializer(Recipe<?> recipe, RecipeSerializer<?> serializer) {
this.recipe = (T) recipe;
public RecipeReg<T, F> withSerializer(RecipeSerializer<?> serializer) {
this.recipeSerializer = (F) serializer;
return this;
}
Expand Down

0 comments on commit a9ad196

Please sign in to comment.