VRIKLODController.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.FinalIK
  5. {
  6. /// <summary>
  7. /// Simple VRIK LOD level controller based on renderer.isVisible and camera distance.
  8. /// </summary>
  9. public class VRIKLODController : MonoBehaviour
  10. {
  11. public Renderer LODRenderer;
  12. public float LODDistance = 15f;
  13. public bool allowCulled = true;
  14. private VRIK ik;
  15. void Start()
  16. {
  17. ik = GetComponent<VRIK>();
  18. }
  19. void Update()
  20. {
  21. ik.solver.LOD = GetLODLevel();
  22. }
  23. // Setting LOD level to 1 saves approximately 20% of solving time. LOD level 2 means IK is culled, with only root position and rotation updated if locomotion enabled.
  24. private int GetLODLevel()
  25. {
  26. if (allowCulled)
  27. {
  28. if (LODRenderer == null) return 0;
  29. if (!LODRenderer.isVisible) return 2;
  30. }
  31. // Set LOD to 1 if too far from camera
  32. float sqrMag = (ik.transform.position - Camera.main.transform.position).sqrMagnitude;
  33. if (sqrMag > LODDistance * LODDistance) return 1;
  34. return 0;
  35. }
  36. }
  37. }