Shader-高光反射(顶点实现);控制高光范围;控制高光颜色

管玉堂
2023-12-01
  • Blinn光照模型     
  • cosθ= dot(reflectDir, viewDir)
  • 直射光 Specular = 直射光 * pow( max(cosθ,0),10)  θ:是反射光方向和视野方向的夹角    10可以控制高光的范围参数越小范围越大
    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    
    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    
    Shader "LXC/07 Specular Vertex"
    {
    	Properties{
    		_Diffuse("Diffuse Color",Color) = (1,1,1,1)
    	}
    		SubShader
    	{
    		Pass{
    		Tags{ "LightMode" = "ForwardBase" }
    		CGPROGRAM
    #include "Lighting.cginc"
    #pragma vertex vert		
    #pragma fragment frag
    		fixed4 _Diffuse;
    
    	struct a2v {
    		float4 vertex:POSITION;
    		float3 normal:NORMAL;
    	};
    
    	struct v2f {
    		float4 position:SV_POSITION;
    		fixed3 color : COLOR0;
    	};
    
    	v2f vert(a2v v) {
    		v2f f;
    		f.position = UnityObjectToClipPos(v.vertex);
    		fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
    		fixed3 normalDir = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
    		fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    		fixed3 diffuse = _LightColor0.rgb * max(dot(normalDir, lightDir), 0)*_Diffuse.rgb;
    		//高光反射
    		//反射光的方向
    		fixed3 reflectDir = normalize(reflect(-lightDir, normalDir));			//reflect计算反射光
    		//视野方向
    		fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(v.vertex, unity_WorldToObject).xyz);	//_WorldSpaceCameraPos相机位置,vertex当前点的位置
    		fixed3 specular = _LightColor0.rgb * pow(max(dot(reflectDir, viewDir), 0), 5);
    		f.color = diffuse + ambient + specular;
    		return f;
    	}
    
    
    	fixed4 frag(v2f f) :SV_Target{		
    		return fixed4(f.color,1);
    	}
    		ENDCG
    	}
    	}
    		Fallback "Diffuse"
    }

     

  • 扩展控制高光范围

    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    
    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    
    Shader "LXC/07 Specular Vertex"
    {
    	Properties{
    		_Diffuse("Diffuse Color",Color) = (1,1,1,1)
    		_Gloss("Gloss",Range(8,200)) = 10
    	}
    		SubShader
    	{
    		Pass{
    		Tags{ "LightMode" = "ForwardBase" }
    		CGPROGRAM
    #include "Lighting.cginc"
    #pragma vertex vert		
    #pragma fragment frag
    		fixed4 _Diffuse;
    		half _Gloss;
    
    	struct a2v {
    		float4 vertex:POSITION;
    		float3 normal:NORMAL;
    	};
    
    	struct v2f {
    		float4 position:SV_POSITION;
    		fixed3 color : COLOR0;
    	};
    
    	v2f vert(a2v v) {
    		v2f f;
    		f.position = UnityObjectToClipPos(v.vertex);
    		fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
    		fixed3 normalDir = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
    		fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    		fixed3 diffuse = _LightColor0.rgb * max(dot(normalDir, lightDir), 0)*_Diffuse.rgb;
    		//高光反射
    		//反射光的方向
    		fixed3 reflectDir = normalize(reflect(-lightDir, normalDir));			//reflect计算反射光
    		//视野方向
    		fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(v.vertex, unity_WorldToObject).xyz);	//_WorldSpaceCameraPos相机位置,vertex当前点的位置
    		fixed3 specular = _LightColor0.rgb * pow(max(dot(reflectDir, viewDir), 0), _Gloss);
    		f.color = diffuse + ambient + specular;
    		return f;
    	}
    
    
    	fixed4 frag(v2f f) :SV_Target{		
    		return fixed4(f.color,1);
    	}
    		ENDCG
    	}
    	}
    		Fallback "Diffuse"
    }

     

  • 控制高光颜色

    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    
    // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    
    Shader "LXC/07 Specular Vertex"
    {
    	Properties{
    		_Diffuse("Diffuse Color",Color) = (1,1,1,1)
    		_Gloss("Gloss",Range(8,200)) = 10
    		//控制高光颜色
    		_Specular("Specular Color",Color)=(1,1,1,1)
    	}
    		SubShader
    	{
    		Pass{
    		Tags{ "LightMode" = "ForwardBase" }
    		CGPROGRAM
    #include "Lighting.cginc"
    #pragma vertex vert		
    #pragma fragment frag
    		fixed4 _Diffuse;
    		half _Gloss;
    		fixed4 _Specular;
    
    	struct a2v {
    		float4 vertex:POSITION;
    		float3 normal:NORMAL;
    	};
    
    	struct v2f {
    		float4 position:SV_POSITION;
    		fixed3 color : COLOR0;
    	};
    
    	v2f vert(a2v v) {
    		v2f f;
    		f.position = UnityObjectToClipPos(v.vertex);
    		fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
    		fixed3 normalDir = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
    		fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
    		fixed3 diffuse = _LightColor0.rgb * max(dot(normalDir, lightDir), 0)*_Diffuse.rgb;
    		//高光反射
    		//反射光的方向
    		fixed3 reflectDir = normalize(reflect(-lightDir, normalDir));			//reflect计算反射光
    		//视野方向
    		fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(v.vertex, unity_WorldToObject).xyz);	//_WorldSpaceCameraPos相机位置,vertex当前点的位置
    		fixed3 specular = _LightColor0.rgb * pow(max(dot(reflectDir, viewDir), 0), _Gloss) * _Specular.rgb;
    		f.color = diffuse + ambient + specular;
    		return f;
    	}
    
    
    	fixed4 frag(v2f f) :SV_Target{		
    		return fixed4(f.color,1);
    	}
    		ENDCG
    	}
    	}
    		Fallback "Diffuse"
    }

     

 类似资料: