Calculate Screenspace Position from 3D Position in Java.

huangapple go评论86阅读模式
英文:

Calculate Screenspace Position from 3D Position in Java

问题

概述

你好,我正在尝试计算屏幕上路标的屏幕空间位置,但我无法弄清楚如何进行数学计算。

   private Vector2f calc(float partialTicks)
    {
        // 获取调整了 GUI 缩放的宽度和高度
        Window window = _minecraft.getWindow();
        int width = window.getGuiScaledWidth();
        int height = window.getGuiScaledHeight();
        
        // 计算屏幕中心
        double screenCenterX = width / 2.0;
        double screenCenterY = height / 2.0;
        
        // 从 Minecraft 获取玩家
        LocalPlayer player = _minecraft.player;
        assert player != null;
        
        // 玩家的朝向角度
        float pitch = player.getXRot();
        float yaw = player.getYRot();
        
        // 玩家的视野
        double fov = Math.toRadians(player.getFieldOfViewModifier() * 70f);
        float pitchRadians = (float) Math.toRadians(pitch);
        float yawRadians = (float) Math.toRadians(yaw);
        double verticalPixelPerRad = screenCenterY / fov;
        double horizontalPixelPerRad = screenCenterX / fov;
        
        
        // 玩家的位置
        double px = player.getX();
        double py = player.getY();
        double pz = player.getZ();
        
        // 路标的位置
        double wx = _warp.x();
        double wy = _warp.y();
        double wz = _warp.z();
        
        // 计算玩家位置和路标位置之间的差值
        double dx = wx - px;
        double dy = wy - py;
        double dz = wz - pz;

        // 计算玩家和路标之间的距离
        double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
        
        // TODO: 计算路标点相对于玩家位置和朝向的屏幕空间位置
        
        return new Vector2f(0,0);
        
    }

GitHub 仓库链接: https://github.com/DcmanProductions/The-Warp-Mod/blob/be5400678cfe3f05a8a0501ac9d2b9f3d04a4a63/common/src/main/java/chase/minecraft/architectury/warpmod/client/gui/waypoint/WaypointOverlay.java

英文:

Overview

Hi,I'm trying to calculate the screen space position of a waypoint on screen, I can't figure out the math to do so.

   private Vector2f calc(float partialTicks)
    {
        // Getting the gui scale adjusted width and height
        Window window = _minecraft.getWindow();
        int width = window.getGuiScaledWidth();
        int height = window.getGuiScaledHeight();
        
        // Calculating the center of the screen
        double screenCenterX = width / 2.0;
        double screenCenterY = height / 2.0;
        
        // Getting the player from minecraft
        LocalPlayer player = _minecraft.player;
        assert player != null;
        
        // The players looking angle
        float pitch = player.getXRot();
        float yaw = player.getYRot();
        
        // The players field of view
        double fov = Math.toRadians(player.getFieldOfViewModifier() * 70f);
        float pitchRadians = (float) Math.toRadians(pitch);
        float yawRadians = (float) Math.toRadians(yaw);
        double verticalPixelPerRad = screenCenterY / fov;
        double horizontalPixelPerRad = screenCenterX / fov;
        
        
        // The players position
        double px = player.getX();
        double py = player.getY();
        double pz = player.getZ();
        
        // The warps position
        double wx = _warp.x();
        double wy = _warp.y();
        double wz = _warp.z();
        
        // Getting the delta between the players position and the position of the warp
        double dx = wx - px;
        double dy = wy - py;
        double dz = wz - pz;

        // Calculate the distance between the player and the warp
        double distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
        
        // TODO: Calculate the screen space position of the warp point relative to the players position and rotation
        
        return new Vector2f(0,0);
        
    }

The github repo: https://github.com/DcmanProductions/The-Warp-Mod/blob/be5400678cfe3f05a8a0501ac9d2b9f3d04a4a63/common/src/main/java/chase/minecraft/architectury/warpmod/client/gui/waypoint/WaypointOverlay.java

答案1

得分: 0

I solved the issue


	private static final Minecraft client = Minecraft.getInstance();
	public static final Matrix4f lastProjMat = new Matrix4f();
	public static final Matrix4f lastModMat = new Matrix4f();
	public static final Matrix4f lastWorldSpaceMatrix = new Matrix4f();
	
	public static Vector3d worldSpaceToScreenSpace(Vector3d pos)
	{
		Camera camera = client.getEntityRenderDispatcher().camera;
		int displayHeight = client.getWindow().getHeight();
		int[] viewport = new int[4];
		GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
		Vector3f target = new Vector3f();
		double dx = pos.x - camera.getPosition().x;
		double dy = pos.y - camera.getPosition().y;
		double dz = pos.z - camera.getPosition().z;
		
		Vector4f transformedCoordinates = new Vector4f((float) dx, (float) dy, (float) dz, 1.f).mul(lastWorldSpaceMatrix);
		
		Matrix4f matrixProj = new Matrix4f(lastProjMat);
		Matrix4f matrixModel = new Matrix4f(lastModMat);
		matrixProj.mul(matrixModel).project(transformedCoordinates.x(), transformedCoordinates.y(), transformedCoordinates.z(), viewport, target);
		
		return new Vector3d(target.x / client.getWindow().getGuiScale(), (displayHeight - target.y) / client.getWindow().getGuiScale(), target.z);
	}
	
	public static boolean screenSpaceCoordinateIsVisible(Vector3d pos)
	{
		return pos != null && (pos.z > -1 & pos.z < 1);
	}

https://github.com/DcmanProductions/The-Warp-Mod/blob/1.19.4-architectury/common/src/main/java/chase/minecraft/architectury/warpmod/client/renderer/RenderUtils.java

英文:

So I solved the issue


	private static final Minecraft client = Minecraft.getInstance();
	public static final Matrix4f lastProjMat = new Matrix4f();
	public static final Matrix4f lastModMat = new Matrix4f();
	public static final Matrix4f lastWorldSpaceMatrix = new Matrix4f();
	
	public static Vector3d worldSpaceToScreenSpace(Vector3d pos)
	{
		Camera camera = client.getEntityRenderDispatcher().camera;
		int displayHeight = client.getWindow().getHeight();
		int[] viewport = new int[4];
		GL11.glGetIntegerv(GL11.GL_VIEWPORT, viewport);
		Vector3f target = new Vector3f();
		double dx = pos.x - camera.getPosition().x;
		double dy = pos.y - camera.getPosition().y;
		double dz = pos.z - camera.getPosition().z;
		
		Vector4f transformedCoordinates = new Vector4f((float) dx, (float) dy, (float) dz, 1.f).mul(lastWorldSpaceMatrix);
		
		Matrix4f matrixProj = new Matrix4f(lastProjMat);
		Matrix4f matrixModel = new Matrix4f(lastModMat);
		matrixProj.mul(matrixModel).project(transformedCoordinates.x(), transformedCoordinates.y(), transformedCoordinates.z(), viewport, target);
		
		return new Vector3d(target.x / client.getWindow().getGuiScale(), (displayHeight - target.y) / client.getWindow().getGuiScale(), target.z);
	}
	
	public static boolean screenSpaceCoordinateIsVisible(Vector3d pos)
	{
		return pos != null && (pos.z > -1 && pos.z < 1);
	}

https://github.com/DcmanProductions/The-Warp-Mod/blob/1.19.4-architectury/common/src/main/java/chase/minecraft/architectury/warpmod/client/renderer/RenderUtils.java

huangapple
  • 本文由 发表于 2023年4月10日 21:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定