using UnityEngine; [AddComponentMenu("Modifiers/Displace Limits")] public class MegaDisplaceLimits : 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; public Vector3 origin = Vector3.zero; public Vector3 size = Vector3.one; [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(); 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) { Vector3 vector = p - origin; if (Mathf.Abs(vector.x) < size.x && Mathf.Abs(vector.y) < size.y && Mathf.Abs(vector.z) < size.z) { Vector2 vector2 = Vector2.Scale(uvs[i] + offset, scale); Color pixelBilinear = map.GetPixelBilinear(vector2.x, vector2.y); float num = amount; if (Decay != 0f) { num *= Mathf.Exp((0f - Decay) * vector.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(); 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; } public override void DrawGizmo(MegaModContext context) { base.DrawGizmo(context); Gizmos.color = Color.yellow; Gizmos.DrawWireCube(origin, size * 2f); } }