5 [DisallowMultipleComponent]
6 [RequireComponent(typeof(InkCanvas))]
18 #region SerializedField
20 [SerializeField]
private bool useMainTextureFluid =
true;
22 [SerializeField]
private bool useNormalMapFluid =
true;
24 [SerializeField]
private int createTextureSize = 1024;
26 [SerializeField]
private ColorSynthesis colorSynthesis = ColorSynthesis.Overwrite;
28 [SerializeField] [Range(0, 1)]
private float alpha = 1f;
30 [SerializeField]
private Vector2 flowDirection = Vector2.up;
32 [SerializeField] [Range(0, 1)]
private float flowingForce = 1;
34 [SerializeField] [Range(0.1f, 10f)]
private float easeOfDripping = 1.0f;
36 [SerializeField] [Range(1f, 0f)]
private float influenceOfNormal = 1;
38 [SerializeField] [Range(0.01f, 1)]
private float horizontalSpread = 0.01f;
40 [SerializeField]
private float normalScaleFactor = 1;
42 [SerializeField] [Range(0.001f, 0.999f)]
43 private float AdhesionBorder = 0.01f;
45 [SerializeField]
private bool performanceOptimize =
true;
47 [SerializeField] [Range(0.01f, 10f)]
private float fluidProcessStopTime = 5f;
49 #endregion SerializedField
53 private bool enabledFluid;
54 private float lastPaintedTime;
55 private Material heightFluid;
56 private Material height2Normal;
57 private Material height2Color;
58 private Material singleColorFill;
59 private Material invertAlpha;
61 private Color lastPaintedColor;
63 #endregion PrivateField
65 #region ShaderKeywords
67 private const string COLOR_SYNTHESIS_ADD =
"COLOR_SYNTHESIS_ADD";
68 private const string COLOR_SYNTHESIS_OVERWRITE =
"COLOR_SYNTHESIS_OVERWRITE";
70 #endregion ShaderKeywords
83 if (heightPaint !=
null)
84 SingleColorFill(heightPaint, Vector4.zero);
87 if (lastPaintedColor != brush.Color)
89 lastPaintedColor = brush.Color;
101 private void SingleColorFill(RenderTexture texture,
Color color)
103 var tmp = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32,
104 RenderTextureReadWrite.Linear);
105 singleColorFill.SetVector(
"_Color", color);
106 Graphics.Blit(texture, tmp, singleColorFill);
107 Graphics.Blit(tmp, texture);
108 RenderTexture.ReleaseTemporary(tmp);
115 private void InvertAlpha(RenderTexture texture)
117 var tmp = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32,
118 RenderTextureReadWrite.Linear);
119 Graphics.Blit(texture, tmp, invertAlpha);
120 Graphics.Blit(tmp, texture);
121 RenderTexture.ReleaseTemporary(tmp);
132 lastPaintedTime = Time.time;
141 private void StopFluid()
145 var materialName =
set.material.name;
147 if (heightPaint !=
null)
148 InvertAlpha(heightPaint);
152 #endregion PrivateMethod
154 #region UnityEventMethod
158 heightFluid =
new Material(Resources.Load<Material>(
"Es.InkPainter.Fluid.HeightDrip"));
159 height2Normal =
new Material(Resources.Load<Material>(
"Es.InkPainter.Fluid.HeightToNormal"));
160 height2Color =
new Material(Resources.Load<Material>(
"Es.InkPainter.Fluid.HeightToColor"));
161 singleColorFill =
new Material(Resources.Load<Material>(
"Es.InkPainter.Fluid.SingleColorFill"));
162 invertAlpha =
new Material(Resources.Load<Material>(
"Es.InkPainter.Fluid.InvertAlpha"));
164 canvas = GetComponent<InkCanvas>();
169 private void OnWillRenderObject()
171 if (performanceOptimize && enabledFluid && Time.time - lastPaintedTime > fluidProcessStopTime)
175 enabledFluid =
false;
183 var materialName =
set.material.name;
185 if (heightPaint ==
null)
187 var newHeightPaint =
new RenderTexture(createTextureSize, createTextureSize, 0,
188 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
189 SingleColorFill(newHeightPaint, Vector4.zero);
191 heightPaint = newHeightPaint;
192 set.material.SetFloat(
"_Parallax", 0);
195 var heightTmp = RenderTexture.GetTemporary(heightPaint.width, heightPaint.height, 0,
196 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
197 heightFluid.SetFloat(
"_ScaleFactor", flowingForce);
198 heightFluid.SetFloat(
"_Viscosity", easeOfDripping);
199 heightFluid.SetFloat(
"_HorizontalSpread", horizontalSpread);
200 heightFluid.SetFloat(
"_InfluenceOfNormal", influenceOfNormal);
201 heightFluid.SetVector(
"_FlowDirection", flowDirection.normalized);
202 heightFluid.SetVector(
"_FixedColor", lastPaintedColor);
203 foreach (var key
in heightFluid.shaderKeywords)
204 heightFluid.DisableKeyword(key);
205 switch (colorSynthesis)
207 case ColorSynthesis.Add:
208 heightFluid.EnableKeyword(COLOR_SYNTHESIS_ADD);
210 case ColorSynthesis.Overwrite:
212 heightFluid.EnableKeyword(COLOR_SYNTHESIS_OVERWRITE);
217 heightFluid.SetTexture(
"_NormalMap", canvas.
GetNormalTexture(materialName));
218 Graphics.Blit(heightPaint, heightTmp, heightFluid);
219 Graphics.Blit(heightTmp, heightPaint);
220 RenderTexture.ReleaseTemporary(heightTmp);
222 if (useMainTextureFluid)
225 if (mainPaint ==
null)
227 var newMainPaint =
new RenderTexture(createTextureSize, createTextureSize, 0,
228 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
232 mainPaint = newMainPaint;
235 var mainTmp = RenderTexture.GetTemporary(mainPaint.width, mainPaint.height, 0,
236 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
237 height2Color.SetTexture(
"_ColorMap", mainPaint);
238 height2Color.SetTexture(
"_BaseColor", canvas.
GetMainTexture(materialName));
239 height2Color.SetFloat(
"_Alpha", alpha);
240 height2Color.SetFloat(
"_Border", AdhesionBorder);
241 Graphics.Blit(heightPaint, mainTmp, height2Color);
242 Graphics.Blit(mainTmp, mainPaint);
243 RenderTexture.ReleaseTemporary(mainTmp);
246 if (useNormalMapFluid)
249 if (normalPaint ==
null)
251 var newNormalPaint =
new RenderTexture(createTextureSize, createTextureSize, 0,
252 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
254 SingleColorFill(newNormalPaint, Vector4.one * 0.5f);
255 set.material.EnableKeyword(
"_NORMALMAP");
259 normalPaint = newNormalPaint;
262 var normalTmp = RenderTexture.GetTemporary(normalPaint.width, normalPaint.height, 0,
263 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
264 height2Normal.SetTexture(
"_BumpMap", normalPaint);
265 height2Normal.SetFloat(
"_NormalScaleFactor", normalScaleFactor);
266 height2Normal.SetFloat(
"_Border", AdhesionBorder);
267 Graphics.Blit(heightPaint, normalTmp, height2Normal);
268 Graphics.Blit(normalTmp, normalPaint);
269 RenderTexture.ReleaseTemporary(normalTmp);
274 #endregion UnityEventMethod
Class managing brush information.
NormalBlendType
Brush normal information synthesis method.
ColorBlendType ColorBlending
Color synthesis method.
ColorBlendType
Color synthesis method.
HeightBlendType HeightBlending
Brush height information synthesis method.
NormalBlendType NormalBlending
Brush normal information synthesis method.
HeightBlendType
Brush height information synthesis method.
Texture paint to canvas. To set the per-material.
Action< InkCanvas, Brush > OnPaintStart
Called at paint start.
Action< InkCanvas > OnInitializedAfter
Called by InkCanvas initialization completion times.
List< PaintSet > PaintDatas
Access data used for painting.
Texture GetMainTexture(string materialName)
To get the original main texture.
RenderTexture GetPaintNormalTexture(string materialName)
To get the paint in normal map.
void SetPaintHeightTexture(string materialName, RenderTexture newTexture)
Set paint texture.
void SetPaintMainTexture(string materialName, RenderTexture newTexture)
Set paint texture.
RenderTexture GetPaintMainTexture(string materialName)
To get the main texture in paint.
Texture GetNormalTexture(string materialName)
To get the original normal map.
RenderTexture GetPaintHeightTexture(string materialName)
To get the paint in height map.
void SetPaintNormalTexture(string materialName, RenderTexture newTexture)
Set paint texture.