using UnityEngine; [AddComponentMenu("Modifiers/Displace WebCam")] public class MegaDisplaceWebCam : MegaModifier { public WebCamTexture map; public float amount; public Vector2 offset = Vector2.zero; public Vector2 scale = Vector2.one; public MegaChannel channel; public bool CentLum = true; public float CentVal = 0.5f; public float Decay; private float width; private float height; public int WebWidth = 160; public int WebHeight = 120; public int WebRate = 15; [HideInInspector] public Vector2[] uvs; [HideInInspector] public Vector3[] normals; public override string ModName() { return "Displace WebCam"; } 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) { Vector2 vector = Vector2.Scale(uvs[i] + offset, scale); Color pixel = map.GetPixel((int)(vector.x * width), (int)(vector.y * height)); float num = amount; if (Decay != 0f) { num *= Mathf.Exp((0f - Decay) * p.magnitude); } num = ((!CentLum) ? (num * pixel[(int)channel]) : (num * (pixel[(int)channel] + CentVal))); p += normals[i] * pixel[(int)channel] * num; } 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) { if (Application.isPlaying && map == null) { map = new WebCamTexture(WebWidth, WebHeight, WebRate); if ((bool)map) { map.Play(); } else { Debug.Log("Couldnt create WebCamTexture"); } } 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; } width = map.width; height = map.height; return true; } }