一些shader 代码

Evaporating.shader

 

Shader "Custom/Evaporating"
{
	Properties
	{
		_MainTex("Texture", 2D) = "white" {}
		_Color("Color", Color) = (1, 1, 1, 1)
		_GradientTex("Gradient Texture", 2D) = "white" {}
		_GradientThreshold("Gradient Threshold", float) = 0.5
		_NoiseTex("Noise Texture", 2D) = "white" {}
		_UVOffset("UV Offset", vector) = (1, 1, 1, 1)
		_Speed("Speed", float) = 1.0
		_Zoom("Zoom", float) = 1.0
		_ShapeMask("Shape Texture", 2D) = "white" {}
		
		_Shine_R("Shine R", float) = 1.0
		_Shine_G("Shine G", float) = 1.0
		_Shine_B("Shine B", float) = 1.0
		_Shine2_R("Shine R", float) = 1.0
		_Shine2_G("Shine G", float) = 1.0
		_Shine2_B("Shine B", float) = 1.0

		_TimeOffset("Time Offset", float) = 0.0
	}

	SubShader
	{
        Tags
		{ 
			"Queue" = "Transparent"
		}

		Pass
		{
			ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			// Properties
			sampler2D _MainTex;
			float4 _Color;
			sampler2D _GradientTex;
			float _GradientThreshold;
			sampler2D _NoiseTex;
			float _Speed;
			float _Zoom;
			float4 _UVOffset;
			sampler2D _ShapeMask;
			float _Shine_R;
			float _Shine_G;
			float _Shine_B;
			float _Shine2_R;
			float _Shine2_G;
			float _Shine2_B;
			float _TimeOffset;

			struct vertexInput
			{
				float4 vertex : POSITION;
				float3 texCoord : TEXCOORD0;
			};

			struct vertexOutput
			{
				float4 pos : SV_POSITION;
				float3 texCoord : TEXCOORD0;
			};

			vertexOutput vert(vertexInput input)
			{
				vertexOutput output;
				output.pos = UnityObjectToClipPos(input.vertex); 
				output.texCoord = input.texCoord;
				return output;
			}

			float4 frag(vertexOutput input) : COLOR
			{
				// the main texture rgba
				float4 albedo = tex2D(_MainTex, input.texCoord.xy);
				// the base color
				float4 col = float4(albedo.rgba * _Color.rgba);
				// noisePadding will be used to zoom and change the offsets of the noise texture (so that different objects can look more random)
				// to randomize the padding from the gameObject script: material.SetVector("_UVOffset", new Vector2(Random.Range(0, 1f), Random.Range(0, 1f)));
				float2 noisePadding = _Zoom * float2(input.texCoord.x + _UVOffset.x, input.texCoord.y +  _UVOffset.y);
				// the gradient texture
				float gradient = tex2Dlod(_GradientTex, float4(input.texCoord.xy, 0, 0));
				// the noise texture padded with the noisePadding and moving up with _Time.y*_Speed
				float noise = tex2Dlod(_NoiseTex, float4(noisePadding.x, noisePadding.y-_Time.y*_Speed, 0, 0));
				// for gradient and noise we can get only one color channel since these textures are just grayscale

				// choosing what will be transparent
				if(noise < (1.0f - gradient.r) * _GradientThreshold) col.a=1;
				else col.a=0;
				
				// color channels multiplier (to change colors near the disappearing areas)
				col.r *= 1.0f + gradient.r * _Shine2_R + gradient.r * (1.0f - noise) *_Shine_R;
				col.g *= 1.0f + gradient.r * _Shine2_G + gradient.r * (1.0f - noise) *_Shine_G;
				col.b *= 1.0f + gradient.r * _Shine2_B + gradient.r * (1.0f - noise) *_Shine_B;
				
				// masking texture (try changing those magic numbers to change the wobbly effect)
				// _TimeOffset is used to make different objects look different
				// to randomize the displacing effect from the gameObject script: material.SetFloat("_TimeOffset", Random.Range(0,1.0f));
				float4 shape = tex2D(_ShapeMask,  float4(input.texCoord.x + 0.4 * gradient.r * sin((_Time.y + _TimeOffset) * 4 + input.texCoord.y * 2) * noise, input.texCoord.y, 0, 0));
				// applying the masking texture to the alpha channel
				col.a *= shape.a;
				
				return col;
			}

			ENDCG
		}
	}
}

边缘发光效果的两种写法

Shader "Custom/Rim Light" {
       Properties {
              _MainTex("Base (RGB)", 2D) = "white" {}
              _RimColor("_RimColor", Color) =(0.17,0.36,0.81,0.0)
              _RimWidth("_RimWidth", Range(0.6,9.0)) = 0.9
       }
 
       SubShader {
              Tags{ "RenderType" = "Opaque"}
 LOD 150


              CGPROGRAM
 
              #pragma surface surf Lambert
             
              struct Input {
                     float2 uv_MainTex;
                     float3 viewDir;
              };
 
              sampler2D _MainTex;
              fixed4 _RimColor;
              fixed _RimWidth;
 
              void surf (Input IN, inout SurfaceOutput o) {
fixed4 t = tex2D (_MainTex, IN.uv_MainTex);
                     o.Albedo = t.rgb;
o.Alpha = t.a;
                     half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
                     o.Emission= _RimColor.rgb * pow (rim, _RimWidth);
              }


              ENDCG
       }
 
       Fallback "Diffuse"
}

 

Shader "Custom/Rim Light VF" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Color ("Main Color", Color) = (1,1,1,1)
        _RimColor ("Rim Color", Color) = (1, 1, 1, 1)
        _RimWidth ("Rim Width", float) = 0.9
    }
    SubShader {
        Pass {
        Lighting Off
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"


                struct appdata 
                {
                    float4 vertex : POSITION;
                    float3 normal : NORMAL;
                    float2 texcoord : TEXCOORD0;
                };


                struct v2f 
                {
                    float4 pos : SV_POSITION;
                    float2 uv : TEXCOORD0;
                    fixed3 color : COLOR;
                };


                uniform float4 _MainTex_ST;
                uniform fixed4 _RimColor;
                float _RimWidth;


                v2f vert (appdata_base v) {
                    v2f o;
                    o.pos = mul (UNITY_MATRIX_MVP, v.vertex);


                    float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                    float dotProduct = 1 - dot(v.normal, viewDir);
                   
                    o.color = smoothstep(1 - _RimWidth, 1.0, dotProduct);
                    o.color *= _RimColor;


                    o.uv = v.texcoord.xy;
                    return o;
                }


                uniform sampler2D _MainTex;
                uniform fixed4 _Color;


                fixed4 frag(v2f i) : COLOR {
                    fixed4 texcol = tex2D(_MainTex, i.uv);
                    texcol *= _Color;
                    texcol.rgb += i.color;
                    return texcol;
                }
            ENDCG
        }
    }
}

 

森林树木随风摆动的shader:

Shader "Consume/Leaf Swing"
{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Pos("Position",Vector) =(0,0,0,0)
_Direction("Direction",Vector) =(0,0,0,0)
_TimeScale("TimeScale",float) = 1
_TimeDelay("TimeDelay",float) = 1
}
SubShader
{
LOD 100
Tags
{
"RenderType"="Opaque"
"Queue"="Transparent"
}


CGPROGRAM
#pragma surface surf Lambert vertex:vert alpha


sampler2D _MainTex;
fixed4 _Pos;
fixed4 _Direction;
half _TimeScale;
half _TimeDelay;


struct Input
{
half2 uv_MainTex;
};


void surf (Input IN, inout SurfaceOutput o)
{
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}


void vert (inout appdata_full v)
{  
half dis = distance(v.vertex ,_Pos) ;
half time = (_Time.y + _TimeDelay) * _TimeScale;
v.vertex.xyz += dis * (sin(time) * cos(time * 2 / 3) + 1) * _Direction.xyz;//核心,动态顶点变换
}


ENDCG
}


FallBack "Transparent/Cutout/VertexLit"
}

 

 

外发光shader

Shader "Faye/OutLightting"
{
Properties
{
_MainTex("Texture (RGB)", 2D) = "black" {}
_Color("Color", Color) = (0, 0, 0, 1)
_AtmoColor("Atmosphere Color", Color) = (0.5, 0.5, 1.0, 1)
_Size("Size", Float) = 0.1
_Falloff("Falloff", Float) = 5
_FalloffPlanet("Falloff Planet", Float) = 5
_Transparency("Transparency", Float) = 15
_TransparencyPlanet("Transparency Planet", Float) = 1
}
SubShader
{
Pass
{
Name "PlanetBase"
Tags {"LightMode" = "Always"}
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform float4 _Color;
uniform float4 _AtmoColor;
uniform float _FalloffPlanet;
uniform float _TransparencyPlanet;
struct v2f
{
float4 pos : SV_POSITION;
float3 normal : TEXCOORD0;
float3 worldvertpos : TEXCOORD1;
float2 texcoord : TEXCOORD2;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.normal = v.normal;
o.worldvertpos = mul(_Object2World, v.vertex).xyz;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 frag(v2f i) : COLOR
{
i.normal = normalize(i.normal);
float3 viewdir = normalize(_WorldSpaceCameraPos-i.worldvertpos);
float4 atmo = _AtmoColor;
atmo.a = pow(1.0-saturate(dot(viewdir, i.normal)), _FalloffPlanet);
atmo.a *= _TransparencyPlanet*_Color;
float4 color = tex2D(_MainTex, i.texcoord)*_Color;
color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);
return color*dot(normalize(i.worldvertpos-_WorldSpaceLightPos0), i.normal);
}
ENDCG
}
Pass
{
Name "AtmosphereBase"
Tags {"LightMode" = "Always"}
Cull Front
Blend SrcAlpha One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform float4 _Color;
uniform float4 _AtmoColor;
uniform float _Size;
uniform float _Falloff;
uniform float _Transparency;
struct v2f
{
float4 pos : SV_POSITION;
float3 normal : TEXCOORD0;
float3 worldvertpos : TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f o;
v.vertex.xyz += v.normal*_Size;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.normal = v.normal;
o.worldvertpos = mul(_Object2World, v.vertex);
return o;
}
float4 frag(v2f i) : COLOR
{
i.normal = normalize(i.normal);
float3 viewdir = normalize(i.worldvertpos-_WorldSpaceCameraPos);
float4 color = _AtmoColor;
color.a = pow(saturate(dot(viewdir, i.normal)), _Falloff);
color.a *= _Transparency*_Color*dot(normalize(i.worldvertpos-_WorldSpaceLightPos0), i.normal);
return color;
}
ENDCG
}
}
FallBack "Diffuse"
}

 

河流效果

Shader "Custom/TexSurfaceShader" {
        Properties {
                _MainTint ("Diffuse Tint", Color) = (1,1,1,1)
                _MainTex ("Base (RGB)", 2D) = "white" {}
                _ScrollXSpeed ("X Scroll Speed",Range(0,10)) = 2
                _ScrollYSpeed ("Y Scroll Speed",Range(0,10)) = 2
        }
        SubShader {
                Tags { "RenderType"="Opaque" }
                LOD 200
                 
                CGPROGRAM
                // Physically based Standard lighting model, and enable shadows on all light types
                #pragma surface surf Standard fullforwardshadows
 
                // Use shader model 3.0 target, to get nicer looking lighting
                #pragma target 3.0
 
                fixed4  _MianTint;
                fixed   _ScrollXSpeed;
                fixed   _ScrollYSpeed;
                sampler2D _MainTex;
 
                struct Input {
                        float2 uv_MainTex;
                };
 
                void surf (Input IN, inout SurfaceOutputStandard o)
                {
                    //创建一个变量 存储图片UV
                    fixed2 scrolledUV  = IN.uv_MainTex;
 
                        //创建临时变量存储 X Y 
                        fixed xScrollValue  = _ScrollXSpeed * _Time;
                        fixed yScrollValue  = _ScrollYSpeed * _Time;
 
                        //计算X Y 的偏移
                        scrolledUV += fixed2(xScrollValue,yScrollValue);
 
                        half4 c = tex2D(_MainTex,scrolledUV);
                        o.Albedo = c.rgb;
                        o.Alpha = c.a;
                }
                ENDCG
        }
        FallBack "Diffuse"
}

 

shader实现动画帧

Shader "Custom/SpriteAnimationShader" {
        Properties {
                _MainTex ("Base (RGB)", 2D) = "white" {}
                _TexWidth("Sheet Width",float)=0.0
                _CellAmout("Cell Amount",float) = 0.0
                _Speed("Speed",Range(0.01,32)) = 12
        }
        SubShader {
                Tags { "RenderType"="Opaque" }
                LOD 200
                 
                CGPROGRAM
                // Physically based Standard lighting model, and enable shadows on all light types
                #pragma surface surf Standard fullforwardshadows
 
                // Use shader model 3.0 target, to get nicer looking lighting
                #pragma target 3.0
 
 
                sampler2D _MainTex;
                fixed  _TexWidth;
                fixed   _CellAmout;
                fixed   _Speed;
 
 
                struct Input {
                        float2 uv_MainTex;
                };
 
 
 
                void surf (Input IN, inout SurfaceOutputStandard o)
                {
                    float2 spriteUV  = IN.uv_MainTex; //将输入的UV值存储到临时变量
 
                        float cellPixelWidth  = _TexWidth/_CellAmout; //得到每个精灵的宽度
                        float cellUVPercentage = cellPixelWidth/_TexWidth ;  //计算每个精灵在整张图中的百分比
 
                        float timeVal = fmod(_Time.y *_Speed , _CellAmout);
                        timeVal  = ceil(timeVal);
 
                        float xValue  = spriteUV.x;
 
                        //计算精灵在X方向上UV偏移量
                        xValue += cellUVPercentage * timeVal * _CellAmout;  
                        xValue *= cellUVPercentage;
 
                        spriteUV = float2(xValue,spriteUV.y);
                        // Albedo comes from a texture tinted by color
                        fixed4 c = tex2D (_MainTex, spriteUV) ;
                        o.Albedo = c.rgb;
                 
                        o.Alpha = c.a;
                }
                ENDCG
        }
        FallBack "Diffuse"
}

你可能感兴趣的:(Shader)