效果展示:
当前效果需要自己手动写一个shader,效果与unity版本无关,模型没有特别要求
步骤一: 在unity创建一个SurfaceShader类型的shader,命名为ClippingShader,双击进入vs进行编辑,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
#pragma surface surf Standard fullforwardshadows
#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;
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
步骤二: 在unity新建一个C#脚本,命名为ClippingPlane,双击进入vs进行编辑,编辑内容如下:
using UnityEngine;
public class ClippingPlane : MonoBehaviour
{
public Material m_Material;
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);
m_Material.SetVector("_Plane", planeVisulization);
}
}
这里做一点说明,当前代码是做的向上生长,如果需要做成左右或者前后生长,可以把new Plane()里面的transform.up改成transform.right或者transform.forward即可
步骤三: 上述shader和脚本写好之后,把需要生长改变的材质赋值成我们自定义的shader,然后新建一个空物体,在空物体上添加我们写好的脚本,赋值好材质,根据空物体的高度决定shader材质是否显示
|