【UnityShader自学日志】星球大气效果

1、创建一个“Planet”着色器

2、在Properties代码块中删除_Metallic和_Glossiness所在的两行,在surf函数中删除Glossiness和Metallic所在的两行(因为大气着色器不需要金属光泽和平滑度两个材质属性)

3、为大气着色器添加厚度、颜色和不透明度三个属性

     在Properties代码块中添加下面两行

        _Thickness  ("Thickness", Range(-1, 1)) = 0.5 //指定星球大气层的厚度
        _AtomsColor ("Atomsphere Color", Color) = (1, 1, 1, 1) //指定大气层的颜色

4、添加第二遍渲染代码

Cull Front
		CGPROGRAM
		#pragma surface surf Standard fullforwardshadows alpha vertex:vert
		struct Input 
		{
			float2 uv_MainTex;
		};
        
        //添加大气层厚度
		float _Thickness;
		void vert(inout appdata_full v) {
			v.vertex.xyz += v.normal * _Thickness;
		}

		fixed4 _AtomsColor;
        //着色器大气颜色_AtomsColor的值作为反光值输出
		void surf(Input IN, inout SurfaceOutputStandard o) {
			o.Albedo = _AtomsColor.rgb;
			o.Alpha = _AtomsColor.a;
		}
		ENDCG

 

完整代码如下

Shader "PACKT/Planet_falloff" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Thickness  ("Thickness", Range(-1, 1)) = 0.5 //指定星球大气层的厚度
		_AtomsColor ("Atomsphere Color", 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 Standard fullforwardshadows

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

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard 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

		Cull Front
		CGPROGRAM
		#pragma surface surf Standard fullforwardshadows alpha vertex:vert
		struct Input 
		{
			float2 uv_MainTex;
		};

		float _Thickness;
		void vert(inout appdata_full v) {
			v.vertex.xyz += v.normal * _Thickness;
		}

		fixed4 _AtomsColor;
		void surf(Input IN, inout SurfaceOutputStandard o) {
			o.Albedo = _AtomsColor.rgb;
			o.Alpha = _AtomsColor.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

 

你可能感兴趣的:(UnityShader)