GpuEcsAnimationBakeServices.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. #if UNITY_EDITOR
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using GPUECSAnimationBaker.Engine.AnimatorSystem;
  8. using GpuEcsAnimationBaker.Engine.Data;
  9. using Unity.Collections;
  10. using Unity.Mathematics;
  11. using UnityEditor;
  12. using UnityEngine;
  13. namespace GPUECSAnimationBaker.Engine.Baker
  14. {
  15. public static class GpuEcsAnimationBakerServices
  16. {
  17. private static readonly int AnimatedBoneMatrices = Shader.PropertyToID("_AnimatedBoneMatrices");
  18. private static readonly int EnableAnimation = Shader.PropertyToID("_EnableAnimation");
  19. public static bool ValidateAnimationBakerData(GpuEcsAnimationBakerData bakerData, GameObject sourceModel, out string errors)
  20. {
  21. StringBuilder sbErrors = new StringBuilder();
  22. if (bakerData.animations.Length == 0)
  23. sbErrors.AppendLine("At least one animation must be baked.");
  24. foreach (AnimationData animation in bakerData.animations)
  25. {
  26. if (string.IsNullOrWhiteSpace(animation.animationID))
  27. sbErrors.AppendLine("Animation ID is mandatory");
  28. if (!Regex.IsMatch(animation.animationID, @"^[a-zA-Z_][a-zA-Z0-9_]+$"))
  29. sbErrors.AppendLine("Animation ID must be only letters, numbers or underscore, must not start with number");
  30. if (string.IsNullOrWhiteSpace(animation.animatorStateName))
  31. sbErrors.AppendLine("Animation State Name is mandatory");
  32. if (animation.animationType == AnimationTypes.SingleClip)
  33. {
  34. if (animation.singleClipData.animationClip == null)
  35. sbErrors.AppendLine("Animation Clip is mandatory");
  36. }
  37. else if (animation.animationType == AnimationTypes.DualClipBlend)
  38. {
  39. if (string.IsNullOrWhiteSpace(animation.dualClipBlendData.blendParameterName))
  40. sbErrors.AppendLine("Blend parameter name is mandatory");
  41. if (animation.dualClipBlendData.clip1.animationClip == null)
  42. sbErrors.AppendLine("Animation Clip 1 is mandatory");
  43. if (animation.dualClipBlendData.clip1.animationClip == null)
  44. sbErrors.AppendLine("Animation Clip 2 is mandatory");
  45. if (animation.dualClipBlendData.nbrOfInBetweenSamples < 2)
  46. sbErrors.AppendLine("Nbr of in between samples must be at least 2");
  47. if (animation.dualClipBlendData.nbrOfInBetweenSamples > 100)
  48. sbErrors.AppendLine("Nbr of in between samples is maximum 100");
  49. }
  50. foreach (AnimatorParameter parameterValue in animation.additionalAnimatorParameterValues)
  51. {
  52. if (string.IsNullOrWhiteSpace(parameterValue.parameterName))
  53. sbErrors.AppendLine("Additional animator parameter name is mandatory");
  54. }
  55. }
  56. bool foundDouble = false;
  57. for (int i = 0; i < bakerData.animations.Length; i++)
  58. {
  59. for (int j = i + 1; j < bakerData.animations.Length; j++)
  60. {
  61. if (bakerData.animations[i].animationID == bakerData.animations[j].animationID)
  62. {
  63. foundDouble = true; break;
  64. }
  65. }
  66. if(foundDouble) break;
  67. }
  68. if(foundDouble) sbErrors.AppendLine("Animation IDs must be unique");
  69. foreach (AttachmentAnchor attachmentAnchor in bakerData.attachmentAnchors)
  70. {
  71. if (string.IsNullOrWhiteSpace(attachmentAnchor.attachmentAnchorID))
  72. sbErrors.AppendLine("Attachment Anchor ID is mandatory");
  73. if(attachmentAnchor.attachmentAnchorTransform == null)
  74. sbErrors.AppendLine("Attachment Anchor reference transform is mandatory");
  75. else if(!hasParent(attachmentAnchor.attachmentAnchorTransform, sourceModel.transform))
  76. sbErrors.AppendLine($"Attachment Anchor reference transform must be nested inside {sourceModel.name}");
  77. }
  78. if(bakerData.boneUsage.numberOfBonesPerVertex < 1)
  79. sbErrors.AppendLine("Nbr of bones per vertex must be at least 1");
  80. foreach (BoneUsagePerLoD boneUsagePerLOD in bakerData.boneUsage.boneUsagesPerLoD)
  81. {
  82. if(boneUsagePerLOD.maxNumberOfBonesPerVertex < 1)
  83. sbErrors.AppendLine("Nbr of bones per vertex must be at least 1");
  84. }
  85. errors = sbErrors.ToString();
  86. return sbErrors.Length == 0;
  87. }
  88. private static bool hasParent(Transform child, Transform parent)
  89. {
  90. if (child.parent == null) return false;
  91. if (child.parent.gameObject == parent.gameObject) return true;
  92. else return hasParent(child.parent, parent);
  93. }
  94. public static GameObject GenerateAnimationObject(string assetPath,
  95. GpuEcsAnimationBakerData bakerData, string animatorName, string generatedAssetsFolder,
  96. string nameSuffixAsset = "_GpuEcsAnimator",
  97. string nameSuffixAnimationIDsEnum = "_AnimationIDs",
  98. string nameSuffixAnimationInitializerBehaviour = "_AnimationInitializerBehaviour",
  99. string nameSuffixAnimationEventIDsEnum = "_AnimationEventIDs",
  100. string nameSuffixAnimationAnchorIDsEnum = "_AttachmentAnchorIDs",
  101. string nameSuffixAttachmentInitializerBehaviour = "_AttachmentInitializerBehaviour",
  102. string meshPartSuffix = "Mesh",
  103. string animationMatricesTexturePartSuffix = "AnimationMatricesTexture",
  104. string materialPartSuffix = "Material"
  105. )
  106. {
  107. GameObject sourceModel = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
  108. GameObject refModel = PrefabUtility.LoadPrefabContents(assetPath);
  109. Debug.Log($"Generating Animation object for {assetPath}");
  110. GameObject animationObject = GenerateAnimationObjectFromModel(refModel, sourceModel, bakerData, animatorName, generatedAssetsFolder,
  111. nameSuffixAsset, nameSuffixAnimationIDsEnum, nameSuffixAnimationInitializerBehaviour, nameSuffixAnimationEventIDsEnum,
  112. nameSuffixAnimationAnchorIDsEnum, nameSuffixAttachmentInitializerBehaviour, meshPartSuffix, animationMatricesTexturePartSuffix, materialPartSuffix);
  113. PrefabUtility.UnloadPrefabContents(refModel);
  114. return animationObject;
  115. }
  116. public static GameObject GenerateAnimationObjectFromModel(GameObject refModel, GameObject sourceModel,
  117. GpuEcsAnimationBakerData bakerData, string animatorName, string generatedAssetsFolder,
  118. string nameSuffixAsset = "_GpuEcsAnimator",
  119. string nameSuffixAnimationIDsEnum = "_AnimationIDs",
  120. string nameSuffixAnimationInitializerBehaviour = "_AnimationInitializerBehaviour",
  121. string nameSuffixAnimationEventIDsEnum = "_AnimationEventIDs",
  122. string nameSuffixAnimationAnchorIDsEnum = "_AttachmentAnchorIDs",
  123. string nameSuffixAttachmentInitializerBehaviour = "_AttachmentInitializerBehaviour",
  124. string meshPartSuffix = "Mesh",
  125. string animationMatricesTexturePartSuffix = "AnimationMatricesTexture",
  126. string materialPartSuffix = "Material"
  127. )
  128. {
  129. if(!ValidateAnimationBakerData(bakerData, sourceModel, out string errors))
  130. {
  131. Debug.LogError(errors);
  132. return null;
  133. }
  134. string targetAssetPath =
  135. Path.Combine(generatedAssetsFolder, $"{animatorName}{nameSuffixAsset}.prefab");
  136. string targetAnimationEnumAssetPath =
  137. Path.Combine(generatedAssetsFolder, $"{animatorName}{nameSuffixAnimationIDsEnum}.cs");
  138. string targetAnimationInitializerBehaviourAssetPath =
  139. Path.Combine(generatedAssetsFolder, $"{animatorName}{nameSuffixAnimationInitializerBehaviour}.cs");
  140. string targetAnimationEventEnumAssetPath =
  141. Path.Combine(generatedAssetsFolder, $"{animatorName}{nameSuffixAnimationEventIDsEnum}.cs");
  142. string targetAttachmentAnchorEnumAssetPath =
  143. Path.Combine(generatedAssetsFolder, $"{animatorName}{nameSuffixAnimationAnchorIDsEnum}.cs");
  144. string targetAttachmentInitializerBehaviourAssetPath =
  145. Path.Combine(generatedAssetsFolder, $"{animatorName}{nameSuffixAttachmentInitializerBehaviour}.cs");
  146. refModel.transform.position = Vector3.zero;
  147. refModel.transform.rotation = quaternion.identity;
  148. Animator refModelAnimator = refModel.GetComponent<Animator>();
  149. refModelAnimator.cullingMode = AnimatorCullingMode.AlwaysAnimate;
  150. LODGroup refModelLoDGroup = refModel.GetComponent<LODGroup>();
  151. LOD[] refModelLoDs = refModelLoDGroup == null ? null : refModelLoDGroup.GetLODs();
  152. List<AnimationMatricesTexture> animationMatricesTexturesCache = new List<AnimationMatricesTexture>();
  153. List<EcsGpuMaterial> ecsGpuMaterialsCache = new List<EcsGpuMaterial>();
  154. GameObject test = AssetDatabase.LoadAssetAtPath<GameObject>(targetAssetPath);
  155. if (test == null)
  156. {
  157. GameObject empty = new GameObject(refModel.name);
  158. PrefabUtility.SaveAsPrefabAsset(empty, targetAssetPath);
  159. GameObject.DestroyImmediate(empty, allowDestroyingAssets:true);
  160. }
  161. GameObject target = PrefabUtility.LoadPrefabContents(targetAssetPath);
  162. target.transform.localScale = refModel.transform.localScale;
  163. GameObject[] toDestroy = new GameObject[target.transform.childCount];
  164. for (int childIndex = 0; childIndex < target.transform.childCount; childIndex++)
  165. toDestroy[childIndex] = target.transform.GetChild(childIndex).gameObject;
  166. foreach(GameObject go in toDestroy) Object.DestroyImmediate(go, allowDestroyingAssets: true);
  167. RemoveComponent<MeshRenderer>(target);
  168. RemoveComponent<MeshFilter>(target);
  169. RemoveComponent<LODGroup>(target);
  170. RemoveComponent<GpuEcsAnimatorBehaviour>(target);
  171. RemoveComponent<GpuEcsAnimatedMeshBehaviour>(target);
  172. GpuEcsAnimatorBehaviour gpuEcsAnimator = AddGpuEcsAnimationBehaviour(target, bakerData, out string[] eventNames);
  173. gpuEcsAnimator.transformUsageFlags = bakerData.transformUsageFlagsParent;
  174. Dictionary<GameObject, GameObject> sourceToTargetMapping = new Dictionary<GameObject, GameObject>();
  175. ProcessAnimationObjectRecursive(refModel, target, sourceToTargetMapping, animatorName,
  176. meshPartSuffix, animationMatricesTexturePartSuffix, materialPartSuffix,
  177. refModelAnimator, refModelLoDs, animationMatricesTexturesCache, ecsGpuMaterialsCache,
  178. bakerData, gpuEcsAnimator.animations, gpuEcsAnimator.totalNbrOfFrames, generatedAssetsFolder, gpuEcsAnimator);
  179. if (refModelLoDs != null)
  180. {
  181. LODGroup targetLoDGroup = target.AddComponent<LODGroup>();
  182. LOD[] targetLoDs = new LOD[refModelLoDs.Length];
  183. for (int lodIndex = 0; lodIndex < refModelLoDs.Length; lodIndex++)
  184. {
  185. targetLoDs[lodIndex] = refModelLoDs[lodIndex];
  186. Renderer[] refModelRenderers = refModelLoDs[lodIndex].renderers;
  187. for (int rendererIndex = 0; rendererIndex < refModelRenderers.Length; rendererIndex++)
  188. {
  189. targetLoDs[lodIndex].renderers[rendererIndex] =
  190. sourceToTargetMapping[refModelRenderers[rendererIndex].gameObject].GetComponent<MeshRenderer>();
  191. }
  192. }
  193. targetLoDGroup.SetLODs(targetLoDs);
  194. }
  195. if(bakerData.generateAnimationIdsEnum)
  196. GenerateAnimationsEnumCode(bakerData, targetAnimationEnumAssetPath, targetAnimationInitializerBehaviourAssetPath);
  197. if(bakerData.generateAnimationEventIdsEnum)
  198. GenerateAnimationEventsEnumCode(bakerData, targetAnimationEventEnumAssetPath, eventNames );
  199. gpuEcsAnimator.nbrOfAttachmentAnchors = bakerData.attachmentAnchors.Length;
  200. gpuEcsAnimator.attachmentAnchorData = BakeAttachmentAnchorTransforms(generatedAssetsFolder, sourceModel, animatorName,
  201. refModelAnimator, bakerData, gpuEcsAnimator.animations, gpuEcsAnimator.totalNbrOfFrames);
  202. if(bakerData.generateAttachmentAnchorIdsEnum)
  203. GenerateAttachmentAnchorsEnumCode(bakerData, targetAttachmentAnchorEnumAssetPath, targetAttachmentInitializerBehaviourAssetPath);
  204. PrefabUtility.SaveAsPrefabAsset(target, targetAssetPath);
  205. PrefabUtility.UnloadPrefabContents(target);
  206. return AssetDatabase.LoadAssetAtPath<GameObject>(targetAssetPath);
  207. }
  208. private const string enumFileTemplate = "namespace GPUECSAnimationBaker.Engine.AnimatorSystem\n"
  209. + "{\n"
  210. + " public enum @ENUMNAME@\n"
  211. + " {\n"
  212. + "@IDLIST@\n"
  213. + " }\n"
  214. + "}";
  215. private const string behaviourFileTemplate = "namespace GPUECSAnimationBaker.Engine.AnimatorSystem\n"
  216. + "{\n"
  217. + " public class @CLASSNAME_@ENUMNAME@_Behaviour : @CLASSNAMEBehaviour<@ENUMNAME@> { } \n"
  218. + "}";
  219. private static void GenerateAnimationsEnumCode(GpuEcsAnimationBakerData bakerData, string targetEnumAssetPath, string targetEnumBehaviourAssetPath)
  220. {
  221. GenerateEnumCodeFiles<AnimationData>($"AnimationIds{bakerData.animationIdsEnumName}", "GpuEcsAnimatorInitializer",
  222. bakerData.animations, (animationData) => animationData.animationID, targetEnumAssetPath, targetEnumBehaviourAssetPath);
  223. }
  224. private static void GenerateAnimationEventsEnumCode(GpuEcsAnimationBakerData bakerData, string targetEnumAssetPath, string[] eventNames)
  225. {
  226. GenerateEnumCodeFiles<string>($"AnimationEventIds{bakerData.animationEventIdsEnumName}", null,
  227. eventNames, (name) => name, targetEnumAssetPath, null);
  228. }
  229. private static void GenerateAttachmentAnchorsEnumCode(GpuEcsAnimationBakerData bakerData, string targetEnumAssetPath, string targetEnumBehaviourAssetPath)
  230. {
  231. GenerateEnumCodeFiles<AttachmentAnchor>($"AnchorIds{bakerData.attachmentAnchorIdsEnumName}", "GpuEcsAttachmentInitializer",
  232. bakerData.attachmentAnchors, (attachmentAnchor) => attachmentAnchor.attachmentAnchorID, targetEnumAssetPath, targetEnumBehaviourAssetPath);
  233. }
  234. private static void GenerateEnumCodeFiles<T>(string enumName, string className, T[] list, System.Func<T, string> listIdGetter,
  235. string targetEnumAssetPath, string targetEnumBehaviourAssetPath)
  236. {
  237. StringBuilder idList = new StringBuilder();
  238. for (int index = 0; index < list.Length; index++)
  239. {
  240. idList.Append($" {listIdGetter(list[index])} = {index.ToString()}");
  241. if (index < list.Length - 1)
  242. {
  243. idList.AppendLine(",");
  244. }
  245. }
  246. string enumCodeText = enumFileTemplate
  247. .Replace("@ENUMNAME@", enumName)
  248. .Replace("@IDLIST@", idList.ToString());
  249. string enumCodePath = Path.Combine(Application.dataPath, "../" + targetEnumAssetPath);
  250. File.WriteAllText(enumCodePath, enumCodeText);
  251. if (targetEnumBehaviourAssetPath != null)
  252. {
  253. string enumBehaviourText = behaviourFileTemplate
  254. .Replace("@CLASSNAME", className)
  255. .Replace("@ENUMNAME@", enumName);
  256. string enumBehaviourPath = Path.Combine(Application.dataPath, "../" + targetEnumBehaviourAssetPath);
  257. File.WriteAllText(enumBehaviourPath, enumBehaviourText);
  258. }
  259. AssetDatabase.Refresh();
  260. }
  261. private static GpuEcsAnimatorBehaviour AddGpuEcsAnimationBehaviour(GameObject target,
  262. GpuEcsAnimationBakerData bakerData, out string[] eventNames)
  263. {
  264. GpuEcsAnimatorBehaviour gpuEcsAnimator = target.AddComponent<GpuEcsAnimatorBehaviour>();
  265. gpuEcsAnimator.animations = new GpuEcsAnimationData[bakerData.animations.Length];
  266. // gpuEcsAnimator.BackAnimationDatas= new AnimationData[bakerData.animations.Length];
  267. List<GpuEcsAnimationEventOccurence> occurences = new List<GpuEcsAnimationEventOccurence>();
  268. List<string> foundEvents = new List<string>();
  269. int currentFrameIndex = 0;
  270. int currentEventOccurenceId = 0;
  271. int currentFoundEventId = 0;
  272. for (int animationIndex = 0; animationIndex < bakerData.animations.Length; animationIndex++)
  273. {
  274. AnimationData animationData = bakerData.animations[animationIndex];
  275. int nbrOfFramesPerSample = 0;
  276. int nbrOfInBetweenSamples = 0;
  277. float blendTimeCorrection = 1;
  278. int startEventOccurenceId = currentEventOccurenceId;
  279. int nbrOfEventOccurenceIds = 0;
  280. if (animationData.animationType == AnimationTypes.SingleClip)
  281. {
  282. SingleClipData singleClipData = animationData.singleClipData;
  283. nbrOfFramesPerSample = (int)(singleClipData.animationClip.length * GlobalConstants.SampleFrameRate) + 1;
  284. nbrOfInBetweenSamples = 1;
  285. blendTimeCorrection = 1;
  286. AddAnimationEvents(bakerData, animationData, occurences, ref currentEventOccurenceId, ref nbrOfEventOccurenceIds, foundEvents,
  287. ref currentFoundEventId, singleClipData.animationClip);
  288. }
  289. else if (animationData.animationType == AnimationTypes.DualClipBlend)
  290. {
  291. DualClipBlendData dualClipBlendData = animationData.dualClipBlendData;
  292. int clip1NbrOfFrames = (int)(dualClipBlendData.clip1.animationClip.length * GlobalConstants.SampleFrameRate) + 1;
  293. int clip2NbrOfFrames = (int)(dualClipBlendData.clip2.animationClip.length * GlobalConstants.SampleFrameRate) + 1;
  294. nbrOfFramesPerSample = math.max(clip1NbrOfFrames, clip2NbrOfFrames);
  295. blendTimeCorrection = dualClipBlendData.clip1.animationClip.length / dualClipBlendData.clip2.animationClip.length;
  296. nbrOfInBetweenSamples = dualClipBlendData.nbrOfInBetweenSamples;
  297. AddAnimationEvents(bakerData, animationData, occurences, ref currentEventOccurenceId, ref nbrOfEventOccurenceIds, foundEvents,
  298. ref currentFoundEventId, dualClipBlendData.clip1.animationClip);
  299. AddAnimationEvents(bakerData, animationData, occurences, ref currentEventOccurenceId, ref nbrOfEventOccurenceIds, foundEvents,
  300. ref currentFoundEventId, dualClipBlendData.clip2.animationClip);
  301. }
  302. bool loop = animationData.loop;
  303. gpuEcsAnimator.animations[animationIndex] = new GpuEcsAnimationData()
  304. {
  305. startFrameIndex = currentFrameIndex,
  306. nbrOfFramesPerSample = nbrOfFramesPerSample,
  307. nbrOfInBetweenSamples = nbrOfInBetweenSamples,
  308. blendTimeCorrection = blendTimeCorrection,
  309. startEventOccurenceId = startEventOccurenceId,
  310. nbrOfEventOccurenceIds = nbrOfEventOccurenceIds,
  311. loop = loop,
  312. animationID = animationData.animationID,
  313. stateName = animationData.animatorStateName
  314. };
  315. currentFrameIndex += nbrOfFramesPerSample * nbrOfInBetweenSamples;
  316. }
  317. gpuEcsAnimator.totalNbrOfFrames = currentFrameIndex;
  318. gpuEcsAnimator.animationEventOccurences = occurences.ToArray();
  319. eventNames = bakerData.usePredefinedAnimationEventIds ? bakerData.predefinedAnimationEventIds : foundEvents.ToArray();
  320. return gpuEcsAnimator;
  321. }
  322. private static void AddAnimationEvents(GpuEcsAnimationBakerData bakerData, AnimationData animationData,
  323. List<GpuEcsAnimationEventOccurence> occurences,
  324. ref int currentEventOccurenceId, ref int nbrOfEventOccurenceIds,
  325. List<string> foundEvents, ref int currentFoundEventId, AnimationClip animationClip)
  326. {
  327. AnimationEvent[] animationEvents = AnimationUtility.GetAnimationEvents(animationClip);
  328. foreach (AnimationEvent animationEvent in animationEvents)
  329. {
  330. int eventId = -1;
  331. if (bakerData.usePredefinedAnimationEventIds)
  332. {
  333. eventId = bakerData.predefinedAnimationEventIds.ToList().FindIndex((n) => n.Equals(animationEvent.stringParameter));
  334. if(eventId == -1) Debug.LogWarning(
  335. $"Found event {animationEvent.stringParameter} that is not in the predefined event Ids list, so it will be ignored.");
  336. }
  337. else
  338. {
  339. string name = $"{animationData.animationID}_{animationEvent.stringParameter}";
  340. eventId = foundEvents.FindIndex((n) => n.Equals(name));
  341. if (eventId == -1)
  342. {
  343. eventId = currentFoundEventId;
  344. foundEvents.Add(name);
  345. currentFoundEventId++;
  346. }
  347. }
  348. if (eventId != -1)
  349. {
  350. occurences.Add(new GpuEcsAnimationEventOccurence()
  351. {
  352. eventId = eventId,
  353. eventNormalizedTime = animationEvent.time / animationClip.length,
  354. });
  355. currentEventOccurenceId++;
  356. nbrOfEventOccurenceIds++;
  357. }
  358. }
  359. }
  360. private static void ProcessAnimationObjectRecursive(GameObject sourceNode, GameObject targetNode,
  361. Dictionary<GameObject, GameObject> sourceToTargetMapping, string animatorName, string meshPartSuffix,
  362. string animationMatricesTexturePartSuffix, string materialPartSuffix,
  363. Animator refModelAnimator, LOD[] refModelLoDs,
  364. List<AnimationMatricesTexture> animationMatricesTexturesCache, List<EcsGpuMaterial> ecsGpuMaterialsCache,
  365. GpuEcsAnimationBakerData bakerData, GpuEcsAnimationData[] animations, int totalNbrOfFrames,
  366. string generatedAssetsFolder, GpuEcsAnimatorBehaviour gpuEcsAnimator)
  367. {
  368. sourceToTargetMapping.Add(sourceNode, targetNode);
  369. SkinnedMeshRenderer existingSkinnedMeshRenderer = sourceNode.GetComponent<SkinnedMeshRenderer>();
  370. if (existingSkinnedMeshRenderer != null)
  371. {
  372. Texture2D animationMatricesTexture = BakeAndSaveAnimationMatricesTexture(
  373. animationMatricesTexturesCache, generatedAssetsFolder, animatorName, animationMatricesTexturePartSuffix,
  374. refModelAnimator, bakerData, existingSkinnedMeshRenderer, animations, totalNbrOfFrames);
  375. Mesh newMesh = BakeAndSaveBoneWeightsIntoMesh(refModelLoDs, generatedAssetsFolder, animatorName,
  376. meshPartSuffix, bakerData, existingSkinnedMeshRenderer);
  377. MeshFilter meshFilter = targetNode.AddComponent<MeshFilter>();
  378. MeshRenderer meshRenderer = targetNode.AddComponent<MeshRenderer>();
  379. Material newMaterial = CreateAndSaveEcsGpuMaterial(ecsGpuMaterialsCache, generatedAssetsFolder,
  380. animatorName, materialPartSuffix, existingSkinnedMeshRenderer);
  381. newMaterial.SetTexture(AnimatedBoneMatrices, animationMatricesTexture);
  382. meshRenderer.sharedMaterial = newMaterial;
  383. meshFilter.sharedMesh = newMesh;
  384. GpuEcsAnimatedMeshBehaviour gpuEcsAnimatedMesh = targetNode.AddComponent<GpuEcsAnimatedMeshBehaviour>();
  385. gpuEcsAnimatedMesh.animator = gpuEcsAnimator;
  386. gpuEcsAnimatedMesh.transformUsageFlags = bakerData.transformUsageFlagsChildren;
  387. }
  388. for (int childIndex = 0; childIndex < sourceNode.transform.childCount; childIndex++)
  389. {
  390. GameObject sourceChild = sourceNode.transform.GetChild(childIndex).gameObject;
  391. if (sourceChild.gameObject.activeSelf && !CheckEmptyRecursive(sourceChild))
  392. {
  393. GameObject targetChild = new GameObject(sourceChild.name);
  394. targetChild.transform.parent = targetNode.transform;
  395. targetChild.transform.localPosition = sourceChild.transform.localPosition;
  396. targetChild.transform.localRotation = sourceChild.transform.localRotation;
  397. targetChild.transform.localScale = sourceChild.transform.localScale;
  398. ProcessAnimationObjectRecursive(sourceChild, targetChild, sourceToTargetMapping,
  399. animatorName, meshPartSuffix, animationMatricesTexturePartSuffix, materialPartSuffix,
  400. refModelAnimator, refModelLoDs, animationMatricesTexturesCache, ecsGpuMaterialsCache,
  401. bakerData, animations, totalNbrOfFrames, generatedAssetsFolder, gpuEcsAnimator);
  402. }
  403. }
  404. }
  405. private static void RemoveComponent<T>(GameObject gameObject) where T : Component
  406. {
  407. T component = gameObject.GetComponent<T>();
  408. if(component != null) Object.DestroyImmediate(component, allowDestroyingAssets:true);
  409. }
  410. private static bool CheckEmptyRecursive(GameObject node)
  411. {
  412. Component[] components = node.GetComponents<Component>();
  413. bool empty = !components.Any(c => (c is SkinnedMeshRenderer));
  414. if (empty)
  415. {
  416. for (int childIndex = 0; childIndex < node.transform.childCount; childIndex++)
  417. {
  418. empty = CheckEmptyRecursive(node.transform.GetChild(childIndex).gameObject);
  419. if (!empty) break;
  420. }
  421. }
  422. return empty;
  423. }
  424. private static Mesh BakeAndSaveBoneWeightsIntoMesh(LOD[] refModelLoDs,
  425. string generatedAssetsFolder, string animatorName, string meshPartSuffix, GpuEcsAnimationBakerData bakerData,
  426. SkinnedMeshRenderer skinnedMeshRenderer)
  427. {
  428. int maxNumberOfBonesPerVertex = GetMaxNumberOfBonesPerVertex(refModelLoDs, bakerData, skinnedMeshRenderer);
  429. Mesh newMesh = BakeBoneWeightsIntoMesh(skinnedMeshRenderer.sharedMesh, maxNumberOfBonesPerVertex);
  430. SavePartAsAsset(newMesh, generatedAssetsFolder, animatorName, skinnedMeshRenderer, meshPartSuffix, "mesh");
  431. return newMesh;
  432. }
  433. private static Texture2D BakeAndSaveAnimationMatricesTexture(List<AnimationMatricesTexture> cache,
  434. string generatedAssetsFolder, string animatorName, string animationMatricesTexturePartSuffix,
  435. Animator refModelAnimator, GpuEcsAnimationBakerData bakerData,
  436. SkinnedMeshRenderer skinnedMeshRenderer, GpuEcsAnimationData[] animations, int totalNbrOfFrames)
  437. {
  438. if (!CheckAnimationMatricesTextureInCache(cache, skinnedMeshRenderer, out Texture2D animationMatricesTexture))
  439. {
  440. animationMatricesTexture = BakeAnimationMatricesTexture(skinnedMeshRenderer, refModelAnimator, bakerData,
  441. animations, totalNbrOfFrames);
  442. SavePartAsAsset(animationMatricesTexture, generatedAssetsFolder, animatorName, skinnedMeshRenderer,
  443. animationMatricesTexturePartSuffix, "asset");
  444. cache.Add(new AnimationMatricesTexture()
  445. {
  446. texture = animationMatricesTexture,
  447. skinnedMeshRenderer = skinnedMeshRenderer
  448. });
  449. }
  450. return animationMatricesTexture;
  451. }
  452. private static Material CreateAndSaveEcsGpuMaterial(List<EcsGpuMaterial> cache,
  453. string generatedAssetsFolder, string animatorName, string materialPartSuffix,
  454. SkinnedMeshRenderer skinnedMeshRenderer)
  455. {
  456. if (!CheckEcsGpuMaterialInCache(cache, skinnedMeshRenderer, out Material ecsGpuMaterial))
  457. {
  458. ecsGpuMaterial = Object.Instantiate<Material>(skinnedMeshRenderer.sharedMaterial);
  459. ecsGpuMaterial.SetFloat(EnableAnimation, 0);
  460. SavePartAsAsset(ecsGpuMaterial, generatedAssetsFolder, animatorName, skinnedMeshRenderer,
  461. materialPartSuffix, "mat");
  462. cache.Add(new EcsGpuMaterial()
  463. {
  464. material = ecsGpuMaterial,
  465. skinnedMeshRenderer = skinnedMeshRenderer
  466. });
  467. }
  468. return ecsGpuMaterial;
  469. }
  470. private static string GenerateAssetPath(string generatedAssetsFolder, string animatorName,
  471. string assetName, string assetFileExtension)
  472. {
  473. return Path.Combine(generatedAssetsFolder, $"{animatorName}_{assetName}.{assetFileExtension}");
  474. }
  475. private static void SavePartAsAsset(Object asset, string generatedAssetsFolder, string animatorName,
  476. SkinnedMeshRenderer skinnedMeshRenderer, string partName, string assetFileExtension)
  477. {
  478. string assetName = $"{partName}_{skinnedMeshRenderer.name}";
  479. string assetPath = GenerateAssetPath(generatedAssetsFolder, animatorName, assetName, assetFileExtension);
  480. AssetDatabase.CreateAsset(asset, assetPath);
  481. }
  482. private static int GetMaxNumberOfBonesPerVertex(LOD[] refModelLoDs, GpuEcsAnimationBakerData bakerData,
  483. SkinnedMeshRenderer skinnedMeshRenderer)
  484. {
  485. int maxNumberOfBonesPerVertex = bakerData.boneUsage.numberOfBonesPerVertex;
  486. if (refModelLoDs != null)
  487. {
  488. for (int lodIndex = 0; lodIndex < refModelLoDs.Length; lodIndex++)
  489. {
  490. if (refModelLoDs[lodIndex].renderers.Any(r => r == (Renderer)skinnedMeshRenderer))
  491. {
  492. BoneUsagePerLoD boneUsagePerLoD = bakerData.boneUsage.boneUsagesPerLoD
  493. .SingleOrDefault(b => b.lodIndex == lodIndex);
  494. if (boneUsagePerLoD != null)
  495. maxNumberOfBonesPerVertex = boneUsagePerLoD.maxNumberOfBonesPerVertex;
  496. break;
  497. }
  498. }
  499. }
  500. return maxNumberOfBonesPerVertex;
  501. }
  502. private static bool CheckAnimationMatricesTextureInCache(List<AnimationMatricesTexture> cache,
  503. SkinnedMeshRenderer skinnedMeshRenderer, out Texture2D foundTexture)
  504. {
  505. bool found = false;
  506. foundTexture = null;
  507. foreach (AnimationMatricesTexture cachedTexture in cache)
  508. {
  509. if (CheckSkinnedMeshRendererHasSameBones(cachedTexture.skinnedMeshRenderer,
  510. skinnedMeshRenderer))
  511. {
  512. found = true;
  513. foundTexture = cachedTexture.texture;
  514. break;
  515. }
  516. }
  517. return found;
  518. }
  519. private static bool CheckEcsGpuMaterialInCache(List<EcsGpuMaterial> cache,
  520. SkinnedMeshRenderer skinnedMeshRenderer, out Material foundMaterial)
  521. {
  522. bool found = false;
  523. foundMaterial = null;
  524. foreach (EcsGpuMaterial cachedEcsGpuMaterial in cache)
  525. {
  526. if (cachedEcsGpuMaterial.skinnedMeshRenderer.sharedMaterial == skinnedMeshRenderer.sharedMaterial
  527. && CheckSkinnedMeshRendererHasSameBones(cachedEcsGpuMaterial.skinnedMeshRenderer,
  528. skinnedMeshRenderer))
  529. {
  530. found = true;
  531. foundMaterial = cachedEcsGpuMaterial.material;
  532. break;
  533. }
  534. }
  535. return found;
  536. }
  537. private static bool CheckSkinnedMeshRendererHasSameBones(SkinnedMeshRenderer skinnedMeshRendererA,
  538. SkinnedMeshRenderer skinnedMeshRendererB)
  539. {
  540. bool hasSameBones = false;
  541. if (skinnedMeshRendererA.bones.Length == skinnedMeshRendererB.bones.Length)
  542. {
  543. hasSameBones = true;
  544. for (int i = 0; i < skinnedMeshRendererA.bones.Length; i++)
  545. {
  546. if (!CheckMatrixEquality(skinnedMeshRendererA.bones[i].localToWorldMatrix,
  547. skinnedMeshRendererB.bones[i].localToWorldMatrix))
  548. {
  549. hasSameBones = false; break;
  550. }
  551. if (!CheckMatrixEquality(skinnedMeshRendererA.sharedMesh.bindposes[i],
  552. skinnedMeshRendererB.sharedMesh.bindposes[i]))
  553. {
  554. hasSameBones = false; break;
  555. }
  556. }
  557. }
  558. return hasSameBones;
  559. }
  560. private static bool CheckMatrixEquality(Matrix4x4 m1, Matrix4x4 m2)
  561. {
  562. return m1.m00 == m2.m00 && m1.m01 == m2.m01 && m1.m02 == m2.m02 && m1.m03 == m2.m03
  563. && m1.m10 == m2.m10 && m1.m11 == m2.m11 && m1.m12 == m2.m12 && m1.m13 == m2.m13
  564. && m1.m20 == m2.m20 && m1.m21 == m2.m21 && m1.m22 == m2.m22 && m1.m23 == m2.m23
  565. && m1.m30 == m2.m30 && m1.m31 == m2.m31 && m1.m32 == m2.m32 && m1.m33 == m2.m33;
  566. }
  567. private static void IterateOverAllFramesThroughAnimator(
  568. Animator refModelAnimator, GpuEcsAnimationBakerData bakerData, GpuEcsAnimationData[] animations,
  569. System.Action<int> actionPerFrame)
  570. {
  571. refModelAnimator.speed = 0;
  572. int currentFrameIndex = 0;
  573. for (int animationIndex = 0; animationIndex < bakerData.animations.Length; animationIndex++)
  574. {
  575. AnimationData animationData = bakerData.animations[animationIndex];
  576. GpuEcsAnimationData gpuEcsAnimationData = animations[animationIndex];
  577. foreach (AnimatorParameter parameterValue in animationData.additionalAnimatorParameterValues)
  578. {
  579. if(parameterValue.parameterType == AnimatorParameterTypes.Bool)
  580. refModelAnimator.SetBool(parameterValue.parameterName, parameterValue.boolValue);
  581. else if(parameterValue.parameterType == AnimatorParameterTypes.Float)
  582. refModelAnimator.SetFloat(parameterValue.parameterName, parameterValue.floatValue);
  583. else if(parameterValue.parameterType == AnimatorParameterTypes.Integer)
  584. refModelAnimator.SetInteger(parameterValue.parameterName, parameterValue.intValue);
  585. }
  586. for (int sampleIndex = 0; sampleIndex < gpuEcsAnimationData.nbrOfInBetweenSamples; sampleIndex++)
  587. {
  588. if (animationData.animationType == AnimationTypes.DualClipBlend)
  589. {
  590. DualClipBlendData dualClipBlendData = animationData.dualClipBlendData;
  591. float sampleRatio = (float)sampleIndex / (float)(gpuEcsAnimationData.nbrOfInBetweenSamples - 1);
  592. float blendValue = dualClipBlendData.clip1.parameterValue +
  593. (dualClipBlendData.clip2.parameterValue - dualClipBlendData.clip1.parameterValue) * sampleRatio;
  594. refModelAnimator.SetFloat(dualClipBlendData.blendParameterName, blendValue);
  595. }
  596. for (int frameIndex = 0; frameIndex < gpuEcsAnimationData.nbrOfFramesPerSample; frameIndex++)
  597. {
  598. float progressRatio = (float)frameIndex / (float)(gpuEcsAnimationData.nbrOfFramesPerSample - 1);
  599. refModelAnimator.Play(animationData.animatorStateName, -1, progressRatio);
  600. foreach(AnimatorState animatorState in animationData.additionalAnimatorStatesPerLayer)
  601. refModelAnimator.Play(animatorState.stateName, animatorState.layer, progressRatio);
  602. refModelAnimator.Update(0);
  603. actionPerFrame(currentFrameIndex);
  604. currentFrameIndex++;
  605. }
  606. }
  607. }
  608. }
  609. private static GpuEcsAttachmentAnchorData BakeAttachmentAnchorTransforms(string generatedAssetsFolder, GameObject sourceModel,
  610. string animatorName, Animator refModelAnimator, GpuEcsAnimationBakerData bakerData, GpuEcsAnimationData[] animations,
  611. int totalNbrOfFrames)
  612. {
  613. int nbrOfAttachments = bakerData.attachmentAnchors.Length;
  614. if (nbrOfAttachments == 0) return null;
  615. else
  616. {
  617. GpuEcsAttachmentAnchorData gpuEcsAttachmentAnchorData = ScriptableObject.CreateInstance<GpuEcsAttachmentAnchorData>();
  618. gpuEcsAttachmentAnchorData.anchorTransforms = new float4x4[totalNbrOfFrames * nbrOfAttachments];
  619. for (int attachmentAnchorIndex = 0; attachmentAnchorIndex < nbrOfAttachments; attachmentAnchorIndex++)
  620. {
  621. AttachmentAnchor attachmentAnchor = bakerData.attachmentAnchors[attachmentAnchorIndex];
  622. Transform referenceAnchorTransform = attachmentAnchor.attachmentAnchorTransform;
  623. Stack<int> siblingIndexStack = new Stack<int>();
  624. GetSiblingIndexStack(referenceAnchorTransform, sourceModel.transform, siblingIndexStack);
  625. Transform anchorTransform = refModelAnimator.transform;
  626. while (siblingIndexStack.Count > 0)
  627. anchorTransform = anchorTransform.GetChild(siblingIndexStack.Pop());
  628. Debug.Assert(anchorTransform.name.Equals(referenceAnchorTransform.name));
  629. int baseIndex = attachmentAnchorIndex * totalNbrOfFrames;
  630. IterateOverAllFramesThroughAnimator(refModelAnimator, bakerData, animations,
  631. (currentFrameIndex) =>
  632. {
  633. gpuEcsAttachmentAnchorData.anchorTransforms[baseIndex + currentFrameIndex] = anchorTransform.localToWorldMatrix;
  634. });
  635. }
  636. string assetPath = GenerateAssetPath(generatedAssetsFolder, animatorName, "AttachmentAnchors", "asset");
  637. AssetDatabase.CreateAsset(gpuEcsAttachmentAnchorData, assetPath);
  638. return gpuEcsAttachmentAnchorData;
  639. }
  640. }
  641. private static void GetSiblingIndexStack(Transform current, Transform root, Stack<int> currentStack)
  642. {
  643. if(current.gameObject == root.gameObject) return;
  644. currentStack.Push(current.GetSiblingIndex());
  645. GetSiblingIndexStack(current.parent, root, currentStack);
  646. }
  647. private static Texture2D BakeAnimationMatricesTexture(SkinnedMeshRenderer skinnedMeshRenderer,
  648. Animator refModelAnimator, GpuEcsAnimationBakerData bakerData, GpuEcsAnimationData[] animations,
  649. int totalNbrOfFrames)
  650. {
  651. int boneCount = skinnedMeshRenderer.bones.Length;
  652. int animationMatricesTextureWidth = boneCount * 3;
  653. NativeArray<half4> animatedBoneMatricesTextureData = new NativeArray<half4>(
  654. totalNbrOfFrames * animationMatricesTextureWidth, Allocator.Temp);
  655. float4x4 invSkinnedMeshRendererLocalToWorld = math.inverse(skinnedMeshRenderer.transform.localToWorldMatrix);
  656. IterateOverAllFramesThroughAnimator(refModelAnimator, bakerData, animations,
  657. (currentFrameIndex) =>
  658. {
  659. for (int boneIndex = 0; boneIndex < boneCount; boneIndex++)
  660. {
  661. Transform boneTransform = skinnedMeshRenderer.bones[boneIndex];
  662. if (boneTransform != null)
  663. {
  664. float4x4 matrix = math.mul(
  665. math.mul(invSkinnedMeshRendererLocalToWorld, boneTransform.localToWorldMatrix),
  666. skinnedMeshRenderer.sharedMesh.bindposes[boneIndex]
  667. );
  668. Debug.Assert(Mathf.Approximately(matrix[0][3], 0f)
  669. && Mathf.Approximately(matrix[1][3], 0f)
  670. && Mathf.Approximately(matrix[2][3], 0f)
  671. && Mathf.Approximately(matrix[3][3], 1f), "matrix row 4 must be 0,0,0,1");
  672. int matrixIndex = (currentFrameIndex * animationMatricesTextureWidth) + (boneIndex * 3);
  673. for (int i = 0; i < 3; i++)
  674. animatedBoneMatricesTextureData[matrixIndex + i] = new half4(
  675. (half)matrix[0][i], (half)matrix[1][i], (half)matrix[2][i], (half)matrix[3][i]);
  676. }
  677. }
  678. });
  679. Texture2D animationMatricesTexture = new Texture2D(animationMatricesTextureWidth, totalNbrOfFrames,
  680. TextureFormat.RGBAHalf, false);
  681. animationMatricesTexture.SetPixelData(animatedBoneMatricesTextureData, 0);
  682. animationMatricesTexture.Apply();
  683. animatedBoneMatricesTextureData.Dispose();
  684. return animationMatricesTexture;
  685. }
  686. private static Mesh BakeBoneWeightsIntoMesh(Mesh sourceMesh, int maxNumberOfBonesPerVertex)
  687. {
  688. Mesh mesh = GameObject.Instantiate(sourceMesh);
  689. int vertexCount = mesh.vertices.Length;
  690. NativeArray<Vector4> uvs1 = new NativeArray<Vector4>(vertexCount, Allocator.Temp);
  691. NativeArray<Vector4> uvs2 = new NativeArray<Vector4>(vertexCount, Allocator.Temp);
  692. NativeArray<Vector4> uvs3 = new NativeArray<Vector4>(vertexCount, Allocator.Temp);
  693. NativeArray<byte> bonesPerVertex = mesh.GetBonesPerVertex();
  694. NativeArray<BoneWeight1> boneWeightsSource = mesh.GetAllBoneWeights();
  695. int startBoneWeightIndex = 0;
  696. for (int vertIndex = 0; vertIndex < vertexCount; vertIndex++)
  697. {
  698. float totalWeight = 0f;
  699. float totalWeightCapped = 0f;
  700. int numberOfBonesForThisVertex = bonesPerVertex[vertIndex];
  701. int boneWeightIndexTemp = startBoneWeightIndex;
  702. for (int i = 0; i < numberOfBonesForThisVertex; i++)
  703. {
  704. BoneWeight1 currentBoneWeight = boneWeightsSource[boneWeightIndexTemp];
  705. totalWeight += currentBoneWeight.weight;
  706. if (i < maxNumberOfBonesPerVertex) totalWeightCapped += currentBoneWeight.weight;
  707. if (i > 0) Debug.Assert(boneWeightsSource[boneWeightIndexTemp - 1].weight >= currentBoneWeight.weight);
  708. boneWeightIndexTemp++;
  709. }
  710. Debug.Assert(Mathf.Approximately(1f, totalWeight));
  711. float weightMultiplier = totalWeight / totalWeightCapped;
  712. int nbrOfBonesToBake = math.min(maxNumberOfBonesPerVertex, numberOfBonesForThisVertex);
  713. totalWeight = 0f;
  714. boneWeightIndexTemp = startBoneWeightIndex;
  715. float4 uv1 = float4.zero;
  716. float4 uv2 = float4.zero;
  717. float4 uv3 = float4.zero;
  718. for (int i = 0; i < nbrOfBonesToBake; i++)
  719. {
  720. BoneWeight1 currentBoneWeight = boneWeightsSource[boneWeightIndexTemp];
  721. float adjustedWeight = currentBoneWeight.weight * weightMultiplier;
  722. totalWeight += adjustedWeight;
  723. boneWeightIndexTemp++;
  724. if (i == 0) uv1 = new float4(currentBoneWeight.boneIndex, adjustedWeight, uv1.z, uv1.w);
  725. else if (i == 1) uv1 = new float4(uv1.x, uv1. y, currentBoneWeight.boneIndex, adjustedWeight);
  726. else if (i == 2) uv2 = new float4(currentBoneWeight.boneIndex, adjustedWeight, uv2.z, uv2.w);
  727. else if (i == 3) uv2 = new float4(uv2.x, uv2. y, currentBoneWeight.boneIndex, adjustedWeight);
  728. else if (i == 4) uv3 = new float4(currentBoneWeight.boneIndex, adjustedWeight, uv3.z, uv3.w);
  729. else if (i == 5) uv3 = new float4(uv3.x, uv3. y, currentBoneWeight.boneIndex, adjustedWeight);
  730. }
  731. Debug.Assert(Mathf.Approximately(1f, totalWeight));
  732. uvs1[vertIndex] = uv1;
  733. uvs2[vertIndex] = uv2;
  734. uvs3[vertIndex] = uv3;
  735. startBoneWeightIndex += numberOfBonesForThisVertex;
  736. }
  737. mesh.SetUVs(1, uvs1);
  738. mesh.SetUVs(2, uvs2);
  739. mesh.SetUVs(3, uvs3);
  740. uvs1.Dispose();
  741. uvs2.Dispose();
  742. uvs3.Dispose();
  743. bonesPerVertex.Dispose();
  744. boneWeightsSource.Dispose();
  745. return mesh;
  746. }
  747. }
  748. }
  749. #endif