LODGroup set transition by given distance through code

yellow there, working on a blender addon that lets the user create LODs based on the distance from viewport camera, but I can’t really figure out how the math behind the transition work in unity, I tried using a function like convert range to make it return the _x meter based on the transition given but it doesn’t work correctly, I also tried looking into UnityEngine.CoreModule dll assembly using dnSpy but no luck, so how does the transition math actually work O_o?

the convert range function I use sometimes:

public static float ConvertRange(float originalStart, float originalEnd, float newStart, float newEnd, float value)
{
       double scale = (float)(newEnd - newStart) / (originalEnd - originalStart);
       return (float)(newStart + ((value - originalStart) * scale));
}

a script I tried to generate a meter based on the given transition and object size ( doesn’t work correctly since the numbers gets larger as transition value nears zero):


public class Test : MonoBehaviour
{
    public float maxTransition = 99.9f;
    public float minTransition = 0.1f;


    public float default_objectSize = 1f;
    public float default_transitionValueForOneMeter = 87f;

    public float currentTransition;

    public float objectSize;



    public float outputMeter;

    // Start is called before the first frame update
    void Start()
    {
    }
    void Update()
    {
        // lowering - meter goes high
        // adding - meter goes low

        outputMeter = TestDefs.ConvertRange(default_transitionValueForOneMeter, 0, 1, 0, currentTransition);
    }
}

note: in the LODGroup component there is a ‘Set To Camera’ button which does exactly what I want, is it possible to execute this button through code?

EDIT: decompiled the unity editor assembly and managed to find the "Set To Camera’ button’s method, just borrowed some code (though I don’t think this is legally okay but still to check if things are working, please lemme know if this isn’t okay so I can immediately remove the code I posted)

the code decompiled:

public static float GetRelativeHeight(this LODGroup lodGroup, Camera camera)
    {
        float magnitude = (lodGroup.transform.TransformPoint(lodGroup.localReferencePoint) - camera.transform.position).magnitude;
        return DistanceToRelativeHeight(camera, magnitude, lodGroup.GetWorldSpaceSize()) * QualitySettings.lodBias;
    }
    public static float DistanceToRelativeHeight(Camera camera, float distance, float size)
    {
        bool orthographic = camera.orthographic;
        float result;
        if (orthographic)
        {
            result = size * 0.5f / camera.orthographicSize;
        }
        else
        {
            float num = Mathf.Tan(0.017453292f * camera.fieldOfView * 0.5f);
            float num2 = size * 0.5f / (distance * num);
            result = num2;
        }
        return result;
    }

    public static float GetWorldSpaceSize(this LODGroup lodGroup)
    {
        return GetWorldSpaceScale(lodGroup.transform) * lodGroup.size;
    }
    private static float GetWorldSpaceScale(Transform t)
    {
        Vector3 lossyScale = t.lossyScale;
        float a = Mathf.Abs(lossyScale.x);
        a = Mathf.Max(a, Mathf.Abs(lossyScale.y));
        return Mathf.Max(a, Mathf.Abs(lossyScale.z));
    }

the above code seems to set the transition (% Screen Size) based on the camera’s position, and it works but not sure if this is legal to borrow code directly from the unity editor’s .dll, is there a different approach to achieve the same result? :thinking: