-
Notifications
You must be signed in to change notification settings - Fork 50
/
TexturePaint.cs
242 lines (169 loc) · 8.55 KB
/
TexturePaint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
public class TexturePaint : MonoBehaviour {
// ======================================================================================================================
// PARAMETERS -----------------------------------------------------------------------------------------------
public Texture baseTexture; // used to deterimne the dimensions of the runtime texture
public Material meshMaterial; // used to bind the runtime texture as the albedo of the mesh
public GameObject meshGameobject;
public Shader UVShader; // the shader usedto draw in the texture of the mesh
public Mesh meshToDraw;
public Shader ilsandMarkerShader;
public Shader fixIlsandEdgesShader;
public Shader combineMetalicSmoothnes;
public static Vector3 mouseWorldPosition;
// --------------------------------
private Camera mainC;
private int clearTexture;
private RenderTexture markedIlsandes;
private CommandBuffer cb_markingIlsdands;
private int numberOfFrames;
private Material fixEdgesMaterial;
private Material createMetalicGlossMap;
private RenderTexture metalicGlossMapCombined;
// ---------------------------------
private PaintableTexture albedo;
private PaintableTexture metalic;
public PaintableTexture smoothness;
// ======================================================================================================================
// INITIALIZE -------------------------------------------------------------------
void Start () {
// Main cam initialization ---------------------------------------------------
mainC = Camera.main;
if (mainC == null) mainC = this.GetComponent<Camera>();
if (mainC == null) mainC = GameObject.FindObjectOfType<Camera>();
// Texture and Mat initalization ---------------------------------------------
markedIlsandes = new RenderTexture(baseTexture.width, baseTexture.height, 0, RenderTextureFormat.R8);
albedo = new PaintableTexture(Color.white, baseTexture.width, baseTexture.height, "_MainTex"
,UVShader, meshToDraw, fixIlsandEdgesShader,markedIlsandes);
metalic = new PaintableTexture(Color.white, baseTexture.width, baseTexture.height, "_MetallicGlossMap"
, UVShader, meshToDraw, fixIlsandEdgesShader, markedIlsandes);
smoothness = new PaintableTexture(Color.black, baseTexture.width, baseTexture.height, "_GlossMap"
, UVShader, meshToDraw, fixIlsandEdgesShader, markedIlsandes);
metalicGlossMapCombined = new RenderTexture(metalic.runTimeTexture.descriptor)
{
format = RenderTextureFormat.ARGB32,
};
meshMaterial.SetTexture(albedo.id, albedo.runTimeTexture);
meshMaterial.SetTexture(metalic.id, metalicGlossMapCombined);
meshMaterial.EnableKeyword("_METALLICGLOSSMAP");
createMetalicGlossMap = new Material(combineMetalicSmoothnes);
// Command buffer inialzation ------------------------------------------------
cb_markingIlsdands = new CommandBuffer();
cb_markingIlsdands.name = "markingIlsnads";
cb_markingIlsdands.SetRenderTarget(markedIlsandes);
Material mIlsandMarker = new Material(ilsandMarkerShader);
cb_markingIlsdands.DrawMesh(meshToDraw, Matrix4x4.identity, mIlsandMarker);
mainC.AddCommandBuffer(CameraEvent.AfterDepthTexture, cb_markingIlsdands);
albedo.SetActiveTexture(mainC);
}
// ======================================================================================================================
// LOOP ---------------------------------------------------------------------------
private void Update()
{
if (numberOfFrames > 2) mainC.RemoveCommandBuffer(CameraEvent.AfterDepthTexture, cb_markingIlsdands);
createMetalicGlossMap.SetTexture("_Smoothness", smoothness.runTimeTexture);
createMetalicGlossMap.SetTexture("_MainTex", metalic.runTimeTexture);
Graphics.Blit(metalic.runTimeTexture, metalicGlossMapCombined, createMetalicGlossMap);
numberOfFrames++;
// ----------------------------------------------------------------------------
// This MUST be called to set up the painting with the mouse.
albedo .UpdateShaderParameters(meshGameobject.transform.localToWorldMatrix);
metalic .UpdateShaderParameters(meshGameobject.transform.localToWorldMatrix);
smoothness.UpdateShaderParameters(meshGameobject.transform.localToWorldMatrix);
// ---------------------------------------------------------------------------
// Setting up Mouse Parameters
RaycastHit hit;
Ray ray = mainC.ScreenPointToRay(Input.mousePosition);
Vector4 mwp = Vector3.positiveInfinity;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.tag == "PaintObject")
mwp = hit.point;
}
mwp.w = Input.GetMouseButton(0)? 1 : 0;
mouseWorldPosition = mwp;
Shader.SetGlobalVector("_Mouse", mwp);
}
// ======================================================================================================================
// HELPER FUNCTIONS ---------------------------------------------------------------------------
public void SetAlbedoActive()
{
metalic .SetInactiveTexture(mainC);
smoothness.SetInactiveTexture(mainC);
albedo .SetActiveTexture(mainC);
}
public void SetMetalicActive()
{
albedo .SetInactiveTexture(mainC);
smoothness.SetInactiveTexture(mainC);
metalic .SetActiveTexture(mainC);
}
public void SetGlossActive()
{
metalic .SetInactiveTexture(mainC);
albedo .SetInactiveTexture(mainC);
smoothness.SetActiveTexture(mainC);
}
}
[System.Serializable]
public class PaintableTexture
{
public string id;
public RenderTexture runTimeTexture;
public RenderTexture paintedTexture;
public CommandBuffer cb;
private Material mPaintInUV;
private Material mFixedEdges;
private RenderTexture fixedIlsands;
public PaintableTexture(Color clearColor, int width, int height, string id,
Shader sPaintInUV, Mesh mToDraw, Shader fixIlsandEdgesShader, RenderTexture markedIlsandes)
{
this.id = id;
runTimeTexture = new RenderTexture(width, height, 0)
{
anisoLevel = 0,
useMipMap = false,
filterMode = FilterMode.Bilinear
};
paintedTexture = new RenderTexture(width, height, 0)
{
anisoLevel = 0,
useMipMap = false,
filterMode = FilterMode.Bilinear
};
fixedIlsands = new RenderTexture(paintedTexture.descriptor);
Graphics.SetRenderTarget(runTimeTexture);
GL.Clear(false, true, clearColor);
Graphics.SetRenderTarget(paintedTexture);
GL.Clear(false, true, clearColor);
mPaintInUV = new Material(sPaintInUV);
if (!mPaintInUV.SetPass(0)) Debug.LogError("Invalid Shader Pass: " );
mPaintInUV.SetTexture("_MainTex", paintedTexture);
mFixedEdges = new Material(fixIlsandEdgesShader);
mFixedEdges.SetTexture("_IlsandMap", markedIlsandes);
mFixedEdges.SetTexture("_MainTex", paintedTexture);
// ----------------------------------------------
cb = new CommandBuffer();
cb.name = "TexturePainting"+ id;
cb.SetRenderTarget(runTimeTexture);
cb.DrawMesh(mToDraw, Matrix4x4.identity, mPaintInUV);
cb.Blit(runTimeTexture, fixedIlsands, mFixedEdges);
cb.Blit(fixedIlsands, runTimeTexture);
cb.Blit(runTimeTexture, paintedTexture);
}
public void SetActiveTexture(Camera mainC)
{
mainC.AddCommandBuffer(CameraEvent.AfterDepthTexture, cb);
}
public void SetInactiveTexture(Camera mainC)
{
mainC.RemoveCommandBuffer(CameraEvent.AfterDepthTexture, cb);
}
public void UpdateShaderParameters(Matrix4x4 localToWorld)
{
mPaintInUV.SetMatrix("mesh_Object2World", localToWorld); // Mus be updated every time the mesh moves, and also at start
}
}