public static SMaterial FromMaterial(Material mat) { if (mat == null) return null; Shader shader = mat.shader; if (shader == null) return null; SMaterial result = new SMaterial(); result.instanceID = mat.GetInstanceID(); result.materialName = mat.name; result.shaderName = shader.name; ShaderProperties.Info info = ShaderPropertyHelper.GetShaderInfo(shader.name); if (info != null){ ComposedByteStream rawData = new ComposedByteStream(); int iMax = info.propertyNames.Length; for (int i = 0; i < iMax; i++) { string propName = info.propertyNames[i]; switch (info.propertyTypes[i]) { case ShaderProperties.PropType.Color: Color color = mat.GetColor(propName); rawData.AddStream(new float[] { color.r, color.g, color.b, color.a }); break; case ShaderProperties.PropType.Range: case ShaderProperties.PropType.Float: rawData.AddStream(new float[] { mat.GetFloat(propName) }); break; case ShaderProperties.PropType.TexEnv: Texture texture = mat.GetTexture(propName); Vector2 offset = mat.GetTextureOffset(propName); Vector2 scale = mat.GetTextureScale(propName); rawData.AddStream(new int[] { texture != null ? texture.GetInstanceID() : -1 }); rawData.AddStream(texture != null ? texture.name : "" ); rawData.AddStream(new float[] { offset.x, offset.y }); rawData.AddStream(new float[] { scale.x, scale.y }); break; case ShaderProperties.PropType.Vector: Vector4 vector = mat.GetVector(propName); rawData.AddStream(new float[] { vector.x, vector.y, vector.z, vector.w }); break; } } result.rawData = rawData.Compose(); return result; } else { if (vBug.settings.general.debugMode) Debug.LogError("No shader-info found @" + shader.name); return null; } }
/// <summary> /// Function to add a new material reference and returning its index in the material reference array. /// </summary> /// <param name="material"></param> /// <param name="fontAsset"></param> /// <param name="materialReferences"></param> /// <param name="materialReferenceIndexLookup"></param> /// <returns></returns> public static int AddMaterialReference(Material material, TMP_FontAsset fontAsset, MaterialReference[] materialReferences, Dictionary<int, int> materialReferenceIndexLookup) { int materialID = material.GetInstanceID(); int index = 0; if (materialReferenceIndexLookup.TryGetValue(materialID, out index)) { return index; } else { index = materialReferenceIndexLookup.Count; // Add new reference index materialReferenceIndexLookup[materialID] = index; materialReferences[index].index = index; materialReferences[index].fontAsset = fontAsset; materialReferences[index].spriteAsset = null; materialReferences[index].material = material; materialReferences[index].isDefaultMaterial = materialID == fontAsset.material.GetInstanceID() ? true : false; //materialReferences[index].padding = 0; materialReferences[index].referenceCount = 0; return index; } }
/// <summary> /// Constructor for new Material Reference. /// </summary> /// <param name="index"></param> /// <param name="fontAsset"></param> /// <param name="spriteAsset"></param> /// <param name="material"></param> /// <param name="padding"></param> public MaterialReference(int index, TMP_FontAsset fontAsset, TMP_SpriteAsset spriteAsset, Material material, float padding) { this.index = index; this.fontAsset = fontAsset; this.spriteAsset = spriteAsset; this.material = material; this.isDefaultMaterial = material.GetInstanceID() == fontAsset.material.GetInstanceID() ? true : false; this.padding = padding; this.referenceCount = 0; }
/// <summary> /// /// </summary> /// <param name="targetMaterial"></param> public static void AddFallbackMaterialReference(Material targetMaterial) { if (targetMaterial == null) return; int sourceID = targetMaterial.GetInstanceID(); long key; // Lookup key to retrieve if (m_fallbackMaterialLookup.TryGetValue(sourceID, out key)) { FallbackMaterial fallback; if (m_fallbackMaterials.TryGetValue(key, out fallback)) { fallback.count += 1; } } }
public MaterialId SaveNormalMaterial(UnityEngine.Material material) { if (root.Materials == null) { root.Materials = new List<GLTF.Schema.Material>(); } var mid = material.GetInstanceID(); if (_material2ID.ContainsKey(mid)) { return _material2ID[mid]; } var mat = ExporterUtils.ConvertMaterial(material, this); root.Materials.Add(mat); var id = new MaterialId { Id = root.Materials.Count - 1, Root = root }; _material2ID.Add(mid, id); return id; }
// Function called internally when a new material is assigned via the fontMaterial property. protected override void SetFontMaterial(Material mat) { // Get Shader PropertyIDs if they haven't been cached already. ShaderUtilities.GetShaderPropertyIDs(); // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer. // This can occur when the Duplicate Material Context menu is used on an inactive object. if (m_uiRenderer == null) m_uiRenderer = GetComponent<CanvasRenderer>(); // Destroy previous instance material. //if (m_fontMaterial != null && m_fontMaterial.GetInstanceID() != mat.GetInstanceID()) DestroyImmediate(m_fontMaterial); // Release masking material if (m_maskingMaterial != null) { MaterialManager.ReleaseStencilMaterial(m_maskingMaterial); m_maskingMaterial = null; } // Get Masking ID m_stencilID = MaterialManager.GetStencilID(gameObject); // Create Instance Material only if the new material is not the same instance previously used. if (m_fontMaterial == null || m_fontMaterial.GetInstanceID() != mat.GetInstanceID()) m_fontMaterial = CreateMaterialInstance(mat); if (m_stencilID > 0) m_fontMaterial = MaterialManager.SetStencil(m_fontMaterial, m_stencilID); m_sharedMaterial = m_fontMaterial; SetShaderDepth(); // Set ZTestMode based on Canvas RenderMode. m_uiRenderer.SetMaterial(m_sharedMaterial, m_sharedMaterial.mainTexture); m_padding = GetPaddingForMaterial(); //m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial); }
// Event received when custom material editor properties are changed. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat) { //Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***"); // Filter events and return if the affected material is not this object's material. if (mat.GetInstanceID() != m_sharedMaterial.GetInstanceID()) return; if (m_TextComponent == null) m_TextComponent = GetComponentInParent<TextMeshPro>(); m_padding = GetPaddingForMaterial(); m_TextComponent.havePropertiesChanged = true; m_TextComponent.SetVerticesDirty(); }
/// <summary> /// Serializes textures to FBX format. /// </summary> /// <param name="gameObj">Parent GameObject being exported.</param> /// <param name="newPath">The path to export to.</param> /// <param name="materials">Materials that holds all the textures.</param> /// <param name="matObjects">The string with the newly serialized texture file.</param> /// <param name="connections">The string to connect this to the material.</param> private static void SerializedTextures(GameObject gameObj, string newPath, Material material, string materialName, bool copyTextures, out string objects, out string connections) { // TODO: FBX import currently only supports Diffuse Color and Normal Map // Because it is undocumented, there is no way to easily find out what other textures // can be attached to an FBX file so it is imported into the PBR shaders at the same time. // Also NOTE, Unity 5.1.2 will import FBX files with legacy shaders. This is fix done // in at least 5.3.4. StringBuilder objectsSb = new StringBuilder(); StringBuilder connectionsSb = new StringBuilder(); int materialId = Mathf.Abs(material.GetInstanceID()); Texture mainTexture = material.GetTexture("_MainTex"); string newObjects = null; string newConnections = null; // Serializeds the Main Texture, one of two textures that can be stored in FBX's sysytem if(mainTexture != null) { SerializeOneTexture(gameObj, newPath, material, materialName, materialId, copyTextures, "_MainTex", "DiffuseColor", out newObjects, out newConnections); objectsSb.AppendLine(newObjects); connectionsSb.AppendLine(newConnections); } if(SerializeOneTexture(gameObj, newPath, material, materialName, materialId, copyTextures, "_BumpMap", "NormalMap", out newObjects, out newConnections)) { objectsSb.AppendLine(newObjects); connectionsSb.AppendLine(newConnections); } connections = connectionsSb.ToString(); objects = objectsSb.ToString(); }
// Event received when custom material editor properties are changed. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat) { //Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***"); int targetMaterialID = mat.GetInstanceID(); int sharedMaterialID = m_sharedMaterial.GetInstanceID(); int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID(); // Filter events and return if the affected material is not this object's material. //if (targetMaterialID != sharedMaterialID && targetMaterialID != maskingMaterialID) return; if (m_TextComponent == null) m_TextComponent = GetComponentInParent<TextMeshProUGUI>(); // Make sure material properties are synchronized between the assigned material and masking material. if (m_MaskMaterial != null) { UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes"); UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes"); if (targetMaterialID == sharedMaterialID) { //Debug.Log("Copy base material properties to masking material if not null."); float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID); float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp); m_MaskMaterial.CopyPropertiesFromMaterial(mat); m_MaskMaterial.shaderKeywords = mat.shaderKeywords; m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID); m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp); } else if (targetMaterialID == maskingMaterialID) { // Update the padding GetPaddingForMaterial(mat); m_sharedMaterial.CopyPropertiesFromMaterial(mat); m_sharedMaterial.shaderKeywords = mat.shaderKeywords; m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0); m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8); } } m_padding = GetPaddingForMaterial(); SetVerticesDirty(); m_ShouldRecalculateStencil = true; RecalculateClipping(); RecalculateMasking(); }
/// <summary> /// Create a Masking Material Instance for the given ID /// </summary> /// <param name="baseMaterial"></param> /// <param name="stencilID"></param> /// <returns></returns> public static Material GetStencilMaterial(Material baseMaterial, int stencilID) { // Check if Material supports masking if (!baseMaterial.HasProperty(ShaderUtilities.ID_StencilID)) { Debug.LogWarning("Selected Shader does not support Stencil Masking. Please select the Distance Field or Mobile Distance Field Shader."); return baseMaterial; } int baseMaterialID = baseMaterial.GetInstanceID(); // If baseMaterial already has a corresponding masking material, return it. for (int i = 0; i < m_materialList.Count; i++) { if (m_materialList[i].baseMaterial.GetInstanceID() == baseMaterialID && m_materialList[i].stencilID == stencilID) { m_materialList[i].count += 1; #if DEBUG_ON ListMaterials(); #endif return m_materialList[i].stencilMaterial; } } // No matching masking material found. Create and return a new one. Material stencilMaterial; //Create new Masking Material Instance for this Base Material stencilMaterial = new Material(baseMaterial); stencilMaterial.hideFlags = HideFlags.HideAndDontSave; #if UNITY_EDITOR stencilMaterial.name += " Masking ID:" + stencilID; #endif stencilMaterial.shaderKeywords = baseMaterial.shaderKeywords; // Set Stencil Properties ShaderUtilities.GetShaderPropertyIDs(); stencilMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID); //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilOp, 0); stencilMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 4); //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, stencilID); //stencilMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 0); MaskingMaterial temp = new MaskingMaterial(); temp.baseMaterial = baseMaterial; temp.stencilMaterial = stencilMaterial; temp.stencilID = stencilID; temp.count = 1; m_materialList.Add(temp); #if DEBUG_ON ListMaterials(); #endif return stencilMaterial; }
/// <summary> /// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset. /// </summary> /// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param> /// <param name="sourceAtlasTexture">The font atlas texture that should be assigned to the new material.</param> /// <returns></returns> public static Material GetFallbackMaterial(Material sourceMaterial, Texture sourceAtlasTexture) { int sourceID = sourceMaterial.GetInstanceID(); int texID = sourceAtlasTexture.GetInstanceID(); long key = (long)sourceID << 32 + texID; FallbackMaterial fallback; if (m_fallbackMaterials.TryGetValue(key, out fallback)) { //Debug.Log("Material [" + fallback.fallbackMaterial.name + "] already exists."); return fallback.fallbackMaterial; } // Create new material from the source material Material fallbackMaterial = new Material(sourceMaterial); fallbackMaterial.hideFlags = HideFlags.HideAndDontSave; #if UNITY_EDITOR fallbackMaterial.name += " + " + sourceAtlasTexture.name; #endif fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, sourceAtlasTexture); fallback = new FallbackMaterial(); fallback.baseID = sourceID; fallback.baseMaterial = sourceMaterial; fallback.fallbackMaterial = fallbackMaterial; fallback.count = 0; m_fallbackMaterials.Add(key, fallback); m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key); #if DEBUG_ON ListFallbackMaterials(); #endif return fallbackMaterial; }
// Function called internally when a new material is assigned via the fontMaterial property. protected override Material GetMaterial(Material mat) { // Get Shader PropertyIDs if they haven't been cached already. ShaderUtilities.GetShaderPropertyIDs(); // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer. // This can occur when the Duplicate Material Context menu is used on an inactive object. //if (m_canvasRenderer == null) // m_canvasRenderer = GetComponent<CanvasRenderer>(); // Create Instance Material only if the new material is not the same instance previously used. if (m_fontMaterial == null || m_fontMaterial.GetInstanceID() != mat.GetInstanceID()) m_fontMaterial = CreateMaterialInstance(mat); m_sharedMaterial = m_fontMaterial; m_padding = GetPaddingForMaterial(); SetVerticesDirty(); SetMaterialDirty(); return m_sharedMaterial; }
// Event received when custom material editor properties are changed. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat) { //Debug.Log("ON_MATERIAL_PROPERTY_CHANGED event received."); // Targeted Material is: " + mat.name + " m_sharedMaterial: " + m_sharedMaterial.name + " with ID:" + m_sharedMaterial.GetInstanceID() + " m_renderer.sharedMaterial: " + m_canvasRenderer.GetMaterial() + " Masking Material:" + m_MaskMaterial.GetInstanceID()); ShaderUtilities.GetShaderPropertyIDs(); // Initialize ShaderUtilities and get shader property IDs. int materialID = mat.GetInstanceID(); int sharedMaterialID = m_sharedMaterial.GetInstanceID(); int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID(); if (m_canvasRenderer.GetMaterial() == null) { if (m_fontAsset != null) { m_canvasRenderer.SetMaterial(m_fontAsset.material, m_sharedMaterial.mainTexture); //Debug.LogWarning("No Material was assigned to " + name + ". " + m_fontAsset.material.name + " was assigned."); } else Debug.LogWarning("No Font Asset assigned to " + name + ". Please assign a Font Asset.", this); } if (m_canvasRenderer.GetMaterial() != m_sharedMaterial && m_fontAsset == null) // || m_renderer.sharedMaterials.Contains(mat)) { //Debug.Log("ON_MATERIAL_PROPERTY_CHANGED Called on Target ID: " + GetInstanceID() + ". Previous Material:" + m_sharedMaterial + " New Material:" + m_uiRenderer.GetMaterial()); // on Object ID:" + GetInstanceID() + ". m_sharedMaterial: " + m_sharedMaterial.name + " m_renderer.sharedMaterial: " + m_renderer.sharedMaterial.name); m_sharedMaterial = m_canvasRenderer.GetMaterial(); } // Make sure material properties are synchronized between the assigned material and masking material. if (m_MaskMaterial != null) { UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes"); UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes"); if (materialID == sharedMaterialID) { //Debug.Log("Copy base material properties to masking material if not null."); float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID); float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp); //float stencilOp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilOp); //float stencilRead = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilReadMask); //float stencilWrite = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilWriteMask); m_MaskMaterial.CopyPropertiesFromMaterial(mat); m_MaskMaterial.shaderKeywords = mat.shaderKeywords; m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID); m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp); //m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilOp, stencilOp); //m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, stencilID); //m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 0); } else if (materialID == maskingMaterialID) { // Update the padding GetPaddingForMaterial(mat); m_sharedMaterial.CopyPropertiesFromMaterial(mat); m_sharedMaterial.shaderKeywords = mat.shaderKeywords; m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0); m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8); //m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilOp, 0); //m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, 255); //m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 255); } } m_padding = GetPaddingForMaterial(); m_havePropertiesChanged = true; SetVerticesDirty(); //SetMaterialDirty(); }
// Event to Track Material Changed resulting from Drag-n-drop. void ON_DRAG_AND_DROP_MATERIAL(Material currentMaterial, Material newMaterial) { //Debug.Log("Drag-n-Drop Event - Receiving Object ID " + GetInstanceID() + ". New Material is " + newMaterial.name + " with ID " + newMaterial.GetInstanceID() + ". Base Material is " + m_baseMaterial.name + " with ID " + m_baseMaterial.GetInstanceID()); // Check if event applies to this current object if (currentMaterial.GetInstanceID() == m_baseMaterial.GetInstanceID()) { Debug.Log("Assigning new Base Material " + newMaterial.name + " to replace " + currentMaterial.name); UnityEditor.Undo.RecordObject(this, "Material Assignment"); fontSharedMaterial = newMaterial; m_baseMaterial = newMaterial; } }
// Function called internally when a new shared material is assigned via the fontSharedMaterial property. void SetSharedFontMaterial(Material mat) { ShaderUtilities.GetShaderPropertyIDs(); // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer. // This can occur when the Duplicate Material Context menu is used on an inactive object. if (m_uiRenderer == null) m_uiRenderer = GetComponent<CanvasRenderer>(); if (mat == null) { mat = m_baseMaterial; m_isNewBaseMaterial = true; } // Check if Material has Stencil Support if (mat.HasProperty(ShaderUtilities.ID_StencilID)) { // Check if Material is a Base Material if (mat.HasProperty(ShaderUtilities.ID_StencilID) && mat.GetFloat(ShaderUtilities.ID_StencilID) == 0) { if (m_baseMaterial == null) m_baseMaterial = m_sharedMaterial; // Addition check in the transition to the use of a Base Material. // if new Base Material if (mat.GetInstanceID() != m_baseMaterial.GetInstanceID()) { m_baseMaterial = mat; m_isNewBaseMaterial = true; } } else { // If new Masking Material if (mat != m_maskingMaterial) { if (m_maskingMaterial != null) MaterialManager.ReleaseMaskingMaterial(m_maskingMaterial); //Debug.Log("A New Masking Material [" + mat + "]. Previous Masking Material [" + m_maskingMaterial); m_maskingMaterial = mat; MaterialManager.AddMaskingMaterial(m_baseMaterial, m_maskingMaterial, 1); } else { // Remove masking material from list //MaterialManager.RemoveMaskingMaterial(m_maskingMaterial); // Add Masking Material back //MaterialManager.AddMaskingMaterial(m_baseMaterial, m_maskingMaterial, 1); //Debug.Log("Masking Material [" + mat.name + "] with ID " + mat.GetInstanceID() + " is assigned."); } } if (m_mask && m_mask.MaskEnabled()) { if (m_isNewBaseMaterial) { if (m_maskingMaterial != null) MaterialManager.ReleaseMaskingMaterial(m_maskingMaterial); m_maskingMaterial = MaterialManager.GetMaskingMaterial(m_baseMaterial, 1); mat = m_maskingMaterial; } } } m_isNewBaseMaterial = false; m_uiRenderer.SetMaterial(mat, null); m_sharedMaterial = mat; m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold); m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial); //Debug.Log("New Material [" + mat.name + "] with ID " + mat.GetInstanceID() + " has been assigned. Base Material is [" + m_baseMaterial.name + "] with ID " + m_baseMaterial.GetInstanceID()); //MaterialManager.ListMaterials(); }
public string SaveMat(Material mat) { int id = mat.GetInstanceID(); string name = null; if (savecache.TryGetValue(id, out name)) { return name; } MyJson.JsonNode_Object _json = new MyJson.JsonNode_Object(); //parser.matParser.WriteToJson(this, mat, _json); #if UNITY_EDITOR if (parser.GetMatParser(mat.shader.name) == null) { var json = nodeParser.GetMatConfig(mat); parser.InitMatParser(mat.shader.name, json); #region 自动保存shaderparser配置,这一段可以关闭 string path = Application.dataPath + "/resources/shaderparser"; if (System.IO.Directory.Exists(path) == false) System.IO.Directory.CreateDirectory(path); string shadername = mat.shader.name; shadername = shadername.Replace("/", "%2f"); string file = path + "/" + shadername + ".shaderparser.txt"; if (System.IO.File.Exists(file)) System.IO.File.Delete(file); System.IO.File.WriteAllText(file, json.ToString()); #endregion } #endif parser.GetMatParser(mat.shader.name).WriteToJson(this, mat, _json); System.Text.StringBuilder sb = new System.Text.StringBuilder(); _json.ConvertToStringWithFormat(sb, 4); byte[] bs = System.Text.Encoding.UTF8.GetBytes(sb.ToString()); string sha1 = ResLibTool.ComputeHashString(bs); name = sha1 + ".jsonmat.txt"; bufs[name] = bs; savecache[id] = name; return name; }
// Event received when custom material editor properties are changed. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat) { //Debug.Log("ON_MATERIAL_PROPERTY_CHANGED event received by object with ID " + GetInstanceID()); // + " and Targeted Material is: " + mat.name + " with ID " + mat.GetInstanceID() + ". Base Material is " + m_baseMaterial.name + " with ID " + m_baseMaterial.GetInstanceID() + ". Masking Material is " + m_maskingMaterial.name + " with ID " + m_maskingMaterial.GetInstanceID()); ShaderUtilities.GetShaderPropertyIDs(); // Initialize ShaderUtilities and get shader property IDs. int materialID = mat.GetInstanceID(); if (m_uiRenderer.GetMaterial() == null) { if (m_fontAsset != null) { m_uiRenderer.SetMaterial(m_fontAsset.material, m_sharedMaterial.mainTexture); //Debug.LogWarning("No Material was assigned to " + name + ". " + m_fontAsset.material.name + " was assigned."); } else Debug.LogWarning("No Font Asset assigned to " + name + ". Please assign a Font Asset.", this); } if (m_uiRenderer.GetMaterial() != m_sharedMaterial && m_fontAsset == null) // || m_renderer.sharedMaterials.Contains(mat)) { //Debug.Log("ON_MATERIAL_PROPERTY_CHANGED Called on Target ID: " + GetInstanceID() + ". Previous Material:" + m_sharedMaterial + " New Material:" + m_uiRenderer.GetMaterial()); // on Object ID:" + GetInstanceID() + ". m_sharedMaterial: " + m_sharedMaterial.name + " m_renderer.sharedMaterial: " + m_renderer.sharedMaterial.name); m_sharedMaterial = m_uiRenderer.GetMaterial(); } // Is Material being modified my Base Material if (m_stencilID > 0 && m_baseMaterial != null && m_maskingMaterial != null) { if (materialID == m_baseMaterial.GetInstanceID()) { //Debug.Log("Copying Material properties from Base Material [" + mat + "] to Masking Material [" + m_maskingMaterial + "]."); float stencilID = m_maskingMaterial.GetFloat(ShaderUtilities.ID_StencilID); float stencilComp = m_maskingMaterial.GetFloat(ShaderUtilities.ID_StencilComp); m_maskingMaterial.CopyPropertiesFromMaterial(mat); m_maskingMaterial.shaderKeywords = mat.shaderKeywords; m_maskingMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID); m_maskingMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp); } else if (materialID == m_maskingMaterial.GetInstanceID()) { //Debug.Log("Copying Material properties from Masking Material [" + mat + "] to Base Material [" + m_baseMaterial + "]."); m_baseMaterial.CopyPropertiesFromMaterial(mat); m_baseMaterial.shaderKeywords = mat.shaderKeywords; m_baseMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0); m_baseMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8); } else if (MaterialManager.GetBaseMaterial(mat) != null && MaterialManager.GetBaseMaterial(mat).GetInstanceID() == m_baseMaterial.GetInstanceID()) { //Debug.Log("Copying Material properties from Masking Material [" + mat + "] to Masking Material [" + m_maskingMaterial + "]."); float stencilID = m_maskingMaterial.GetFloat(ShaderUtilities.ID_StencilID); float stencilComp = m_maskingMaterial.GetFloat(ShaderUtilities.ID_StencilComp); m_maskingMaterial.CopyPropertiesFromMaterial(mat); m_maskingMaterial.shaderKeywords = mat.shaderKeywords; m_maskingMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID); m_maskingMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp); } } //Debug.Log("Assigned Material is " + m_sharedMaterial.name + " with ID " + m_sharedMaterial.GetInstanceID()); // + // ". Target Mat is " + mat.name + " with ID " + mat.GetInstanceID() + // ". Base Material is " + m_baseMaterial.name + " with ID " + m_baseMaterial.GetInstanceID() + // ". Masking Material is " + m_maskingMaterial.name + " with ID " + m_maskingMaterial.GetInstanceID()); m_padding = ShaderUtilities.GetPadding(m_uiRenderer.GetMaterial(), m_enableExtraPadding, m_isUsingBold); //m_alignmentPadding = ShaderUtilities.GetFontExtent(m_uiRenderer.GetMaterial()); m_havePropertiesChanged = true; /* ScheduleUpdate(); */ }
/// <summary> /// Function to release the fallback material. /// </summary> /// <param name="fallackMaterial"></param> public static void ReleaseFallbackMaterial(Material fallackMaterial) { if (fallackMaterial == null) return; int materialID = fallackMaterial.GetInstanceID(); long key; if (m_fallbackMaterialLookup.TryGetValue(materialID, out key)) { FallbackMaterial fallback; if (m_fallbackMaterials.TryGetValue(key, out fallback)) { if (fallback.count > 1) fallback.count -= 1; else { Object.DestroyImmediate(fallback.fallbackMaterial); m_fallbackMaterials.Remove(key); m_fallbackMaterialLookup.Remove(materialID); fallackMaterial = null; } } } #if DEBUG_ON ListFallbackMaterials(); #endif }
// Function called internally when a new material is assigned via the fontMaterial property. Material GetMaterial(Material mat) { // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer. // This can occur when the Duplicate Material Context menu is used on an inactive object. //if (m_renderer == null) // m_renderer = GetComponent<Renderer>(); // Create Instance Material only if the new material is not the same instance previously used. if (m_material == null || m_material.GetInstanceID() != mat.GetInstanceID()) m_material = CreateMaterialInstance(mat); m_sharedMaterial = m_material; m_padding = GetPaddingForMaterial(); SetVerticesDirty(); SetMaterialDirty(); return m_sharedMaterial; }
/// <summary> /// Function to release the stencil material. /// </summary> /// <param name="stencilMaterial"></param> public static void ReleaseStencilMaterial(Material stencilMaterial) { int stencilMaterialID = stencilMaterial.GetInstanceID(); for (int i = 0; i < m_materialList.Count; i++) { if (m_materialList[i].stencilMaterial.GetInstanceID() == stencilMaterialID) { if (m_materialList[i].count > 1) m_materialList[i].count -= 1; else { Object.DestroyImmediate(m_materialList[i].stencilMaterial); m_materialList.RemoveAt(i); stencilMaterial = null; } break; } } #if DEBUG_ON ListMaterials(); #endif }