summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/NGS.MeshFusionPro/CombinedLODGroup.cs
blob: e5f2a9308256355d76db7fcf4162433552014b8a (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace NGS.MeshFusionPro;

public class CombinedLODGroup : MonoBehaviour, ICombinedObject<CombinedLODGroupPart, LODGroupCombineSource>, ICombinedObject
{
	private class LevelOfDetailCombiner : StaticObjectsCombiner
	{
		private Transform _transform;

		private CombinedLODGroup _group;

		private Renderer[] _renderers;

		private int _level;

		public LevelOfDetailCombiner(int level, CombinedLODGroup group, ICombinedMeshFactory factory, int vertexLimit)
			: base(factory, vertexLimit)
		{
			_level = level;
			_group = group;
			_transform = new GameObject("LOD" + _level).transform;
			_transform.parent = group.transform;
			_transform.localPosition = Vector3.zero;
		}

		public Renderer[] GetRenderers()
		{
			if (_renderers == null || _renderers.Length != base.CombinedObjects.Count)
			{
				UpdateRenderersList();
			}
			return _renderers;
		}

		public Bounds CalculateBounds()
		{
			Bounds result = new Bounds(_group.transform.position, Vector3.zero);
			GetRenderers();
			for (int i = 0; i < _renderers.Length; i++)
			{
				if (base.CombinedObjects[i].Parts.Count > 0)
				{
					result.Encapsulate(_renderers[i].bounds);
				}
			}
			return result;
		}

		protected override CombinedObject CreateCombinedObject(CombineSource source)
		{
			CombinedObject combinedObject = base.CreateCombinedObject(source);
			combinedObject.transform.parent = _transform;
			_group._updateLODs = true;
			return combinedObject;
		}

		private void UpdateRenderersList()
		{
			_renderers = base.CombinedObjects.Select((CombinedObject r) => r.GetComponent<Renderer>()).ToArray();
		}
	}

	private LODGroup _group;

	private List<CombinedLODGroupPart> _parts;

	private LevelOfDetailCombiner[] _levelCombiners;

	private LOD[] _lods;

	private Bounds _localBounds;

	private int _lodCount;

	private LODGroupSettings _originalSettings;

	private float _originalBoundsSize;

	private bool _updateLODs;

	IReadOnlyList<ICombinedObjectPart> ICombinedObject.Parts => _parts;

	public IReadOnlyList<CombinedLODGroupPart> Parts => _parts;

	public LODGroupSettings Settings => _originalSettings;

	public Bounds Bounds
	{
		get
		{
			Bounds localBounds = _localBounds;
			localBounds.center += base.transform.position;
			return localBounds;
		}
	}

	public static CombinedLODGroup Create(MeshType meshType, CombineMethod combineMethod, LODGroupSettings settings, int vertexLimit = 45000)
	{
		return Create(new CombinedMeshFactory(meshType, combineMethod), settings, vertexLimit);
	}

	public static CombinedLODGroup Create(ICombinedMeshFactory factory, LODGroupSettings settings, int vertexLimit = 45000)
	{
		CombinedLODGroup combinedLODGroup = new GameObject("CombinedLODGroup").AddComponent<CombinedLODGroup>();
		combinedLODGroup.Construct(settings, factory, vertexLimit);
		return combinedLODGroup;
	}

	private void Construct(LODGroupSettings settings, ICombinedMeshFactory factory, int vertexLimit)
	{
		if (factory == null)
		{
			throw new ArgumentException("CombinedLODGroup::factory is null");
		}
		_group = base.gameObject.AddComponent<LODGroup>();
		_parts = new List<CombinedLODGroupPart>();
		_group.fadeMode = settings.fadeMode;
		_group.animateCrossFading = settings.animateCrossFading;
		_originalSettings = settings;
		_lodCount = _originalSettings.lodCount;
		_levelCombiners = new LevelOfDetailCombiner[_lodCount];
		_lods = new LOD[_originalSettings.lodCount];
		for (int i = 0; i < _originalSettings.lodCount; i++)
		{
			_levelCombiners[i] = new LevelOfDetailCombiner(i, this, factory, vertexLimit);
			_lods[i] = new LOD
			{
				fadeTransitionWidth = _originalSettings.fadeTransitionsWidth[i],
				screenRelativeTransitionHeight = _originalSettings.screenTransitionsHeight[i],
				renderers = null
			};
		}
	}

	public void Combine(IEnumerable<ICombineSource> sources)
	{
		Combine(sources.Select((ICombineSource s) => (LODGroupCombineSource)s));
	}

	public void Combine(IEnumerable<LODGroupCombineSource> sourceGroups)
	{
		if (sourceGroups == null || sourceGroups.Count() == 0)
		{
			throw new ArgumentException("CombinedLODGroup::sources is null");
		}
		LODGroupCombineSource[] array = sourceGroups.ToArray();
		if (_parts.Count == 0)
		{
			InitBeforeFirstCombine(array);
		}
		List<CombinedObjectPart>[] combinedParts = FillCombinersAndCreateBaseParts(array);
		for (int i = 0; i < _lodCount; i++)
		{
			_levelCombiners[i].Combine();
		}
		RecalculateBounds();
		if (_updateLODs)
		{
			UpdateLODs();
			_updateLODs = false;
		}
		CreatePartsAndNotifySources(array, combinedParts);
	}

	public void Destroy(CombinedLODGroupPart part, IList<CombinedObjectPart> baseParts)
	{
		if (_parts.Remove(part))
		{
			for (int i = 0; i < baseParts.Count; i++)
			{
				baseParts[i].Destroy();
			}
			RecalculateBounds();
		}
	}

	private void InitBeforeFirstCombine(LODGroupCombineSource[] sources)
	{
		Vector3 zero = Vector3.zero;
		for (int i = 0; i < sources.Length; i++)
		{
			zero += sources[i].Position;
		}
		base.transform.position = zero / sources.Length;
		_originalBoundsSize = sources[0].Bounds.size.magnitude;
	}

	private List<CombinedObjectPart>[] FillCombinersAndCreateBaseParts(LODGroupCombineSource[] sourceGroups)
	{
		List<CombinedObjectPart>[] parts = new List<CombinedObjectPart>[sourceGroups.Length];
		for (int i = 0; i < sourceGroups.Length; i++)
		{
			LODGroupCombineSource sourceGroup = sourceGroups[i];
			parts[i] = new List<CombinedObjectPart>();
			for (int j = 0; j < _lodCount; j++)
			{
				CombineSource[] array = sourceGroup.BaseSources[j];
				foreach (CombineSource obj in array)
				{
					int g = i;
					obj.onCombinedTyped += delegate(CombinedObject o, CombinedObjectPart p)
					{
						parts[g].Add(p);
					};
					obj.onCombineErrorTyped += delegate(CombinedObject root, string msg)
					{
						sourceGroup.CombineError(this, msg);
					};
				}
				_levelCombiners[j].AddSources(array);
			}
		}
		return parts;
	}

	private void RecalculateBounds()
	{
		_localBounds = new Bounds(base.transform.position, Vector3.zero);
		for (int i = 0; i < _levelCombiners.Length; i++)
		{
			_localBounds.Encapsulate(_levelCombiners[i].CalculateBounds());
		}
		_localBounds.center -= base.transform.position;
	}

	private void UpdateLODs()
	{
		float value = _localBounds.size.magnitude / _originalBoundsSize;
		float num = 1f;
		value = Mathf.Clamp(value, 1f, 100f);
		for (int i = 0; i < _lodCount; i++)
		{
			LOD lOD = _lods[i];
			lOD.renderers = _levelCombiners[i].GetRenderers();
			lOD.screenRelativeTransitionHeight = _originalSettings.screenTransitionsHeight[i] * value;
			if (lOD.screenRelativeTransitionHeight >= num)
			{
				num = (lOD.screenRelativeTransitionHeight = num - 0.03f);
			}
			_lods[i] = lOD;
		}
		_group.SetLODs(_lods);
	}

	private void CreatePartsAndNotifySources(LODGroupCombineSource[] sourceGroups, List<CombinedObjectPart>[] combinedParts)
	{
		for (int i = 0; i < sourceGroups.Length; i++)
		{
			LODGroupCombineSource lODGroupCombineSource = sourceGroups[i];
			List<CombinedObjectPart> list = combinedParts[i];
			if (list.Count == 0)
			{
				lODGroupCombineSource.CombineFailed(this);
				continue;
			}
			CombinedLODGroupPart combinedLODGroupPart = new CombinedLODGroupPart(this, list);
			_parts.Add(combinedLODGroupPart);
			lODGroupCombineSource.Combined(this, combinedLODGroupPart);
		}
	}
}