【核心思路】
让顶点坐标沿着顶点法线方向移动
【效果】
【Shader实现】
Shader "Custom/Swell"//采用表面着色器
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_Metallic ("Metallic", 2D) = "white"{}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Occlusion("Occlusion",2D) = "white"{}
_Normal("Normal",2D) = "white"{}
_SwellFactor("SwellFactor",Range(0,0.1)) = 0.0 //膨胀系数
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert//设置顶点修改函数
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Metallic;
sampler2D _Occlusion;
sampler2D _Normal;
struct Input//定义输入结构体
{
float2 uv_MainTex;
float2 uv_Normal;
};
half _Glossiness;
fixed4 _Color;
fixed _SwellFactor;
void vert(inout appdata_full v)
{
v.vertex.xyz += v.normal * _SwellFactor;//让顶点坐标沿着顶点法线方向移动
}
//表面着色器的表面函数
void surf (Input IN, inout SurfaceOutputStandard o)//SurfaceOutputStandard为适用于金属工作流的物理光照模型的表面结构体
{ //该结构体在UnityPBSLighting.cgnic中被定义
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic comes from a texture
o.Metallic = tex2D(_Metallic, IN.uv_MainTex);
//Smoothness come from slider variables
o.Smoothness = _Glossiness;
o.Occlusion = tex2D(_Occlusion, IN.uv_MainTex);
o.Alpha = c.a;
//采样法线贴图并解包(像素值范围为[0,1],而法线范围为[-1,1]
float3 normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal));
o.Normal = normal;
}
ENDCG
}
FallBack "Diffuse"
}
【材质赋值】