Example #1
0
        public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
        {
            if (material == null)
            {
                throw new ArgumentNullException("material");
            }

            // _Emission property is lost after assigning Standard shader to the material
            // thus transfer it before assigning the new shader
            if (material.HasProperty("_Emission"))
            {
                material.SetColor("_EmissionColor", material.GetColor("_Emission"));
            }

            base.AssignNewShaderToMaterial(material, oldShader, newShader);

            if (oldShader == null || !oldShader.name.Contains("Legacy Shaders/"))
            {
                PSXMaterialUtils.SetupMaterialBlendMode(material);
                return;
            }

            PSXMaterialUtils.SurfaceType surfaceType = PSXMaterialUtils.SurfaceType.Opaque;
            PSXMaterialUtils.BlendMode   blendMode   = PSXMaterialUtils.BlendMode.AlphaPostmultiply;
            if (oldShader.name.Contains("/Transparent/Cutout/"))
            {
                surfaceType = PSXMaterialUtils.SurfaceType.Opaque;
                material.SetFloat("_AlphaClip", 1);
            }
            else if (oldShader.name.Contains("/Transparent/"))
            {
                // NOTE: legacy shaders did not provide physically based transparency
                // therefore Fade mode
                surfaceType = PSXMaterialUtils.SurfaceType.Transparent;
                blendMode   = PSXMaterialUtils.BlendMode.AlphaPostmultiply;
            }
            material.SetFloat("_Surface", (float)surfaceType);
            material.SetFloat("_Blend", (float)blendMode);

            MaterialChanged(material);
        }
        public static void SetupMaterialBlendMode(Material material)
        {
            if (material == null)
            {
                throw new ArgumentNullException("material");
            }

            SurfaceType         surfaceType = (SurfaceType)material.GetFloat(PropertyIDs._Surface);
            RenderQueueCategory category    = (RenderQueueCategory)(int)material.GetFloat(PropertyIDs._RenderQueueCategory);
            bool transparent       = surfaceType == SurfaceType.Transparent;
            int  renderQueueOffset = 0; // TODO: Expose options for user to offset within the queue.
            bool alphaClip         = material.GetFloat(PropertyIDs._AlphaClip) == 1;

            material.renderQueue = GetRenderQueueFromCategory(category, transparent, renderQueueOffset, alphaClip);

            if (alphaClip)
            {
                material.EnableKeyword(Keywords._ALPHATEST_ON);
            }
            else
            {
                material.DisableKeyword(Keywords._ALPHATEST_ON);
            }


            if (surfaceType == SurfaceType.Opaque)
            {
                if (alphaClip)
                {
                    // TODO: Do I actually need to support these override tags? I'm not using them.
                    material.SetOverrideTag(Tags.RenderType, Tags.TransparentCutout);
                }
                else
                {
                    // TODO: Do I actually need to support these override tags? I'm not using them.
                    material.SetOverrideTag(Tags.RenderType, Tags.Opaque);
                }
                material.SetInt(PropertyIDs._BlendOp, (int)UnityEngine.Rendering.BlendOp.Add);
                material.SetInt(PropertyIDs._SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                material.SetInt(PropertyIDs._DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                material.SetInt(PropertyIDs._ZWrite, 1);
                material.DisableKeyword(Keywords._ALPHAPREMULTIPLY_ON);
                // material.SetShaderPassEnabled("ShadowCaster", true);
            }
            else // SurfaceType == SurfaceType.Transparent
            {
                PSXMaterialUtils.BlendMode blendMode = (PSXMaterialUtils.BlendMode)material.GetFloat(PropertyIDs._Blend);

                // Specific Transparent Mode Settings
                switch (blendMode)
                {
                case PSXMaterialUtils.BlendMode.AlphaPostmultiply:
                    material.SetInt(PropertyIDs._BlendOp, (int)UnityEngine.Rendering.BlendOp.Add);
                    material.SetInt(PropertyIDs._SrcBlend, (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt(PropertyIDs._DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.DisableKeyword(Keywords._ALPHAPREMULTIPLY_ON);
                    break;

                case PSXMaterialUtils.BlendMode.AlphaPremultiply:
                    material.SetInt(PropertyIDs._BlendOp, (int)UnityEngine.Rendering.BlendOp.Add);
                    material.SetInt(PropertyIDs._SrcBlend, (int)UnityEngine.Rendering.BlendMode.One);
                    material.SetInt(PropertyIDs._DstBlend, (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                    material.EnableKeyword(Keywords._ALPHAPREMULTIPLY_ON);
                    break;

                case PSXMaterialUtils.BlendMode.Additive:
                    material.SetInt(PropertyIDs._BlendOp, (int)UnityEngine.Rendering.BlendOp.Add);
                    material.SetInt(PropertyIDs._SrcBlend, (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt(PropertyIDs._DstBlend, (int)UnityEngine.Rendering.BlendMode.One);
                    material.DisableKeyword(Keywords._ALPHAPREMULTIPLY_ON);
                    break;

                case PSXMaterialUtils.BlendMode.Multiply:
                    material.SetInt(PropertyIDs._BlendOp, (int)UnityEngine.Rendering.BlendOp.Add);
                    material.SetInt(PropertyIDs._SrcBlend, (int)UnityEngine.Rendering.BlendMode.DstColor);
                    material.SetInt(PropertyIDs._DstBlend, (int)UnityEngine.Rendering.BlendMode.Zero);
                    material.DisableKeyword(Keywords._ALPHAPREMULTIPLY_ON);
                    material.EnableKeyword(Keywords._ALPHAMODULATE_ON);
                    break;

                case PSXMaterialUtils.BlendMode.Subtractive:
                    material.SetInt(PropertyIDs._BlendOp, (int)UnityEngine.Rendering.BlendOp.ReverseSubtract);
                    material.SetInt(PropertyIDs._SrcBlend, (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                    material.SetInt(PropertyIDs._DstBlend, (int)UnityEngine.Rendering.BlendMode.One);
                    material.DisableKeyword(Keywords._ALPHAPREMULTIPLY_ON);
                    material.DisableKeyword(Keywords._ALPHAMODULATE_ON);
                    break;

                default:
                    Debug.Assert(false, "Error: Encountered unsupported blendmode: " + blendMode);
                    break;
                }

                // General Transparent Material Settings
                // TODO: Do I actually need to support these override tags? I'm not using them.
                material.SetOverrideTag(Tags.RenderType, Tags.Transparent);
                material.SetInt(PropertyIDs._ZWrite, 0);
            }
        }