Skip to content

Commit

Permalink
fix: ray test
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Dec 3, 2024
1 parent 6142cfb commit e54f25c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void playerLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!Envs.isDevEnvironment()) return;
Panel.test = new PanelRenderer(player.getForward(), Vec3.ZERO);
// TODO: deal with this
// KasugaLibClient.PANEL_RENDERERS.add(Panel.test);
KasugaLibClient.PANEL_RENDERERS.add(Panel.test);
}

@SubscribeEvent
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/kasuga/lib/core/util/projectile/CameraTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package kasuga.lib.core.util.projectile;

import com.mojang.math.Vector3f;
import lombok.Getter;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.world.phys.Vec3;

import static kasuga.lib.core.util.projectile.PanelRenderer.BASE_OFFSET_2;

@Getter
public class CameraTracker {

private final Camera camera;
private final Gui gui;

public CameraTracker(Gui gui, Camera camera) {
this.camera = camera;
this.gui = gui;
}

public Ray test(float mouseX, float mouseY) {
Vec3 vec = camera.getPosition();
Vec3 forward = new Vec3(camera.getLookVector()).add(BASE_OFFSET_2);
return new Ray(vec, forward);
}


}
66 changes: 48 additions & 18 deletions src/main/java/kasuga/lib/core/util/projectile/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import kasuga.lib.core.client.render.texture.Vec2f;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;

import javax.annotation.Nullable;
import java.util.function.Function;
Expand Down Expand Up @@ -46,6 +48,10 @@ public void setO(Vector3f o) {
this.o = o;
}

public Vector3f getO() {
return o;
}

public Grid copy() {
return new Grid(this.panel, this.o, this.xAxis, this.yAxis, xAxis2d, yAxis2d);
}
Expand Down Expand Up @@ -73,20 +79,31 @@ public Vec2f get(Vector3f vec) {
Function<Float, Float> mapper =
angle -> angle > pi ? 2 * pi - angle : angle;

float angleX = mapper.apply(xAxis2d.getRotation());
float angleY = mapper.apply(yAxis2d.getRotation());
float angleO = mapper.apply(offset2d.getRotation());
// use the law of sine.
// vector a, b and c; ua + vb = c;
float sina = vecSine(vec, yAxis);
float sinb = vecSine(vec, xAxis);
float sinc = vecSine(xAxis, yAxis);

// offset2d = u * xAxis2d + v * yAxis2d
// use law of sines.
float angleXY = pi - angleX - angleY;
float angleXO = angleO + angleX;
float angleYO = pi - angleXY - angleXO;
float k = offset2d.length() / (float) Math.sin(angleXY);
float u = k * (float) Math.sin(angleYO);
float v = k * (float) Math.sin(angleXO);
float cLen = len(vec);
float bLen = cLen / sinc * sinb;
float aLen = cLen / sinc * sina;
return new Vec2f(aLen / this.xAxis2d.length(), bLen / this.yAxis2d.length());
}

return new Vec2f(u, v);
public float vecSine(Vector3f vec1, Vector3f vec2) {
Vector3f a = vec1.copy();
a.normalize();
Vector3f b = vec2.copy();
b.normalize();
a.cross(b);
return len(a);
}

public float len(Vector3f vector3f) {
return (float) Math.sqrt(vector3f.x() * vector3f.x() +
vector3f.y() * vector3f.y() +
vector3f.z() * vector3f.z());
}

public void flex(float xScale, float yScale) {
Expand Down Expand Up @@ -114,7 +131,7 @@ public void rotDeg(float deg) {

public Ray getNormalRay(float x, float y) {
Vector3f source = get(x, y);
return new Ray(new Vector3f(panel.normal), source);
return new Ray(source, new Vector3f(panel.normal));
}

public Ray getNormalRay(Vec2f pos) {
Expand All @@ -127,11 +144,24 @@ public Ray getNormalRay(Vec2f pos) {
return get(hitPoint);
}

@Override
public String toString() {
return "Grid<\n " + panel + ", \n " +
o + ", \n " + xAxis2d + ", " + yAxis2d + ",\n "
+ xAxis + ", " + yAxis + "\n>";
}

public static void main(String[] args) {
Vec2f vec2f = new Vec2f(1, 0);
for (int i = 0; i < 24; i++) {
vec2f = vec2f.rotateDeg(Vec2f.ZERO, 15);
System.out.println(vec2f.toString() + " deg:" + (vec2f.getRotation() * 180f / (float) Math.PI));
}
Vec3 normal = new Vec3(1, 1, 1);
Panel panel = new Panel(Vec3.ZERO, normal);
Vec2f xo2f = new Vec2f(1, 0);
Vec2f yo2f = new Vec2f(0, 1);
Grid grid = new Grid(panel, Vector3f.ZERO, xo2f, yo2f);
grid.rotDeg(80);
Vector3f vector3f = grid.get(new Vec2f(0.5f, 0.5f));
Vec2f vec2f = grid.get(vector3f);
System.out.println(grid);
System.out.println(vector3f);
System.out.println(vec2f);
}
}
8 changes: 6 additions & 2 deletions src/main/java/kasuga/lib/core/util/projectile/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Panel {
@Setter
public Vec3 normal;

public final double constant;
public double constant;

public Panel(Panel panel) {
this.normal = panel.normal;
Expand Down Expand Up @@ -55,10 +55,14 @@ public Panel copy() {
return new Panel(this);
}

public Panel moveTo(Vec3 point) {
public Panel moveAndCopy(Vec3 point) {
return new Panel(this.normal, point);
}

public void moveTo(Vec3 point) {
this.constant = point.dot(normal);
}

public Panel offset(Vec3 offset) {
double offsetConst = offset.dot(normal);
return new Panel(this.normal.add(offset), this.constant + offsetConst);
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/kasuga/lib/core/util/projectile/PanelRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PanelRenderer {
private final Panel panel;
private final Grid grid;
private final Vec3 pos;
private final CameraTracker tracker;

private static final LazyRecomputable<AnimModel> panelModel = LazyRecomputable.of(
() -> {
Expand Down Expand Up @@ -64,26 +65,28 @@ public PanelRenderer(Vec3 normal, Vec3 pos) {
grid = new Grid(panel, Vector3f.ZERO);
this.pos = pos;
this.rays = new ArrayList<>();
tracker = new CameraTracker(Minecraft.getInstance().gui, Minecraft.getInstance().gameRenderer.getMainCamera());
}

public PanelRenderer(Panel panel, Vec3 pos, Grid grid, ArrayList<Ray> rays) {
this.panel = panel;
this.grid = grid;
this.pos = pos;
this.rays = rays;
tracker = new CameraTracker(Minecraft.getInstance().gui, Minecraft.getInstance().gameRenderer.getMainCamera());
}

public void render(PoseStack pose, MultiBufferSource buffer, int light, int overlay, float partial) {
Camera camera = Minecraft.getInstance().gameRenderer.getMainCamera();
Vector3f lookVec = camera.getLookVector();
panel.setNormal(new Vec3(lookVec));
Vec3 position = camera.getPosition().add(BASE_OFFSET_2);
panel.moveTo(position);
// panel.moveTo(position);
rays.add(grid.getNormalRay(testRayPos));
grid.setO(new Vector3f(position));
Quaternion quaternion = panel.getQuaternion();
pose.pushPose();
pose.translate(position.x(), position.y(), position.z());
// pose.translate(position.x(), position.y(), position.z());
pose.mulPose(quaternion);
pose.translate(-BASE_OFFSET.x(), BASE_OFFSET.y(), -BASE_OFFSET.z());
if (!panel.valid())
Expand All @@ -109,19 +112,6 @@ public void render(PoseStack pose, MultiBufferSource buffer, int light, int over
arrow2Model.get().render(pose, buffer, light, overlay);

pose.popPose();
rays.forEach(
ray -> {
pose.pushPose();
Vec3 rayPos = new Vec3(ray.getSource()).add(BASE_OFFSET_2);
pose.translate(rayPos.x(), rayPos.y(), rayPos.z());
Quaternion q = Panel.getQuaternion(ray.getForward());
pose.translate(-BASE_OFFSET.x(), BASE_OFFSET.y(), -BASE_OFFSET.z());
pose.mulPose(q);
arrowModel.get().resetAnimation();
arrowModel.get().render(pose, buffer, light, overlay);
pose.popPose();
}
);
rays.clear();
}
}
40 changes: 40 additions & 0 deletions src/main/java/kasuga/lib/core/util/projectile/RayRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package kasuga.lib.core.util.projectile;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.model.AnimModelLoader;
import kasuga.lib.core.client.model.anim_model.AnimModel;
import kasuga.lib.core.util.LazyRecomputable;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.resources.ResourceLocation;

import static kasuga.lib.core.util.projectile.PanelRenderer.BASE_OFFSET;

public class RayRenderer {

private static final LazyRecomputable<AnimModel> arrowModel = LazyRecomputable.of(
() -> {
AnimModel model = AnimModelLoader.INSTANCE.getModel(new ResourceLocation(KasugaLib.MOD_ID, "arrow"));
model.init();
return model;
}
);

private final Ray ray;
public RayRenderer(Ray ray) {
this.ray = ray;
}

public void render(PoseStack pose, MultiBufferSource buffer, int light, int overlay) {
pose.pushPose();
Vector3f source = ray.getSource();
pose.translate(source.x(), source.y(), source.z());
Quaternion quaternion = Panel.getQuaternion(ray.getForward());
pose.mulPose(quaternion);
pose.translate(-BASE_OFFSET.x(), BASE_OFFSET.y(), -BASE_OFFSET.z());
arrowModel.get().render(pose, buffer, light, overlay);
pose.popPose();
}
}

0 comments on commit e54f25c

Please sign in to comment.