UnityShader 植物被风吹弯效果

csharp 复制代码
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "AchonorShader/Plant"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BaseColor ("Base Color", Color) = (1, 1, 1, 1)
        _WindDirectionX("WindDirectionX", Range(-1,1)) = 0
        _WindDirectionZ("WindDirectionZ", Range(-1,1)) = 0
        //模型高度
        _ModelHeight("ModelHeight", Float) = 1
        //根的世界坐标
        _RootPosition("RootPosition", vector) = (0, 0, 0, 1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog
            #pragma enable_d3d11_debug_symbols
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _BaseColor;
            float _WindDirectionX;
            float _WindDirectionZ;
            float _ModelHeight;
            float4 _RootPosition;

            v2f vert (appdata v)
            {
                v2f o;
                //_Matrix_M = UNITY_MATRIX_M;
                float4 worldPos = mul(UNITY_MATRIX_M, v.vertex);
                float4 rootWorldPos = _RootPosition;//mul(_Matrix_M, _RootOffset);
                float weight = (worldPos.y - rootWorldPos.y) / _ModelHeight;
                float weightX = (weight * _WindDirectionX) + 0.5 * 3.1415926f;
                float weightZ = -1 * ((weight * _WindDirectionZ) + 1.5 * 3.1415926f);
                float3x3 matrixX = float3x3(
                                        float3(sin(weightX), -cos(weightX), 0),
                                        float3(cos(weightX), sin(weightX), 0),
                                        float3(0, 0, 1));
                float3x3 matrixZ = float3x3(
                                        float3(1, 0, 0),
                                        float3(0, sin(weightZ), -cos(weightZ)),
                                        float3(0, cos(weightZ), sin(weightZ)));
                float3 offsetPos = worldPos - rootWorldPos;
                float4 newWorldPos = float4(mul(matrixZ, mul(matrixX, offsetPos)) + rootWorldPos, 1);
                
                o.vertex = mul(UNITY_MATRIX_VP, newWorldPos);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv) * _BaseColor;
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}
相关推荐
DanmF--5 小时前
Protobuf协议生成和使用
网络·unity·c#·游戏引擎·游戏程序
AgilityBaby5 小时前
关于在Unity项目中使用Post Processing插件打包到web端出现的问题
3d·unity·游戏引擎
FAREWELL0007514 小时前
Unity学习总结篇(1)关于各种坐标系
学习·unity·c#·游戏引擎
与火星的孩子对话1 天前
Unity3D开发AI桌面精灵/宠物系列 【六】 人物模型 语音口型同步 LipSync 、梅尔频谱MFCC技术、支持中英文自定义编辑- 基于 C# 语言开发
人工智能·unity·c#·游戏引擎·宠物·lipsync
敲代码的 蜡笔小新1 天前
【行为型之访问者模式】游戏开发实战——Unity灵活数据操作与跨系统交互的架构秘诀
unity·设计模式·c#·访问者模式
动感光博1 天前
Unity序列化字段、单例模式(Singleton Pattern)
unity·单例模式·c#
FAREWELL000752 天前
Unity基础学习(十五)核心系统——音效系统
学习·unity·c#·游戏引擎
向宇it2 天前
【unity游戏开发——编辑器扩展】使用MenuItem自定义菜单栏拓展
开发语言·ui·unity·c#·编辑器·游戏引擎
动感光博2 天前
Unity碰撞检测(射线投射、胶囊体投射)、交互(图层、掩码)
unity·c#·游戏引擎·游戏程序·动画