Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-Phong
2018-03-07 17:00
579 查看
表面着色器实现:Shader "Phong"
{
Properties
{
_MainTex("Base(RGB)",2D) = ""{}
_MainTint("Main Tint",Color) = (1,1,1,1)
_SpecularColor("Specular Color",Color) = (1,1,1,1)
_SpecPower("Specular Power",Range(0,30)) = 1
}
SubShader
{
Tags{"RenderType" = "Opaque"}
LOD 200
CGPROGRAM
#pragma surface surf Phong
sampler2D _MainTex;
fixed4 _MainTint;
fixed4 _SpecularColor;
fixed _SpecPower;
struct Input
{
half2 uv_MainTex;
};
void surf(Input IN,inout SurfaceOutput o)
{
fixed4 col = tex2D(_MainTex,IN.uv_MainTex);
o.Albedo = col.rgb;
o.Alpha = col.a;
}
inline fixed4 LightingPhong(SurfaceOutput s,fixed3 lightDir,fixed3 viewDir,fixed atten)
{
fixed3 normal = normalize(s.Normal);
fixed NdotL = dot(normal,lightDir);
fixed3 reflectionDir = normalize(2 * normal * NdotL - lightDir);
//fixed3 halfDir = normalize(lightDir + viewDir);
//fixed NdotH = max(0,dot(normal,halfDir));
fixed spec = pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
fixed3 diffuse = _LightColor0.rgb * s.Albedo * max(0,NdotL);
fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
//fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(NdotH,_SpecPower);
return fixed4(diffuse+specular,1.0);
}
ENDCG
}
FallBack "Diffuse"
}
顶点-片元实现:Shader "Phong"
{
Properties
{
_MainTex("Base(RGB)",2D) = ""{}
_MainTint("Main Tint",Color) = (1,1,1,1)
_SpecularColor("Specular Color",Color) = (1,1,1,1)
_SpecPower("Specular Power",Range(0,30)) = 1
}
SubShader
{
Pass
{
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
sampler2D _MainTex;
fixed4 _MainTex_ST;
fixed4 _MainTint;
fixed4 _SpecularColor;
fixed _SpecPower;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
half2 uv:TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.worldNormal = mul(unity_ObjectToWorld,v.normal);
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed NdotL = dot(worldNormal,lightDir);
fixed3 reflectionDir = normalize(2 * worldNormal * NdotL - lightDir);
//fixed3 halfDir = normalize(lightDir + viewDir
4000
);
//fixed NdotH = max(0,dot(worldNormal,halfDir));
fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _MainTint.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0,NdotL);
fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
//fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(NdotH,_SpecPower);
return fixed4(ambient + diffuse + specular,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
{
Properties
{
_MainTex("Base(RGB)",2D) = ""{}
_MainTint("Main Tint",Color) = (1,1,1,1)
_SpecularColor("Specular Color",Color) = (1,1,1,1)
_SpecPower("Specular Power",Range(0,30)) = 1
}
SubShader
{
Tags{"RenderType" = "Opaque"}
LOD 200
CGPROGRAM
#pragma surface surf Phong
sampler2D _MainTex;
fixed4 _MainTint;
fixed4 _SpecularColor;
fixed _SpecPower;
struct Input
{
half2 uv_MainTex;
};
void surf(Input IN,inout SurfaceOutput o)
{
fixed4 col = tex2D(_MainTex,IN.uv_MainTex);
o.Albedo = col.rgb;
o.Alpha = col.a;
}
inline fixed4 LightingPhong(SurfaceOutput s,fixed3 lightDir,fixed3 viewDir,fixed atten)
{
fixed3 normal = normalize(s.Normal);
fixed NdotL = dot(normal,lightDir);
fixed3 reflectionDir = normalize(2 * normal * NdotL - lightDir);
//fixed3 halfDir = normalize(lightDir + viewDir);
//fixed NdotH = max(0,dot(normal,halfDir));
fixed spec = pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
fixed3 diffuse = _LightColor0.rgb * s.Albedo * max(0,NdotL);
fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
//fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(NdotH,_SpecPower);
return fixed4(diffuse+specular,1.0);
}
ENDCG
}
FallBack "Diffuse"
}
顶点-片元实现:Shader "Phong"
{
Properties
{
_MainTex("Base(RGB)",2D) = ""{}
_MainTint("Main Tint",Color) = (1,1,1,1)
_SpecularColor("Specular Color",Color) = (1,1,1,1)
_SpecPower("Specular Power",Range(0,30)) = 1
}
SubShader
{
Pass
{
Tags{"LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
sampler2D _MainTex;
fixed4 _MainTex_ST;
fixed4 _MainTint;
fixed4 _SpecularColor;
fixed _SpecPower;
struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos:SV_POSITION;
half2 uv:TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.worldNormal = mul(unity_ObjectToWorld,v.normal);
o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed NdotL = dot(worldNormal,lightDir);
fixed3 reflectionDir = normalize(2 * worldNormal * NdotL - lightDir);
//fixed3 halfDir = normalize(lightDir + viewDir
4000
);
//fixed NdotH = max(0,dot(worldNormal,halfDir));
fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _MainTint.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0,NdotL);
fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(max(0,dot(reflectionDir,viewDir)),_SpecPower);
//fixed3 specular = _LightColor0.rgb * _SpecularColor.rgb * pow(NdotH,_SpecPower);
return fixed4(ambient + diffuse + specular,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
相关文章推荐
- Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-BasicDiffuse
- Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-SimpleReflection
- Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-AnimatedSprite
- Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-SpecularMask
- Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-BRDFDiffuse
- Unity着色器和屏幕特效开发秘籍 顶点-片元着色器实现系列-NormalMapping
- 《着色器和屏幕特效》读书笔记第八章-用Unity的渲染纹理实现屏幕特效
- 关键帧实现的动画效果顶点着色器的开发(Shader特效1)
- 波动效果顶点着色器的实现(Shader特效4)
- 【Unity Shader编程】之十五 屏幕高斯模糊(Gaussian Blur)后期特效的实现
- 【unity】【shader】屏幕高斯模糊(Gaussian Blur)后期特效的实现
- Unity开发安卓游戏:1.实现移动设备屏幕自适应
- 【《Unity着色器和屏幕特效开发秘笈》】学习整理:关于模型顶点色的显示问题
- Android官方开发文档Training系列课程中文版:多样屏幕之实现自适应UI
- unity使用屏幕后处理实现闪烁特效,创建新的shader文件过程
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四)实现2D人物动画①
- BlogEngine.Net架构与源代码分析系列part9:开发扩展(上)——Extension与管理上的实现
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(五)实现2D人物动画②
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(四)实现2D人物动画①
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(十一)地图遮罩层的实现