123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class MaterialMgr
- {
- public MaterialMgr(TBodySkin tbskin)
- {
- this.m_tbSkin = tbskin;
- this.m_SlotId = tbskin.SlotId;
- this.m_tbody = tbskin.body;
- this.m_bMan = tbskin.body.boMAN;
- }
- public void Init(GameObject obj, Material[] mats)
- {
- this.m_materials = mats;
- }
- public void Remove()
- {
- this.m_materials = null;
- this.m_tbSkin.body.GetSlot(0).MaterialMgr.RemoveSkinMask(this.m_SlotId, this.m_tbSkin.m_subNo);
- this.RemoveSkinMaskAll();
- }
- public Material GetMaterial(int f_nMatNo)
- {
- if (this.m_materials == null)
- {
- return null;
- }
- foreach (Transform transform in this.m_tbSkin.obj.transform.GetComponentsInChildren<Transform>(true))
- {
- Renderer component = transform.GetComponent<Renderer>();
- if (component != null && component.material != null)
- {
- if (f_nMatNo < component.materials.Length)
- {
- return component.materials[f_nMatNo];
- }
- NDebug.Assert(string.Concat(new object[]
- {
- "マテリアル番号指定が ",
- component.name,
- " のマテリアル数を超えています。",
- this.m_SlotId.ToString(),
- " / ",
- f_nMatNo
- }), false);
- }
- }
- return null;
- }
- public int materialCount
- {
- get
- {
- if (this.m_materials == null)
- {
- return -1;
- }
- return this.m_materials.Length;
- }
- }
- public float MayuThick
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[3];
- float num = 0.5f * (value - 0.5f) / -0.5f;
- material.SetTextureScale("_MainTex", new Vector2(1f, 1f + num));
- material.SetTextureOffset("_MainTex", new Vector2(0f, -(num / 2f)));
- }
- }
- }
- public float FutaePosX
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[9];
- float x = 0.05f * (value - 0.5f) / 0.5f;
- material.SetTextureOffset("_MainTex", new Vector2(x, material.GetTextureOffset("_MainTex").y));
- }
- }
- }
- public float FutaePosY
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[9];
- float num = 0.1f * (value - 0.5f) / 0.5f;
- material.SetTextureOffset("_MainTex", new Vector2(material.GetTextureOffset("_MainTex").x, -num));
- }
- }
- }
- public float FutaeRot
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[9];
- float value2 = 0.4f * (value - 0.5f) / 0.5f;
- material.EnableKeyword("USE_UV_ROT");
- material.SetFloat("_UVRotAngle", value2);
- }
- }
- }
- public float HitomiHiPosX
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[2];
- Material material2 = this.m_materials[7];
- float num = 0.05f * (value - 0.5f) / 0.5f;
- Vector2 textureOffset = material.GetTextureOffset("_MainTex");
- material.SetTextureOffset("_MainTex", new Vector2(-num, textureOffset.y));
- material2.SetTextureOffset("_MainTex", new Vector2(-num, textureOffset.y));
- }
- }
- }
- public float HitomiHiPosY
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[2];
- Material material2 = this.m_materials[7];
- float num = 0.1f * (value - 0.5f) / 0.5f;
- Vector2 textureOffset = material.GetTextureOffset("_MainTex");
- material.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -num + this.m_hitomiHiSclYOffsY));
- material2.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -num + this.m_hitomiHiSclYOffsY));
- this.m_hitomiHiPosYOffsY = -num;
- }
- }
- }
- public float HitomiHiSclY
- {
- set
- {
- if (this.m_tbSkin.m_bMan)
- {
- return;
- }
- if (this.m_SlotId == TBody.SlotID.head && 120 <= this.m_tbSkin.PartsVersion)
- {
- Material material = this.m_materials[2];
- Material material2 = this.m_materials[7];
- float num = -1f * (value - 0.5f) / 0.5f;
- Vector2 textureOffset = material.GetTextureOffset("_MainTex");
- material.SetTextureScale("_MainTex", new Vector2(1f, 1f + num));
- material2.SetTextureScale("_MainTex", new Vector2(1f, 1f + num));
- material.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -(num / 2f) + this.m_hitomiHiPosYOffsY));
- material2.SetTextureOffset("_MainTex", new Vector2(textureOffset.x, -(num / 2f) + this.m_hitomiHiPosYOffsY));
- this.m_hitomiHiSclYOffsY = -(num / 2f);
- }
- }
- }
- public void AddSkinMask(TBody.SlotID slot, int subNo, string texFileName)
- {
- NDebug.Assert(this.m_SlotId == TBody.SlotID.body, "skin mask destination is must be body.");
- if (this.m_skinMaskTexs == null)
- {
- this.m_skinMaskTexs = new Dictionary<uint, Texture2D>();
- }
- if (this.m_skinMaskDestTex == null)
- {
- this.m_skinMaskDestTex = new Texture2D(512, 512, TextureFormat.ARGB32, false);
- }
- TextureResource textureResource = ImportCM.LoadTexture(null, texFileName, false);
- NDebug.Assert(textureResource != null, "skin mask tex is not found. -> " + texFileName);
- Texture2D value = textureResource.CreateTexture2D();
- this.m_skinMaskTexs.Add((uint)(((uint)slot << 16) + subNo), value);
- this.m_isSkinMaskBody = true;
- }
- public void RemoveSkinMask(TBody.SlotID slot, int subNo)
- {
- if (!this.m_isSkinMaskBody)
- {
- return;
- }
- uint key = (uint)(((uint)slot << 16) + subNo);
- Texture2D obj;
- if (this.m_skinMaskTexs.TryGetValue(key, out obj))
- {
- UnityEngine.Object.Destroy(obj);
- this.m_skinMaskTexs.Remove(key);
- }
- }
- public void RemoveSkinMaskAll()
- {
- if (!this.m_isSkinMaskBody)
- {
- return;
- }
- foreach (KeyValuePair<uint, Texture2D> keyValuePair in this.m_skinMaskTexs)
- {
- if (keyValuePair.Value != null)
- {
- UnityEngine.Object.Destroy(keyValuePair.Value);
- }
- }
- this.m_skinMaskTexs = null;
- if (this.m_skinMaskDestTex != null)
- {
- UnityEngine.Object.Destroy(this.m_skinMaskDestTex);
- }
- this.m_isSkinMaskBody = false;
- }
- public bool FixSkinMask()
- {
- if (!this.m_isSkinMaskBody)
- {
- return false;
- }
- RenderTexture active = RenderTexture.active;
- RenderTexture temporary = RenderTexture.GetTemporary(this.m_skinMaskDestTex.width, this.m_skinMaskDestTex.height, 0, RenderTextureFormat.ARGB32);
- RenderTexture.active = temporary;
- GL.Clear(false, true, Color.white);
- Material systemMaterial = GameUty.GetSystemMaterial(GameUty.SystemMaterial.Multiply);
- foreach (KeyValuePair<uint, Texture2D> keyValuePair in this.m_skinMaskTexs)
- {
- TBodySkin slot = this.m_tbody.GetSlot((int)(keyValuePair.Key >> 16), (int)(keyValuePair.Key & 16U));
- if (slot.boVisible)
- {
- Graphics.Blit(keyValuePair.Value, temporary, systemMaterial);
- }
- }
- this.m_skinMaskDestTex.ReadPixels(new Rect(0f, 0f, (float)temporary.width, (float)temporary.height), 0, 0);
- this.m_skinMaskDestTex.Apply();
- RenderTexture.ReleaseTemporary(temporary);
- RenderTexture.active = active;
- return true;
- }
- public bool CheckSkinMaskUV(Vector2 uv)
- {
- 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;
- }
- private Material[] m_materials;
- private TBody m_tbody;
- private TBodySkin m_tbSkin;
- private TBody.SlotID m_SlotId;
- private bool m_bMan;
- private float m_hitomiHiPosYOffsY;
- private float m_hitomiHiSclYOffsY;
- private Dictionary<uint, Texture2D> m_skinMaskTexs;
- private Texture2D m_skinMaskDestTex;
- private bool m_isSkinMaskBody;
- }
|