| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using UnityEngine; |
| using UnityEngine; |
| |
| |
| public class MaterialVariation : MonoBehaviour |
| public class MaterialVariation : MonoBehaviour |
| { |
| { |
| private void Start() |
| private void Start() |
| { |
| { |
| this.m_nview = base.GetComponentInParent<ZNetView>(); |
| this.m_nview = base.GetComponentInParent<ZNetView>(); |
| . | |
| this.m_piece = base.GetComponentInParent<Piece>(); |
| this.m_renderer = base.GetComponent<SkinnedMeshRenderer>(); |
| this.m_renderer = base.GetComponent<SkinnedMeshRenderer>(); |
| . | |
| if (!this.m_renderer) |
| |
| { |
| |
| this.m_renderer = base.GetComponent<MeshRenderer>(); |
| |
| } |
| if (!this.m_nview || !this.m_renderer) |
| if (!this.m_nview || !this.m_renderer) |
| { |
| { |
| ZLog.LogError("Missing nview or renderer on '" + base.transform.gameObject.name + "'"); |
| ZLog.LogError("Missing nview or renderer on '" + base.transform.gameObject.name + "'"); |
| } |
| } |
| . | |
| base.InvokeRepeating("CheckMaterial", 0f, 0.2f); |
| } |
| } |
| |
| |
| . | private void Update() |
| private void CheckMaterial() |
| { |
| { |
| . | if (this.m_variation < 0 && this.m_nview && this.m_nview.GetZDO() != null && this.m_renderer) |
| if (((!this.m_isSet && this.m_variation < 0) || (this.m_isSet && this.m_renderer.materials[this.m_materialIndex].name != this.m_matName && (!this.m_piece || !Player.IsPlacementGhost(this.m_piece.gameObject)))) && this.m_nview && this.m_nview.GetZDO() != null && this.m_renderer) |
| { |
| { |
| this.m_variation = this.m_nview.GetZDO().GetInt("MatVar" + this.m_materialIndex.ToString(), -1); |
| this.m_variation = this.m_nview.GetZDO().GetInt("MatVar" + this.m_materialIndex.ToString(), -1); |
| if (this.m_variation < 0 && this.m_nview.IsOwner()) |
| if (this.m_variation < 0 && this.m_nview.IsOwner()) |
| { |
| { |
| this.m_variation = this.GetWeightedVariation(); |
| this.m_variation = this.GetWeightedVariation(); |
| this.m_nview.GetZDO().Set("MatVar" + this.m_materialIndex.ToString(), this.m_variation); |
| this.m_nview.GetZDO().Set("MatVar" + this.m_materialIndex.ToString(), this.m_variation); |
| } |
| } |
| if (this.m_variation >= 0) |
| if (this.m_variation >= 0) |
| { |
| { |
| Material[] materials = this.m_renderer.materials; |
| Material[] materials = this.m_renderer.materials; |
| materials[this.m_materialIndex] = this.m_materials[this.m_variation].m_material; |
| materials[this.m_materialIndex] = this.m_materials[this.m_variation].m_material; |
| this.m_renderer.materials = materials; |
| this.m_renderer.materials = materials; |
| . | |
| this.m_matName = this.m_renderer.materials[this.m_materialIndex].name; |
| |
| this.m_isSet = true; |
| } |
| } |
| } |
| } |
| . | |
| this.m_checks++; |
| |
| if (this.m_checks >= 5) |
| |
| { |
| |
| base.CancelInvoke("CheckMaterial"); |
| |
| } |
| } |
| } |
| |
| |
| private int GetWeightedVariation() |
| private int GetWeightedVariation() |
| { |
| { |
| float num = 0f; |
| float num = 0f; |
| foreach (MaterialVariation.MaterialEntry materialEntry in this.m_materials) |
| foreach (MaterialVariation.MaterialEntry materialEntry in this.m_materials) |
| { |
| { |
| num += materialEntry.m_weight; |
| num += materialEntry.m_weight; |
| } |
| } |
| float num2 = UnityEngine.Random.Range(0f, num); |
| float num2 = UnityEngine.Random.Range(0f, num); |
| float num3 = 0f; |
| float num3 = 0f; |
| for (int i = 0; i < this.m_materials.Count; i++) |
| for (int i = 0; i < this.m_materials.Count; i++) |
| { |
| { |
| num3 += this.m_materials[i].m_weight; |
| num3 += this.m_materials[i].m_weight; |
| if (num2 <= num3) |
| if (num2 <= num3) |
| { |
| { |
| return i; |
| return i; |
| } |
| } |
| } |
| } |
| return 0; |
| return 0; |
| } |
| } |
| |
| |
| public int m_materialIndex; |
| public int m_materialIndex; |
| |
| |
| public List<MaterialVariation.MaterialEntry> m_materials = new List<MaterialVariation.MaterialEntry>(); |
| public List<MaterialVariation.MaterialEntry> m_materials = new List<MaterialVariation.MaterialEntry>(); |
| |
| |
| private ZNetView m_nview; |
| private ZNetView m_nview; |
| |
| |
| . | private SkinnedMeshRenderer m_renderer; |
| private Renderer m_renderer; |
| |
| |
| private int m_variation = -1; |
| private int m_variation = -1; |
| . | |
| |
| |
| private string m_matName; |
| |
| |
| |
| private bool m_isSet; |
| |
| |
| |
| private Piece m_piece; |
| |
| |
| |
| private int m_checks; |
| |
| |
| [Serializable] |
| [Serializable] |
| public class MaterialEntry |
| public class MaterialEntry |
| { |
| { |
| public Material m_material; |
| public Material m_material; |
| |
| |
| public float m_weight = 1f; |
| public float m_weight = 1f; |
| } |
| } |
| } |
| } |
| |
| |