实现思路:
沿着3D物体每个面的法线,将面偏移一定的位置。
Shader Graph实现如下:
Shader Lab 实现如下:
Shader "Unlit/MeshExplode"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Distance("Distance",Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 normal : Normal;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Distance;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex + v.normal * clamp(_Distance,0,100) * 0.01);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
实现效果如下:
参考链接:
Mesh Explosion in Shader Graph | Bytesize Gamedev (youtube.com)