Tanoda
TextureRecovery.cs
Go to the documentation of this file.
1using System.Collections;
3using UnityEngine;
4
6{
7 [RequireComponent(typeof(InkCanvas))]
8 public class TextureRecovery : MonoBehaviour
9 {
10 [SerializeField] private float lerpCoefficient = 0.1f;
11
12 [SerializeField] private float callTimer = 0.1f;
13
14 [SerializeField] private bool @fixed;
15
16 private Material material;
17 private InkCanvas canvas;
18
19 private Texture defaultMainTexture;
20 private RenderTexture paintMainTexture;
21 private Texture defaultNormalMap;
22 private RenderTexture paintNormalMap;
23 private Texture defaultHeightMap;
24 private RenderTexture paintHeightMap;
25
26 private void Awake()
27 {
28 canvas = GetComponent<InkCanvas>();
29 canvas.OnInitializedAfter += Init;
30 }
31
32 private void Init(InkCanvas canvas)
33 {
34 material = GetComponent<MeshRenderer>().sharedMaterial;
35 defaultMainTexture = canvas.GetMainTexture(material.name);
36 paintMainTexture = canvas.GetPaintMainTexture(material.name);
37 defaultNormalMap = canvas.GetNormalTexture(material.name);
38 paintNormalMap = canvas.GetPaintNormalTexture(material.name);
39 defaultHeightMap = canvas.GetHeightTexture(material.name);
40 paintHeightMap = canvas.GetPaintHeightTexture(material.name);
41 StartCoroutine(TextureLerp());
42 }
43
44 public void FixedUpdate()
45 {
46 if (!@fixed)
47 return;
48
49 if (defaultMainTexture != null && paintMainTexture != null)
50 TextureMorphing.Lerp(defaultMainTexture, paintMainTexture, lerpCoefficient);
51 if (defaultNormalMap != null && paintNormalMap != null)
52 TextureMorphing.Lerp(defaultNormalMap, paintNormalMap, lerpCoefficient);
53 if (defaultHeightMap != null && paintHeightMap != null)
54 TextureMorphing.Lerp(defaultHeightMap, paintHeightMap, lerpCoefficient);
55 }
56
57 private IEnumerator TextureLerp()
58 {
59 const int CALL_COUNT = 10;
60 while (true)
61 if (@fixed)
62 yield return new WaitForSeconds(1f);
63 else
64 for (var i = 0; i < CALL_COUNT; ++i)
65 {
66 yield return new WaitForSeconds(callTimer / 10);
67 if (defaultMainTexture != null && paintMainTexture != null)
68 TextureMorphing.Lerp(defaultMainTexture, paintMainTexture, lerpCoefficient / CALL_COUNT);
69 if (defaultNormalMap != null && paintNormalMap != null)
70 TextureMorphing.Lerp(defaultNormalMap, paintNormalMap, lerpCoefficient / CALL_COUNT);
71 if (defaultHeightMap != null && paintHeightMap != null)
72 TextureMorphing.Lerp(defaultHeightMap, paintHeightMap, lerpCoefficient / CALL_COUNT);
73 }
74 }
75 }
76}
Texture paint to canvas. To set the per-material.
Definition: InkCanvas.cs:23
Action< InkCanvas > OnInitializedAfter
Called by InkCanvas initialization completion times.
Definition: InkCanvas.cs:176
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
RenderTexture GetPaintMainTexture(string materialName)
To get the main texture in paint.
Definition: InkCanvas.cs:926
Texture GetHeightTexture(string materialName)
To get the original height map.
Definition: InkCanvas.cs:1008
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
Definition: ClipPainter.cs:5