Shader_ShaderForge_NGUI_序列帧/

序列帧


  • Shader篇

Shader Forge序列帧算法!

  

附上Shader代码部分:

// Shader created with Shader Forge v1.26 
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.26;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,lico:0,lgpr:1,limd:0,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:3,bdst:7,dpts:2,wrdp:False,dith:0,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:True,qofs:0,qpre:3,rntp:2,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False;n:type:ShaderForge.SFN_Final,id:2639,x:33086,y:32702,varname:node_2639,prsc:2|custl-3205-RGB,alpha-3205-A;n:type:ShaderForge.SFN_Tex2d,id:3205,x:32770,y:32838,ptovrint:False,ptlb:MainTex,ptin:_MainTex,varname:node_3205,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,tex:18c6fa7ae2c58cb4bbfea44780c46b23,ntxv:0,isnm:False|UVIN-1068-UVOUT;n:type:ShaderForge.SFN_UVTile,id:1068,x:32549,y:32801,varname:node_1068,prsc:2|UVIN-772-UVOUT,WDT-971-OUT,HGT-9174-OUT,TILE-3717-OUT;n:type:ShaderForge.SFN_TexCoord,id:772,x:32322,y:32636,varname:node_772,prsc:2,uv:0;n:type:ShaderForge.SFN_ValueProperty,id:9174,x:32136,y:32993,ptovrint:False,ptlb:V,ptin:_V,varname:node_9174,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:5;n:type:ShaderForge.SFN_ValueProperty,id:971,x:32149,y:32804,ptovrint:False,ptlb:H,ptin:_H,varname:node_971,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:5;n:type:ShaderForge.SFN_ValueProperty,id:7120,x:31230,y:32665,ptovrint:False,ptlb:Index,ptin:_Index,varname:node_7120,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,v1:15;n:type:ShaderForge.SFN_Fmod,id:5299,x:31778,y:33126,varname:node_5299,prsc:2|A-7838-OUT,B-8860-OUT;n:type:ShaderForge.SFN_Divide,id:5519,x:31434,y:33360,varname:node_5519,prsc:2|A-757-OUT,B-9769-OUT;n:type:ShaderForge.SFN_Floor,id:6464,x:31612,y:33360,varname:node_6464,prsc:2|IN-5519-OUT;n:type:ShaderForge.SFN_Subtract,id:5218,x:31522,y:33582,varname:node_5218,prsc:2|A-9536-OUT,B-8336-OUT;n:type:ShaderForge.SFN_Vector1,id:8336,x:31270,y:33654,varname:node_8336,prsc:2,v1:1;n:type:ShaderForge.SFN_Subtract,id:4840,x:31841,y:33340,varname:node_4840,prsc:2|A-5218-OUT,B-6464-OUT;n:type:ShaderForge.SFN_Set,id:1131,x:32149,y:32893,varname:HValue,prsc:2|IN-971-OUT;n:type:ShaderForge.SFN_Set,id:1440,x:32136,y:33071,varname:VValue,prsc:2|IN-9174-OUT;n:type:ShaderForge.SFN_Get,id:9769,x:31199,y:33428,varname:node_9769,prsc:2|IN-1131-OUT;n:type:ShaderForge.SFN_Get,id:8860,x:31568,y:33198,varname:node_8860,prsc:2|IN-1131-OUT;n:type:ShaderForge.SFN_Get,id:9536,x:31249,y:33582,varname:node_9536,prsc:2|IN-1440-OUT;n:type:ShaderForge.SFN_Add,id:3717,x:32252,y:33252,varname:node_3717,prsc:2|A-5840-OUT,B-2489-OUT;n:type:ShaderForge.SFN_Multiply,id:2489,x:32077,y:33350,varname:node_2489,prsc:2|A-4840-OUT,B-3624-OUT;n:type:ShaderForge.SFN_Get,id:3624,x:31891,y:33526,varname:node_3624,prsc:2|IN-1131-OUT;n:type:ShaderForge.SFN_Set,id:5485,x:31753,y:32715,varname:Index,prsc:2|IN-3812-OUT;n:type:ShaderForge.SFN_Get,id:7838,x:31568,y:33126,varname:node_7838,prsc:2|IN-5485-OUT;n:type:ShaderForge.SFN_Get,id:757,x:31199,y:33336,varname:node_757,prsc:2|IN-5485-OUT;n:type:ShaderForge.SFN_Fmod,id:3812,x:31545,y:32679,varname:node_3812,prsc:2|A-7120-OUT,B-5438-OUT;n:type:ShaderForge.SFN_Multiply,id:5438,x:31343,y:32747,varname:node_5438,prsc:2|A-3892-OUT,B-3297-OUT;n:type:ShaderForge.SFN_Get,id:3892,x:31119,y:32757,varname:node_3892,prsc:2|IN-1131-OUT;n:type:ShaderForge.SFN_Get,id:3297,x:31119,y:32803,varname:node_3297,prsc:2|IN-1440-OUT;n:type:ShaderForge.SFN_Floor,id:5840,x:31974,y:33136,varname:node_5840,prsc:2|IN-5299-OUT;proporder:3205-971-9174-7120;pass:END;sub:END;*/

Shader "Custom/FramesPicShaderV0" {
    Properties {
        _MainTex ("MainTex", 2D) = "white" {}
        _H ("H", Float ) = 5
        _V ("V", Float ) = 5
        _Index ("Index", Float ) = 15
        [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    }
    SubShader {
        Tags {
            "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent"
        }
        LOD 200
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase
            #pragma multi_compile_fog
            #pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2 
            #pragma target 3.0
            uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
            uniform float _V;
            uniform float _H;
            uniform float _Index;
            struct VertexInput {
                float4 vertex : POSITION;
                float2 texcoord0 : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
                UNITY_FOG_COORDS(1)
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.uv0 = v.texcoord0;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex );
                UNITY_TRANSFER_FOG(o,o.pos);
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
////// Lighting:
                float HValue = _H;
                float VValue = _V;
                float Index = fmod(_Index,(HValue*VValue));
                float node_3717 = (floor(fmod(Index,HValue))+(((VValue-1.0)-floor((Index/HValue)))*HValue));
                float2 node_1068_tc_rcp = float2(1.0,1.0)/float2( _H, _V );
                float node_1068_ty = floor(node_3717 * node_1068_tc_rcp.x);
                float node_1068_tx = node_3717 - _H * node_1068_ty;
                float2 node_1068 = (i.uv0 + float2(node_1068_tx, node_1068_ty)) * node_1068_tc_rcp;
                float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(node_1068, _MainTex));
                float3 finalColor = _MainTex_var.rgb;
                fixed4 finalRGBA = fixed4(finalColor,_MainTex_var.a);
                UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
                return finalRGBA;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
View Code

总体思路:

ShaderForge提供了序列帧算法节点,但是其节点的运算方式采用的是笛卡尔坐标系,而大多数序列帧图使用的屏幕坐标系,2个坐标系的顺序如下图所示,开头考虑的是换UV,发现很麻烦,就写了个算法换Index下标,成功解决,但是NGUI对Shader很不友好,如果你想实时更新渲染,就需要每次都去Enable&Disable UITexture 导致运行的时候卡的不行,这个方法就被我Pass掉了,还是通过代码控制UITexture里面的UV 缩放位移吧!


  • 代码版

  下面附上代码控制NGui序列帧播放的方法!!

using UnityEngine;
using System.Collections;

public class WUvPlay : MonoBehaviour {
    //图片性质 x-countX 行 y-countY 列 z-最大位置
    public Vector2 textureCount;
    
//    public GameObject thisObj;
    //偏移量
    private Vector2 offset;
    
    //现在偏移位置
    private int frame = -1;
    private float lastTime=0.0f;
    public float speed = 0.03f;

    public UITexture uitexture;
    private Rect v ;

    public bool isLoop = true;
    public bool isStop = false;
    public bool activeFlage = false;
    public bool pinpang = false;
    public int beginFrame = 0;
    public int endFrame = 0;
    protected int inc = 1;
    public int playCount = -1;
    public float dealyTime = 0;
    public int beginShowFrame = -1;


    private bool isLoop_begin ;
    private bool isStop_begin ;
    private bool activeFlage_begin ;
    private bool pinpang_begin ;
    private int beginFrame_begin;
    private int endFrame_begin ;
    private int playCount_begin ;
    private float dealyTime_begin ;
    private int beginShowFrame_begin ;


    private int nowFrame_set = -1;


    public bool isJiangePlay;
    public float randTimeMin;
    public float randTimeMax;
    private float nextPlayTime;
    public int randPlayerCountMin;
    public int randPlayerCountMax;
    void Awake()
    {
        isLoop_begin = isLoop;
        isStop_begin = isStop;
        activeFlage_begin = activeFlage;
        pinpang_begin = pinpang;
        beginFrame_begin = beginFrame;
        endFrame_begin = endFrame;
        playCount_begin = playCount;
        dealyTime_begin = dealyTime;
        beginShowFrame_begin = beginShowFrame;
    }

    void Start () {
    






        v.width = 1 / textureCount.x;
        v.height = 1 / textureCount.y;

        if(beginShowFrame <0)
        {
            v.x = 1;
            v.y = 1;
        }
        else
        {
            offset.x = (beginShowFrame % textureCount.x) / textureCount.x;
            offset.y = (textureCount.y - 1 - (beginShowFrame / Mathf.RoundToInt (textureCount.x))) / textureCount.y;
            v.x = offset.x;
            v.y = offset.y;

        }
        uitexture.uvRect = v;
        frame = beginFrame-1;
        lastTime = dealyTime + Time.time;



    }

    void Update () {

        if(isStop && isJiangePlay && nextPlayTime < Time.time)
        {
            nextPlayTime = Time.time + Random.Range(randTimeMin,randTimeMax);

            play();
            playCount = Random.Range(randPlayerCountMin,randPlayerCountMax);
        }
    


        if(pinpang)
        {
            if (!isStop && Time.time > lastTime + speed) 
            {
                lastTime = Time.time;
                
                frame  = frame + inc;
                if (frame >= endFrame && inc >0) {
                    frame = endFrame-1;
                    inc = -inc;
                    playCountIndex();
                }
                else if(frame < beginFrame && inc <0) 
                {
                    frame = beginFrame;
                    inc = -inc;
                    playCountIndex();
                }


                offset.x = (frame % textureCount.x) / textureCount.x;
                offset.y = (textureCount.y - 1 - (frame / Mathf.RoundToInt (textureCount.x))) / textureCount.y;
                
                v.x = offset.x;
                v.y = offset.y;
                
                uitexture.uvRect = v;
                

                if(nowFrame_set!=-1)
                {
                    frame = beginFrame+nowFrame_set%(endFrame - beginFrame);
                    nowFrame_set = -1;

                }
            }
        }
        else
        {
            if (!isStop && Time.time > lastTime + speed) 
            {
                lastTime = Time.time;
                
                frame  = frame + 1;
                if (frame >= endFrame) {
                    
                    if(!isLoop)
                    {
                        frame = (endFrame -1);
//                        isStop = true;
                        if(activeFlage)
                        {
                            uitexture.gameObject.SetActive(false);
                        }
                        playCountIndex();
                    }
                    else
                    {

                        playCountIndex();
                    }
                    
                }
                offset.x = (frame % textureCount.x) / textureCount.x;
                offset.y = (textureCount.y - 1 - (frame / Mathf.RoundToInt (textureCount.x))) / textureCount.y;
                
                v.x = offset.x;
                v.y = offset.y;
                
                uitexture.uvRect = v;

                if(nowFrame_set!=-1)
                {
                    frame = beginFrame+nowFrame_set%(endFrame - beginFrame);
                    nowFrame_set = -1;

                }
                
            }
        }


    }


    public void play()
    {
        frame = beginFrame-1;
        if(activeFlage)
        {
            uitexture.gameObject.SetActive(true);
        }
        isStop = false;

    }

    
    public void setNowFrame(int now)
    {
        nowFrame_set = now;
        
    }

    public void play(int b,int e)
    {
        beginFrame = b;
        endFrame = e;
        frame = beginFrame-1;
        if(activeFlage)
        {
            uitexture.gameObject.SetActive(true);
        }
        isStop = false;
    }

    public void playChangeLoop(bool isL)
    {

        frame = beginFrame-1;
        if(activeFlage)
        {
            uitexture.gameObject.SetActive(true);
        }
        isStop = false;
        isLoop = isL;
    }

    public void playChangeLoop(int b,int e,bool isL)
    {

        beginFrame = b;
        endFrame = e;
        frame = beginFrame-1;
        if(activeFlage)
        {
            uitexture.gameObject.SetActive(true);
        }
        isStop = false;
        isLoop = isL;
    }

    public void setFrame(int i)
    {
        frame = i;
        offset.x = (frame % textureCount.x) / textureCount.x;
        offset.y = (textureCount.y - 1 - (Mathf.RoundToInt (frame) / Mathf.RoundToInt (textureCount.x))) / textureCount.y;
        
        v.x = offset.x;
        v.y = offset.y;
        
        uitexture.uvRect = v;
    }


    public void playerResetBegin()
    {
        isLoop = isLoop_begin;
        isStop = isStop_begin;
        activeFlage = activeFlage_begin;
        pinpang =pinpang_begin ;
        beginFrame = beginFrame_begin;
        endFrame = endFrame_begin;
        playCount = playCount_begin;
        dealyTime = dealyTime_begin;
        beginShowFrame = beginShowFrame_begin;
        v.width = 1 / textureCount.x;
        v.height = 1 / textureCount.y;
        
        if(beginShowFrame <0)
        {
            v.x = 1;
            v.y = 1;
        }
        else
        {
            offset.x = (beginShowFrame % textureCount.x) / textureCount.x;
            offset.y = (textureCount.y - 1 - (beginShowFrame / Mathf.RoundToInt (textureCount.x))) / textureCount.y;
            v.x = offset.x;
            v.y = offset.y;
            
        }
        uitexture.uvRect = v;
        frame = beginFrame-1;
        lastTime = dealyTime + Time.time;
    }

    protected void playCountIndex()
    {
        if ( playCount > 0 )
        {
            playCount--;
            if ( playCount == 0 )
            {
                isStop = true;
                Debug.Log("序列帧运行结束!!");
                if ( activeFlage )
                {
                    uitexture.gameObject.SetActive (false);
                }
            }
            else
            {
                frame = beginFrame;
            }

        }
        else if ( playCount == 0 )
        {
            isStop = true;
        }
        else
        {
            frame = beginFrame;
        }
    }
}
View Code

  代码我就懒得读了~会用就好~,代码第303行可添加委托(观察者模式)将事件传出来!!

  

原文地址:https://www.cnblogs.com/jwv5/p/9149193.html