using System;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace AlicizaX
{
///
/// Unity 渲染帮助类
///
public static class UnityRendererHelper
{
///
/// 判断渲染组件是否在相机范围内
///
/// 渲染组件
/// 相机对象
///
public static bool IsVisibleFrom(Renderer renderer, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
///
/// 判断渲染组件是否在相机范围内
///
/// 渲染对象
/// 相机对象
///
public static bool IsVisibleFrom(MeshRenderer renderer, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
///
/// 获取相机快照
///
/// 相机
/// 缩放比
public static Texture2D GetCaptureScreenshot(Camera main, float scale = 0.5f)
{
Rect rect = new Rect(0, 0, Screen.width * scale, Screen.height * scale);
string name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
RenderTexture renderTexture = RenderTexture.GetTemporary((int)rect.width, (int)rect.height, 0);
renderTexture.name = SceneManager.GetActiveScene().name + "_" + renderTexture.width + "_" + renderTexture.height + "_" + name;
main.targetTexture = renderTexture;
main.Render();
RenderTexture.active = renderTexture;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false)
{
name = renderTexture.name
};
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
main.targetTexture = null;
RenderTexture.active = null;
RenderTexture.ReleaseTemporary(renderTexture);
return screenShot;
}
}
}