EricFreeman / DynamicDecals

Decal solution for Unity's Built-In Render Pipeline
310 stars 41 forks source link

Custom Shader issue #3

Closed rizwan1239 closed 12 months ago

rizwan1239 commented 12 months ago

Hi. I am using a custom shader with flipbook logic. Attaching the shader code. Takes sort of sprite sheet of multiple frames and plays it in a sequence according to values of columns, rows and speed. It works fine if I apply it to a cube directly. When I create a decal from it, it actually renders but the whole sprite sheet is being rendered instead of per frame. I verified shader properties on runtime but those properties don't seem to make an effect.

Shader Code

Shader "Projection/Decal/LiveSpray"
{
    Properties
    {
        _Color("Albedo", Color) = (1,1,1,1)
        _MainTex("Albedo Map", 2D) = "white" {}

        _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
        _NormalCutoff("Normal Cutoff", Range(0.0, 1.0)) = 0.5

        _TilingOffset("Tiling / Offset", Vector) = (1, 1, 0, 0)

        _MaskBase("Mask Base", Range(0.0, 1.0)) = 0.0
        _MaskLayers("Layers", Color) = (0.5, 0.5, 0.5, 0.5)

        _FlipbookRows("Rows", Int) = 1
        _FlipbookColumns("Columns", Int) = 8
        _Speed("Animation Speed", Float) = 10.0
    }

        // 2.5
            SubShader
        {
            Tags{ "Queue" = "AlphaTest+1" "DisableBatching" = "True" "IgnoreProjector" = "True" }
            ZWrite Off ZTest Always Cull Back

            Pass
            {
                Name "FORWARD"
                Tags{ "LightMode" = "ForwardBase" }

                Blend SrcAlpha OneMinusSrcAlpha

                CGPROGRAM
                #pragma target 2.5
                #pragma multi_compile_instancing
                #pragma multi_compile_fwdbase
                #pragma multi_compile_fog
                #pragma glsl

                #pragma multi_compile _PrecisionDepthNormals _CustomDepthNormals
                #pragma multi_compile _ _AlphaTest
                #pragma multi_compile ___ _Omni

                #include "../../Cginc/ForwardPasses.cginc"

                #pragma vertex vertProjection
                #pragma fragment fragUnlit
                ENDCG
            }

            // Add a new pass for the Flipbook animation
            Pass
            {
                Name "Flipbook"
                Stencil
                {
                    Ref 1
                    Comp always
                    Pass replace
                }

                Tags { "Queue" = "Transparent" }

                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"

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

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

                sampler2D _MainTex;
                float4 _MainTex_ST;
                float4 _Color;
                float _Cutoff;
                int _FlipbookRows;
                int _FlipbookColumns;
                float _Speed;

                v2f vert(appdata_t v)
                {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);

                    // Calculate UV coordinates for the flipbook with Y inversion
                    float frameWidth = 1.0 / _FlipbookColumns;
                    float frameHeight = 1.0 / _FlipbookRows;
                    float time = _Time.y * _Speed;
                    int frameIndex = (int)floor(time) % (_FlipbookRows * _FlipbookColumns);
                    int rowIndex = frameIndex / _FlipbookColumns;
                    int colIndex = frameIndex % _FlipbookColumns;

                    o.texcoord = v.texcoord;
                    o.texcoord.x = (v.texcoord.x * frameWidth) + (frameWidth * colIndex);
                    o.texcoord.y = (v.texcoord.y * frameHeight) + (frameHeight * rowIndex);

                    return o;
                }

                fixed4 frag(v2f i) : SV_Target
                {
                    fixed4 col = tex2D(_MainTex, i.texcoord) * _Color;
                    clip(col.a - _Cutoff); // Use alpha cutoff
                    return col;
                }
                ENDCG
            }
        }

            Fallback Off
}

This is what it looks when directly applied on a cube. https://github.com/EricFreeman/DynamicDecals/assets/33382155/cab3169a-8065-4131-b809-0fcff2d3e259

This is what it looks like when rendered through decal even though those flipbook parameters are being set on runtime. image

EricFreeman commented 12 months ago

I haven't had a chance to look into your shader yet, but have you seen the SheetAnimator modifier? It can help point you in the right direction without needing a custom shader: https://github.com/EricFreeman/DynamicDecals/blob/master/Assets/Scripts/Modifiers/SheetAnimator.cs

rizwan1239 commented 12 months ago

I haven't had a chance to look into your shader yet, but have you seen the SheetAnimator modifier? It can help point you in the right direction without needing a custom shader: https://github.com/EricFreeman/DynamicDecals/blob/master/Assets/Scripts/Modifiers/SheetAnimator.cs

I missed this modifier in documentation. It works as I wanted. Cheers mate for quick reply!