Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Unity.Mathmatics;
- using UnityEngine;
- public class FABRIKTest : MonoBehaviour
- {
- void Awake() => camera = Camera.main;
- void Update()
- {
- float3 mousePosition = Input.mousePosition;
- mousePosition.z = -camera.transform.position.z;
- float3 worldMousePosition = new float3(camera.ScreenToWorldPoint(mousePosition));
- float3 screenCenter = camera.ViewportToWorldPoint(new float3(new float2(0.5f, 0.5f), -camera.transform.position.z));
- Debug.DrawLine(screenCenter + new float3(-0.1f, 0, 0), screenCenter + new float3(0.1f, 0, 0));
- Debug.DrawLine(screenCenter + new float3(0, -0.1f, 0), screenCenter + new float3(0, 0.1f, 0));
- if (Input.GetMouseButton(0))
- target = math.lerp(worldMousePosition.xy - screenCenter.xy, target, math.pow(2, -5 * Time.deltaTime));
- if (Input.GetMouseButton(1))
- {
- float3 newCameraPosition = camera.transform.position;
- newCameraPosition = math.lerp(worldMousePosition + new float3(0, 0, camera.transform.position.z), newCameraPosition, math.pow(2, -Time.deltaTime));
- camera.transform.position = newCameraPosition;
- }
- float2 root = segments[segments.Length - 1];
- for (int i = 0; segments.Length - 1 > i; i++)
- {
- float2 r = Reach(segments[i], segments[i + 1], target);
- segments[i] = target;
- target = r;
- }
- segments[segments.Length - 1] = target;
- target = root;
- for (int i = segments.Length - 1; i > 0; i--)
- {
- float2 r = Reach(segments[i], segments[i - 1], target);
- segments[i] = target;
- target = r;
- }
- segments[0] = target;
- for (int i = 0; segments.Length - 1 > i; i++)
- {
- solutions[i + 1] = screenCenter + new float3(segments[i], 0);
- solutions[i + 2] = screenCenter + new float3(segments[i + 1], 0);
- }
- solutions[0] = solutions[1];
- solutions[solutions.Length - 1] = solutions[solutions.Length - 2];
- for (int i = 0; solutions.Length - 1 > i; i++)
- Debug.DrawLine(solutions[i], solutions[i + 1]);
- }
- private float2 Reach(float2 head, float2 tail, float2 target)
- {
- float2 direction = tail - target;
- return target + (direction * math.length(tail - head) / math.length(direction));
- }
- float2[] segments = new float2[6] { new float2(5, 4), new float2(4, 3), new float2(3, 2), new float2(2, 1), new float2(1, 0), new float2(0, 0) };
- float3[] solutions = new float3[6 + 2];
- float2 target = float2.zero;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement