收集的各种shder效果代码

//流光效果
//by:puppet_master
//2017.7.30
Shader “ApcShader/FlashEffect”
{
Properties
{
_MainTex(“MainTex(RGB)”, 2D) = “white” {}
_FlashTex(“FlashTex”, 2D) = “black” {}
_FlashColor(“FlashColor”,Color) = (1,1,1,1)
_FlashFactor(“FlashFactor”, Vector) = (0, 1, 0.5, 0.5)
_FlashStrength (“FlashStrength”, Range(0, 5)) = 1
}

CGINCLUDE
#include "Lighting.cginc"
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _FlashTex;
uniform fixed4 _FlashColor;
//改为一个vector4,减少传参次数消耗
uniform fixed4 _FlashFactor;
uniform fixed _FlashStrength;

struct v2f 
{
	float4 pos : SV_POSITION;
	float3 worldNormal : NORMAL;
	float2 uv : TEXCOORD0;
	float3 worldLight : TEXCOORD1;
	float4 worldPos : TEXCOORD2;
};

v2f vert(appdata_base v)
{
	v2f o;
	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
	o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
	//顶点转化到世界空间
	o.worldPos = mul(unity_ObjectToWorld, v.vertex);
	o.worldNormal = UnityObjectToWorldNormal(v.normal);
	o.worldLight = UnityObjectToWorldDir(_WorldSpaceLightPos0.xyz);
	return o;
}
		
fixed4 frag(v2f i) : SV_Target
{
	half3 normal = normalize(i.worldNormal);
	half3 light = normalize(i.worldLight);
	fixed diff = max(0, dot(normal, light));
	fixed4 albedo = tex2D(_MainTex, i.uv);
	//通过时间偏移世界坐标对flashTex进行采样
	half2 flashuv = i.worldPos.xy * _FlashFactor.zw + _FlashFactor.xy * _Time.y;
	fixed4 flash = tex2D(_FlashTex, flashuv) * _FlashColor * _FlashStrength;
	fixed4 c;
	//将flash图与原图叠加
	c.rgb = diff * albedo + flash.rgb;
	c.a = 1;
	return c;
}
ENDCG

SubShader
{
	
	Pass
	{
		Tags{ "RenderType" = "Opaque" }
		
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		ENDCG	
	}
}
FallBack "Diffuse"

}

//溶解效果
//by:puppet_master
//2017.8.11

Shader “ApcShader/DissolveEffectX”
{
Properties{
_Diffuse(“Diffuse”, Color) = (1,1,1,1)
_DissolveColor(“Dissolve Color”, Color) = (1,1,1,1)
_MainTex(“Base 2D”, 2D) = “white”{}
_DissolveMap(“DissolveMap”, 2D) = “white”{}
_DissolveThreshold(“DissolveThreshold”, Range(0,1)) = 0
_DissolveSpeedFactor(“DissolveSpeed”, Range(0,5)) = 2
_DissolveControl(“ColorFactorB”, Float) = 0
}

CGINCLUDE
#include "Lighting.cginc"
uniform fixed4 _Diffuse;
uniform fixed4 _DissolveColor;
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
uniform sampler2D _DissolveMap;
uniform float _DissolveThreshold;
uniform float _DissolveSpeedFactor;
uniform float _DissolveControl; 

struct v2f
{
	float4 pos : SV_POSITION;
	float3 worldNormal : TEXCOORD0;
	float2 uv : TEXCOORD1;
	float4 objPos : TEXCOORD2; 
};

v2f vert(appdata_base v)
{
	v2f o;
	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
	o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
	o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
	o.objPos = v.vertex;  
	return o;
}

fixed4 frag(v2f i) : SV_Target
{
	fixed4 dissolve = tex2D(_DissolveMap, i.uv);
	//Diffuse + Ambient光照计算
	fixed3 worldNormal = normalize(i.worldNormal);
	fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
	fixed3 lambert = saturate(dot(worldNormal, worldLightDir));
	fixed3 albedo = lambert * _Diffuse.xyz * _LightColor0.xyz + UNITY_LIGHTMODEL_AMBIENT.xyz;
	fixed3 color = tex2D(_MainTex, i.uv).rgb * albedo;
	float factor = i.objPos.x - _DissolveControl;
	if(factor < 0)
	{
		clip(_DissolveThreshold - dissolve.r * abs(factor) * _DissolveSpeedFactor);
	}
	return fixed4(color, 1);
}
ENDCG

SubShader
{
	Tags{ "RenderType" = "Opaque" }
	Pass
	{
		Cull Off
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag	
		ENDCG
	}
}
FallBack "Diffuse"

}

溶解效果代码例如以下:

Shader “Esfog/Dissolve”
{
Properties
{
_MainTex (“Base (RGB)”, 2D) = “white” {}
_NoiseTex (“NoiseTex ®”,2D) = “white”{}
_DissolveSpeed (“DissolveSpeed (Second)”,Float) = 1
_EdgeWidth(“EdgeWidth”,Range(0,0.5)) = 0.1
_EdgeColor(“EdgeColor”,Color) = (1,1,1,1)
_StartTime(“StartTime”,Float) = 0
}
SubShader
{
Tags { “RenderType”=“Opaque” }

    Pass
    {
        CGPROGRAM
        #pragma vertex vert_img
        #pragma fragment frag
        #include "UnityCG.cginc"
         
        uniform sampler2D _MainTex;
        uniform sampler2D _NoiseTex;
        uniform float _DissolveSpeed;
        uniform float _EdgeWidth;
        uniform float4 _EdgeColor;
        uniform float _StartTime;
        
        float4 frag(v2f_img i):COLOR
        {
            float DissolveFactor = saturate((_Time.y - _StartTime) / _DissolveSpeed);
            float noiseValue = tex2D(_NoiseTex,i.uv).r;            
            if(noiseValue <= DissolveFactor)
            {
                discard;
            }
            
            float4 texColor = tex2D(_MainTex,i.uv);
            float EdgeFactor = saturate((noiseValue - DissolveFactor)/(_EdgeWidth*DissolveFactor));
            float4 BlendColor = texColor * _EdgeColor;
                            
            return lerp(texColor,BlendColor,1 - EdgeFactor);
        }
        
        ENDCG
    }
} 

FallBack Off

}

重现效果代码例如以下:
Shader “lyh/Show”
{
Properties
{
_MainTex (“Base (RGB)”, 2D) = “white” {}
_NoiseTex (“NoiseTex ®”,2D) = “white”{}
_DissolveSpeed (“DissolveSpeed (Second)”,Float) = 1
_EdgeWidth(“EdgeWidth”,Range(0,0.5)) = 0.1
_EdgeColor(“EdgeColor”,Color) = (1,1,1,1)
_StartTime(“StartTime”,Float) = 0
}
SubShader
{
Tags { “RenderType”=“Opaque” }

    Pass
    {
        CGPROGRAM
        #pragma vertex vert_img
        #pragma fragment frag
        #include "UnityCG.cginc"
         
        uniform sampler2D _MainTex;
        uniform sampler2D _NoiseTex;
        uniform float _DissolveSpeed;
        uniform float _EdgeWidth;
        uniform float4 _EdgeColor;
        uniform float _StartTime;

        float4 frag(v2f_img i):COLOR
        {
            float DissolveFactor = saturate((_Time.y - _StartTime) / _DissolveSpeed);
            float noiseValue = tex2D(_NoiseTex,i.uv).r;            
            
            float4 texColor = tex2D(_MainTex,i.uv);
            float EdgeFactor = saturate((noiseValue - DissolveFactor)/(_EdgeWidth*DissolveFactor));
            float4 BlendColor = texColor * _EdgeColor;
            
            clip(1 - EdgeFactor - 0.01);
            return lerp(BlendColor,texColor,1 - EdgeFactor);
        }
        
        ENDCG
    }
} 

FallBack Off

}

轮廓自发光
两个Pass,一个渲染原本的贴图,一个渲染颜色,将顶点的xy方向扩大。

Shader “Custom/OutLine2.0”
{
Properties
{
_MainTex (“Texture”, 2D) = “white” {}
_TestColor(“轮廓自发光颜色”,Color)=(1,1,1,1)
_Width(“轮廓宽度”,Float)=1.1
}
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always//后期屏蔽的

	Pass
	{

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

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

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

		float _Width;/*****************************/
		v2f vert (appdata v)
		{
			v2f o;
			
			v.vertex.xy *= _Width;/*********************/
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}
		
		sampler2D _MainTex;

		Vector _TestColor;/************************/
		fixed4 frag (v2f i) : SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv);
			// just invert the colors
			//col.rgb = 1 - col.rgb;
			return fixed4(_TestColor);/*********************/
			//return col;
		}
		ENDCG
	}

		Pass
		{
			ZTest Always
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

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

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

			v2f vert(appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}

			sampler2D _MainTex;

			fixed4 frag(v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
			// just invert the colors
			//col.rgb = 1 - col.rgb;//后期屏蔽的
			
			return col;
			}
			ENDCG
		}
}

}
2.2 Alpha测试
Shader “Hidden/AlphaTestShader2.0”
{
Properties
{
_MainTex (“Texture”, 2D) = “white” {}
_AlphaValue(“Alpha值”,float)=0
}
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always
Blend SrcAlpha OneMinusSrcAlpha//打开Alpha通道
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

		#include "UnityCG.cginc"

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

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

		v2f vert (appdata v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}
		
		sampler2D _MainTex;

		float _AlphaValue;
		fixed4 frag (v2f i) : SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv);

		if (col.a < _AlphaValue)
		{
			return fixed4(0, 0, 0, 0);
		}
		else
		{
			return col;
		}
		}
		ENDCG
	}
}

}

3.Shader2.0 的UV动画
3.1 UV滚动(流动)
Shader “Custom/River”
{
Properties
{
_MainTex (“Texture”, 2D) = “white” {}
_SpeedY(“Y向流动速度”,Range(0,10))=1.0
_SpeedX(“X向流动速度”,Range(0,20)) = 0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

		#include "UnityCG.cginc"

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

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

		v2f vert (appdata v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}
		
		sampler2D _MainTex;


		float _SpeedY;/*增***********************/
		float _SpeedX;/*增***********************/
		fixed4 frag (v2f i) : SV_Target
		{
			float2 tempUV = i.uv;/*增***********************/
			tempUV.x += _Time.x*_SpeedX;/*增***********************/
			tempUV.y += _Time.y*_SpeedY;/*增***********************/

			fixed4 col = tex2D(_MainTex, tempUV);/*改***********************/
			
			return col;
		}
		ENDCG
	}
}

}
3.2 UV波动(wave)
Shader “Custom/Wave”
{
Properties
{
_MainTex (“Texture”, 2D) = “white” {}
_Arange(“波动幅度”,float) = 1
_Frenquncy(“波动频率”,float)=0.5
_Speed(“波动速度”, float) = 0.5
}
SubShader
{

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

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

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

		float _Arange;/*增******************************/
		float _Frenquncy;/*增******************************/
		float _Speed;/*增******************************/
		v2f vert(appdata v)
		{

			v2f o;

			float timer = _Time.y*_Speed;/*增******************************/
			float waver = _Arange * sin(timer + v.vertex.x*_Frenquncy);/*增******************************/

			v.vertex.y = v.vertex.y + waver;/*增******************************/

			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}
		
		sampler2D _MainTex;

		

		fixed4 frag (v2f i) : SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv);
			// just invert the colors
			//col.rgb = 1 - col.rgb;
			return col;
		}
		ENDCG
	}
}

}
3.3 UV旋转(Loading)
Shader “Custom/Loading_Icon”
{
Properties
{
_MainTex (“Texture”, 2D) = “white” {}
_RotateSpeed(“旋转速度”,float) = 15.0
}
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always

	Blend SrcAlpha OneMinusSrcAlpha//打开Alpha通道
	Pass
	{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		
		#include "UnityCG.cginc"

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

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

		v2f vert (appdata v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}
		
		sampler2D _MainTex;/**增******************************/
		float _RotateSpeed;/**增******************************/
		fixed4 frag (v2f i) : SV_Target
		{
			/**增******************************/
			float2 tempUV = i.uv;

			tempUV -= float2(0.5, 0.5);//1.平移到原点
									  
			if (length(tempUV) > 0.5)//防止对角线多余的长度显示
			{
				return fixed4(0, 0, 0, 0);
			}

			/*2.绕z轴旋转******************************/
			float2 finalUV = 0;
			float angle = _Time.x*_RotateSpeed;

			finalUV.x = tempUV.x*cos(angle) - tempUV.y*sin(angle);
			finalUV.y = tempUV.x*sin(angle) + tempUV.y*cos(angle);

			finalUV += float2(0.5, 0.5);//3.将贴图平移回原位置
			/**增******************************/
			fixed4 col = tex2D(_MainTex, finalUV);/**改******************************/
			
			return col;
		}
		ENDCG
	}
}

}
3.4 屏幕高斯效果
3.4.1 Shader
Shader “Custom/MyShader”
{
Properties
{
_MainTex (“贴图”, 2D) = “white” {}
_Amnient(“高斯模糊程度”,Float) = 0.001
}
SubShader
{
// No culling or depth
//Cull Off ZWrite Off ZTest Always

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

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

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

		v2f vert (appdata v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}
		
		sampler2D _MainTex;

		float _Amnient;
		fixed4 frag (v2f i) : SV_Target
		{
			float2 tempUV = i.uv;/*增**********************/
			float ambient = _Amnient;/*增**********************/
			fixed4 col = tex2D(_MainTex, tempUV);/*改**********************/
			// just invert the colors
			//col.rgb = 1 - col.rgb;
			fixed4 col2 = tex2D(_MainTex, tempUV + float2(-ambient, 0));/*增**********************/
			fixed4 col3 = tex2D(_MainTex, tempUV + float2(0,-ambient));/*增**********************/
			fixed4 col4 = tex2D(_MainTex, tempUV + float2(ambient, 0));/*增**********************/
			fixed4 col5 = tex2D(_MainTex, tempUV + float2(0,ambient));/*增**********************/

			col = (col + col2 + col3 + col4 + col5) / 5.0;/*增**********************/
			return col;
		}
		ENDCG
	}
}

}
3.4.2 C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///
/// 需要挂载到相机上
///
public class CameraMove : MonoBehaviour {

// Use this for initialization
//public Shader myShader;
public Material material;
void Start () {
    //material = new Material(myShader);
}

// Update is called once per frame
void Update () {
	
}
/// <summary>
/// 
/// </summary>
/// <param name="source">拦截到相机所渲染的图片</param>
/// <param name="destination">更改后返回的图片,重新交给引擎</param>
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
    Graphics.Blit(source, destination, material);
}

}
4. Surface5.0
4.1 UV 滚动
Shader “Custom/TestParameter” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}

}
SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Lambert vertex:MyVertex

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;

	struct Input {
		float2 uv_MainTex;
		float3 myColor;
	};

	
	fixed4 _Color;

	void MyVertex(inout appdata_base v,out Input o)
	{
		UNITY_INITIALIZE_OUTPUT(Input, o);
		o.myColor = _Color.rgb*abs(v.normal);
	}

	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
		o.Albedo = c.rgb*IN.myColor;
		
		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}
4.2 高光
Shader “Custom/CustomLighting” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_SpeclPower(“高光强度”,float) = 1
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Simple

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;

	struct Input {
		float2 uv_MainTex;
	};


	fixed4 _Color;

	
	UNITY_INSTANCING_BUFFER_START(Props)
		// put more per-instance properties here
	UNITY_INSTANCING_BUFFER_END(Props)

	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
		o.Albedo = c.rgb;
		
		o.Alpha = c.a;
	}

	float _SpeclPower;
	///自定义的灯光入口函数
	half4 LightingSimple(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
	{
		float NDotL = dot(lightDir, s.Normal);

		half4 result = 0;
		//result.rgb = s.Albedo* _LightColor0*NDotL*atten;//漫反射


		half3 H = viewDir - lightDir;
		float HDotN = dot(H, s.Normal);

		result.rgb = s.Albedo* _LightColor0*NDotL*atten+HDotN* s.Albedo* _LightColor0*_SpeclPower;
		result.a = s.Alpha;

		return result;
	}

	ENDCG
}
FallBack "Diffuse"

}
4.3 法线贴图
可以使对比度增强,使凸的地方看起来更凸。

Shader “Custom/NormalMap” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_NormalMap(“法线贴图”, 2D) = “white” {}
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Lambert

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;
	sampler2D _NormalMap;
	struct Input {
		float2 uv_MainTex;
		float2 uv_NormalMap;
	};


	fixed4 _Color;

	
	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
		
		o.Normal= UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
		o.Albedo = c.rgb;
		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}
4.4 边缘自发光
Shader “Custom/NormalOutLine” {
Properties {
_Color (“自发光颜色”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_NormalTex(“法线贴图”, 2D) = “white” {}
_EmisionPower(“自发光强度”,float) = 1
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Lambert

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;
	sampler2D _NormalTex;
	struct Input {
		float2 uv_MainTex;
		float2 uv_NormalTex;

		float3 viewDir;
	};

	
	fixed4 _Color;
	float _EmisionPower;
	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
		o.Albedo = c.rgb;

		//读取法线
		float3 tempNormal= UnpackNormal(tex2D(_NormalTex, IN.uv_NormalTex));
		
		o.Normal = tempNormal;
		//算出法线和视角的点乘
		float tempFloat=1- clamp(dot(IN.viewDir, tempNormal),0, 1);//clamp()让取值变到一个范围
		
		o.Emission = _Color *pow(tempFloat,_EmisionPower);
		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}
4.5 Fog
当需要在某个物体周围呈现雾效,而不是整个场景中呈现雾效,就需要写Shader。

Shader “Custom/TestFog” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_FogStart(“雾的起点”,float) = 1
_FogEnd(“雾的终点”,float) = 1
_FogColor(“雾的颜色”,Color)=(1,1,1,1)
}
SubShader{
Tags { “RenderType” = “Opaque” }
LOD 200

		CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf   Lambert    vertex:MyVertex    finalcolor:FinalColor

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;

	struct Input {
		float2 uv_MainTex;

		float fogData;
	};

	
	fixed4 _Color;
	float _FogStart;
	float _FogEnd;
	fixed4 _FogColor;
	void FinalColor(Input IN,SurfaceOutput o,inout fixed4 color)
	{
		//color *= float4(1, 0, 0, 1);

		color = lerp(_FogColor, color, IN.fogData);
	}

	
	void MyVertex(inout appdata_full v, out Input data)
	{
		UNITY_INITIALIZE_OUTPUT(Input, data);//初始化
		
		float tempZ = _FogEnd - length(UnityObjectToViewPos(v.vertex).xyz);

		data.fogData = tempZ/(_FogEnd- _FogStart);
	}
	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
		o.Albedo = c.rgb;
		
		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}
4.6 菲尼尔反射
Shader “Custom/TestFresnel” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_CubeMap(“CubeMap”,Cube) = “”{}
_Rate(“折射率”,float) = 1

	_FresnelBias("菲尼尔偏移",float) = 1
		_FresnelScale("菲尼尔缩放系数",float) = 1
		_FresnelPower("菲尼尔指数",float) = 0.5
}
SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Lambert vertex:MyVertex

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;
	samplerCUBE _CubeMap;
	struct Input {
		float2 uv_MainTex;
		float3 worldRefl;//反射

		float refract;//折射
		float reflectFact;//定义的反射系数

	};

	
	fixed4 _Color;
	float _Rate;
	float _FresnelBias;
	float  _FresnelScale;
	float	_FresnelPower;
	void MyVertex(inout appdata_full v, out Input data)
	{
		UNITY_INITIALIZE_OUTPUT(Input, data);//初始化

		//右世界变成物体的
		float3 localNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
		//入射角
		float3 viewDir = -WorldSpaceViewDir(v.vertex);

		data.refract = refract(viewDir, localNormal, _Rate);

		data.reflectFact = _FresnelBias + _FresnelScale * pow(1 + dot(viewDir, localNormal), _FresnelPower);
	}
	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		//fixed4 cflect = tex2D (_MainTex, IN.worldRefl);//反射
		//fixed4 cfract = tex2D(_MainTex, IN.refract);

		fixed4 cflect = texCUBE(_CubeMap, IN.worldRefl);//反射
		fixed4 cfract = texCUBE(_CubeMap, IN.refract);


		o.Albedo = IN.reflectFact*cflect.rgb + (1 - IN.reflectFact)*cfract.rgb;
		
		
		o.Alpha = cflect.a;
	}
	ENDCG
}
FallBack "Diffuse"

}
4.7 BRDF
双向反射分布函数(Bidirectional Reflectance Distribution Function,BRDF)用来定义给定入射方向上的辐射照度(irradiance)如何影响给定出射方向上的辐射率(radiance)。更笼统地说,它描述了入射光线经过某个表面反射后如何在各个出射方向上分布–这可以是从理想镜面反射到漫反射、各向同性(isotropic)或者各向异性(anisotropic)的各种反射。

Shader “Custom/TestBRDF” {
Properties {
_SpeColor (“高光颜色”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_SpecPower(“高光强度”,float)=1
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf  BRDFLigthing

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;

	struct Input {
		float2 uv_MainTex;
	};

	
	fixed4 _SpeColor;
	float _SpecPower;

	#define PI 3.1415926
	half4 LightingBRDFLigthing(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
	{
		float3 H = normalize(lightDir + viewDir);
		float3 N = normalize(s.Normal);
		
		float d = (_SpecPower + 2)*pow(dot(N, H),_SpecPower) / 8.0;

		float f = _SpeColor + (1 - _SpeColor)*pow(1 - dot(H, N), 5);

		float k = 2 / (sqrt(PI*(_SpecPower + 2)));

		float v = 1 / (dot(N, lightDir)*(1 - k) + k)*(dot(N, viewDir)*(1 - k) + k);


		float all = d * f*v;
		float diff = dot(lightDir, N);

		float tempResult = all + (1 - all)*diff;

		half4 finalColor = 0;
		finalColor.rgb = tempResult * s.Albedo*_LightColor0.rgb;

		finalColor.a = s.Alpha;

		return finalColor;
	}


	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
		o.Albedo = c.rgb;
		
		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}

  1. CubeMap
    5.1 真反射
    利用CubeMap做真反射;新建一个相机,对准需要反射的内容,新建一个Render Texture,并将此Render Texture赋予反射摄像机的Target Texture;新建一个材质球(标准材质球即可),在贴图通道赋予新建的Render Texture,最后将材质赋予需要有反射性的物体,该物体就会实时反射后一个新建摄像机的内容。但是真反射非常消耗性能。

5.2 假反射
也需要建一个需要反射出的内容的CubeMap,一般需要反射的为天空盒。

Shader “Custom/FalseRelf” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_CubeMap(“CubeMap”,Cube) = “”{}
_CubePower(“CubePower”,float) = 1
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Lambert

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;
	
samplerCUBE _CubeMap;
	struct Input {
		float2 uv_MainTex;

		float3 worldRefl;//反射角
	};

	
	fixed4 _Color;
	float _CubePower;
	
	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;

		o.Emission= texCUBE(_CubeMap, IN.worldRefl)*_CubePower;
		o.Albedo = c.rgb;
		
		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}
5.3 折射
也需要建一个需要折射看到的内容的CubeMap。

Shader “Custom/Refract” {
Properties {
_Color (“Color”, Color) = (1,1,1,1)
_MainTex (“Albedo (RGB)”, 2D) = “white” {}
_Rate(“折射率”,float) = 1
_CubeMap(“CubeMap”,Cube) = “”{}
}
SubShader {
Tags { “RenderType”=“Opaque” }
LOD 200

	CGPROGRAM
	// Physically based Standard lighting model, and enable shadows on all light types
	#pragma surface surf Lambert vertex:MyVertex

	// Use shader model 3.0 target, to get nicer looking lighting
	#pragma target 3.0

	sampler2D _MainTex;
	samplerCUBE _CubeMap;
	struct Input {
		float2 uv_MainTex;

		float3 refr;
	};

	float _Rate;
	void MyVertex(inout appdata_full v,out Input data)
	{
		UNITY_INITIALIZE_OUTPUT(Input, data);//初始化

		//右世界变成物体的
		float3 localNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));

		//入射角
		float3 viewDir=- WorldSpaceViewDir(v.vertex);

		data.refr= refract(viewDir, localNormal, _Rate);
	}
	fixed4 _Color;
	
	
	void surf (Input IN, inout SurfaceOutput o) {
		// Albedo comes from a texture tinted by color
		fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;

		o.Emission= texCUBE(_CubeMap, IN.refr).rgb;
		o.Albedo = c.rgb;

		o.Alpha = c.a;
	}
	ENDCG
}
FallBack "Diffuse"

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值