Files
2026-02-21 16:45:37 +08:00

104 lines
2.3 KiB
C#

using System;
using UnityEngine;
[AddComponentMenu("Modifiers/Tree Bend")]
public class MegaTreeBend : MegaModifier
{
public float fBendScale = 1f;
public Vector3 vWind = Vector3.zero;
public float WindDir;
public float WindSpeed;
private Vector2 waveIn = Vector2.zero;
private Vector2 waves = Vector2.zero;
public override string ModName()
{
return "Tree Bend";
}
public override string GetHelpURL()
{
return "?page_id=41";
}
private float frac(float val)
{
int num = (int)val;
return val - (float)num;
}
private Vector2 smoothCurve(Vector2 x)
{
x.x = x.x * x.x * (3f - 2f * x.x);
x.y = x.y * x.y * (3f - 2f * x.y);
return x;
}
private Vector2 triangleWave(Vector2 x)
{
x.x = Mathf.Abs(frac(x.x + 0.5f) * 2f - 1f);
x.y = Mathf.Abs(frac(x.y + 0.5f) * 2f - 1f);
return x;
}
private Vector2 smoothTriangleWave(Vector2 x)
{
return smoothCurve(triangleWave(x));
}
private float windTurbulence(float bbPhase, float frequency, float strength)
{
waveIn.x = bbPhase + frequency;
waveIn.y = bbPhase + frequency;
waves.x = frac(waveIn.x * 1.975f) * 2f - 1f;
waves.y = frac(waveIn.y * 0.793f) * 2f - 1f;
waves = smoothTriangleWave(waves);
return (waves.x + waves.y) * strength;
}
private Vector2 windEffect(float bbPhase, Vector2 windDirection, float gustLength, float gustFrequency, float gustStrength, float turbFrequency, float turbStrength)
{
float num = windTurbulence(bbPhase, turbFrequency, turbStrength);
float num2 = Mathf.Clamp01(Mathf.Sin((bbPhase - gustFrequency) / gustLength));
float num3 = num2 * gustStrength + (0.2f + num2) * num;
return num3 * windDirection;
}
public override void SetValues(MegaModifier mod)
{
}
public override Vector3 Map(int i, Vector3 p)
{
p = tm.MultiplyPoint3x4(p);
float magnitude = p.magnitude;
float num = p.y * fBendScale;
num += 1f;
num *= num;
num = num * num - num;
Vector3 vector = p;
vector.x += vWind.x * num;
vector.z += vWind.y * num;
p = vector.normalized * magnitude;
p = invtm.MultiplyPoint3x4(p);
return p;
}
public override bool ModLateUpdate(MegaModContext mc)
{
return Prepare(mc);
}
public override bool Prepare(MegaModContext mc)
{
vWind.x = Mathf.Sin(WindDir * ((float)Math.PI / 180f)) * WindSpeed;
vWind.y = Mathf.Cos(WindDir * ((float)Math.PI / 180f)) * WindSpeed;
return true;
}
}