130 lines
2.4 KiB
C#
130 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Displace")]
|
|
public class MegaDisplace : MegaModifier
|
|
{
|
|
public Texture2D map;
|
|
|
|
public float amount;
|
|
|
|
public Vector2 offset = Vector2.zero;
|
|
|
|
public float vertical;
|
|
|
|
public Vector2 scale = Vector2.one;
|
|
|
|
public MegaChannel channel;
|
|
|
|
public bool CentLum = true;
|
|
|
|
public float CentVal = 0.5f;
|
|
|
|
public float Decay;
|
|
|
|
[HideInInspector]
|
|
public Vector2[] uvs;
|
|
|
|
[HideInInspector]
|
|
public Vector3[] normals;
|
|
|
|
public override string ModName()
|
|
{
|
|
return "Displace";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=168";
|
|
}
|
|
|
|
public override MegaModChannel ChannelsReq()
|
|
{
|
|
return (MegaModChannel)3;
|
|
}
|
|
|
|
public override MegaModChannel ChannelsChanged()
|
|
{
|
|
return MegaModChannel.Verts;
|
|
}
|
|
|
|
[ContextMenu("Init")]
|
|
public virtual void Init()
|
|
{
|
|
MegaModifyObject component = GetComponent<MegaModifyObject>();
|
|
uvs = component.cachedMesh.uv;
|
|
normals = component.cachedMesh.normals;
|
|
}
|
|
|
|
public override void MeshChanged()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
p = tm.MultiplyPoint3x4(p);
|
|
if (i >= 0)
|
|
{
|
|
Vector2 vector = Vector2.Scale(uvs[i] + offset, scale);
|
|
Color pixelBilinear = map.GetPixelBilinear(vector.x, vector.y);
|
|
float num = amount;
|
|
if (Decay != 0f)
|
|
{
|
|
num *= Mathf.Exp((0f - Decay) * p.magnitude);
|
|
}
|
|
num = ((!CentLum) ? (num * pixelBilinear[(int)channel]) : (num * (pixelBilinear[(int)channel] + CentVal)));
|
|
float num2 = pixelBilinear[(int)channel] * num;
|
|
p.x += normals[i].x * num2 + normals[i].x * vertical;
|
|
p.y += normals[i].y * num2 + normals[i].y * vertical;
|
|
p.z += normals[i].z * num2 + normals[i].z * vertical;
|
|
}
|
|
return invtm.MultiplyPoint3x4(p);
|
|
}
|
|
|
|
public override void Modify(MegaModifiers mc)
|
|
{
|
|
for (int i = 0; i < verts.Length; i++)
|
|
{
|
|
sverts[i] = Map(i, verts[i]);
|
|
}
|
|
}
|
|
|
|
public override bool ModLateUpdate(MegaModContext mc)
|
|
{
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
if (uvs == null || uvs.Length == 0)
|
|
{
|
|
uvs = mc.mod.mesh.uv;
|
|
}
|
|
if (normals == null || normals.Length == 0)
|
|
{
|
|
MegaModifyObject component = GetComponent<MegaModifyObject>();
|
|
if ((bool)component)
|
|
{
|
|
normals = component.cachedMesh.normals;
|
|
}
|
|
else
|
|
{
|
|
normals = mc.mod.mesh.normals;
|
|
}
|
|
}
|
|
if (uvs.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (normals.Length == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (map == null)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|