Shader-TexturePanner源码的简单改写

如题,本篇博客会介绍一下对TexturePanner源码的简单改写;TexturePanner是一个表面着色器,本人把它改写为顶点、片元着色器,并补充一些功能,同时在源码均有注释且汉化了一下Inspector面板的模块说明。改写后的顶点、片元着色器更为强大、可调参数更多、功能增加。

修改后的源码如下:

/*
 * 修改于:2020/04/17
 * 原作者:https://github.com/AdultLink/TexturePanner
 *
 * y = Asin(ωx + φ) + k
 *
 * 振幅: A(Amplitude)
 * 频率:ω / 2π(Frequency)
 * 初相:φ(PrimaryPhase)
 * 偏移:k(Offset)
 *
 */

Shader "NeonLights/TexturePanner"
{
    Properties
    {
        //主纹理
        [NoScaleOffset]_MainTex("MainTex", 2D) = "white" {}
        //缩放
        _Tiling("Tiling", Vector) = (1,1,0,0)
        //偏移
        _Offset("Offset", Vector) = (0,0,0,0)
        //主颜色
        [HDR]_MainColor("MainColor", Color) = (1,1,1,1)
        //颜色混合模式(枚举)
        [KeywordEnum(Original,Hueshift,Multiply,Replace)] _Colormode("Color mode", Float) = 2

        //遮罩纹理
        [NoScaleOffset]_MaskTexture("Mask Texture", 2D) = "white" {}
        //取反
        [Toggle]_Negation("Negation",Float) = 0
        //遮罩纹理旋转速度
        _MaskTextureRotationSpeed("Mask Texture Rotation Speed", Float) = 0
        //遮罩纹理缩放
        _MaskTextureTiling("Mask Ttexture Tiling", Vector) = (1,1,0,0)
        //遮罩纹理偏移
        _MaskTextureOffset("Mask Texture Offset", Vector) = (0,0,0,0)

        //是否开启水平位移(顶点位移)
        [Toggle]_HorizontalDisplacement("HorizontalDisplacement", Float) = 0
        //水平位移振幅
        _HorizontalDisplacementAmplitude("Horizontal Displacement Amplitude", Float) = 0.5
        //水平位移频率
        _HorizontalDisplacementFrequency("Horizontal Displacement Frequency", Float) = 1
        //水平位移初相
        _HorizontalDisplacementPrimaryPhase("Horizontal Displacement Primary Phase",Float) = 0
        //水平位移偏移
        _HorizontalDisplacementOffset("Horizontal Displacement Offset", Float) = 0

        //是否开启垂直位移(顶点位移)
        [Toggle]_VerticalDisplacement("Vertical Displacement", Float) = 0
        //垂直位移振幅
        _VerticalDisplacementAmplitude("Vertical Displacement Amplitude", Float) = 0.5
        //垂直位移频率
        _VerticalDisplacementFrequency("Vertical Displacement Frequency", Float) = 1
        //垂直位移初相
        _VerticalDisplacementPrimaryPhase("Vertical Displacement Primary Phase",Float) = 0
        //垂直位移偏移
        _VerticalDisplacementOffset("Vertical Displacement Offset", Float) = 0

        //是否开启深度位移(顶点位移)
        [Toggle]_DepthDisplacement("Depth Displacement", Float) = 0
        //深度位移振幅
        _DepthDisplacementAmplitude("Depth Displacement Amplitude", Float) = 0.5
        //深度位移频率
        _DepthDisplacementFrequency("Depth Displacement Frequency", Float) = 1
        //深度位移初相
        _DepthDisplacementPrimaryPhase("Depth Displacement Primary Phase",Float) = 0
        //深度位移偏移
        _DepthDisplacementOffset("Depth Displacement Offset", Float) = 0
		
        //是否水平拉伸
        [Toggle]_HorizontalStretch("Horizontal Stretch", Float) = 0
        //水平拉伸振幅
        _HorizontalStretchAmplitude("Horizontal Stretch Amplitude", Range( 0 , 1)) = 0.5
        //水平拉伸频率
        _HorizontalStretchFrequency("Horizontal Stretch Frequency", Float) = 0.5
        //水平拉伸初相
        _HorizontalStretchPrimaryPhase("Horizontal Stretch Primary Phase",Float) = 0
        //水平拉伸偏移
        _HorizontalStretchOffset("Horizontal Stretch Offset", Float) = 0
        //水平拉伸轴心点
        _HorizontalStretchPivotPoint("Horizontal Stretch Pivot Point", Range( -1 , 1)) = 0

        //是否垂直拉伸
        [Toggle]_VerticalStretch("Vertical Stretch", Float) = 0
        //垂直拉伸振幅
        _VerticalStretchAmplitude("Vertical Stretch Amplitude", Range( 0 , 1)) = 0.5
        //垂直拉伸频率
        _VerticalStretchFrequency("Vertical Stretch Frequency", Float) = 0.5
        //垂直拉伸初相
        _VerticalStretchPrimaryPhase("Vertical Stretch Primary Phase",Float) = 0
        //垂直拉伸偏移
        _VerticalStretchOffset("Vertical Stretch Offset", Float) = 0
        //垂直拉伸轴心点
        _VerticalStretchPivotPoint("Vertical Stretch Pivot Point", Range( -1 , 1)) = 0

        //是否深度拉伸
        [Toggle]_DepthStretch("Depth Stretch", Float) = 0
        //深度拉伸振幅
        _DepthStretchAmplitude("Depth Stretch Amplitude", Range( 0 , 1)) = 0.5
        //深度拉伸频率
        _DepthStretchFrequency("Depth Stretch Frequency", Float) = 0.5
        //深度拉伸初相
        _DepthStretchPrimaryPhase("Depth Stretch Primary Phase",Float) = 0
        //深度拉伸偏移
        _DepthStretchOffset("Depth Stretch Offset", Float) = 0
        //深度拉伸轴心点
        _DepthStretchPivotPoint("Depth Stretch Pivot Point", Range( -1 , 1)) = 0

        //是否开启X轴扫描线
        [Toggle]_ScanlinesX("Scanlines X", Float) = 0
        //X轴扫描线
        _ScanlinesScaleX("Scanlines Scale X", Float) = 30
        //X轴扫描线速度
        _ScanlinesSpeedX("Scanlines Speed X", Float) = -5
        //(直译:锋利的、锐利的)X轴是否加强效果
        [Toggle]_SharpX("SharpX", Float) = 1

        //是否开启Y轴扫描线
        [Toggle]_ScanlinesY("Scanlines Y", Float) = 0
        //Y轴扫描线
        _ScanlinesScaleY("Scanlines Scale Y", Float) = 30
        //Y轴扫描线速度
        _ScanlinesSpeedY("Scanlines Speed Y", Float) = -5
        //(直译:锋利的、锐利的)Y轴是否加强效果
        [Toggle]_SharpY("SharpY", Float) = 1

        //是否开启Z轴扫描线
        [Toggle]_ScanlinesZ("Scanlines Z", Float) = 0
        //Z轴扫描线
        _ScanlinesScaleZ("Scanlines Scale Z", Float) = 30
        //Z轴扫描线速度
        _ScanlinesSpeedZ("Scanlines Speed Z", Float) = -5
        //(直译:锋利的、锐利的)Z轴是否加强效果
        [Toggle]_SharpZ("SharpZ", Float) = 1
		
        //滚动、旋转(枚举)
        [KeywordEnum(Scroll,Rotate,None)] _ScrollRotate("Scroll Rotate", Float) = 0
        //X轴滚动速度
        _ScrollingSpeedX("Scrolling Speed X", Float) = -1
        //Y轴滚动速度
        _ScrollingSpeedY("Scrolling Speed Y", Float) = 0
        //旋转速度
        _RotationSpeed("Rotation Speed", Float) = 1

        //是否开启全局自发光
        [Toggle]_GlobalEmissionFlicker("Global Emission Flicker", Float) = 0
        //全局自发光振幅
        _GlobalEmissionFlickerAmplitude("Global Emission Flicker Amplitude", Float) = 0.5
        //全局自发光频率
        _GlobalEmissionFlickerFrequency("Global Emission Flicker Frequency", Float) = 1
        //全局自发光初相
        _GlobalEmissionFlickerPrimaryPhase("Global Emission Flicker Primary Phase",Float) = 0
        //全局自发光偏移
        _GlobalEmissionFlickerOffset("Global Emission Flicker Offset", Float) = 1
    }
    SubShader
    {
        Tags{ "RenderType" = "Transparent"  "Queue" = "Transparent" "IgnoreProjector" = "True" }

        Cull Off ZWrite Off ZTest Less
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
			
            //预定义宏
            #pragma multi_compile _COLORMODE_ORIGINAL _COLORMODE_HUESHIFT _COLORMODE_MULTIPLY _COLORMODE_REPLACE
            #pragma multi_compile _SCROLLROTATE_SCROLL _SCROLLROTATE_ROTATE _SCROLLROTATE_NONE
            
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            //主纹理
            uniform sampler2D _MainTex;
            uniform float2 _Tiling;
            uniform float2 _Offset;
            uniform float4 _MainColor;

            //遮罩纹理
            uniform sampler2D _MaskTexture;
            uniform float _Negation;
            uniform float2 _MaskTextureTiling;
            uniform float2 _MaskTextureOffset;
            uniform float _MaskTextureRotationSpeed;

            //水平位移
            uniform float _HorizontalDisplacement;
            uniform float _HorizontalDisplacementAmplitude;
            uniform float _HorizontalDisplacementFrequency;
            uniform float _HorizontalDisplacementPrimaryPhase;
            uniform float _HorizontalDisplacementOffset;

            //垂直位移
            uniform float _VerticalDisplacement;
            uniform float _VerticalDisplacementAmplitude;
            uniform float _VerticalDisplacementFrequency;
            uniform float _VerticalDisplacementPrimaryPhase;
            uniform float _VerticalDisplacementOffset;

            //深度位移
            uniform float _DepthDisplacement;
            uniform float _DepthDisplacementAmplitude;
            uniform float _DepthDisplacementFrequency;
            uniform float _DepthDisplacementPrimaryPhase;
            uniform float _DepthDisplacementOffset;

            //水平拉伸
            uniform float _HorizontalStretch;
            uniform float _HorizontalStretchAmplitude;
            uniform float _HorizontalStretchFrequency;
            uniform float _HorizontalStretchPrimaryPhase;
            uniform float _HorizontalStretchOffset;
            uniform float _HorizontalStretchPivotPoint;

            //垂直拉伸
            uniform float _VerticalStretch;
            uniform float _VerticalStretchAmplitude;
            uniform float _VerticalStretchFrequency;
            uniform float _VerticalStretchPrimaryPhase;
            uniform float _VerticalStretchOffset;
            uniform float _VerticalStretchPivotPoint;

            //深度拉伸
            uniform float _DepthStretch;
            uniform float _DepthStretchAmplitude;
            uniform float _DepthStretchFrequency;
            uniform float _DepthStretchPrimaryPhase;
            uniform float _DepthStretchOffset;
            uniform float _DepthStretchPivotPoint;

            //水平切割线
            uniform float _ScanlinesX;
            uniform float _SharpX;
            uniform float _ScanlinesScaleX;
            uniform float _ScanlinesSpeedX;
			
            //垂直切割线
            uniform float _ScanlinesY;
            uniform float _SharpY;
            uniform float _ScanlinesScaleY;
            uniform float _ScanlinesSpeedY;

            //深度切割线
            uniform float _ScanlinesZ;
            uniform float _SharpZ;
            uniform float _ScanlinesScaleZ;
            uniform float _ScanlinesSpeedZ;

            //滚动/旋转
            uniform float _ScrollingSpeedX;
            uniform float _ScrollingSpeedY;
            uniform float _RotationSpeed;

            //全局自发光
            uniform float _GlobalEmissionFlicker;
            uniform float _GlobalEmissionFlickerFrequency;
            uniform float _GlobalEmissionFlickerAmplitude;
            uniform float _GlobalEmissionFlickerPrimaryPhase;
            uniform float _GlobalEmissionFlickerOffset;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

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

            float3 HSVToRGB( float3 c )
            {
                float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
                float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
                return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
            }

            float3 RGBToHSV(float3 c)
            {
                float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
                float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
                float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
                float d = q.x - min(q.w, q.y);
                float e = 1.0e-10;
                return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
            }

            v2f vert (appdata v)
            {
                v2f o;

                //水平位移
                float horizontalDisplacementTime = _Time.y * _HorizontalDisplacementFrequency;
                float horizontalDisplacement = lerp(0.0,(sin(horizontalDisplacementTime + _HorizontalDisplacementPrimaryPhase) * _HorizontalDisplacementAmplitude + _HorizontalDisplacementOffset),_HorizontalDisplacement);

                //垂直位移
                float verticalDisplacementTime = _Time.y * _VerticalDisplacementFrequency;
                float verticalDisplacement = lerp(0.0,(sin(verticalDisplacementTime + _VerticalDisplacementPrimaryPhase) * _VerticalDisplacementAmplitude + _VerticalDisplacementOffset),_VerticalDisplacement);

                //深度位移
                float depthDisplacementTime = _Time.y * _DepthDisplacementFrequency;
                float depthDisplacement = lerp(0.0,(sin(depthDisplacementTime + _DepthDisplacementPrimaryPhase) * _DepthDisplacementAmplitude + _DepthDisplacementOffset),_DepthDisplacement);
	
                float3 vertexPos = v.vertex.xyz;

                //水平拉伸
                float horizontalStretchTime = _Time.y * _HorizontalStretchFrequency;
                float horizontalStretch = lerp(0.0,((sin(horizontalStretchTime + _HorizontalStretchPrimaryPhase) * _HorizontalStretchAmplitude + _HorizontalStretchOffset) * (_HorizontalStretchPivotPoint + vertexPos.x)),_HorizontalStretch);

                //垂直拉伸
                float verticalStretchTime = _Time.y * _VerticalStretchFrequency;
                float verticalStretch = lerp(0.0,((sin(verticalStretchTime + _VerticalStretchPrimaryPhase) * _VerticalStretchAmplitude + _VerticalStretchOffset) * (_VerticalStretchPivotPoint + vertexPos.y)),_VerticalStretch);
	
                //深度拉伸
                float depthStretchTime = _Time.y * _DepthStretchFrequency;
                float depthStretch = lerp(0.0,((sin(depthStretchTime + _DepthStretchPrimaryPhase) * _DepthStretchAmplitude + _DepthStretchOffset) * (_DepthStretchPivotPoint + vertexPos.z)),_DepthStretch);

                //合并位移和拉伸
                float4 appendResult = float4((horizontalDisplacement + horizontalStretch),(verticalDisplacement + verticalStretch),(depthDisplacement + depthStretch),0.0);
                v.vertex.xyz += appendResult.xyz;

                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.uv = v.uv;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //全局自发光处理
                float emissionFlickerTime = _Time.y * _GlobalEmissionFlickerFrequency;
                float emissionFlicker = lerp(1.0,(pow(sin(emissionFlickerTime + _GlobalEmissionFlickerPrimaryPhase),2.0) * (0.0 + (_GlobalEmissionFlickerAmplitude - 0.0) * (0.1 - 0.0) / (1.0 - 0.0)) + _GlobalEmissionFlickerOffset),_GlobalEmissionFlicker);

                //水平切割线
                float scanLinesXTime = _Time.y * _ScanlinesSpeedX;
                float scanLinesXClamp = clamp(sin((scanLinesXTime + i.uv.x * _ScanlinesScaleX)),0.0,1.0);
                float scanLinesX = lerp(1.0,lerp(scanLinesXClamp,ceil(scanLinesXClamp),_SharpX),_ScanlinesX);

                //垂直切割线
                float scanLinesYTime = _Time.y * _ScanlinesSpeedY;
                float scanLinesYClamp = clamp(sin((scanLinesYTime + i.uv.y * _ScanlinesScaleY)),0.0 ,1.0);
                float scanLinesY = lerp(1.0,lerp(scanLinesYClamp,ceil(scanLinesYClamp),_SharpY),_ScanlinesY);

                //深度切割线
                float3 worldPos = i.worldPos;//mul(unity_WorldToObject,float4(i.worldPos,1));
                float scanLinesZTime = _Time.y * _ScanlinesSpeedZ;
                float scanLinesZClamp = clamp(sin((scanLinesZTime + worldPos.z * _ScanlinesScaleZ)),0.0,1.0);
                float scanLinesZ = lerp(1.0,lerp(scanLinesZClamp,ceil(scanLinesZClamp),_SharpZ),_ScanlinesZ);

                float2 uv = i.uv * _Tiling + _Offset;

                //滚动处理
                float2 scrolling = float2(_ScrollingSpeedX,_ScrollingSpeedY);
                float2 scrollingUV = (_Time.y * scrolling + uv);

                //旋转处理
                float rotationTime = _Time.y * _RotationSpeed;
                float mainCos = cos(rotationTime);
                float mainSin = sin(rotationTime);
                float2 rotatorUV = mul(uv - (_Tiling * float2(0.5,0.5)),float2x2(mainCos,-mainSin,mainSin,mainCos)) + (_Tiling * float2(0.5,0.5));

                //滚动、旋转枚举
                #if defined(_SCROLLROTATE_SCROLL)
                    float2 mainTextureUV = scrollingUV;
                #elif defined(_SCROLLROTATE_ROTATE)
                    float2 mainTextureUV = rotatorUV;
                #elif defined(_SCROLLROTATE_NONE)
                    float2 mainTextureUV = uv;
                #else
                    float2 mainTextureUV = scrollingUV;
                #endif

                //混合效果
                float4 mainTexture = tex2D(_MainTex, mainTextureUV);
                float4 combinedTexture = (emissionFlicker * scanLinesX * scanLinesY * scanLinesZ * mainTexture);
                float3 hsvTorgb125 = RGBToHSV(combinedTexture.rgb);
                float3 hsvTorgb126 = RGBToHSV(_MainColor.rgb);
                float3 hsvTorgb127 = HSVToRGB(float3((hsvTorgb125.x + hsvTorgb126.x),hsvTorgb125.y,hsvTorgb125.z));

                //遮罩纹理旋转
                float2 maskTextureUV = i.uv * _MaskTextureTiling + _MaskTextureOffset;
                float maskTextureRotationTime = _Time.y * _MaskTextureRotationSpeed;
                float maskCos = cos(maskTextureRotationTime);
                float maskSin = sin(maskTextureRotationTime);
                float2 maskTextureRotatioUV = mul(maskTextureUV - (_MaskTextureTiling * float2(0.5,0.5)) , float2x2(maskCos,-maskSin,maskSin,maskCos)) + (_MaskTextureTiling * float2(0.5,0.5));

                //Alpha
                float maskTextureAlpha = tex2D(_MaskTexture,maskTextureRotatioUV).a;
                float maskTextureAlphaNegation = 1 - maskTextureAlpha;
                float alpha = combinedTexture.a * lerp(maskTextureAlpha,maskTextureAlphaNegation,_Negation);

                //颜色混合模式枚举(自发光)
                #if defined(_COLORMODE_ORIGINAL)
                    float4 finalColor = combinedTexture;
                #elif defined(_COLORMODE_HUESHIFT)
                    float4 finalColor = float4(hsvTorgb127,0.0);
                #elif defined(_COLORMODE_MULTIPLY)
                    float4 finalColor = combinedTexture * _MainColor;
                #elif defined(_COLORMODE_REPLACE)
                    float4 finalColor = _MainColor * alpha;
                #else
                    float4 finalColor = combinedTexture * _MainColor;
                #endif

                return fixed4(finalColor.rgb,alpha);
            }
            ENDCG
        }
    }
	CustomEditor "TexturePannerEditor"
}

修改后的顶点、片元着色器和原作者基本相同,表面着色器的顶点函数对应顶点、片元着色器的顶点函数,表面函数对应片元函数;由于原作者是通过可视化工具实现的,所以源代码充斥着变量为xxx_123等不宜阅读的代码,修改后的变量比较易于阅读。

同时修改后的Inspector面板如下:

全部汉化,请叫我“汉化大师”。

TexturePannerEditor的源码如下,有兴趣的朋友请自行研究:

using UnityEngine;
using UnityEditor;
 
public class TexturePannerEditor : ShaderGUI
{
    MaterialEditor _materialEditor;
    MaterialProperty[] _properties;

    //基本设置
    private MaterialProperty _MainTex = null;
    private MaterialProperty _Offset = null;
    private MaterialProperty _Tiling = null;
    private MaterialProperty _MainColor = null;
    private MaterialProperty _Colormode = null;

    //遮罩纹理
    private MaterialProperty _MaskTexture = null;
    private MaterialProperty _Negation = null;
    private MaterialProperty _MaskTextureRotationSpeed = null;

    //水平位移
    private MaterialProperty _HorizontalDisplacement = null;
    private MaterialProperty _HorizontalDisplacementAmplitude = null;
    private MaterialProperty _HorizontalDisplacementFrequency = null;
    private MaterialProperty _HorizontalDisplacementPrimaryPhase = null;
    private MaterialProperty _HorizontalDisplacementOffset = null;

    //垂直位移
    private MaterialProperty _VerticalDisplacement = null;
    private MaterialProperty _VerticalDisplacementAmplitude = null;
    private MaterialProperty _VerticalDisplacementFrequency = null;
    private MaterialProperty _VerticalDisplacementPrimaryPhase = null;
    private MaterialProperty _VerticalDisplacementOffset = null;

    //深度位移
    private MaterialProperty _DepthDisplacement = null;
    private MaterialProperty _DepthDisplacementAmplitude = null;
    private MaterialProperty _DepthDisplacementFrequency = null;
    private MaterialProperty _DepthDisplacementPrimaryPhase = null;
    private MaterialProperty _DepthDisplacementOffset = null;

    //水平拉伸
    private MaterialProperty _HorizontalStretch = null;
    private MaterialProperty _HorizontalStretchAmplitude = null;
    private MaterialProperty _HorizontalStretchFrequency = null;
    private MaterialProperty _HorizontalStretchPrimaryPhase = null;
    private MaterialProperty _HorizontalStretchOffset = null;
    private MaterialProperty _HorizontalStretchPivotPoint = null;

    //垂直拉伸
    private MaterialProperty _VerticalStretch = null;
    private MaterialProperty _VerticalStretchAmplitude = null;
    private MaterialProperty _VerticalStretchFrequency = null;
    private MaterialProperty _VerticalStretchPrimaryPhase = null;
    private MaterialProperty _VerticalStretchOffset = null;
    private MaterialProperty _VerticalStretchPivotPoint = null;

    //深度拉伸
    private MaterialProperty _DepthStretch = null;
    private MaterialProperty _DepthStretchAmplitude = null;
    private MaterialProperty _DepthStretchFrequency = null;
    private MaterialProperty _DepthStretchPrimaryPhase = null;
    private MaterialProperty _DepthStretchOffset = null;
    private MaterialProperty _DepthStretchPivotPoint = null;

    //滚动/旋转
    private MaterialProperty _ScrollRotate = null;
    private MaterialProperty _ScrollingSpeedX = null;
    private MaterialProperty _ScrollingSpeedY = null;
    private MaterialProperty _RotationSpeed = null;

    //扫描线
    private MaterialProperty _ScanlinesX = null;
    private MaterialProperty _ScanlinesY = null;
    private MaterialProperty _ScanlinesZ = null;
    private MaterialProperty _ScanlinesScaleX = null;
    private MaterialProperty _ScanlinesScaleY = null;
    private MaterialProperty _ScanlinesScaleZ = null;
    private MaterialProperty _ScanlinesSpeedX = null;
    private MaterialProperty _ScanlinesSpeedY = null;
    private MaterialProperty _ScanlinesSpeedZ = null;
    private MaterialProperty _SharpX = null;
    private MaterialProperty _SharpY = null;
    private MaterialProperty _SharpZ = null;

    //自发光
    private MaterialProperty _GlobalEmissionFlicker = null;
    private MaterialProperty _GlobalEmissionFlickerAmplitude = null;
    private MaterialProperty _GlobalEmissionFlickerFrequency = null;
    private MaterialProperty _GlobalEmissionFlickerPrimaryPhase = null;
    private MaterialProperty _GlobalEmissionFlickerOffset = null;
 
    //FOLDOUT BOOLS
    protected static bool ShowGeneralSettings = false;
    protected static bool ShowTextureMaskingSettings = false;
    protected static bool ShowScanlinesSettings = false;
    protected static bool ShowScanlinesXSettings = false;
    protected static bool ShowScanlinesYSettings = false;
    protected static bool ShowScanlinesZSettings = false;
    protected static bool ShowStretchSettings = false;
    protected static bool ShowHorizontalStretchSettings = false;
    protected static bool ShowVerticalStretchSettings = false;
    protected static bool ShowDepthStretchSettings = false;
    protected static bool ShowDisplacementSettings = false;
    protected static bool ShowHorizontalDisplacementSettings = false;
    protected static bool ShowVerticalDisplacementSettings = false;
    protected static bool ShowDepthDisplacementSettings = false;
    protected static bool ShowScrollingSettings = false;
    protected static bool ShowEmissionSettings = false;

    public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
    {
        _properties = properties;
        _materialEditor = materialEditor;
        EditorGUI.BeginChangeCheck();
        DrawGUI();
    }

    //void InitializeFoldoutStyle() {
    //foldoutStyle.fontStyle = FontStyle.Bold;
    //}
    void DrawGUI() {
        //InitializeFoldoutStyle();
        GetProperties();
        DrawBanner();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowGeneralSettings = EditorGUILayout.Foldout(ShowGeneralSettings, "主纹理");
        if (ShowGeneralSettings){
            DrawMainSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowTextureMaskingSettings = EditorGUILayout.Foldout(ShowTextureMaskingSettings, "遮罩纹理");
        if (ShowTextureMaskingSettings)
        {
            DrawTextureMaskingSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowDisplacementSettings = EditorGUILayout.Foldout(ShowDisplacementSettings, "顶点位移");
        if (ShowDisplacementSettings)
        {
            DrawDisplacementSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowStretchSettings = EditorGUILayout.Foldout(ShowStretchSettings, "顶点拉伸");
        if (ShowStretchSettings)
        {
            DrawStretchSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowScanlinesSettings = EditorGUILayout.Foldout(ShowScanlinesSettings, "切割线");
        if (ShowScanlinesSettings)
        {
            DrawScanlinesSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowScrollingSettings = EditorGUILayout.Foldout(ShowScrollingSettings, "滚动/旋转");
        if (ShowScrollingSettings){
            DrawScrollingSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(EditorStyles.helpBox);
        EditorGUI.indentLevel++;
        ShowEmissionSettings = EditorGUILayout.Foldout(ShowEmissionSettings, "自发光");
        if (ShowEmissionSettings){
            DrawEmissionSettings();
        }
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();		
    }
    
    void GetProperties() {

        //主纹理
        _MainTex = FindProperty("_MainTex", _properties);
        _Offset = FindProperty("_Offset", _properties);
        _Tiling = FindProperty("_Tiling", _properties);
        _MainColor = FindProperty("_MainColor", _properties);
        _Colormode = FindProperty("_Colormode", _properties);
        
        //遮罩纹理
        _MaskTexture = FindProperty("_MaskTexture", _properties);
        _Negation = FindProperty("_Negation", _properties);
        _MaskTextureRotationSpeed = FindProperty("_MaskTextureRotationSpeed", _properties);

        //水平位移
        _HorizontalDisplacement = FindProperty("_HorizontalDisplacement", _properties);
        _HorizontalDisplacementAmplitude = FindProperty("_HorizontalDisplacementAmplitude", _properties);
        _HorizontalDisplacementFrequency = FindProperty("_HorizontalDisplacementFrequency", _properties);
        _HorizontalDisplacementPrimaryPhase = FindProperty("_HorizontalDisplacementPrimaryPhase", _properties);
        _HorizontalDisplacementOffset = FindProperty("_HorizontalDisplacementOffset", _properties);

        //垂直位移
        _VerticalDisplacement = FindProperty("_VerticalDisplacement", _properties);
        _VerticalDisplacementAmplitude = FindProperty("_VerticalDisplacementAmplitude", _properties);
        _VerticalDisplacementFrequency = FindProperty("_VerticalDisplacementFrequency", _properties);
        _VerticalDisplacementPrimaryPhase = FindProperty("_VerticalDisplacementPrimaryPhase", _properties);
        _VerticalDisplacementOffset = FindProperty("_VerticalDisplacementOffset", _properties);

        //深度位移
        _DepthDisplacement = FindProperty("_DepthDisplacement", _properties);
        _DepthDisplacementAmplitude = FindProperty("_DepthDisplacementAmplitude", _properties);
        _DepthDisplacementFrequency = FindProperty("_DepthDisplacementFrequency", _properties);
        _DepthDisplacementPrimaryPhase = FindProperty("_DepthDisplacementPrimaryPhase", _properties);
        _DepthDisplacementOffset = FindProperty("_DepthDisplacementOffset", _properties);

        //水平拉伸
        _HorizontalStretch = FindProperty("_HorizontalStretch", _properties);
        _HorizontalStretchAmplitude = FindProperty("_HorizontalStretchAmplitude", _properties);
        _HorizontalStretchFrequency = FindProperty("_HorizontalStretchFrequency", _properties);
        _HorizontalStretchPrimaryPhase = FindProperty("_HorizontalStretchPrimaryPhase", _properties);
        _HorizontalStretchOffset = FindProperty("_HorizontalStretchOffset", _properties);
        _HorizontalStretchPivotPoint = FindProperty("_HorizontalStretchPivotPoint", _properties);

        //垂直拉伸
        _VerticalStretch = FindProperty("_VerticalStretch", _properties);
        _VerticalStretchAmplitude = FindProperty("_VerticalStretchAmplitude", _properties);
        _VerticalStretchFrequency = FindProperty("_VerticalStretchFrequency", _properties);
        _VerticalStretchPrimaryPhase = FindProperty("_VerticalStretchPrimaryPhase", _properties);
        _VerticalStretchOffset = FindProperty("_VerticalStretchOffset", _properties);
        _VerticalStretchPivotPoint = FindProperty("_VerticalStretchPivotPoint", _properties);

        //深度拉伸
        _DepthStretch = FindProperty("_DepthStretch", _properties);
        _DepthStretchAmplitude = FindProperty("_DepthStretchAmplitude", _properties);
        _DepthStretchFrequency = FindProperty("_DepthStretchFrequency", _properties);
        _DepthStretchPrimaryPhase = FindProperty("_DepthStretchPrimaryPhase", _properties);
        _DepthStretchOffset = FindProperty("_DepthStretchOffset", _properties);
        _DepthStretchPivotPoint = FindProperty("_DepthStretchPivotPoint", _properties);

        //切割线
        _ScanlinesX = FindProperty("_ScanlinesX", _properties);
        _ScanlinesScaleX = FindProperty("_ScanlinesScaleX", _properties);
        _ScanlinesSpeedX = FindProperty("_ScanlinesSpeedX", _properties);
        _SharpX = FindProperty("_SharpX", _properties);

        _ScanlinesY = FindProperty("_ScanlinesY", _properties);
        _ScanlinesScaleY = FindProperty("_ScanlinesScaleY", _properties);
        _ScanlinesSpeedY = FindProperty("_ScanlinesSpeedY", _properties);
        _SharpY = FindProperty("_SharpY", _properties);

        _ScanlinesZ = FindProperty("_ScanlinesZ", _properties);
        _ScanlinesScaleZ = FindProperty("_ScanlinesScaleZ", _properties);
        _ScanlinesSpeedZ = FindProperty("_ScanlinesSpeedZ", _properties);
        _SharpZ = FindProperty("_SharpZ", _properties);

        //滚动/旋转
        _ScrollRotate = FindProperty("_ScrollRotate", _properties);
        _ScrollingSpeedX = FindProperty("_ScrollingSpeedX", _properties);
        _ScrollingSpeedY = FindProperty("_ScrollingSpeedY", _properties);
        _RotationSpeed = FindProperty("_RotationSpeed", _properties);

        //自发光
        _GlobalEmissionFlicker = FindProperty("_GlobalEmissionFlicker", _properties);
        _GlobalEmissionFlickerAmplitude = FindProperty("_GlobalEmissionFlickerAmplitude", _properties);
        _GlobalEmissionFlickerFrequency = FindProperty("_GlobalEmissionFlickerFrequency", _properties);
        _GlobalEmissionFlickerPrimaryPhase = FindProperty("_GlobalEmissionFlickerPrimaryPhase", _properties);
        _GlobalEmissionFlickerOffset = FindProperty("_GlobalEmissionFlickerOffset", _properties);
    }
    
    static Texture2D bannerTexture = null;
    static GUIStyle title = null;
    static GUIStyle linkStyle = null;
    static string repoURL = "https://github.com/adultlink/texturepanner";

    void DrawBanner()
    {
        if (bannerTexture == null)
            bannerTexture = Resources.Load<Texture2D>("TexturePannerBanner");

        if (title == null)
        {
            title = new GUIStyle();
            title.fontSize = 20;
            title.alignment = TextAnchor.MiddleCenter;
            title.normal.textColor = new Color(1f, 1f, 1f);
        }
		
        if (linkStyle == null) linkStyle = new GUIStyle();

        if (bannerTexture != null)
        {
            GUILayout.Space(4);
            var rect = GUILayoutUtility.GetRect(0, int.MaxValue, 60, 60);
            EditorGUI.DrawPreviewTexture(rect, bannerTexture, null, ScaleMode.ScaleAndCrop);
            //
            EditorGUI.LabelField(rect, "Texture Panner", title);

            if (GUI.Button(rect, "", linkStyle)) {
                Application.OpenURL(repoURL);
            }
            GUILayout.Space(4);
        }
    }

    /// <summary>
    /// 基本设置
    /// </summary>
    void DrawMainSettings() {
        //MAIN SETTINGS
        _materialEditor.SetDefaultGUIWidths();
        _materialEditor.ShaderProperty(_MainTex, "主纹理");
        _materialEditor.ShaderProperty(_Tiling, "缩放");
        _materialEditor.ShaderProperty(_Offset, "偏移");
        _materialEditor.ShaderProperty(_MainColor, "主颜色");
        _materialEditor.ShaderProperty(_Colormode, "颜色混合模式");
    }
    
    /// <summary>
    /// 遮罩纹理设置
    /// </summary>
    void DrawTextureMaskingSettings() {
        //TEXTURE MASKING
        _materialEditor.SetDefaultGUIWidths();
        _materialEditor.ShaderProperty(_MaskTexture, "遮罩纹理");
        _materialEditor.ShaderProperty(_Negation, "是否取反");
        _materialEditor.ShaderProperty(_MaskTextureRotationSpeed, "旋转速度");
    }	

    /// <summary>
    /// 顶点移动设置
    /// </summary>
    void DrawDisplacementSettings() {
        //VERTICAL MOVEMENT
        EditorGUI.indentLevel++;
        ShowHorizontalDisplacementSettings = EditorGUILayout.Foldout(ShowHorizontalDisplacementSettings, "水平位移");
        if (ShowHorizontalDisplacementSettings)
        {
            _materialEditor.SetDefaultGUIWidths();
            _materialEditor.ShaderProperty(_HorizontalDisplacement, "是否开启水平位移");
            _materialEditor.ShaderProperty(_HorizontalDisplacementAmplitude, "振幅");
            _materialEditor.ShaderProperty(_HorizontalDisplacementFrequency, "频率");
            _materialEditor.ShaderProperty(_HorizontalDisplacementPrimaryPhase, "初相");
            _materialEditor.ShaderProperty(_HorizontalDisplacementOffset, "锚点偏移");
        }
        ShowVerticalDisplacementSettings = EditorGUILayout.Foldout(ShowVerticalDisplacementSettings, "垂直位移");
        if (ShowVerticalDisplacementSettings){
            _materialEditor.SetDefaultGUIWidths();
            _materialEditor.ShaderProperty(_VerticalDisplacement, "是否开启垂直位移");
            _materialEditor.ShaderProperty(_VerticalDisplacementAmplitude, "振幅");
            _materialEditor.ShaderProperty(_VerticalDisplacementFrequency, "频率");
            _materialEditor.ShaderProperty(_VerticalDisplacementPrimaryPhase, "初相");
            _materialEditor.ShaderProperty(_VerticalDisplacementOffset, "锚点偏移");
        }
        ShowDepthDisplacementSettings = EditorGUILayout.Foldout(ShowDepthDisplacementSettings, "深度位移");
        if (ShowDepthDisplacementSettings)
        {
            _materialEditor.SetDefaultGUIWidths();
            _materialEditor.ShaderProperty(_DepthDisplacement, "是否开启深度位移");
            _materialEditor.ShaderProperty(_DepthDisplacementAmplitude, "振幅");
            _materialEditor.ShaderProperty(_DepthDisplacementFrequency, "频率");
            _materialEditor.ShaderProperty(_DepthDisplacementPrimaryPhase, "初相");
            _materialEditor.ShaderProperty(_DepthDisplacementOffset, "锚点偏移");
        }
        EditorGUI.indentLevel--;
    }

    /// <summary>
    /// 顶点拉伸设置
    /// </summary>
    void DrawStretchSettings() {
        //VERTICAL STRETCH
        EditorGUI.indentLevel++;
        _materialEditor.SetDefaultGUIWidths();
        ShowHorizontalStretchSettings = EditorGUILayout.Foldout(ShowHorizontalStretchSettings, "水平拉伸");
        if (ShowHorizontalStretchSettings)
        {
            _materialEditor.ShaderProperty(_HorizontalStretch, "是否开启水平拉伸");
            _materialEditor.ShaderProperty(_HorizontalStretchAmplitude, "振幅");
            _materialEditor.ShaderProperty(_HorizontalStretchFrequency, "频率");
            _materialEditor.ShaderProperty(_HorizontalStretchPrimaryPhase, "初相");
            _materialEditor.ShaderProperty(_HorizontalStretchOffset, "偏移");
            _materialEditor.ShaderProperty(_HorizontalStretchPivotPoint, "锚点偏移");
        }
        ShowVerticalStretchSettings = EditorGUILayout.Foldout(ShowVerticalStretchSettings, "垂直拉伸");
        if (ShowVerticalStretchSettings){
            _materialEditor.ShaderProperty(_VerticalStretch, "是否开启垂直拉伸");
            _materialEditor.ShaderProperty(_VerticalStretchAmplitude, "振幅");
            _materialEditor.ShaderProperty(_VerticalStretchFrequency, "频率");
            _materialEditor.ShaderProperty(_VerticalStretchPrimaryPhase, "初相");
            _materialEditor.ShaderProperty(_VerticalStretchOffset, "偏移");
            _materialEditor.ShaderProperty(_VerticalStretchPivotPoint, "锚点偏移");
        }
        ShowDepthStretchSettings = EditorGUILayout.Foldout(ShowDepthStretchSettings, "深度拉伸");
        if (ShowDepthStretchSettings)
        {
            _materialEditor.ShaderProperty(_DepthStretch, "是否开启深度拉伸");
            _materialEditor.ShaderProperty(_DepthStretchAmplitude, "振幅");
            _materialEditor.ShaderProperty(_DepthStretchFrequency, "频率");
            _materialEditor.ShaderProperty(_DepthStretchPrimaryPhase, "初相");
            _materialEditor.ShaderProperty(_DepthStretchOffset, "偏移");
            _materialEditor.ShaderProperty(_DepthStretchPivotPoint, "锚点偏移");
        }
        EditorGUI.indentLevel--;
    }

    /// <summary>
    /// 切割线设置
    /// </summary>
    void DrawScanlinesSettings() {
        //SCANLINES
        EditorGUI.indentLevel++;
        _materialEditor.SetDefaultGUIWidths();
		
        ShowScanlinesXSettings = EditorGUILayout.Foldout(ShowScanlinesXSettings, "x轴切割线");
        if (ShowScanlinesXSettings){
            _materialEditor.ShaderProperty(_ScanlinesX, "是否开启x轴切割线");
            _materialEditor.ShaderProperty(_ScanlinesScaleX, "大小");
            _materialEditor.ShaderProperty(_ScanlinesSpeedX, "速度");
            _materialEditor.ShaderProperty(_SharpX, "Sharp");
        }
        //Y
        ShowScanlinesYSettings = EditorGUILayout.Foldout(ShowScanlinesYSettings, "y轴切割线");
        if (ShowScanlinesYSettings){
            _materialEditor.ShaderProperty(_ScanlinesY, "是否开启y轴切割线");
            _materialEditor.ShaderProperty(_ScanlinesScaleY, "大小");
            _materialEditor.ShaderProperty(_ScanlinesSpeedY, "速度");
            _materialEditor.ShaderProperty(_SharpY, "Sharp");
        }
        //Z
        ShowScanlinesZSettings = EditorGUILayout.Foldout(ShowScanlinesZSettings, "z轴切割线");
        if (ShowScanlinesZSettings){
            _materialEditor.ShaderProperty(_ScanlinesZ, "是否开启z轴切割线");
            _materialEditor.ShaderProperty(_ScanlinesScaleZ, "大小");
            _materialEditor.ShaderProperty(_ScanlinesSpeedZ, "速度");
            _materialEditor.ShaderProperty(_SharpZ, "Sharp");
        }
        EditorGUI.indentLevel--;
    }

    /// <summary>
    /// 滚动旋转设置
    /// </summary>
    void DrawScrollingSettings() {
        _materialEditor.SetDefaultGUIWidths();
        _materialEditor.ShaderProperty(_ScrollRotate, "滚动/旋转/默认");
        _materialEditor.ShaderProperty(_ScrollingSpeedX, "x轴滚动速度");
        _materialEditor.ShaderProperty(_ScrollingSpeedY, "y轴滚动速度");
        _materialEditor.ShaderProperty(_RotationSpeed, "旋转速度");
    }

    /// <summary>
    /// 自发光设置
    /// </summary>
    void DrawEmissionSettings() {
        //GLOBAL EMISSION
        _materialEditor.SetDefaultGUIWidths();
        _materialEditor.ShaderProperty(_GlobalEmissionFlicker, "是否开启自发光");
        _materialEditor.ShaderProperty(_GlobalEmissionFlickerAmplitude, "振幅");
        _materialEditor.ShaderProperty(_GlobalEmissionFlickerFrequency, "频率");
        _materialEditor.ShaderProperty(_GlobalEmissionFlickerPrimaryPhase, "初相");
        _materialEditor.ShaderProperty(_GlobalEmissionFlickerOffset, "偏移");
    }
}

最后,附上修改后的资源地址:

蓝奏云:https://lanzous.com/b00njzwni
密码:8yyw

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值