private void SetMaterialProperties(bool forceCreateMaterial = false)
        {
            if (!enabled || сomponentExtension == null || !ShaderResources)
            {
                return;
            }
            if (!IsShaderFamilySupported(ShaderFamily))
            {
                return;
            }

            var maskEnabled = MaskMode != MaskMode.Disabled;
            var shaderName  = ShaderUtilities.BuildShaderName(ShaderFamily, RenderMode, maskEnabled, FramebufferEnabled, UnifiedGrabEnabled);

            if (!ShaderResources.ShaderExists(shaderName))
            {
                return;
            }

            var shareMaterial = ShareMaterial && сomponentExtension.AllowMaterialSharing();

            if (shareMaterial)
            {
                var shader = ShaderResources.GetShaderByName(shaderName);
                var sharedMaterialCreated = false;
                blendMaterial = SharedMaterials.GetSharedFor(shader, this, out sharedMaterialCreated);
                if (sharedMaterialCreated)
                {
                    сomponentExtension.OnEffectMaterialCreated(blendMaterial);
                }
            }
            else if (!blendMaterial || blendMaterial.shader.name != shaderName || forceCreateMaterial) // Re-use current blend material when possible.
            {
                var shader = ShaderResources.GetShaderByName(shaderName);
                blendMaterial = CreateBlendMaterial(shader);
                сomponentExtension.OnEffectMaterialCreated(blendMaterial);
            }

            ShaderUtilities.SelectBlendModeKeyword(blendMaterial, BlendMode);

            var propertyBlock = shareMaterial ? сomponentExtension.GetPropertyBlock() : null;

            if (RenderMode == RenderMode.TextureWithSelf)
            {
                SetOverlayMaterialProperties(blendMaterial, propertyBlock);
            }
            if (maskEnabled)
            {
                SetMaskMaterialProperties(blendMaterial, propertyBlock);
            }
            if (propertyBlock != null)
            {
                сomponentExtension.SetPropertyBlock(propertyBlock);
            }

            сomponentExtension.ApplyShaderProperties(blendMaterial);
            сomponentExtension.SetRenderMaterial(blendMaterial);

            isMaterialDirty = false;
        }
示例#2
0
        public void SetValue(object value)
        {
            if (!ShaderUtilities.CheckPropertyValueType(value, Type))
            {
                return;
            }

            switch (Type)
            {
            case ShaderPropertyType.Color:
                colorValue = (Color)value;
                break;

            case ShaderPropertyType.Vector:
                vectorValue = (Vector4)value;
                break;

            case ShaderPropertyType.Float:
                floatValue = (float)value;
                break;

            case ShaderPropertyType.Texture:
                textureValue = (Texture)value;
                break;
            }
        }
示例#3
0
        private static void TransferTMProProperties(Material fromMaterial, Material toMaterial)
        {
            // Not using Material.CopyPropertiesFromMaterial(), cause we shouldn't copy stencil props and some other stuff.

            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_MainTex, ShaderPropertyType.Texture, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_FaceTex, ShaderPropertyType.Texture, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_FaceColor, ShaderPropertyType.Color, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_FaceDilate, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_Shininess, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_UnderlayColor, ShaderPropertyType.Color, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_UnderlayOffsetX, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_UnderlayOffsetY, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_UnderlayDilate, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_UnderlaySoftness, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_WeightNormal, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_WeightBold, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_OutlineTex, ShaderPropertyType.Texture, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_OutlineWidth, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_OutlineSoftness, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_OutlineColor, ShaderPropertyType.Color, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_Padding, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_GradientScale, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_ScaleX, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_ScaleY, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_PerspectiveFilter, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_TextureWidth, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_TextureHeight, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_BevelAmount, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_GlowColor, ShaderPropertyType.Color, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_GlowOffset, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_GlowPower, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_GlowOuter, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_LightAngle, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_ShaderFlags, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_ScaleRatio_A, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_ScaleRatio_B, ShaderPropertyType.Float, fromMaterial, toMaterial);
            ShaderUtilities.TransferMaterialProperty(TMPro.ShaderUtilities.ID_ScaleRatio_C, ShaderPropertyType.Float, fromMaterial, toMaterial);

            if (fromMaterial.IsKeywordEnabled(TMPro.ShaderUtilities.Keyword_Bevel))
            {
                toMaterial.EnableKeyword(TMPro.ShaderUtilities.Keyword_Bevel);
            }
            if (fromMaterial.IsKeywordEnabled(TMPro.ShaderUtilities.Keyword_Glow))
            {
                toMaterial.EnableKeyword(TMPro.ShaderUtilities.Keyword_Glow);
            }
            if (fromMaterial.IsKeywordEnabled(TMPro.ShaderUtilities.Keyword_Underlay))
            {
                toMaterial.EnableKeyword(TMPro.ShaderUtilities.Keyword_Underlay);
            }
            if (fromMaterial.IsKeywordEnabled(TMPro.ShaderUtilities.Keyword_Ratios))
            {
                toMaterial.EnableKeyword(TMPro.ShaderUtilities.Keyword_Ratios);
            }
            if (fromMaterial.IsKeywordEnabled(TMPro.ShaderUtilities.Keyword_Outline))
            {
                toMaterial.EnableKeyword(TMPro.ShaderUtilities.Keyword_Outline);
            }
        }
示例#4
0
        private void SetMaterialProperties(bool forceCreateMaterial = false)
        {
            if (!isActiveAndEnabled || !componentExtension || !ShaderResources)
            {
                return;
            }
            if (!IsShaderFamilySupported(ShaderFamily))
            {
                return;
            }

            var maskEnabled = MaskMode != MaskMode.Disabled;
            var shaderName  = ShaderUtilities.BuildShaderName(ShaderFamily, RenderMode, maskEnabled, FramebufferEnabled, UnifiedGrabEnabled);

            if (!ShaderResources.ShaderExists(shaderName))
            {
                return;
            }

            var materials     = componentExtension.GetRenderMaterials();
            var shareMaterial = ShareMaterial && componentExtension.AllowMaterialSharing();

            if (materials == null || materials.Length == 0)
            {
                materials = new Material[] { null }
            }
            ;
            for (int i = 0; i < materials.Length; i++)
            {
                if (shareMaterial)
                {
                    var shader = ShaderResources.GetShaderByName(shaderName);
                    var sharedMaterialCreated = false;
                    materials[i] = MaterialCache.GetSharedFor(shader, this, out sharedMaterialCreated);
                    if (sharedMaterialCreated)
                    {
                        componentExtension.OnEffectMaterialCreated(materials[i]);
                    }
                }
                else if (forceCreateMaterial || !materials[i] || materials[i].shader.name != shaderName)
                {
                    var shader = ShaderResources.GetShaderByName(shaderName);
                    materials[i] = CreateTransientMaterial(shader);
                    componentExtension.OnEffectMaterialCreated(materials[i]);
                }

                ShaderUtilities.SelectBlendModeKeyword(materials[i], BlendMode);

                var propertyBlock = shareMaterial ? componentExtension.GetPropertyBlock(i) : null;
                if (RenderMode == RenderMode.TextureWithSelf)
                {
                    SetOverlayMaterialProperties(materials[i], propertyBlock);
                }
                if (maskEnabled)
                {
                    SetMaskMaterialProperties(materials[i], propertyBlock);
                }
                if (propertyBlock != null)
                {
                    componentExtension.SetPropertyBlock(propertyBlock, i);
                }
            }

            componentExtension.ApplyShaderProperties(materials);
            componentExtension.SetRenderMaterials(materials);

            isMaterialDirty = false;
        }