91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
namespace Es.InkPainter.Effective
|
|
{
|
|
public static class GrabArea
|
|
{
|
|
public enum GrabTextureWrapMode
|
|
{
|
|
Clamp = 0,
|
|
Repeat = 1,
|
|
Clip = 2
|
|
}
|
|
|
|
private const string GRAB_AREA_MATERIAL = "Es.InkPainter.Effective.GrabArea";
|
|
|
|
private const string CLIP = "_ClipTex";
|
|
|
|
private const string TARGET = "_TargetTex";
|
|
|
|
private const string CLIP_SCALE = "_ClipScale";
|
|
|
|
private const string CLIP_UV = "_ClipUV";
|
|
|
|
private const string ROTATE = "_Rotate";
|
|
|
|
private const string WM_CLAMP = "WRAP_MODE_CLAMP";
|
|
|
|
private const string WM_REPEAT = "WRAP_MODE_REPEAT";
|
|
|
|
private const string WM_CLIP = "WRAP_MODE_CLIP";
|
|
|
|
private const string ALPHA_REPLACE = "ALPHA_REPLACE";
|
|
|
|
private const string ALPHA_NOT_REPLACE = "ALPHA_NOT_REPLACE";
|
|
|
|
private static Material grabAreaMaterial;
|
|
|
|
public static void Clip(Texture clipTexture, float clipScale, Texture grabTargetTexture, Vector2 targetUV, float rotateAngle, GrabTextureWrapMode wrapMode, RenderTexture dst, bool replaceAlpha = true)
|
|
{
|
|
if (grabAreaMaterial == null)
|
|
{
|
|
InitGrabAreaMaterial();
|
|
}
|
|
SetGrabAreaProperty(clipTexture, clipScale, grabTargetTexture, targetUV, rotateAngle, wrapMode, replaceAlpha);
|
|
RenderTexture temporary = RenderTexture.GetTemporary(clipTexture.width, clipTexture.height, 0);
|
|
Graphics.Blit(clipTexture, temporary, grabAreaMaterial);
|
|
Graphics.Blit(temporary, dst);
|
|
RenderTexture.ReleaseTemporary(temporary);
|
|
}
|
|
|
|
private static void InitGrabAreaMaterial()
|
|
{
|
|
grabAreaMaterial = new Material(Resources.Load<Material>("Es.InkPainter.Effective.GrabArea"));
|
|
}
|
|
|
|
private static void SetGrabAreaProperty(Texture clip, float clipScale, Texture grabTarget, Vector2 targetUV, float rotateAngle, GrabTextureWrapMode wrapMpde, bool replaceAlpha)
|
|
{
|
|
grabAreaMaterial.SetTexture("_ClipTex", clip);
|
|
grabAreaMaterial.SetTexture("_TargetTex", grabTarget);
|
|
grabAreaMaterial.SetFloat("_ClipScale", clipScale);
|
|
grabAreaMaterial.SetFloat("_Rotate", rotateAngle);
|
|
grabAreaMaterial.SetVector("_ClipUV", targetUV);
|
|
string[] shaderKeywords = grabAreaMaterial.shaderKeywords;
|
|
foreach (string keyword in shaderKeywords)
|
|
{
|
|
grabAreaMaterial.DisableKeyword(keyword);
|
|
}
|
|
switch (wrapMpde)
|
|
{
|
|
case GrabTextureWrapMode.Clamp:
|
|
grabAreaMaterial.EnableKeyword("WRAP_MODE_CLAMP");
|
|
break;
|
|
case GrabTextureWrapMode.Repeat:
|
|
grabAreaMaterial.EnableKeyword("WRAP_MODE_REPEAT");
|
|
break;
|
|
case GrabTextureWrapMode.Clip:
|
|
grabAreaMaterial.EnableKeyword("WRAP_MODE_CLIP");
|
|
break;
|
|
}
|
|
if (replaceAlpha)
|
|
{
|
|
grabAreaMaterial.EnableKeyword("ALPHA_REPLACE");
|
|
}
|
|
else
|
|
{
|
|
grabAreaMaterial.EnableKeyword("ALPHA_NOT_REPLACE");
|
|
}
|
|
}
|
|
}
|
|
}
|