我在使用LOD Group时遇到问题,因为我想知道我在屏幕上看到的当前活动LOD级别。我只能通过
访问百分比GameObject.GetComponent<LODGroup>().GetLODs()[size].screenRelativeTransitionHeight;
有人知道如何解决这个问题?提前谢谢。
答案 0 :(得分:3)
搜索答案answer.unity3d.com,我来到这里: http://answers.unity3d.com/questions/684467/find-the-lod-step-which-is-currently-used.html
LODGroup lodGroup = obj.GetComponent<LODGroup>();
if (lodGroup != null)
{
Transform lodTransform = lodGroup.transform;
foreach (Transform child in lodTransform)
{
var renderer = child.GetComponent<Renderer> ();
if (renderer != null && renderer.isVisible)
{
Debug.Log("This LODlevel is used: " + child.name);
}
}
}
您可以通过查看当前可见(在屏幕上可见)的儿童GameObjects Renderers的名称来找出当前处于活动状态的LOD级别。
答案 1 :(得分:0)
我遇到了同样的问题,最后找到了一个切实可行的解决方案。 (不幸的是,renderer.isVisible的更新频率不足以使其可靠,而且我不想在任何LOD子对象上添加其他组件。)
我在此处上传了解决方案:https://github.com/JulienHeijmans/EditorScripts/blob/master/Scripts/Utility/Editor/LODExtendedUtility.cs
我大部分是从这里获取的代码:https://github.com/Unity-Technologies/AutoLOD/blob/master/Scripts/Extensions/LODGroupExtensions.cs我删除了不必要的内容,并添加了我经常使用的其他实用程序功能。
它已被用作编辑器内的实用程序脚本,但是这里有获取当前可见的LOD级别的数学方法。
答案 2 :(得分:-1)
private static int GetLODCurShowLevel(Camera cam, LODGroup lodGroup)
{
//var inv_SceneViewCamHeight = 1.0f / (cam.pixelHeight - 6.0f);
var inv_SceneViewCamHeight = 1.0f / (cam.pixelHeight);
var lods = lodGroup.GetLODs();
for (int lodIDX = 0; lodIDX < lods.Length; lodIDX++)
{
var lod = lods[lodIDX];
var renderers = lod.renderers;
for (int renderIDX = 0; renderIDX < renderers.Length; renderIDX++)
{
var renderer = renderers[renderIDX];
// method1:
//var heightInScreen = Mathf.Abs(cam.WorldToScreenPoint(renderer.bounds.max).y - cam.WorldToScreenPoint(renderer.bounds.min).y);
// method2:
var heightInScreen = GetHeightInScreen(cam, renderer);
var ratioInScren = heightInScreen * inv_SceneViewCamHeight;
if (ratioInScren > lod.screenRelativeTransitionHeight)
{
return lodIDX;
}
}
}
return -1;
}
private static float GetHeightInScreen(Camera cam, Renderer renderer)
{
var min = renderer.bounds.min;
var max = renderer.bounds.max;
// F = Front
var FTL = new Vector3(min.x, max.y, min.z);
var FTR = new Vector3(max.x, max.y, min.z);
var FBR = new Vector3(max.x, min.y, min.z);
var FBL = new Vector3(min.x, min.y, min.z);
// Back
var BTL = new Vector3(min.x, max.y, max.z);
var BTR = new Vector3(max.x, max.y, max.z);
var BBR = new Vector3(max.x, min.y, max.z);
var BBL = new Vector3(min.x, min.y, max.z);
// to screen space pos
FTL = cam.WorldToScreenPoint(FTL);
FTR = cam.WorldToScreenPoint(FTR);
FBR = cam.WorldToScreenPoint(FBR);
FBL = cam.WorldToScreenPoint(FBL);
BTL = cam.WorldToScreenPoint(BTL);
BTR = cam.WorldToScreenPoint(BTR);
BBR = cam.WorldToScreenPoint(BBR);
BBL = cam.WorldToScreenPoint(BBL);
var maxY = FTL.y;
maxY = Mathf.Max(FTR.y, maxY);
maxY = Mathf.Max(FBR.y, maxY);
maxY = Mathf.Max(FBL.y, maxY);
maxY = Mathf.Max(BTL.y, maxY);
maxY = Mathf.Max(BTR.y, maxY);
maxY = Mathf.Max(BBR.y, maxY);
maxY = Mathf.Max(BBL.y, maxY);
var minY = FTL.y;
minY = Mathf.Min(FTR.y, minY);
minY = Mathf.Min(FBR.y, minY);
minY = Mathf.Min(FBL.y, minY);
minY = Mathf.Min(BTL.y, minY);
minY = Mathf.Min(BTR.y, minY);
minY = Mathf.Min(BBR.y, minY);
minY = Mathf.Min(BBL.y, minY);
return maxY - minY;
}
答案 3 :(得分:-1)
public class TestingStoreLODGroupCurLODLevel : MonoBehaviour
{
public bool[] lodVisibleArr;
public int GetCurLOD()
{
if (lodVisibleArr != null)
{
var len = lodVisibleArr.Length;
for (int i = 0; i < len; i++)
{
if (lodVisibleArr[i])
{
return i;
}
}
}
return -1;
}
}
public class TestingCheckVisibleChanged : MonoBehaviour
{
public int belongLOD;
public TestingStoreLODGroupCurLODLevel storeLOD;
private void OnBecameInvisible()
{
if (storeLOD)
{
storeLOD.lodVisibleArr[belongLOD] = false;
}
}
private void OnBecameVisible()
{
if (storeLOD)
{
storeLOD.lodVisibleArr[belongLOD] = true;
}
}
}
private void SetupCheckLODInfo(GameObject go)
{
var lodGroup = go.GetComponent<LODGroup>();
if (lodGroup == null)
{
return;
}
var storeLODComp = go.GetComponent<TestingStoreLODGroupCurLODLevel>();
if (storeLODComp != null)
{
return;
}
storeLODComp = go.AddComponent<TestingStoreLODGroupCurLODLevel>();
var lods = lodGroup.GetLODs();
for (int lodIDX = 0; lodIDX < lods.Length; lodIDX++)
{
var lod = lods[lodIDX];
var renderers = lod.renderers;
for (int rendererIDX = 0; rendererIDX < renderers.Length; rendererIDX++)
{
var renderer = renderers[rendererIDX];
var checkVisibleComp = renderer.gameObject.GetComponent<TestingCheckVisibleChanged>();
if (checkVisibleComp == null)
{
checkVisibleComp = renderer.gameObject.AddComponent<TestingCheckVisibleChanged>();
}
checkVisibleComp.belongLOD = lodIDX;
checkVisibleComp.storeLOD = storeLODComp;
}
}
storeLODComp.lodVisibleArr = new bool[lods.Length];
}
private void UnSetupCheckLODInfo(GameObject go)
{
var lodGroup = go.GetComponent<LODGroup>();
if (lodGroup == null)
{
return;
}
var storeLODComp = go.GetComponent<TestingStoreLODGroupCurLODLevel>();
if (storeLODComp != null)
{
GameObject.Destroy(storeLODComp);
}
var lods = lodGroup.GetLODs();
for (int lodIDX = 0; lodIDX < lods.Length; lodIDX++)
{
var lod = lods[lodIDX];
var renderers = lod.renderers;
for (int rendererIDX = 0; rendererIDX < renderers.Length; rendererIDX++)
{
var renderer = renderers[rendererIDX];
var checkVisibleComp = renderer.gameObject.GetComponent<TestingCheckVisibleChanged>();
if (checkVisibleComp != null)
{
GameObject.Destroy(checkVisibleComp);
}
}
}
}