unity建筑物生长切片动画
效果如下图:
对应shader如下,将对应模型材质shader替换为下文shader
Shader "Custom/ClippingShader"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Albedo (RGB)", 2D) = "white" {}
_Glossiness("Smoothness", Range(0,1)) = 0.5
_Metallic("Metallic", Range(0,1)) = 0.0
[HDR] _CutoffColor("Cutoff Color",Color) = (1,0,0,0)
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 200
Cull off
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
float4 _Plane;
float4 _CutoffColor;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
float facing : VFACE;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf(Input IN, inout SurfaceOutputStandard o)
{
float distance = dot(IN.worldPos, _Plane.xyz);
distance = distance + _Plane.w;
clip(-distance);
float facing = IN.facing*0.5 + 0.5;
// Albedo comes from a texture tinted by color
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Standard"
}
对应C#脚本如下,将模型中对应材质赋值给下面脚本中,然后修改挂载此脚本的物体的y值,即可实现此效果
using UnityEngine;
public class ClippingPlane : MonoBehaviour
{
public Material[] materials;
private void Update()
{
Plane plane = new Plane(transform.up, transform.position);
Vector4 planeVisulization = new Vector4(plane.normal.x, plane.normal.y, plane.normal.z, plane.distance);
for (var i = 0; i < materials.Length; i++)
{
materials[i].SetVector("_Plane", planeVisulization);
}
}
}
如果你感觉手动赋值materials比较麻烦,可以写一个编辑器工具,自动搜索项目中对应的材质,然后赋值到materials中即可。
对应的测试工程在这里