光照

1、根据surface shader简化后的vs,ps版shader ----------------------- Bumped Specular

Shader "CM/Bumped Specular" {
Properties 
{
	_Color ("Main Color", Color) = (1,1,1,1)
	_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
	_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
	_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
	_BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader { 
	Tags { "RenderType"="Opaque" }
	LOD 400
	Pass {
		Name "FORWARD"
		Tags { "LightMode" = "ForwardBase" }

		CGPROGRAM
		// compile directives
		#pragma vertex vert_surf
		#pragma fragment frag_surf
		#pragma multi_compile_fwdbase
		#include "HLSLSupport.cginc"
		#include "UnityShaderVariables.cginc"
		#define UNITY_PASS_FORWARDBASE
		#include "UnityCG.cginc"
		#include "Lighting.cginc"
		#include "AutoLight.cginc"



		sampler2D _MainTex;
		sampler2D _BumpMap;
		float4 _MainTex_ST;
		float4 _BumpMap_ST;
		fixed4 _Color;
		half _Shininess;



		struct v2f_surf 
		{
		  float4 pos : SV_POSITION;
		  float4 pack0 : TEXCOORD0;
		  fixed3 lightDir : TEXCOORD1;
		  //fixed3 vlight : TEXCOORD2;
		  float3 viewDir : TEXCOORD3;
		  //LIGHTING_COORDS(4,5)
		};

		v2f_surf vert_surf (appdata_full v) 
		{
			v2f_surf o;
			o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
			o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
			o.pack0.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
			float3 worldN = mul((float3x3)_Object2World, SCALED_NORMAL);
			TANGENT_SPACE_ROTATION;
			o.lightDir = mul (rotation, ObjSpaceLightDir(v.vertex));
			o.viewDir = mul (rotation, ObjSpaceViewDir(v.vertex));
			//o.vlight = ShadeSH9 (float4(worldN,1.0));
			//TRANSFER_VERTEX_TO_FRAGMENT(o);
			return o;
		}

		fixed4 frag_surf (v2f_surf IN) : SV_Target 
		{
			fixed4 tex = tex2D(_MainTex, IN.pack0.xy);
			fixed3 Albedo = tex.rgb * _Color.rgb;
			fixed Gloss = tex.a;
			fixed Alpha = tex.a * _Color.a;
			fixed Specular = _Shininess;
			fixed3 Normal = UnpackNormal(tex2D(_BumpMap, IN.pack0.zw));

			fixed4 c = 0;
			half3 h = normalize (IN.lightDir + normalize(half3(IN.viewDir)));
			fixed diff = max (0, dot (Normal, IN.lightDir));
			float nh = max (0, dot (Normal, h));
			float spec = pow (nh, Specular*128.0) * Gloss;
			fixed atten = 1;//LIGHT_ATTENUATION(IN);
			c.rgb = (Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
			c.a = Alpha + _LightColor0.a * _SpecColor.a * spec * atten;

			c.rgb += Albedo;// * IN.vlight;

			return c;
		}

		ENDCG
	}
}
}

  

 2、根据surface shader简化后的vs,ps版shader ----------------------- Bumped Diffuse

Shader "CM/Bumped Diffuse" {
Properties 
{
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _BumpMap ("Normalmap", 2D) = "bump" {}
}

SubShader 
{
    Tags { "RenderType"="Opaque" }
    LOD 300
    Pass {
        Name "FORWARD"
        Tags { "LightMode" = "ForwardBase" }

        CGPROGRAM
        // compile directives
        #pragma vertex vert_surf
        #pragma fragment frag_surf
        #pragma multi_compile_fwdbase
        #include "HLSLSupport.cginc"
        #include "UnityShaderVariables.cginc"
        #define UNITY_PASS_FORWARDBASE
        #include "UnityCG.cginc"
        #include "Lighting.cginc"
        #include "AutoLight.cginc"

        sampler2D _MainTex;
        sampler2D _BumpMap;
        float4 _MainTex_ST;
        float4 _BumpMap_ST;
        fixed4 _Color;

        struct v2f_surf 
        {
          float4 pos : SV_POSITION;
          float4 pack0 : TEXCOORD0;
          fixed3 lightDir : TEXCOORD1;
          //fixed3 vlight : TEXCOORD2;
          //LIGHTING_COORDS(3,4)
        };


        // vertex shader
        v2f_surf vert_surf (appdata_full v) 
        {
            v2f_surf o;
            o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
            o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
            o.pack0.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);

            float3 worldN = mul((float3x3)_Object2World, SCALED_NORMAL);
            TANGENT_SPACE_ROTATION;
            o.lightDir = mul (rotation, ObjSpaceLightDir(v.vertex));
            //o.vlight = ShadeSH9 (float4(worldN,1.0));
            //TRANSFER_VERTEX_TO_FRAGMENT(o);
            return o;
        }

        // fragment shader
        fixed4 frag_surf (v2f_surf IN) : SV_Target 
        {
            fixed4 tex = tex2D(_MainTex, IN.pack0.xy) * _Color;
            fixed3 Albedo = tex.rgb;
            fixed Alpha = tex.a;
            fixed3 Normal = UnpackNormal(tex2D(_BumpMap, IN.pack0.zw));

            fixed atten = 1;//LIGHT_ATTENUATION(IN);
            fixed4 c = 0;
            fixed diff = max (0, dot (Normal, IN.lightDir));
            c.rgb = Albedo * _LightColor0.rgb * (diff * atten * 2);
            c.a = Alpha;
            c.rgb += Albedo;// * IN.vlight;

            return c;
        }

        ENDCG

    }
}
}

3、根据surface shader简化后的vs,ps版shader ----------------------- Diffuse

Shader "CM/Diffuse" {
Properties 
{
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200
    Pass 
    {
        Name "FORWARD"
        Tags { "LightMode" = "ForwardBase" }

        CGPROGRAM
        // compile directives
        #pragma vertex vert_surf
        #pragma fragment frag_surf
        #pragma multi_compile_fwdbase
        #include "HLSLSupport.cginc"
        #include "UnityShaderVariables.cginc"
        #define UNITY_PASS_FORWARDBASE
        #include "UnityCG.cginc"
        #include "Lighting.cginc"
        #include "AutoLight.cginc"

        sampler2D _MainTex;
        float4 _MainTex_ST;
        fixed4 _Color;

        struct v2f_surf 
        {
          float4 pos : SV_POSITION;
          float2 pack0 : TEXCOORD0;
          fixed3 normal : TEXCOORD1;
          //fixed3 vlight : TEXCOORD2;
          //LIGHTING_COORDS(3,4)
        };


        // vertex shader
        v2f_surf vert_surf (appdata_full v) 
        {
            v2f_surf o;
            o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
            o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);

            o.normal = mul((float3x3)_Object2World, SCALED_NORMAL);
            //o.vlight = ShadeSH9 (float4(worldN,1.0));
            //TRANSFER_VERTEX_TO_FRAGMENT(o);
            return o;
        }

        // fragment shader
        fixed4 frag_surf (v2f_surf IN) : SV_Target 
        {
            fixed4 tex = tex2D(_MainTex, IN.pack0.xyx) * _Color;
            fixed3 Albedo = tex.rgb;
            fixed Alpha = tex.a;
            fixed3 Normal = IN.normal;
            
            fixed4 c = 0;
            fixed atten = 1;//LIGHT_ATTENUATION(IN);
            fixed diff = max (0, dot (Normal, _WorldSpaceLightPos0.xyz));
            c.rgb = Albedo * _LightColor0.rgb * (diff * atten * 2);
            c.a = Alpha;
            c.rgb += Albedo;// * IN.vlight;

            return c;
        }

        ENDCG

    }
}

Fallback "VertexLit"
}

 4 、最简单的cubmap

Shader "CM/Cubmap"
{
	Properties
	{
		_Cubemap("CubeMap", CUBE) = ""{}
	}


		SubShader
	{

		Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
		Pass
		{
			Tags{ "LightMode" = "ForwardBase" }
			Fog{ Mode off }
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"
			samplerCUBE _Cubemap;

			struct appdata
			{
				float4 vertex : POSITION;
				float4 normal : NORMAL;
			};

			struct v2f
			{
				float4	pos : SV_POSITION;
				float3 worldNormal : TEXCOORD1;
				float3 worldPos : TEXCOORD2;
			};

			v2f vert(appdata v)
			{
				v2f o;
				o.worldPos = mul(_Object2World, v.vertex);
				o.worldNormal = mul((float3x3)_Object2World, v.normal);
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				return o;
			}

			float4 frag(v2f i) : COLOR
			{
				float3 worldNormal = normalize(i.worldNormal);
				float3 worldViewDir = normalize(i.worldPos - _WorldSpaceCameraPos.xyz);
				float3 worldReflect = reflect(worldViewDir, worldNormal);
				return float4(texCUBE(_Cubemap, worldReflect).rgb, 1);
			}

			ENDCG
		}
	}
}

物理光照:

Shader "GeneratedFromSurface-NewSurfaceShader" 
{
	Properties 
	{
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		Pass 
		{
			Name "FORWARD"
			Tags { "LightMode" = "ForwardBase" }

			CGPROGRAM
			// compile directives
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			#pragma multi_compile_fwdbase
			#include "UnityPBSLighting.cginc"

			sampler2D _MainTex;
			float4 _MainTex_ST;
			half _Glossiness;
			half _Metallic;
			fixed4 _Color;


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

			v2f vert(appdata_full v)
			{
				v2f o;

				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);

				float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
				fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
				fixed tangentSign = v.tangent.w * unity_WorldTransformParams.w;
				fixed3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign;
				o.worldPos = worldPos;
				o.worldNormal = worldNormal;

				return o;
			}

			fixed4 frag(v2f IN) : SV_Target
			{
				float3 worldPos = IN.worldPos;
				fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
				fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));

				SurfaceOutputStandard o;
				fixed4 c = tex2D(_MainTex, IN.uv.xy) * _Color;
				o.Albedo = c.rgb;
				o.Emission = 0.0;
				o.Alpha = c.a;
				o.Occlusion = 1.0;
				o.Metallic = _Metallic;
				o.Smoothness = _Glossiness;
				o.Normal = IN.worldNormal;

				// Setup lighting environment
				UnityGI gi;
				UNITY_INITIALIZE_OUTPUT(UnityGI, gi);
				gi.indirect.diffuse = 0;
				gi.indirect.specular = 0;
				gi.light.color = _LightColor0.rgb;
				gi.light.dir = lightDir;
				gi.light.ndotl = LambertTerm (o.Normal, gi.light.dir);

				// Call GI (lightmaps/SH/reflections) lighting function
				UnityGIInput giInput;
				UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput);
				giInput.light = gi.light;
				giInput.worldPos = worldPos;
				giInput.worldViewDir = worldViewDir;
				giInput.atten = 1;
				giInput.lightmapUV = 0.0;
				giInput.ambient.rgb = 0.0;
				giInput.probeHDR[0] = unity_SpecCube0_HDR;
				giInput.probeHDR[1] = unity_SpecCube1_HDR;
				giInput.boxMin[0] = unity_SpecCube0_BoxMin;

				LightingStandard_GI(o, giInput, gi);
				fixed4 finalCol = LightingStandard(o, worldViewDir, gi);
				return finalCol;
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}

  

 

posted on 2016-09-26 17:57  sskyline  阅读(340)  评论(0编辑  收藏  举报