zoukankan      html  css  js  c++  java
  • 【兰伯特光照与Blinn_Phong光照】

    <1>兰伯特与半兰伯特

    Shader "Tang/614/Lambert"{
    
        Properties{
    		_Color("Tin Color",Color) = (1,1,1,1)
    
    	}
    
    	SubShader{
    		Pass{
    			Tags { "LightMode" = "ForwardBase" }
    			CGPROGRAM
    			#pragma vertex vert 
    			#pragma fragment frag 
    			#include "Lighting.cginc"
    			fixed4 _Color;
    
    			struct a2v{
    				float4 vertex:POSITION;
    				float3 normal:NORMAL; 
    			};
    			struct v2f{
    				float4 pos:SV_POSITION;
    				float3 worldNormal:TEXCOORD0;
    			};
    
    			v2f vert(a2v v){
    				v2f o;
    				o.pos = UnityObjectToClipPos(v.vertex);
    				o.worldNormal = UnityObjectToWorldNormal(v.normal);
    				return o;
    			}
    			//兰伯特光照
    			fixed4 frag(v2f i):SV_Target{
    				fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    				fixed3 worldNormal = normalize(i.worldNormal);
    				//兰伯特
    				//fixed3 diffuse = _LightColor0.rgb*_Color.rgb* saturate(dot(lightDir,worldNormal));
    				//半兰伯特
    				fixed halfLambert = dot(lightDir,worldNormal)*0.5+0.5;
    				fixed3 diffuse = _LightColor0.rgb*_Color.rgb* halfLambert;
    				return fixed4(diffuse,1);
    			}
    			ENDCG
    		}
    	}
    
    }
    

      <2>Blinn与Phong高光

    Shader "Tang/614/Blinn"{
        Properties{
    		_Color("漫反射",Color) = (1,1,1,1)
    		_Specular("高光",Color) = (1,1,1,1)
    		_Gloss("高光光滑度",Range(1,64)) = 2
    	}
    
    	SubShader{
    		Pass{
    			Tags {"LightMode" = "ForwardBase"}
    			CGPROGRAM
    			#pragma vertex vert 
    			#pragma fragment frag 
    			#include "Lighting.cginc"
    			fixed4 _Color;
    			fixed4 _Specular;
    			fixed _Gloss;
    
    			struct a2v{
    				float4 vertex:POSITION;
    				float3 normal:NORMAL;
    			};
    			struct v2f{
    				float4 pos:SV_POSITION;
    				float3 worldNormal:TEXCOORD0;
    				float3 worldPos:TEXCOORD1;
    			};
    
    			v2f vert(a2v v){
    				v2f o;
    				o.pos = UnityObjectToClipPos(v.vertex);
    				o.worldNormal = UnityObjectToWorldNormal(v.normal);
    				o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
    				return o;
    			}
    			//blinn高光
    			fixed4 frag(v2f i):SV_Target{
    				fixed3 worldNormal = normalize(i.worldNormal);
    				fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    
    				//环境光
    				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
    				//漫反射颜色
    				//fixed halfLambert = dot(worldNormal,lightDir)*0.5+0.5;
    				fixed lambert = saturate(dot(worldNormal,lightDir));
    				fixed3 diffuse = _LightColor0.rgb*_Color.rgb*lambert;
    				//高光 
    				//blinn高光 不再求反射 而是引入一个新的向量 h = 视角+入射光 再用h与法线点乘
    				//phong高光 用入射和法线求出反射n,再用n与视角点乘
    				//上面2者的效果差不多,有些时候前者更是我们想要的效果,而且前者计算开销少些
    				fixed3 viewDir = UnityWorldSpaceViewDir(i.worldPos);
    				fixed3 h = normalize(lightDir+viewDir);
    				fixed3 specular = _LightColor0.rgb*_Color.rgb* pow( saturate( dot(h,worldNormal) ),_Gloss);
    				return fixed4(ambient+diffuse+specular,1);				
    			}
    
    			ENDCG
    		}
    
    	}
    
    }
    

      <3>采样反照率贴图的Blinn

    Shader "Tang/614/BlinnTex"{
    	Properties{
    		_MainTex("反照贴图",2D) = "white" { }
    		_Color("Tin Color",Color) = (1,1,1,1)
    		_Specular("Specular",Color) = (1,1,1,1)
    		_Gloss("Specular Gloss",Range(1,64)) = 8
    	}
    	SubShader{
    		Pass{
    			Tags {"LightMode" = "ForwardBase"}
    			CGPROGRAM
    			#pragma vertex vert 
    			#pragma fragment frag 
    			#include "Lighting.cginc"
    			struct a2v{
    				float4 vertex:POSITION;
    				float3 normal:NORMAL;
    				float4 texcoord:TEXCOORD0;
    			};
    			struct v2f{
    				float4 pos:SV_POSITION;
    				float3 worldNormal:TEXCOORD0;
    				float3 worldPos:TEXCOORD1;
    				float2 uv:TEXCOORD2;
    			};
    			sampler2D _MainTex;float4 _MainTex_ST;
    			fixed4 _Color;
    			fixed4 _Specular;
    			fixed _Gloss;
    
    			v2f vert(a2v v){
                    v2f o;
    				o.pos = UnityObjectToClipPos(v.vertex);
    				o.worldNormal = UnityObjectToWorldNormal(v.normal);
    				o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
    				o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    				return o;
    			}
    
    			fixed4 frag(v2f i):SV_Target{
    				fixed3 worldNormal = normalize(i.worldNormal);
    				fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    				fixed3 viewDir = UnityWorldSpaceViewDir(i.worldPos);
    				//反照率
    				fixed3 albedo = tex2D(_MainTex,i.uv).rgb*_Color.rgb;
    				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
    				fixed lambert = saturate(dot(worldNormal,lightDir));
    				fixed3 diffuse = _LightColor0.rgb*albedo*lambert;
    				
    				fixed3 h = normalize(viewDir+lightDir);
    				fixed sp = pow( saturate(dot(h,worldNormal)) ,_Gloss );
    				fixed3 specular = _LightColor0.rgb*_Specular.rgb*sp;
    
    				return fixed4(ambient+diffuse+specular,1);
    
    
    			}
    
    			ENDCG
    		}
    
    	}
    
    }
    

      

  • 相关阅读:
    听说你技术很牛?对不起,屁用没有
    Java内存区域与内存溢出异常
    java mongodb连接配置实践——生产环境最优
    MngoDb MongoClientOptions 配置信息及常用配置信息
    java mongodb的MongoOptions生产级配置
    【深入 MongoDB 开发】使用正确的姿势连接分片集群
    MongoDB索引管理-索引的创建、查看、删除
    面试题:判断字符串是否回文
    面试题:Add Two Numbers(模拟单链表)
    面试题:获取一个整数的16进制表现形式
  • 原文地址:https://www.cnblogs.com/cocotang/p/11023350.html
Copyright © 2011-2022 走看看