Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Vector3 Raycast2DHitCoords(Camera cam, Vector2 screenCoord, float raycastToDists = 1000f)
- {
- Vector3 camPos = cam.Position;
- Vector3 camRot = cam.Rotation;
- float raycastToDist = raycastToDists;
- const float raycastFromDist = 1.0f;
- var target3D = ScreenRelToWorld(camPos, camRot, screenCoord);
- var source3D = camPos;
- var dir = (target3D - source3D);
- dir.Normalize();
- var raycastResults = World.Raycast(source3D + dir * raycastFromDist,
- source3D + dir * raycastToDist, IntersectOptions.Everything);
- return raycastResults.HitCoords;
- }
- private Vector3 ScreenRelToWorld(Vector3 camPos, Vector3 camRot, Vector2 coord)
- {
- var camForward = RotationToDirection(camRot);
- var rotUp = camRot + new Vector3(10, 0, 0);
- var rotDown = camRot + new Vector3(-10, 0, 0);
- var rotLeft = camRot + new Vector3(0, 0, -10);
- var rotRight = camRot + new Vector3(0, 0, 10);
- var camRight = RotationToDirection(rotRight) - RotationToDirection(rotLeft);
- var camUp = RotationToDirection(rotUp) - RotationToDirection(rotDown);
- var rollRad = -DegToRad(camRot.Y);
- var camRightRoll = camRight * (float)Math.Cos(rollRad) - camUp * (float)Math.Sin(rollRad);
- var camUpRoll = camRight * (float)Math.Sin(rollRad) + camUp * (float)Math.Cos(rollRad);
- var point3D = camPos + camForward * 10.0f + camRightRoll + camUpRoll;
- Vector2 point2D;
- if (!WorldToScreenRel(point3D, out point2D)) return camPos + camForward * 10.0f;
- var point3DZero = camPos + camForward * 10.0f;
- Vector2 point2DZero;
- if (!WorldToScreenRel(point3DZero, out point2DZero)) return camPos + camForward * 10.0f;
- const double eps = 0.001;
- if (Math.Abs(point2D.X - point2DZero.X) < eps || Math.Abs(point2D.Y - point2DZero.Y) < eps) return camPos + camForward * 10.0f;
- var scaleX = (coord.X - point2DZero.X) / (point2D.X - point2DZero.X);
- var scaleY = (coord.Y - point2DZero.Y) / (point2D.Y - point2DZero.Y);
- var point3Dret = camPos + camForward * 10.0f + camRightRoll * scaleX + camUpRoll * scaleY;
- return point3Dret;
- }
- private bool WorldToScreenRel(Vector3 worldCoords, out Vector2 screenCoords)
- {
- OutputArgument num1 = new OutputArgument();
- OutputArgument num2 = new OutputArgument();
- if (!Function.Call<bool>(Hash._WORLD3D_TO_SCREEN2D, worldCoords.X, worldCoords.Y, worldCoords.Z, num1, num2))
- {
- screenCoords = new Vector2();
- return false;
- }
- screenCoords = new Vector2((num1.GetResult<float>() - 0.5f) * 2, (num2.GetResult<float>() - 0.5f) * 2);
- return true;
- }
- private double DegToRad(double deg)
- {
- return deg * Math.PI / 180.0;
- }
- private double RadToDeg(double deg)
- {
- return deg * 180.0 / Math.PI;
- }
- private Vector3 RotationToDirection(Vector3 rotation)
- {
- var z = DegToRad(rotation.Z);
- var x = DegToRad(rotation.X);
- var num = Math.Abs(Math.Cos(x));
- return new Vector3
- {
- X = (float)(-Math.Sin(z) * num),
- Y = (float)(Math.Cos(z) * num),
- Z = (float)Math.Sin(x)
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement