From 6ce8b9e22fc13be34b442c7b6af48b42cd44275a Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 13 Mar 2024 11:00:58 +0800 Subject: +init --- RuntimeDecalCombiner.cs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 RuntimeDecalCombiner.cs (limited to 'RuntimeDecalCombiner.cs') diff --git a/RuntimeDecalCombiner.cs b/RuntimeDecalCombiner.cs new file mode 100644 index 0000000..31c1822 --- /dev/null +++ b/RuntimeDecalCombiner.cs @@ -0,0 +1,68 @@ +using System.Collections.Generic; +using ch.sycoforge.Decal; +using ch.sycoforge.Decal.Projectors.Geometry; +using UnityEngine; + +public class RuntimeDecalCombiner +{ + public static List Combine(IList decals) + { + Dictionary> dictionary = new Dictionary>(); + foreach (EasyDecal decal in decals) + { + if (decal.Source == SourceMode.Atlas && decal.Projector != null) + { + if (!dictionary.ContainsKey(decal.Atlas)) + { + dictionary.Add(decal.Atlas, new List()); + } + dictionary[decal.Atlas].Add(decal); + } + } + return Combine(dictionary); + } + + private static List Combine(Dictionary> mappings) + { + List list = new List(); + if (mappings.Count > 0) + { + foreach (DecalTextureAtlas key in mappings.Keys) + { + IList 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 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(); + MeshRenderer meshRenderer = gameObject.AddComponent(); + 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; + } +} -- cgit v1.1-26-g67d0