8 public static class GrabArea
13 public enum GrabTextureWrapMode
22 private const string GRAB_AREA_MATERIAL =
"Es.InkPainter.Effective.GrabArea";
23 private const string CLIP =
"_ClipTex";
24 private const string TARGET =
"_TargetTex";
25 private const string CLIP_SCALE =
"_ClipScale";
26 private const string CLIP_UV =
"_ClipUV";
27 private const string ROTATE =
"_Rotate";
29 private const string WM_CLAMP =
"WRAP_MODE_CLAMP";
30 private const string WM_REPEAT =
"WRAP_MODE_REPEAT";
31 private const string WM_CLIP =
"WRAP_MODE_CLIP";
33 private const string ALPHA_REPLACE =
"ALPHA_REPLACE";
34 private const string ALPHA_NOT_REPLACE =
"ALPHA_NOT_REPLACE";
36 private static Material grabAreaMaterial;
38 #endregion PrivateField
53 public static void Clip(Texture clipTexture,
float clipScale, Texture grabTargetTexture, Vector2 targetUV,
54 float rotateAngle, GrabTextureWrapMode wrapMode, RenderTexture dst,
bool replaceAlpha =
true)
56 if (grabAreaMaterial ==
null)
57 InitGrabAreaMaterial();
58 SetGrabAreaProperty(clipTexture, clipScale, grabTargetTexture, targetUV, rotateAngle, wrapMode,
60 var tmp = RenderTexture.GetTemporary(clipTexture.width, clipTexture.height, 0);
61 Graphics.Blit(clipTexture, tmp, grabAreaMaterial);
62 Graphics.Blit(tmp, dst);
63 RenderTexture.ReleaseTemporary(tmp);
66 #endregion PublicMethod
73 private static void InitGrabAreaMaterial()
75 grabAreaMaterial =
new Material(Resources.Load<Material>(GRAB_AREA_MATERIAL));
87 private static void SetGrabAreaProperty(Texture clip,
float clipScale, Texture grabTarget, Vector2 targetUV,
88 float rotateAngle, GrabTextureWrapMode wrapMpde,
bool replaceAlpha)
90 grabAreaMaterial.SetTexture(CLIP, clip);
91 grabAreaMaterial.SetTexture(TARGET, grabTarget);
92 grabAreaMaterial.SetFloat(CLIP_SCALE, clipScale);
93 grabAreaMaterial.SetFloat(ROTATE, rotateAngle);
94 grabAreaMaterial.SetVector(CLIP_UV, targetUV);
96 foreach (var key
in grabAreaMaterial.shaderKeywords)
97 grabAreaMaterial.DisableKeyword(key);
100 case GrabTextureWrapMode.Clamp:
101 grabAreaMaterial.EnableKeyword(WM_CLAMP);
104 case GrabTextureWrapMode.Repeat:
105 grabAreaMaterial.EnableKeyword(WM_REPEAT);
108 case GrabTextureWrapMode.Clip:
109 grabAreaMaterial.EnableKeyword(WM_CLIP);
116 switch (replaceAlpha)
119 grabAreaMaterial.EnableKeyword(ALPHA_REPLACE);
122 grabAreaMaterial.EnableKeyword(ALPHA_NOT_REPLACE);
127 #endregion PrivateMethod