public static float RelativeHeightToDistance(Camera camera, float relativeHeight, LODGroup group) { float size = group.GetWorldSpaceSize(); if (camera.orthographic) { return(0); } var halfAngle = Mathf.Tan(Mathf.Deg2Rad * camera.fieldOfView * 0.5F); var distance = size * 0.5F / (relativeHeight * halfAngle); return(distance); }
public static float DistanceToRelativeHeight(Camera camera, float distance, LODGroup group) { float size = group.GetWorldSpaceSize(); if (camera.orthographic) { return(size * 0.5F / camera.orthographicSize); } var halfAngle = Mathf.Tan(Mathf.Deg2Rad * camera.fieldOfView * 0.5F); var relativeHeight = size * 0.5F / (distance * halfAngle); return(relativeHeight); }
private async Task <ForgelightActor> LoadNewActor(Adr actorDefinition) { await new WaitForBackgroundThread(); ForgelightActor actor; // Attempt to find the model's Dme. Dme modelDef = GetActorDme(actorDefinition); if (modelDef == null) { actor = CreateMissingPlaceholder(actorDefinition.Name); } else { // Deserialization MeshData meshData; try { meshData = actorMeshFactory.GenerateMeshData(modelDef); } catch (Exception e) { Debug.LogError($"Could not create mesh {modelDef.Name}. {e}"); await new WaitForUpdate(); return(CreateMissingPlaceholder(actorDefinition.Name)); } await new WaitForUpdate(); UnityEngine.Mesh mesh = actorMeshFactory.CreateMeshFromData(modelDef.Name, meshData); Material[] materials = materialFactory.GetActorMaterials(modelDef); assetManager.Dispose(modelDef); Assert.IsNotNull(mesh); Assert.IsNotNull(materials); // GameObject "Prefab" GameObject actorSource = new GameObject(); actor = actorSource.AddComponent <ForgelightActor>(); MeshFilter meshFilter = actorSource.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = actorSource.AddComponent <MeshRenderer>(); // Assign deserialized data meshFilter.sharedMesh = mesh; meshRenderer.sharedMaterials = materials; // LODs if (actorDefinition.Lods.Count > 0) { float cullDist = actorDefinition.Lods.Max(lod => lod.Distance); LODGroup lodGroup = actorSource.AddComponent <LODGroup>(); LOD lod0 = new LOD(1.0f, new Renderer[] { meshRenderer }); lodGroup.SetLODs(new [] { lod0 }); lod0.screenRelativeTransitionHeight = LODGroupExtensions.DistanceToRelativeHeight(cameraManager.UnityCamera, cullDist, lodGroup.GetWorldSpaceSize()); lodGroup.SetLODs(new [] { lod0 }); } } return(actor); }
static float GetRelativeHeight(this LODGroup lodGroup, Camera camera) { float distance = (lodGroup.transform.TransformPoint(lodGroup.localReferencePoint) - camera.transform.position).magnitude; return(DistanceToRelativeHeight(camera, distance, lodGroup.GetWorldSpaceSize())); }