MB_SerializableSourceBlendShape2CombinedMap.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DigitalOpus.MB.Core;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. /// <summary>
  8. /// It is possible to combine multiple skinned meshes each of which may have blend shapes.
  9. /// This builds a map for mapping source blend shapes to combined blend shapes.
  10. /// The map can be serialized and saved in a prefab, this makes it possible to save combined
  11. /// meshes with Blend Shapes in a prefab.
  12. /// </summary>
  13. [System.Serializable]
  14. public class SerializableSourceBlendShape2Combined
  15. {
  16. public GameObject[] srcGameObject;
  17. public int[] srcBlendShapeIdx;
  18. public GameObject[] combinedMeshTargetGameObject;
  19. public int[] blendShapeIdx;
  20. public void SetBuffers(GameObject[] srcGameObjs, int[] srcBlendShapeIdxs,
  21. GameObject[] targGameObjs, int[] targBlendShapeIdx)
  22. {
  23. Debug.Assert(srcGameObjs.Length == srcBlendShapeIdxs.Length &&
  24. srcGameObjs.Length == targGameObjs.Length &&
  25. srcGameObjs.Length == targBlendShapeIdx.Length);
  26. srcGameObject = srcGameObjs;
  27. srcBlendShapeIdx = srcBlendShapeIdxs;
  28. combinedMeshTargetGameObject = targGameObjs;
  29. blendShapeIdx = targBlendShapeIdx;
  30. }
  31. public void DebugPrint()
  32. {
  33. if (srcGameObject == null)
  34. {
  35. Debug.LogError("Empty");
  36. return;
  37. }
  38. else
  39. {
  40. for (int i = 0; i < srcGameObject.Length; i++)
  41. {
  42. Debug.LogFormat("{0} {1} {2} {3}", srcGameObject[i], srcBlendShapeIdx[i], combinedMeshTargetGameObject[i], blendShapeIdx[i]);
  43. }
  44. }
  45. }
  46. public Dictionary<MB3_MeshCombiner.MBBlendShapeKey, MB3_MeshCombiner.MBBlendShapeValue> GenerateMapFromSerializedData()
  47. {
  48. if (srcGameObject == null || srcBlendShapeIdx == null || combinedMeshTargetGameObject == null || blendShapeIdx == null ||
  49. srcGameObject.Length != srcBlendShapeIdx.Length ||
  50. srcGameObject.Length != combinedMeshTargetGameObject.Length ||
  51. srcGameObject.Length != blendShapeIdx.Length)
  52. {
  53. Debug.LogError("Error GenerateMapFromSerializedData. Serialized data was malformed or missing.");
  54. return null;
  55. }
  56. Dictionary<MB3_MeshCombiner.MBBlendShapeKey, MB3_MeshCombiner.MBBlendShapeValue> map = new Dictionary<MB3_MeshCombiner.MBBlendShapeKey, MB3_MeshCombiner.MBBlendShapeValue>();
  57. for (int i = 0; i < srcGameObject.Length; i++)
  58. {
  59. GameObject src = srcGameObject[i];
  60. GameObject targ = combinedMeshTargetGameObject[i];
  61. if (src == null || targ == null)
  62. {
  63. Debug.LogError("Error GenerateMapFromSerializedData. There were null references in the serialized data to source or target game objects. This can happen " +
  64. "if the SerializableSourceBlendShape2Combined was serialized in a prefab but the source and target SkinnedMeshRenderer GameObjects " +
  65. " were not.");
  66. return null;
  67. }
  68. map.Add(new MB3_MeshCombiner.MBBlendShapeKey(src, srcBlendShapeIdx[i]),
  69. new MB3_MeshCombiner.MBBlendShapeValue()
  70. {
  71. combinedMeshGameObject = targ,
  72. blendShapeIndex = blendShapeIdx[i]
  73. }
  74. );
  75. }
  76. return map;
  77. }
  78. }
  79. }