Shader各种效果

1.Shader1.0的Alpha测试

Shader "Hidden/AlphaTestShader1.0"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_AlphaTest("Alpha值",float) = 0
	}
	SubShader
	{
		AlphaTest Greater [_AlphaTest]
		Pass
		{
			SetTexture[_MainTex]
			{
				combine texture
			}
		}
	}
}

2.Shader2.0

2.1 轮廓自发光

        两个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;

/// <summary>
/// 需要挂载到相机上
/// </summary>
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"
}

5. 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"
}

猜你喜欢

转载自blog.csdn.net/a962035/article/details/80331846
今日推荐