Unity - Shader Graph 中的 Flipbook 节点算法BUG - Unity_Flipbook_float

这篇具有很好参考价值的文章主要介绍了Unity - Shader Graph 中的 Flipbook 节点算法BUG - Unity_Flipbook_float。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


环境

Unity : 2020.3.37f1


原因

比如,我有
flipx : 10,
flipy : 4,
flipidx : 10 (其实是编号,不是索引)

应该是采样到是 10 的位置
到时结果采样到的是:20 的位置


解决

Unity - Shader Graph 中的 Flipbook 节点算法BUG - Unity_Flipbook_float,unity,unity-shader,unity 搬砖,unity,算法,bug文章来源地址https://www.toymoban.com/news/detail-622188.html


Shader

// jave.lin 2022/12/15 Unlit, Texture, Fog toggle, Flipbook
// 优化点:props 中 _FlipX, _FlipY, _FlipIDX 都可以合并在一 vector
// 优化点:props 中 _FogIntensity, _ClampBrightness 都可以合并在一 vector

Shader "Game/Unlit_Tex_FogToggle_Flipbook"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
		_FlipX ("Flip X", Int) = 1
		_FlipY ("Flip Y", Int) = 1
		_FlipIDX ("Flip IDX", Int) = 1
        [HDR] _Color ("Color", Color) = (1, 1, 1, 1)
		//[Toggle(_IS_FOG_ON)] _IS_FOG_ON ("Is Fog On", Float) = 1
		[Toggle] _IS_FOG_ON ("Is Fog On", Float) = 1
        _FogIntensity ("Fog Intensity", Range(0, 1)) = 1
        [Toggle] _ClampBrightness ("Clamp Brightness", Int) = 0

		[Header(DrawState)]
		//[HideInInspector] _Mode ("__mode", Float) = 0.0
		[Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 2
		[Enum(Off, 0, On, 1)] _ZWrite("ZWrite", Float) = 1
		[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend Mode", Float) = 1
		[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend Mode", Float) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        Pass
        {
            Blend [_SrcBlend] [_DstBlend]
            ZWrite [_ZWrite]
            Cull [_Cull]
            CGPROGRAM
	        #pragma skip_variants FOG_EXP FOG_EXP2

            //#pragma multi_compile_fog
            //#define INSTANCING_ON
            //#define UNITY_INSTANCING_ENABLED
            #define FOG_LINEAR

            //#pragma shader_feature _ _IS_FOG_ON
            #pragma multi_compile _ _CURVE_WORLD_ON
            #pragma multi_compile_instancing
            
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            #include "UnityInstancing.cginc"
            #include "Assets/Art/Shaders/CurveWorldLib.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };
            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                //#ifdef _IS_FOG_ON
                    UNITY_FOG_COORDS(1)
                //#endif
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            sampler2D _MainTex;

            UNITY_INSTANCING_BUFFER_START(Props)
            
                UNITY_DEFINE_INSTANCED_PROP(int, _IS_FOG_ON)

                //UNITY_DEFINE_INSTANCED_PROP(float4, _MainTex_ST)
	            UNITY_DEFINE_INSTANCED_PROP(int, _FlipX)
	            UNITY_DEFINE_INSTANCED_PROP(int, _FlipY)
	            UNITY_DEFINE_INSTANCED_PROP(int, _FlipIDX)
                UNITY_DEFINE_INSTANCED_PROP(half4, _Color)
                UNITY_DEFINE_INSTANCED_PROP(half, _FogIntensity)
                UNITY_DEFINE_INSTANCED_PROP(half, _ClampBrightness)

            UNITY_INSTANCING_BUFFER_END(Props)

            v2f vert (appdata v)
            {
                v2f o = (v2f)0;

                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);

                float4 posWorld = mul(UNITY_MATRIX_M, v.vertex);
                CURVE_WORLD_APPLY(posWorld.xyz, v.vertex.xyz)

                o.vertex = UnityObjectToClipPos(v.vertex);

                //o.uv = TRANSFORM_TEX(v.uv, UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex));

                o.uv = v.uv;

                //#ifdef _IS_FOG_ON
                    UNITY_TRANSFER_FOG(o,o.vertex);
                //#endif
                return o;
            }
	        void Unity_Flipbook_float(float2 UV, float Width, float Height, float Tile, float2 Invert, out float2 Out)
	        {
		        Tile = fmod(Tile, Width * Height);
		        float2 tileCount = float2(1.0, 1.0) / float2(Width, Height);
		        float tileY = abs(Invert.y * Height - (floor(Tile * tileCount.x) + Invert.y * 1));
		        float tileX = abs(Invert.x * Width - ((Tile - Width * floor(Tile * tileCount.x)) + Invert.x * 1));
		        Out = (UV + float2(tileX, tileY)) * tileCount;
	        }
            // https://forum.unity.com/threads/flipbook.1324218/
            void Unity_Flipbook_float_FIXED(float2 UV, float Width, float Height, float Tile, float2 Invert, out float2 Out)
            {
                Tile = floor(fmod(Tile + float(0.00001), Width*Height));
                float2 tileScale = float2(1.0, 1.0) / float2(Width, Height);
                float x = floor((Tile + float(0.5)) * tileScale.x);
                float tileX = (Tile - Width * x);
                float tileY = (Invert.y * Height - (x + Invert.y * 1));
                Out = (UV + float2(tileX, tileY)) * tileScale;
            }
            fixed4 frag (v2f i) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(i);

				float2 flipbook_uv = 0;
				Unity_Flipbook_float_FIXED(i.uv.xy, 
                    UNITY_ACCESS_INSTANCED_PROP(Props, _FlipX), 
                    UNITY_ACCESS_INSTANCED_PROP(Props, _FlipY), 
                    UNITY_ACCESS_INSTANCED_PROP(Props, _FlipIDX) - 1,
                    float2(-1, 1), flipbook_uv);
                //return half4(flipbook_uv, 0, 1);
				i.uv.xy = flipbook_uv;
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv) * UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
                //#ifdef _IS_FOG_ON
                //    // apply fog
                //    fixed4 src_col = col;
                //    UNITY_APPLY_FOG(i.fogCoord, col);
                //    col = lerp(src_col, col, _FogIntensity);
                //#endif

                // apply fog
                fixed4 src_col = col;
                UNITY_APPLY_FOG(i.fogCoord, col);
                col = lerp(src_col, col, 
                    UNITY_ACCESS_INSTANCED_PROP(Props, _IS_FOG_ON) * 
                    UNITY_ACCESS_INSTANCED_PROP(Props, _FogIntensity));

                return lerp(col, saturate(col), UNITY_ACCESS_INSTANCED_PROP(Props, _ClampBrightness));
            }
            ENDCG
        }
    }
}


References

  • Bug Flipbook

到了这里,关于Unity - Shader Graph 中的 Flipbook 节点算法BUG - Unity_Flipbook_float的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包