-
Notifications
You must be signed in to change notification settings - Fork 2
/
LightProbeGenerator.cs
118 lines (97 loc) · 3.16 KB
/
LightProbeGenerator.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(LightProbeGroup))]
[AddComponentMenu("Light Probe Helper/Light Probe Generator")]
public class LightProbeGenerator : MonoBehaviour
{
[System.Serializable]
public class LightProbeArea
{
public Bounds ProbeVolume = new Bounds(Vector3.zero, new Vector3(10f, 10f, 10f));
public Vector3 Subdivisions = new Vector3(5, 5, 5);
public Quaternion Rotation = Quaternion.identity;
public int RandomCount = 0;
}
public enum LightProbePlacementType
{
Grid,
Random
}
#if UNITY_EDITOR
public LightProbeArea LightProbeVolumes;
public LightProbePlacementType PlacementAlgorithm;
private void Reset()
{
ClearProbes();
}
public void ClearProbes()
{
if (!TryGetComponent<LightProbeGroup>(out var lprobe))
{
Debug.LogError("LightProbeGenerator: Must have LightProbeGroup attached!");
return;
}
lprobe.probePositions = null;
}
public void GenProbes()
{
ClearProbes();
if (!TryGetComponent(out LightProbeGroup lprobe))
{
gameObject.AddComponent<LightProbeGroup>();
}
transform.SetPositionAndRotation(LightProbeVolumes.ProbeVolume.center, LightProbeVolumes.Rotation);
List<Vector3> probePositions = new List<Vector3>();
if (PlacementAlgorithm == LightProbePlacementType.Grid)
{
probePositions.AddRange(GetProbesForVolume_Grid(LightProbeVolumes.ProbeVolume, LightProbeVolumes.Subdivisions));
}
else
{
probePositions.AddRange(GetProbesForVolume_Random(LightProbeVolumes.ProbeVolume, LightProbeVolumes.RandomCount));
}
lprobe.probePositions = probePositions.ToArray();
}
List<Vector3> GetProbesForVolume_Grid(Bounds ProbeVolume, Vector3 Subdivisions)
{
List<Vector3> probePositions = new List<Vector3>();
Vector3 step = new Vector3(ProbeVolume.extents.x * 2 / Subdivisions.x,
ProbeVolume.extents.y * 2 / Subdivisions.y,
ProbeVolume.extents.z * 2 / Subdivisions.z);
for (int x = 0; x <= Subdivisions.x; x++)
{
for (int y = 0; y <= Subdivisions.y; y++)
{
for (int z = 0; z <= Subdivisions.z; z++)
{
Vector3 probePos = ProbeVolume.center - ProbeVolume.extents + new Vector3(step.x * x, step.y * y, step.z * z);
probePositions.Add(probePos - transform.position);
}
}
}
return probePositions;
}
List<Vector3> GetProbesForVolume_Random(Bounds ProbeVolume, int Count)
{
List<Vector3> probePositions = new List<Vector3>();
for (int c = 0; c <= Count; c++)
{
Vector3 probePos = ProbeVolume.center + new Vector3(Random.Range(-0.5f, 0.5f) * ProbeVolume.extents.x,
Random.Range(-0.5f, 0.5f) * ProbeVolume.extents.y,
Random.Range(-0.5f, 0.5f) * ProbeVolume.extents.z);
probePositions.Add(probePos - transform.position);
}
return probePositions;
}
void OnDrawGizmosSelected()
{
if (LightProbeVolumes != null)
{
Gizmos.color = Color.red;
Matrix4x4 matrix = Matrix4x4.TRS(LightProbeVolumes.ProbeVolume.center, LightProbeVolumes.Rotation, Vector3.one);
Gizmos.matrix = matrix;
Gizmos.DrawWireCube(Vector3.zero, LightProbeVolumes.ProbeVolume.extents * 2);
}
}
#endif
}