[Shader] 风吹草效果

image

原理:

让模型的顶点随着时间变化进行周期性的移动

随时间移动效果

通过sin函数让顶点沿着z轴来回移动,但这是草是整体左右移动,并不是摇摆效果

void vert(inout appdata_full v)
{
	v.vertex.z = sin(_Time.y);
}
增加摇摆效果

草的底部uv的x是1,顶部是0,所以1-uv.x就可以让草的底部不变,越靠上移动幅度越大

void vert(inout appdata_full v)
{
	v.vertex.z += sin(_Time.y) * (1 - v.texcoord.x);
}
完整代码,增加控制摇摆力度和速度
Shader "Custom/L3 T1"
{
    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

        _WindFrequency ("WindFrequency", Range(0.01,10)) = 1
        _WindAmplitude ("WindAmplitude", Range(0,0.5)) = 1
    }
    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;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        float _WindFrequency;
        float _WindAmplitude;

        void vert(inout appdata_full v)
        {
            const float PIx2 = 3.14159 * 2;
			v.vertex.z = _WindAmplitude * sin(_Time.y * _WindFrequency * PIx2) * (1 - v.texcoord.x);
        }

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // 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 "Diffuse"
}
posted @ 2022-01-04 20:34  Weekend  阅读(288)  评论(0)    收藏  举报