-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRendererExtension.cs
39 lines (36 loc) · 1.34 KB
/
RendererExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using UnityEngine;
namespace Aya.Extension
{
public static class RendererExtension
{
public static bool IsVisible(this Renderer renderer, Camera camera)
{
var planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
public static Bounds GetBounds(this Renderer renderer, bool includeChildren = true)
{
if (includeChildren)
{
var center = renderer.transform.position;
var bounds = new Bounds(center, Vector3.zero);
var rendererList = renderer.gameObject.GetComponentsInChildren<Renderer>();
if (rendererList.Length == 0) return bounds;
foreach (var r in rendererList)
{
bounds.Encapsulate(r.bounds);
}
return bounds;
}
else
{
return renderer.bounds;
}
}
public static Material GetMaterial(this Renderer renderer, int materialIndex)
{
if (materialIndex < 0 || materialIndex >= renderer.sharedMaterials.Length) return null;
return Application.isPlaying ? renderer.materials[materialIndex] : renderer.sharedMaterials[materialIndex];
}
}
}