Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/MegaDisplaceRT.cs
2026-02-21 16:45:37 +08:00

142 lines
2.7 KiB
C#

using UnityEngine;
[AddComponentMenu("Modifiers/DisplaceRT")]
public class MegaDisplaceRT : MegaModifier
{
public RenderTexture rtmap;
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;
private Texture2D map;
public override string ModName()
{
return "DisplaceRT";
}
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 (rtmap == null)
{
return false;
}
if (map == null || rtmap.width != map.width || rtmap.height != map.height)
{
map = new Texture2D(rtmap.width, rtmap.height);
}
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;
}
RenderTexture.active = rtmap;
map.ReadPixels(new Rect(0f, 0f, rtmap.width, rtmap.height), 0, 0);
return true;
}
}