126 lines
2.4 KiB
C#
126 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Noise")]
|
|
public class MegaNoise : MegaModifier
|
|
{
|
|
public float Scale = 1f;
|
|
|
|
public bool Fractal;
|
|
|
|
public float Freq = 0.25f;
|
|
|
|
public float Iterations = 6f;
|
|
|
|
public bool Animate;
|
|
|
|
public float Phase;
|
|
|
|
public float Rough;
|
|
|
|
public Vector3 Strength = new Vector3(0f, 0f, 0f);
|
|
|
|
private MegaPerlin iperlin = MegaPerlin.Instance;
|
|
|
|
private float time;
|
|
|
|
private float scale;
|
|
|
|
private float rt;
|
|
|
|
private Vector3 sp = Vector3.zero;
|
|
|
|
private Vector3 d = Vector3.zero;
|
|
|
|
public override string ModName()
|
|
{
|
|
return "Noise";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=262";
|
|
}
|
|
|
|
public override void Modify(MegaModifiers mc)
|
|
{
|
|
for (int i = 0; i < verts.Length; i++)
|
|
{
|
|
Vector3 point = tm.MultiplyPoint3x4(verts[i]);
|
|
sp.x = point.x * scale + 0.5f;
|
|
sp.y = point.y * scale + 0.5f;
|
|
sp.z = point.z * scale + 0.5f;
|
|
if (Fractal)
|
|
{
|
|
d.x = iperlin.fBm1(sp.y, sp.z, time, rt, 2f, Iterations);
|
|
d.y = iperlin.fBm1(sp.x, sp.z, time, rt, 2f, Iterations);
|
|
d.z = iperlin.fBm1(sp.x, sp.y, time, rt, 2f, Iterations);
|
|
}
|
|
else
|
|
{
|
|
d.x = iperlin.Noise(sp.y, sp.z, time);
|
|
d.y = iperlin.Noise(sp.x, sp.z, time);
|
|
d.z = iperlin.Noise(sp.x, sp.y, time);
|
|
}
|
|
point.x += d.x * Strength.x;
|
|
point.y += d.y * Strength.y;
|
|
point.z += d.z * Strength.z;
|
|
sverts[i] = invtm.MultiplyPoint3x4(point);
|
|
}
|
|
}
|
|
|
|
public override void ModStart(MegaModifiers mc)
|
|
{
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
p = tm.MultiplyPoint3x4(p);
|
|
float x = p.x * scale + 0.5f;
|
|
float num = p.y * scale + 0.5f;
|
|
float y = p.z * scale + 0.5f;
|
|
float num2;
|
|
float num3;
|
|
float num4;
|
|
if (Fractal)
|
|
{
|
|
num2 = iperlin.fBm1(num, y, time, rt, 2f, Iterations);
|
|
num3 = iperlin.fBm1(x, y, time, rt, 2f, Iterations);
|
|
num4 = iperlin.fBm1(x, num, time, rt, 2f, Iterations);
|
|
}
|
|
else
|
|
{
|
|
num2 = iperlin.Noise(num, y, time);
|
|
num3 = iperlin.Noise(x, y, time);
|
|
num4 = iperlin.Noise(x, num, time);
|
|
}
|
|
p.x += num2 * Strength.x;
|
|
p.y += num3 * Strength.y;
|
|
p.z += num4 * Strength.z;
|
|
return invtm.MultiplyPoint3x4(p);
|
|
}
|
|
|
|
public override bool ModLateUpdate(MegaModContext mc)
|
|
{
|
|
if (Animate)
|
|
{
|
|
Phase += Time.deltaTime * Freq;
|
|
}
|
|
time = Phase;
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
if (Scale == 0f)
|
|
{
|
|
scale = 1E-06f;
|
|
}
|
|
else
|
|
{
|
|
scale = 1f / Scale;
|
|
}
|
|
rt = 1f - Rough;
|
|
return true;
|
|
}
|
|
}
|