summaryrefslogtreecommitdiff
path: root/ReliefTerrain.cs
blob: b5967511a90e1c4ddd9dfe2198c14f97fd8c48af (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Reflection;
using UnityEngine;

[AddComponentMenu("Relief Terrain/Engine - Terrain or Mesh")]
[ExecuteInEditMode]
public class ReliefTerrain : MonoBehaviour
{
	public Texture2D controlA;

	public Texture2D controlB;

	public Texture2D controlC;

	public string save_path_controlA = string.Empty;

	public string save_path_controlB = string.Empty;

	public string save_path_controlC = string.Empty;

	public string save_path_colormap = string.Empty;

	public string save_path_BumpGlobalCombined = string.Empty;

	public string save_path_WetMask = string.Empty;

	public Texture2D NormalGlobal;

	public Texture2D TreesGlobal;

	public Texture2D ColorGlobal;

	public Texture2D AmbientEmissiveMap;

	public Texture2D BumpGlobalCombined;

	public Texture2D TERRAIN_WetMask;

	public Texture2D tmp_globalColorMap;

	public Texture2D tmp_CombinedMap;

	public Texture2D tmp_WaterMap;

	public bool globalColorModifed_flag;

	public bool globalCombinedModifed_flag;

	public bool globalWaterModifed_flag;

	public bool splat_layer_ordered_mode;

	public RTPColorChannels[] source_controls_channels;

	public int[] splat_layer_seq;

	public float[] splat_layer_boost;

	public bool[] splat_layer_calc;

	public bool[] splat_layer_masked;

	public RTPColorChannels[] source_controls_mask_channels;

	public Texture2D[] source_controls;

	public bool[] source_controls_invert;

	public Texture2D[] source_controls_mask;

	public bool[] source_controls_mask_invert;

	public Vector2 customTiling = new Vector2(3f, 3f);

	[SerializeField]
	public ReliefTerrainPresetHolder[] presetHolders;

	[SerializeField]
	public ReliefTerrainGlobalSettingsHolder globalSettingsHolder;

	public void GetGlobalSettingsHolder()
	{
		if (globalSettingsHolder != null)
		{
			return;
		}
		ReliefTerrain[] array = (ReliefTerrain[])UnityEngine.Object.FindObjectsOfType(typeof(ReliefTerrain));
		bool flag = GetComponent(typeof(Terrain));
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i].transform.parent == base.transform.parent && array[i].globalSettingsHolder != null && ((flag && array[i].GetComponent(typeof(Terrain)) != null) || (!flag && array[i].GetComponent(typeof(Terrain)) == null)))
			{
				globalSettingsHolder = array[i].globalSettingsHolder;
				if ((bool)globalSettingsHolder.Get_RTP_LODmanagerScript() && !globalSettingsHolder.Get_RTP_LODmanagerScript().RTP_WETNESS_FIRST && !globalSettingsHolder.Get_RTP_LODmanagerScript().RTP_WETNESS_ADD)
				{
					BumpGlobalCombined = array[i].BumpGlobalCombined;
					globalCombinedModifed_flag = false;
				}
				break;
			}
		}
		if (globalSettingsHolder == null)
		{
			globalSettingsHolder = new ReliefTerrainGlobalSettingsHolder();
			if (flag)
			{
				globalSettingsHolder.numTiles = 0;
				Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
				globalSettingsHolder.splats = new Texture2D[terrain.terrainData.splatPrototypes.Length];
				globalSettingsHolder.Bumps = new Texture2D[terrain.terrainData.splatPrototypes.Length];
				for (int j = 0; j < terrain.terrainData.splatPrototypes.Length; j++)
				{
					globalSettingsHolder.splats[j] = terrain.terrainData.splatPrototypes[j].texture;
					globalSettingsHolder.Bumps[j] = terrain.terrainData.splatPrototypes[j].normalMap;
				}
			}
			else
			{
				globalSettingsHolder.splats = new Texture2D[4];
			}
			globalSettingsHolder.numLayers = globalSettingsHolder.splats.Length;
			globalSettingsHolder.ReturnToDefaults(string.Empty);
		}
		else if (flag)
		{
			GetSplatsFromGlobalSettingsHolder();
		}
		source_controls_mask = new Texture2D[12];
		source_controls = new Texture2D[12];
		source_controls_channels = new RTPColorChannels[12];
		source_controls_mask_channels = new RTPColorChannels[12];
		splat_layer_seq = new int[12]
		{
			0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
			10, 11
		};
		splat_layer_boost = new float[12]
		{
			1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f,
			1f, 1f
		};
		splat_layer_calc = new bool[12];
		splat_layer_masked = new bool[12];
		source_controls_invert = new bool[12];
		source_controls_mask_invert = new bool[12];
		if (flag)
		{
			globalSettingsHolder.numTiles++;
		}
	}

	private void GetSplatsFromGlobalSettingsHolder()
	{
		SplatPrototype[] array = new SplatPrototype[globalSettingsHolder.numLayers];
		for (int i = 0; i < globalSettingsHolder.numLayers; i++)
		{
			array[i] = new SplatPrototype();
			array[i].tileSize = Vector2.one;
			array[i].tileOffset = new Vector2(1f / customTiling.x, 1f / customTiling.y);
			array[i].texture = globalSettingsHolder.splats[i];
			array[i].normalMap = globalSettingsHolder.Bumps[i];
		}
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		terrain.terrainData.splatPrototypes = array;
	}

	public void InitTerrainTileSizes()
	{
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		if ((bool)terrain)
		{
			globalSettingsHolder.terrainTileSize = terrain.terrainData.size;
			return;
		}
		globalSettingsHolder.terrainTileSize = GetComponent<Renderer>().bounds.size;
		globalSettingsHolder.terrainTileSize.y = globalSettingsHolder.tessHeight;
	}

	private void Awake()
	{
		UpdateBasemapDistance(apply_material_if_applicable: false);
		RefreshTextures();
	}

	public void InitArrays()
	{
		RefreshTextures();
	}

	private void UpdateBasemapDistance(bool apply_material_if_applicable)
	{
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		if (!terrain || globalSettingsHolder == null)
		{
			return;
		}
		terrain.basemapDistance = globalSettingsHolder.distance_start + globalSettingsHolder.distance_transition;
		if (apply_material_if_applicable)
		{
			if (terrain.materialTemplate == null)
			{
				terrain.materialType = Terrain.MaterialType.Custom;
				Shader shader = Shader.Find("Relief Pack/ReliefTerrain-FirstPass");
				if ((bool)shader)
				{
					Material material = new Material(shader);
					material.name = base.gameObject.name + " material";
					terrain.materialTemplate = material;
				}
			}
			else
			{
				Material materialTemplate = terrain.materialTemplate;
				terrain.materialTemplate = null;
				terrain.materialTemplate = materialTemplate;
			}
		}
		if (globalSettingsHolder != null && globalSettingsHolder._RTP_LODmanagerScript != null && globalSettingsHolder._RTP_LODmanagerScript.numLayersProcessedByFarShader != globalSettingsHolder.numLayers)
		{
			terrain.basemapDistance = 500000f;
		}
		globalSettingsHolder.Refresh(terrain.materialTemplate);
	}

	public void RefreshTextures(Material mat = null, bool check_weak_references = false)
	{
		GetGlobalSettingsHolder();
		InitTerrainTileSizes();
		if (globalSettingsHolder != null && BumpGlobalCombined != null)
		{
			globalSettingsHolder.BumpGlobalCombinedSize = BumpGlobalCombined.width;
		}
		UpdateBasemapDistance(apply_material_if_applicable: true);
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		globalSettingsHolder.use_mat = mat;
		if (!terrain && !mat)
		{
			if (GetComponent<Renderer>().sharedMaterial == null || GetComponent<Renderer>().sharedMaterial.name != "RTPMaterial")
			{
				GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("Relief Pack/Terrain2Geometry"));
				GetComponent<Renderer>().sharedMaterial.name = "RTPMaterial";
			}
			globalSettingsHolder.use_mat = GetComponent<Renderer>().sharedMaterial;
		}
		if ((bool)terrain && terrain.materialTemplate != null)
		{
			globalSettingsHolder.use_mat = terrain.materialTemplate;
			terrain.materialTemplate.SetVector("RTP_CustomTiling", new Vector4(1f / customTiling.x, 1f / customTiling.y, 0f, 0f));
		}
		globalSettingsHolder.use_mat = null;
		RefreshControlMaps(mat);
		if ((bool)mat)
		{
			mat.SetVector("RTP_CustomTiling", new Vector4(1f / customTiling.x, 1f / customTiling.y, 0f, 0f));
		}
	}

	public void RefreshControlMaps(Material mat = null)
	{
		globalSettingsHolder.use_mat = mat;
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		if (!terrain && !mat)
		{
			globalSettingsHolder.use_mat = GetComponent<Renderer>().sharedMaterial;
		}
		if ((bool)terrain && !mat && terrain.materialTemplate != null)
		{
			globalSettingsHolder.use_mat = terrain.materialTemplate;
		}
		globalSettingsHolder.SetShaderParam("_Control1", controlA);
		if (globalSettingsHolder.numLayers > 4)
		{
			globalSettingsHolder.SetShaderParam("_Control3", controlB);
			globalSettingsHolder.SetShaderParam("_Control2", controlB);
		}
		if (globalSettingsHolder.numLayers > 8)
		{
			globalSettingsHolder.SetShaderParam("_Control3", controlC);
		}
		globalSettingsHolder.SetShaderParam("_ColorMapGlobal", ColorGlobal);
		globalSettingsHolder.SetShaderParam("_NormalMapGlobal", NormalGlobal);
		globalSettingsHolder.SetShaderParam("_TreesMapGlobal", TreesGlobal);
		globalSettingsHolder.SetShaderParam("_AmbientEmissiveMapGlobal", AmbientEmissiveMap);
		globalSettingsHolder.SetShaderParam("_BumpMapGlobal", BumpGlobalCombined);
		globalSettingsHolder.use_mat = null;
	}

	public void GetControlMaps()
	{
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		if (!terrain)
		{
			Debug.Log("Can't fint terrain component !!!");
			return;
		}
		Type type = terrain.terrainData.GetType();
		PropertyInfo property = type.GetProperty("alphamapTextures", BindingFlags.Instance | BindingFlags.Public);
		if (property != null)
		{
			Texture2D[] array = (Texture2D[])property.GetValue(terrain.terrainData, null);
			if (array.Length > 0)
			{
				controlA = array[0];
			}
			else
			{
				controlA = null;
			}
			if (array.Length > 1)
			{
				controlB = array[1];
			}
			else
			{
				controlB = null;
			}
			if (array.Length > 2)
			{
				controlC = array[2];
			}
			else
			{
				controlC = null;
			}
		}
		else
		{
			Debug.LogError("Can't access alphamapTexture directly...");
		}
	}

	public void SetCustomControlMaps()
	{
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		if (!terrain)
		{
			Debug.Log("Can't fint terrain component !!!");
		}
		else
		{
			if (controlA == null)
			{
				return;
			}
			if (terrain.terrainData.alphamapResolution != controlA.width)
			{
				Debug.LogError("Terrain controlmap resolution differs fromrequested control texture...");
			}
			else
			{
				if (!controlA)
				{
					return;
				}
				float[,,] alphamaps = terrain.terrainData.GetAlphamaps(0, 0, terrain.terrainData.alphamapResolution, terrain.terrainData.alphamapResolution);
				Color[] pixels = controlA.GetPixels();
				for (int i = 0; i < terrain.terrainData.alphamapLayers; i++)
				{
					int num = 0;
					switch (i)
					{
					case 4:
						if (!controlB)
						{
							return;
						}
						pixels = controlB.GetPixels();
						break;
					case 8:
						if (!controlC)
						{
							return;
						}
						pixels = controlC.GetPixels();
						break;
					}
					int index = i & 3;
					for (int j = 0; j < terrain.terrainData.alphamapResolution; j++)
					{
						for (int k = 0; k < terrain.terrainData.alphamapResolution; k++)
						{
							alphamaps[j, k, i] = pixels[num++][index];
						}
					}
				}
				terrain.terrainData.SetAlphamaps(0, 0, alphamaps);
			}
		}
	}

	public void RestorePreset(ReliefTerrainPresetHolder holder)
	{
		controlA = holder.controlA;
		controlB = holder.controlB;
		controlC = holder.controlC;
		SetCustomControlMaps();
		ColorGlobal = holder.ColorGlobal;
		NormalGlobal = holder.NormalGlobal;
		TreesGlobal = holder.TreesGlobal;
		AmbientEmissiveMap = holder.AmbientEmissiveMap;
		BumpGlobalCombined = holder.BumpGlobalCombined;
		TERRAIN_WetMask = holder.TERRAIN_WetMask;
		globalColorModifed_flag = holder.globalColorModifed_flag;
		globalCombinedModifed_flag = holder.globalCombinedModifed_flag;
		globalWaterModifed_flag = holder.globalWaterModifed_flag;
		RefreshTextures();
		globalSettingsHolder.RestorePreset(holder);
	}

	public ReliefTerrainPresetHolder GetPresetByID(string PresetID)
	{
		if (presetHolders != null)
		{
			for (int i = 0; i < presetHolders.Length; i++)
			{
				if (presetHolders[i].PresetID == PresetID)
				{
					return presetHolders[i];
				}
			}
		}
		return null;
	}

	public ReliefTerrainPresetHolder GetPresetByName(string PresetName)
	{
		if (presetHolders != null)
		{
			for (int i = 0; i < presetHolders.Length; i++)
			{
				if (presetHolders[i].PresetName == PresetName)
				{
					return presetHolders[i];
				}
			}
		}
		return null;
	}

	public bool InterpolatePresets(string PresetID1, string PresetID2, float t)
	{
		ReliefTerrainPresetHolder presetByID = GetPresetByID(PresetID1);
		ReliefTerrainPresetHolder presetByID2 = GetPresetByID(PresetID2);
		if (presetByID == null || presetByID2 == null || presetByID.Spec == null || presetByID2.Spec == null || presetByID.Spec.Length != presetByID2.Spec.Length)
		{
			return false;
		}
		globalSettingsHolder.InterpolatePresets(presetByID, presetByID2, t);
		return true;
	}
}