MaterialMgr.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MaterialMgr
  5. {
  6. public MaterialMgr(TBodySkin tbskin)
  7. {
  8. this.m_tbSkin = tbskin;
  9. this.m_SlotId = tbskin.SlotId;
  10. this.m_tbody = tbskin.body;
  11. this.m_bMan = tbskin.body.boMAN;
  12. }
  13. public void Init(GameObject obj, Material[] mats)
  14. {
  15. this.m_materials = mats;
  16. }
  17. public void Remove()
  18. {
  19. this.m_materials = null;
  20. this.m_tbSkin.body.GetSlot(0).MaterialMgr.RemoveSkinMask(this.m_SlotId, this.m_tbSkin.m_subNo);
  21. this.RemoveSkinMaskAll();
  22. }
  23. public Material GetMaterial(int f_nMatNo)
  24. {
  25. if (this.m_materials == null)
  26. {
  27. return null;
  28. }
  29. foreach (Transform transform in this.m_tbSkin.obj.transform.GetComponentsInChildren<Transform>(true))
  30. {
  31. Renderer component = transform.GetComponent<Renderer>();
  32. if (component != null && component.material != null)
  33. {
  34. if (f_nMatNo < component.materials.Length)
  35. {
  36. return component.materials[f_nMatNo];
  37. }
  38. NDebug.Assert(string.Concat(new object[]
  39. {
  40. "マテリアル番号指定が ",
  41. component.name,
  42. " のマテリアル数を超えています。",
  43. this.m_SlotId.ToString(),
  44. " / ",
  45. f_nMatNo
  46. }), false);
  47. }
  48. }
  49. return null;
  50. }
  51. public int materialCount
  52. {
  53. get
  54. {
  55. if (this.m_materials == null)
  56. {
  57. return -1;
  58. }
  59. return this.m_materials.Length;
  60. }
  61. }
  62. public float MayuThick
  63. {
  64. set
  65. {
  66. if (this.m_tbSkin.m_bMan)
  67. {
  68. return;
  69. }
  70. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  71. {
  72. Material material = this.m_materials[3];
  73. float num = 0.5f * (value - 0.5f) / -0.5f;
  74. material.SetTextureScale("_MainTex", new Vector2(1f, 1f + num));
  75. material.SetTextureOffset("_MainTex", new Vector2(0f, -(num / 2f)));
  76. }
  77. }
  78. }
  79. public float FutaePosX
  80. {
  81. set
  82. {
  83. if (this.m_tbSkin.m_bMan)
  84. {
  85. return;
  86. }
  87. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  88. {
  89. Material material = this.m_materials[9];
  90. float x = 0.05f * (value - 0.5f) / 0.5f;
  91. material.SetTextureOffset("_MainTex", new Vector2(x, material.GetTextureOffset("_MainTex").y));
  92. }
  93. }
  94. }
  95. public float FutaePosY
  96. {
  97. set
  98. {
  99. if (this.m_tbSkin.m_bMan)
  100. {
  101. return;
  102. }
  103. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  104. {
  105. Material material = this.m_materials[9];
  106. float num = 0.1f * (value - 0.5f) / 0.5f;
  107. material.SetTextureOffset("_MainTex", new Vector2(material.GetTextureOffset("_MainTex").x, -num));
  108. }
  109. }
  110. }
  111. public float FutaeRot
  112. {
  113. set
  114. {
  115. if (this.m_tbSkin.m_bMan)
  116. {
  117. return;
  118. }
  119. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  120. {
  121. Material material = this.m_materials[9];
  122. float value2 = 0.4f * (value - 0.5f) / 0.5f;
  123. material.EnableKeyword("USE_UV_ROT");
  124. material.SetFloat("_UVRotAngle", value2);
  125. }
  126. }
  127. }
  128. public float HitomiHiPosX
  129. {
  130. set
  131. {
  132. if (this.m_tbSkin.m_bMan)
  133. {
  134. return;
  135. }
  136. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  137. {
  138. Material material = this.m_materials[2];
  139. Material material2 = this.m_materials[7];
  140. float num = 0.05f * (value - 0.5f) / 0.5f;
  141. Vector2 textureOffset = material.GetTextureOffset("_MainTex");
  142. material.SetTextureOffset("_MainTex", new Vector2(-num, textureOffset.y));
  143. material2.SetTextureOffset("_MainTex", new Vector2(-num, textureOffset.y));
  144. }
  145. }
  146. }
  147. public float HitomiHiPosY
  148. {
  149. set
  150. {
  151. if (this.m_tbSkin.m_bMan)
  152. {
  153. return;
  154. }
  155. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  156. {
  157. Material material = this.m_materials[2];
  158. Material material2 = this.m_materials[7];
  159. float num = 0.1f * (value - 0.5f) / 0.5f;
  160. Vector2 textureOffset = material.GetTextureOffset("_MainTex");
  161. material.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -num + this.m_hitomiHiSclYOffsY));
  162. material2.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -num + this.m_hitomiHiSclYOffsY));
  163. this.m_hitomiHiPosYOffsY = -num;
  164. }
  165. }
  166. }
  167. public float HitomiHiSclY
  168. {
  169. set
  170. {
  171. if (this.m_tbSkin.m_bMan)
  172. {
  173. return;
  174. }
  175. if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
  176. {
  177. Material material = this.m_materials[2];
  178. Material material2 = this.m_materials[7];
  179. float num = -1f * (value - 0.5f) / 0.5f;
  180. Vector2 textureOffset = material.GetTextureOffset("_MainTex");
  181. material.SetTextureScale("_MainTex", new Vector2(1f, 1f + num));
  182. material2.SetTextureScale("_MainTex", new Vector2(1f, 1f + num));
  183. material.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -(num / 2f) + this.m_hitomiHiPosYOffsY));
  184. material2.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -(num / 2f) + this.m_hitomiHiPosYOffsY));
  185. this.m_hitomiHiSclYOffsY = -(num / 2f);
  186. }
  187. }
  188. }
  189. public void AddSkinMask(TBody.SlotID slot, int subNo, string texFileName)
  190. {
  191. NDebug.Assert(this.m_SlotId == TBody.SlotID.body, "skin mask destination is must be body.");
  192. if (this.m_skinMaskTexs == null)
  193. {
  194. this.m_skinMaskTexs = new Dictionary<uint, Texture2D>();
  195. }
  196. if (this.m_skinMaskDestTex == null)
  197. {
  198. this.m_skinMaskDestTex = new Texture2D(512, 512, TextureFormat.ARGB32, false);
  199. }
  200. TextureResource textureResource = ImportCM.LoadTexture(null, texFileName, false);
  201. NDebug.Assert(textureResource != null, "skin mask tex is not found. -> " + texFileName);
  202. Texture2D value = textureResource.CreateTexture2D();
  203. this.m_skinMaskTexs.Add((uint)(((uint)slot << 16) + subNo), value);
  204. this.m_isSkinMaskBody = true;
  205. }
  206. public void RemoveSkinMask(TBody.SlotID slot, int subNo)
  207. {
  208. if (!this.m_isSkinMaskBody)
  209. {
  210. return;
  211. }
  212. uint key = (uint)(((uint)slot << 16) + subNo);
  213. Texture2D obj;
  214. if (this.m_skinMaskTexs.TryGetValue(key, out obj))
  215. {
  216. UnityEngine.Object.Destroy(obj);
  217. this.m_skinMaskTexs.Remove(key);
  218. }
  219. }
  220. public void RemoveSkinMaskAll()
  221. {
  222. if (!this.m_isSkinMaskBody)
  223. {
  224. return;
  225. }
  226. foreach (KeyValuePair<uint, Texture2D> keyValuePair in this.m_skinMaskTexs)
  227. {
  228. if (keyValuePair.Value != null)
  229. {
  230. UnityEngine.Object.Destroy(keyValuePair.Value);
  231. }
  232. }
  233. this.m_skinMaskTexs = null;
  234. if (this.m_skinMaskDestTex != null)
  235. {
  236. UnityEngine.Object.Destroy(this.m_skinMaskDestTex);
  237. }
  238. this.m_isSkinMaskBody = false;
  239. }
  240. public bool FixSkinMask()
  241. {
  242. if (!this.m_isSkinMaskBody)
  243. {
  244. return false;
  245. }
  246. RenderTexture active = RenderTexture.active;
  247. RenderTexture temporary = RenderTexture.GetTemporary(this.m_skinMaskDestTex.width, this.m_skinMaskDestTex.height, 0, RenderTextureFormat.ARGB32);
  248. RenderTexture.active = temporary;
  249. GL.Clear(false, true, Color.white);
  250. Material systemMaterial = GameUty.GetSystemMaterial(GameUty.SystemMaterial.Multiply);
  251. foreach (KeyValuePair<uint, Texture2D> keyValuePair in this.m_skinMaskTexs)
  252. {
  253. TBodySkin slot = this.m_tbody.GetSlot((int)(keyValuePair.Key >> 16), (int)(keyValuePair.Key & 16U));
  254. if (slot.boVisible)
  255. {
  256. Graphics.Blit(keyValuePair.Value, temporary, systemMaterial);
  257. }
  258. }
  259. this.m_skinMaskDestTex.ReadPixels(new Rect(0f, 0f, (float)temporary.width, (float)temporary.height), 0, 0);
  260. this.m_skinMaskDestTex.Apply();
  261. RenderTexture.ReleaseTemporary(temporary);
  262. RenderTexture.active = active;
  263. return true;
  264. }
  265. public bool CheckSkinMaskUV(Vector2 uv)
  266. {
  267. return this.m_isSkinMaskBody && this.m_skinMaskDestTex.GetPixel((int)((float)this.m_skinMaskDestTex.width * uv.x + 0.5f), (int)((float)this.m_skinMaskDestTex.height * uv.y + 0.5f)).r < 0.5f;
  268. }
  269. private Material[] m_materials;
  270. private TBody m_tbody;
  271. private TBodySkin m_tbSkin;
  272. private TBody.SlotID m_SlotId;
  273. private bool m_bMan;
  274. private float m_hitomiHiPosYOffsY;
  275. private float m_hitomiHiSclYOffsY;
  276. private Dictionary<uint, Texture2D> m_skinMaskTexs;
  277. private Texture2D m_skinMaskDestTex;
  278. private bool m_isSkinMaskBody;
  279. }