-
Notifications
You must be signed in to change notification settings - Fork 1
/
SlightmapperRuntimeState.cs
94 lines (77 loc) · 3.18 KB
/
SlightmapperRuntimeState.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
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using SubsurfaceStudios.Slightmapper.Global;
using UnityEngine;
namespace SubsurfaceStudios.Slightmapper.Assets {
[CreateAssetMenu(fileName = "NewSlightmapperState", menuName = "Debug/Slightmapper Runtime State")]
public class SlightmapperRuntimeState : ScriptableObject {
public LightmapReference[] lightmaps;
public LightmapsMode lightmapsMode;
public LightProbes lightProbes;
public RendererInfo[] staticRendererData;
public ReflectionProbeInfo[] reflectionProbeData;
public void Load() {
LightmapSettings.lightmaps = lightmaps.Select(x => x.ToLightmapData()).ToArray();
LightmapSettings.lightmapsMode = lightmapsMode;
LightmapSettings.lightProbes = lightProbes;
int renderer_iterations = staticRendererData.Length;
for (int i = 0; i < renderer_iterations; i++) {
staticRendererData[i].Apply();
}
int reflection_probe_iterations = reflectionProbeData.Length;
for (int i = 0; i < reflection_probe_iterations; i++) {
reflectionProbeData[i].Apply();
}
}
}
[Serializable]
public struct LightmapReference {
public Texture2D lightmapColor;
public Texture2D lightmapDir;
public Texture2D shadowMask;
public readonly LightmapData ToLightmapData() => new() {
lightmapColor = lightmapColor,
lightmapDir = lightmapDir,
shadowMask = shadowMask
};
public LightmapReference(LightmapData original) {
lightmapColor = original.lightmapColor;
lightmapDir = original.lightmapDir;
shadowMask = original.shadowMask;
}
}
[Serializable]
public struct RendererInfo {
public uint rendererId;
public Vector4 lightmapScaleOffset;
public int lightmapIndex;
public RendererInfo(MeshRenderer renderer) {
bool success = renderer.TryGetComponent(out RendererId reference);
Debug.Assert(success);
rendererId = reference.Id;
lightmapScaleOffset = renderer.lightmapScaleOffset;
lightmapIndex = renderer.lightmapIndex;
}
public void Apply() {
RendererId reference = RendererIdAllocator.Index[rendererId];
reference.RendererIfAvailable.lightmapScaleOffset = lightmapScaleOffset;
reference.RendererIfAvailable.lightmapIndex = lightmapIndex;
}
}
[Serializable]
public struct ReflectionProbeInfo {
public uint rendererId;
public Texture bakedTexture;
public ReflectionProbeInfo(ReflectionProbe probe) {
bool success = probe.TryGetComponent(out RendererId reference);
Debug.Assert(success);
rendererId = reference.Id;
bakedTexture = probe.bakedTexture;
}
public void Apply() {
RendererId reference = RendererIdAllocator.Index[rendererId];
reference.ReflectionProbeIfAvailable.bakedTexture = bakedTexture;
}
}
}