赞
踩
这里以Unity3D的官方例子为例。外加一些自己的理解。
surface shader相比于fixed shader,语法有很大的区别。
首先来看一个简单的surface shader。
1、给物体添加一个白色的颜色
其中:"RenderType" = "Opaque" 表示渲染模式为非透明的,#pragma surface surf Lambert表示光照模型是Lambert。
Shader "Custom/Surface Shader" {
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float4 color : COLOR;
};
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = 1;
}
ENDCG
}
Fallback "Diffuse"
}
2、给物体增加一个贴图
第一步要给属性添加一个空白的贴图
_MainTex ("Texture", 2D) = "white" {}
第二步提供一个输入的结构,然后把贴图给转换一下(tex2D (_MainTex, IN.uv_MainTex))。
Shader "Custom/Surface Shader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}
3、法线贴图
这里的法线贴图有两个图:一个是外表面的贴图(MainTex),一个是凹凸贴图(BumpMap),法线贴图使得原本平面的贴图显示得有立体效果,可以减少物体的面。
Shader "Custom/Surface Shader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
sampler2D _MainTex;
sampler2D _BumpMap;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
Fallback "Diffuse"
}
边缘光效果使得物体边缘足够亮,而物体的光取决于面的法线和观察线之间的夹角。从效果图中可以看到,边缘比较明显,另外不同面的光照强度不一样。
Shader "Custom/Surface Shader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _RimColor;
float _RimPower;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower);
}
ENDCG
}
Fallback "Diffuse"
}
5、细节纹理
细节纹理有三层贴图,第一层是主纹理图,第二层是凹凸图,主要体现法线的效果,第三层是细节图。第一层图与第三层图叠加显示,叠加后再与法线贴图结合起来,呈现出细节和法线贴图的效果。
Shader "Custom/Surface Shader" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_Detail ("Detail", 2D) = "gray" {}
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv_Detail;
};
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _Detail;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 2;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
Fallback "Diffuse"
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。