Forum rules - please read before posting.

Object Fade Sprite (fade out/in) doesn't seem to work after changing the material on a sprite

edited December 2024 in Engine development

Hello,

I hope everyone is doing well.

I’ve encountered an issue where the Object Fade Sprite (fade out/fade in) function no longer works after changing the material on a sprite using Object Change Material.

Context: The shader is designed to turn everything to grayscale. However, it seems to interfere with specific Adventure Creator functions, such as highlight and sprite fade in/out functionality.

Could this be an issue with the script, the material, or something I’m doing wrong?

Thank you in advance, as always, for your help!

Here's the script I used for the shader.

Shader "Custom/GrayscaleWithHighlight"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_GrayscaleAmount("Grayscale Amount", Range(0,1)) = 1
_EmissionColor("Emission Color", Color) = (0,0,0)
}
SubShader
{
Tags { "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _GrayscaleAmount;
            float4 _EmissionColor;

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                // Sample the texture
                float4 col = tex2D(_MainTex, i.uv);

                // Compute grayscale color
                float gray = dot(col.rgb, float3(0.3, 0.59, 0.11));

                // Blend between original and grayscale
                float4 grayscaleColor = lerp(col, float4(gray, gray, gray, col.a), _GrayscaleAmount);

                // Add emission color for highlighting
                grayscaleColor.rgb += _EmissionColor.rgb;

                // Preserve alpha for transparency
                grayscaleColor.a = col.a;

                return grayscaleColor;
            }
            ENDCG
        }
    }

}

Comments

  • The Sprite Fader feature works by adjusting the alpha channel of the Sprite Renderer's Color property in its Inspector. You can try adjusting this manually to see if it has an effect - if not, it'll be down to the shader not reacting/accounting for it.

    Shader code isn't really my area, but one way to approach this may be to add a separate _Alpha float property in your shader, multiply by it when setting the grayscaleColor alpha, and then have a custom script link this with the Sprite Renderer's color channel in an Update loop, i.e.:

    material.SetFloat ("_Alpha", GetComponent<SpriteRenderer> ().color.a);
    

    For the Highlight feature: by default, this will affect a property named either _Color or _BaseColor. You can override this within its Inspector, but again: you'll likely need to introduce a separate Color property in the shader that it can update, and then incorporate that into your shader's final calculation.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.