summaryrefslogtreecommitdiff
path: root/RuntimeDecalCombiner.cs
blob: 31c1822777c8348dcfc86805a7e9774357600fd9 (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
using System.Collections.Generic;
using ch.sycoforge.Decal;
using ch.sycoforge.Decal.Projectors.Geometry;
using UnityEngine;

public class RuntimeDecalCombiner
{
	public static List<GameObject> Combine(IList<EasyDecal> decals)
	{
		Dictionary<DecalTextureAtlas, List<EasyDecal>> dictionary = new Dictionary<DecalTextureAtlas, List<EasyDecal>>();
		foreach (EasyDecal decal in decals)
		{
			if (decal.Source == SourceMode.Atlas && decal.Projector != null)
			{
				if (!dictionary.ContainsKey(decal.Atlas))
				{
					dictionary.Add(decal.Atlas, new List<EasyDecal>());
				}
				dictionary[decal.Atlas].Add(decal);
			}
		}
		return Combine(dictionary);
	}

	private static List<GameObject> Combine(Dictionary<DecalTextureAtlas, List<EasyDecal>> mappings)
	{
		List<GameObject> list = new List<GameObject>();
		if (mappings.Count > 0)
		{
			foreach (DecalTextureAtlas key in mappings.Keys)
			{
				IList<EasyDecal> list2 = mappings[key];
				foreach (EasyDecal item in list2)
				{
					GameObject gameObject = Combine(list2, key);
					if (gameObject != null)
					{
						list.Add(gameObject);
					}
				}
			}
			return list;
		}
		return list;
	}

	private static GameObject Combine(IList<EasyDecal> decals, DecalTextureAtlas atlas)
	{
		if (decals.Count > 0)
		{
			DynamicMesh dynamicMesh = new DynamicMesh(DecalBase.DecalRoot, RecreationMode.Always);
			GameObject gameObject = new GameObject($"Combined Decals Root [{atlas.name}]");
			MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
			MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
			foreach (EasyDecal decal in decals)
			{
				if (decal.Source == SourceMode.Atlas && decal.Projector != null)
				{
					dynamicMesh.Add(decal.Projector.Mesh, decal.LocalToWorldMatrix, gameObject.transform.worldToLocalMatrix);
					decal.gameObject.SetActive(value: false);
				}
			}
			meshRenderer.material = atlas.Material;
			meshFilter.sharedMesh = dynamicMesh.ConvertToMesh(null);
		}
		return null;
	}
}