Repository navigation
Expand file tree
/
Copy pathVolumeLightMesh.cs
More file actions
63 lines (53 loc) · 1.79 KB
/
Copy pathVolumeLightMesh.cs
File metadata and controls
63 lines (53 loc) · 1.79 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(Light))]
public class VolumetricLightMesh : MonoBehaviour
{
public float maximumOpacity = 0.25f;
private MeshFilter filter;
private Light light;
private Mesh mesh;
// Use this for initialization
void Start()
{
filter = GetComponent<MeshFilter>();
light = GetComponent<Light>();
if (light.type != LightType.Spot)
{
Debug.LogError("Attached Volumetric Light Mesh to a non-supported Light Type. Please use Spotlight lights.");
}
}
// Update is called once per frame
void Update()
{
mesh = BuildMesh();
filter.mesh = mesh;
}
private Mesh BuildMesh()
{
mesh = new Mesh();
float farPosition = Mathf.Tan(light.spotAngle * 0.5f * Mathf.Deg2Rad) * light.range;
mesh.vertices = new Vector3[] {
new Vector3(0,0,0),
new Vector3(farPosition, farPosition, light.range),
new Vector3(-farPosition, farPosition, light.range),
new Vector3(-farPosition, -farPosition, light.range),
new Vector3(farPosition, -farPosition, light.range)
};
mesh.colors = new Color[] {
new Color(light.color.r, light.color.g, light.color.b, light.color.a * maximumOpacity),
new Color(light.color.r, light.color.g, light.color.b, 0),
new Color(light.color.r, light.color.g, light.color.b, 0),
new Color(light.color.r, light.color.g, light.color.b, 0),
new Color(light.color.r, light.color.g, light.color.b, 0)
};
mesh.triangles = new int[] {
0,1,2,
0,2,3,
0,3,4,
0,4,1
};
return mesh;
}
}