Tanoda
HeightFluid.cs
Go to the documentation of this file.
1using UnityEngine;
2
4{
5 [DisallowMultipleComponent]
6 [RequireComponent(typeof(InkCanvas))]
7 public class HeightFluid : MonoBehaviour
8 {
12 enum ColorSynthesis
13 {
14 Add,
15 Overwrite
16 }
17
18 #region SerializedField
19
20 [SerializeField] private bool useMainTextureFluid = true;
21
22 [SerializeField] private bool useNormalMapFluid = true;
23
24 [SerializeField] private int createTextureSize = 1024;
25
26 [SerializeField] private ColorSynthesis colorSynthesis = ColorSynthesis.Overwrite;
27
28 [SerializeField] [Range(0, 1)] private float alpha = 1f;
29
30 [SerializeField] private Vector2 flowDirection = Vector2.up;
31
32 [SerializeField] [Range(0, 1)] private float flowingForce = 1;
33
34 [SerializeField] [Range(0.1f, 10f)] private float easeOfDripping = 1.0f;
35
36 [SerializeField] [Range(1f, 0f)] private float influenceOfNormal = 1;
37
38 [SerializeField] [Range(0.01f, 1)] private float horizontalSpread = 0.01f;
39
40 [SerializeField] private float normalScaleFactor = 1;
41
42 [SerializeField] [Range(0.001f, 0.999f)]
43 private float AdhesionBorder = 0.01f;
44
45 [SerializeField] private bool performanceOptimize = true;
46
47 [SerializeField] [Range(0.01f, 10f)] private float fluidProcessStopTime = 5f;
48
49 #endregion SerializedField
50
51 #region PrivateField
52
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;
60 private InkCanvas canvas;
61 private Color lastPaintedColor;
62
63 #endregion PrivateField
64
65 #region ShaderKeywords
66
67 private const string COLOR_SYNTHESIS_ADD = "COLOR_SYNTHESIS_ADD";
68 private const string COLOR_SYNTHESIS_OVERWRITE = "COLOR_SYNTHESIS_OVERWRITE";
69
70 #endregion ShaderKeywords
71
72 #region PrivateMethod
73
78 private void Init(InkCanvas canvas)
79 {
80 foreach (var set in canvas.PaintDatas)
81 {
82 var heightPaint = canvas.GetPaintHeightTexture(set.material.name);
83 if (heightPaint != null)
84 SingleColorFill(heightPaint, Vector4.zero);
85 canvas.OnPaintStart += (own, brush) =>
86 {
87 if (lastPaintedColor != brush.Color)
88 {
89 lastPaintedColor = brush.Color;
90 StopFluid();
91 }
92 };
93 }
94 }
95
101 private void SingleColorFill(RenderTexture texture, Color color)
102 {
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);
109 }
110
115 private void InvertAlpha(RenderTexture texture)
116 {
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);
122 }
123
129 private void EnabledFluid(InkCanvas canvas, Brush brush)
130 {
131 enabledFluid = true;
132 lastPaintedTime = Time.time;
133 brush.ColorBlending = Brush.ColorBlendType.AlphaOnly;
134 brush.NormalBlending = Brush.NormalBlendType.UseBrush;
135 brush.HeightBlending = Brush.HeightBlendType.ColorRGB_HeightA;
136 }
137
141 private void StopFluid()
142 {
143 foreach (var set in canvas.PaintDatas)
144 {
145 var materialName = set.material.name;
146 var heightPaint = canvas.GetPaintHeightTexture(materialName);
147 if (heightPaint != null)
148 InvertAlpha(heightPaint);
149 }
150 }
151
152 #endregion PrivateMethod
153
154 #region UnityEventMethod
155
156 private void Awake()
157 {
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"));
163
164 canvas = GetComponent<InkCanvas>();
165 canvas.OnInitializedAfter += Init;
166 canvas.OnPaintStart += EnabledFluid;
167 }
168
169 private void OnWillRenderObject()
170 {
171 if (performanceOptimize && enabledFluid && Time.time - lastPaintedTime > fluidProcessStopTime)
172 {
173 //In order to prevent continuation of dripping, reversing the sign of the adhesion amount.
174 StopFluid();
175 enabledFluid = false;
176 }
177
178 if (!enabledFluid)
179 return;
180
181 foreach (var set in canvas.PaintDatas)
182 {
183 var materialName = set.material.name;
184 var heightPaint = canvas.GetPaintHeightTexture(materialName);
185 if (heightPaint == null)
186 {
187 var newHeightPaint = new RenderTexture(createTextureSize, createTextureSize, 0,
188 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
189 SingleColorFill(newHeightPaint, Vector4.zero);
190 canvas.SetPaintHeightTexture(materialName, newHeightPaint);
191 heightPaint = newHeightPaint;
192 set.material.SetFloat("_Parallax", 0);
193 }
194
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)
206 {
207 case ColorSynthesis.Add:
208 heightFluid.EnableKeyword(COLOR_SYNTHESIS_ADD);
209 break;
210 case ColorSynthesis.Overwrite:
211 default:
212 heightFluid.EnableKeyword(COLOR_SYNTHESIS_OVERWRITE);
213 break;
214 }
215
216 if (canvas.GetNormalTexture(materialName) != null)
217 heightFluid.SetTexture("_NormalMap", canvas.GetNormalTexture(materialName));
218 Graphics.Blit(heightPaint, heightTmp, heightFluid);
219 Graphics.Blit(heightTmp, heightPaint);
220 RenderTexture.ReleaseTemporary(heightTmp);
221
222 if (useMainTextureFluid)
223 {
224 var mainPaint = canvas.GetPaintMainTexture(materialName);
225 if (mainPaint == null)
226 {
227 var newMainPaint = new RenderTexture(createTextureSize, createTextureSize, 0,
228 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
229 if (canvas.GetMainTexture(materialName) != null)
230 Graphics.Blit(canvas.GetMainTexture(materialName), newMainPaint);
231 canvas.SetPaintMainTexture(materialName, newMainPaint);
232 mainPaint = newMainPaint;
233 }
234
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);
244 }
245
246 if (useNormalMapFluid)
247 {
248 var normalPaint = canvas.GetPaintNormalTexture(materialName);
249 if (normalPaint == null)
250 {
251 var newNormalPaint = new RenderTexture(createTextureSize, createTextureSize, 0,
252 RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
253 //DXT5nm color initialization.
254 SingleColorFill(newNormalPaint, Vector4.one * 0.5f);
255 set.material.EnableKeyword("_NORMALMAP");
256 if (canvas.GetNormalTexture(materialName) != null)
257 Graphics.Blit(canvas.GetNormalTexture(materialName), newNormalPaint);
258 canvas.SetPaintNormalTexture(materialName, newNormalPaint);
259 normalPaint = newNormalPaint;
260 }
261
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);
270 }
271 }
272 }
273
274 #endregion UnityEventMethod
275 }
276}
UnityEngine.Color Color
Definition: TestScript.cs:32
Class managing brush information.
Definition: Brush.cs:11
NormalBlendType
Brush normal information synthesis method.
Definition: Brush.cs:42
ColorBlendType ColorBlending
Color synthesis method.
Definition: Brush.cs:206
ColorBlendType
Color synthesis method.
Definition: Brush.cs:16
HeightBlendType HeightBlending
Brush height information synthesis method.
Definition: Brush.cs:224
NormalBlendType NormalBlending
Brush normal information synthesis method.
Definition: Brush.cs:215
HeightBlendType
Brush height information synthesis method.
Definition: Brush.cs:73
Texture paint to canvas. To set the per-material.
Definition: InkCanvas.cs:23
Action< InkCanvas, Brush > OnPaintStart
Called at paint start.
Definition: InkCanvas.cs:181
Action< InkCanvas > OnInitializedAfter
Called by InkCanvas initialization completion times.
Definition: InkCanvas.cs:176
List< PaintSet > PaintDatas
Access data used for painting.
Definition: InkCanvas.cs:158
Texture GetMainTexture(string materialName)
To get the original main texture.
Definition: InkCanvas.cs:912
RenderTexture GetPaintNormalTexture(string materialName)
To get the paint in normal map.
Definition: InkCanvas.cs:974
void SetPaintHeightTexture(string materialName, RenderTexture newTexture)
Set paint texture.
Definition: InkCanvas.cs:1036
void SetPaintMainTexture(string materialName, RenderTexture newTexture)
Set paint texture.
Definition: InkCanvas.cs:940
RenderTexture GetPaintMainTexture(string materialName)
To get the main texture in paint.
Definition: InkCanvas.cs:926
Texture GetNormalTexture(string materialName)
To get the original normal map.
Definition: InkCanvas.cs:960
RenderTexture GetPaintHeightTexture(string materialName)
To get the paint in height map.
Definition: InkCanvas.cs:1022
void SetPaintNormalTexture(string materialName, RenderTexture newTexture)
Set paint texture.
Definition: InkCanvas.cs:988