[A] wind cloth

This commit is contained in:
2025-01-08 14:18:52 +08:00
parent e1775823a8
commit 6752d8b4f8
17 changed files with 391 additions and 347 deletions

View File

@@ -0,0 +1,56 @@
//UNITY_SHADER_NO_UPGRADE
#ifndef WIND_ANIM_INCLUDE
#define WIND_ANIM_INCLUDE
inline float4 SmoothCurve( float4 x ) {
return x * x *( 3.0 - 2.0 * x );
}
inline float4 TriangleWave( float4 x ) {
return abs( frac( x + 0.5 ) * 2.0 - 1.0 );
}
inline float4 SmoothTriangleWave( float4 x ) {
return SmoothCurve( TriangleWave( x ) );
}
inline float4 ClothWindAnim(float4 pos, float3 normal, float4 animParams,float4 wind,float2 time)
{
// animParams.x = branch phase
// animParams.y = edge flutter factor
// animParams.z = primary factor
// animParams.w = secondary factor
float fDetailAmp = 0.1f;
float fBranchAmp = 0.3f;
// Phases (object, vertex, branch)
float fObjPhase = dot(unity_ObjectToWorld[3].xyz, 1);
float fBranchPhase = fObjPhase + animParams.x;
float fVtxPhase = dot(pos.xyz, animParams.y + fBranchPhase);
// x is used for edges; y is used for branches
float2 vWavesIn = time + float2(fVtxPhase, fBranchPhase );
// 1.975, 0.793, 0.375, 0.193 are good frequencies
float4 vWaves = (frac( vWavesIn.xxyy * float4(1.975, 0.793, 0.375, 0.193) ) * 2.0 - 1.0);
vWaves = SmoothTriangleWave( vWaves );
float2 vWavesSum = vWaves.xz + vWaves.yw;
// Edge (xz) and branch bending (y)
float3 bend = animParams.y * fDetailAmp * normal.xyz;
bend.y = animParams.w * fBranchAmp;
pos.xyz += ((vWavesSum.xyx * bend)*wind.w + (wind.xyz * vWavesSum.y * animParams.w)) * wind.w;
// Primary bending
// Displace position
pos.xyz += animParams.z * wind.xyz;
return pos;
}
#endif //WIND_ANIM_INCLUDE

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 418d88a69b1774954ace0d91b8c685d7
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,105 @@
Shader "zzwind/WindClothUnlit"
{
Properties
{
[MainTexture] _BaseMap("Texture", 2D) = "white" {}
[MainColor] _BaseColor("Color", Color) = (1, 1, 1, 1)
_Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.5
_Wind("Wind params",Vector) = (1,1,1,1)
_WindAmplitude("Wind Amplitude", float) = 0.5
_WindSpeed("Wind Speed",float) = 0.5
}
SubShader
{
Tags
{
"RenderType" = "Opaque"
"IgnoreProjector" = "True"
"UniversalMaterialType" = "Unlit"
"RenderPipeline" = "UniversalPipeline"
}
Pass
{
Name "Unlit"
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
ZWrite On
Cull Back
ZTest LEqual
ZClip On
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/zzwater/shaders/libs/WindAnim.hlsl"
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
CBUFFER_START(UnityPerMaterial)
float _Cutoff;
half4 _BaseColor;
half4 _BaseMap_ST;
half4 _Wind;
float _WindAmplitude;
float _WindSpeed;
CBUFFER_END
struct Attributes
{
float4 positionOS : POSITION;
float4 tangentOS : TANGENT;
float2 uv : TEXCOORD0;
float3 normalOS : NORMAL;
float4 color : COLOR;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : TEXCOORD2;
};
Varyings vert(Attributes input)
{
Varyings output;
float bendingFact = input.color.a;
float4 wind;
wind.xyz = _Wind.xyz;
wind.w = _Wind.w * bendingFact.x;
float4 windParams = float4(0,_WindAmplitude,bendingFact,0);
float windTime = _Time.y * _WindSpeed;
float4 VertexInWndPos = ClothWindAnim(input.positionOS,input.normalOS,windParams,wind,windTime);
output.positionCS = TransformObjectToHClip(VertexInWndPos);
output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
output.color = input.color;
return output;
}
float4 frag(Varyings input) : SV_Target
{
half2 uv = input.uv;
half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
half3 color = texColor.rgb * _BaseColor.rgb;
half alpha = texColor.a * _BaseColor.a;
clip(alpha - _Cutoff);
return float4(color, alpha);
}
ENDHLSL
}
}
}

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: cbc110f6abccf47ada5a8569ec9eb889
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant: