-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathOutlineEffect.cs
463 lines (412 loc) · 14.1 KB
/
OutlineEffect.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (c) 2015 José Guerreiro. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.Rendering;
namespace cakeslice
{
[DisallowMultipleComponent]
[RequireComponent(typeof(Camera))]
/* [ExecuteInEditMode] */
public class OutlineEffect : MonoBehaviour
{
/* #if UNITY_EDITOR
private void OnValidate()
{
CreateMaterialsIfNeeded();
}
#endif */
public static OutlineEffect Instance { get; private set; }
private readonly LinkedSet<Outline> outlines = new LinkedSet<Outline>();
[Range(1.0f, 6.0f)]
public float lineThickness = 1.25f;
[Range(0, 10)]
public float lineIntensity = .5f;
[Range(0, 1)]
public float fillAmount = 0.2f;
public Color lineColor0 = Color.red;
public Color lineColor1 = Color.green;
public Color lineColor2 = Color.blue;
public bool additiveRendering = false;
public bool backfaceCulling = true;
public Texture2D fillTexture;
public Vector2 fillTextureScale = Vector2.one;
public bool useFillColor = false;
public Color fillColor = Color.blue;
[Header("These settings can affect performance!")]
public bool cornerOutlines = false;
public bool addLinesBetweenColors = false;
[Header("Advanced settings")]
public bool scaleWithScreenSize = true;
[Range(0.0f, 1.0f)]
public float alphaCutoff = .5f;
public bool flipY = false;
public Camera sourceCamera;
public bool autoEnableOutlines = false;
[HideInInspector]
public Camera outlineCamera;
Material outline1Material;
Material outline2Material;
Material outline3Material;
Material outlineEraseMaterial;
Shader outlineShader;
Shader outlineBufferShader;
[HideInInspector]
public Material outlineShaderMaterial;
[HideInInspector]
public RenderTexture renderTexture;
[HideInInspector]
public RenderTexture extraRenderTexture;
CommandBuffer commandBuffer;
Material GetMaterialFromID(int ID)
{
if (ID == 0)
return outline1Material;
else if (ID == 1)
return outline2Material;
else if (ID == 2)
return outline3Material;
else
return outline1Material;
}
List<Material> materialBuffer = new List<Material>();
Material CreateMaterial(Color emissionColor)
{
Material m = new Material(outlineBufferShader);
m.SetColor("_Color", emissionColor);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
return m;
}
private void Awake()
{
if (Instance != null)
{
Destroy(this);
throw new System.Exception("you can only have one outline camera in the scene");
}
Instance = this;
}
void Start()
{
CreateMaterialsIfNeeded();
UpdateMaterialsPublicProperties();
if (sourceCamera == null)
{
sourceCamera = GetComponent<Camera>();
if (sourceCamera == null)
sourceCamera = Camera.main;
}
if (outlineCamera == null)
{
foreach (Camera c in GetComponentsInChildren<Camera>())
{
if (c.name == "Outline Camera")
{
outlineCamera = c;
c.enabled = false;
break;
}
}
if (outlineCamera == null)
{
GameObject cameraGameObject = new GameObject("Outline Camera");
cameraGameObject.transform.parent = sourceCamera.transform;
outlineCamera = cameraGameObject.AddComponent<Camera>();
outlineCamera.enabled = false;
}
}
if (renderTexture != null)
renderTexture.Release();
if (extraRenderTexture != null)
renderTexture.Release();
renderTexture = new RenderTexture(sourceCamera.pixelWidth, sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
extraRenderTexture = new RenderTexture(sourceCamera.pixelWidth, sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
UpdateOutlineCameraFromSource();
commandBuffer = new CommandBuffer();
outlineCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, commandBuffer);
}
bool RenderTheNextFrame;
public void OnPreRender()
{
if (commandBuffer == null)
return;
// The first frame during which there are no outlines, we still need to render
// to clear out any outlines that were being rendered on the previous frame
if (outlines.Count == 0)
{
if (!RenderTheNextFrame)
return;
RenderTheNextFrame = false;
}
else
{
RenderTheNextFrame = true;
}
CreateMaterialsIfNeeded();
if (renderTexture == null || renderTexture.width != sourceCamera.pixelWidth || renderTexture.height != sourceCamera.pixelHeight)
{
if (renderTexture != null)
renderTexture.Release();
if (extraRenderTexture != null)
renderTexture.Release();
renderTexture = new RenderTexture(sourceCamera.pixelWidth, sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
extraRenderTexture = new RenderTexture(sourceCamera.pixelWidth, sourceCamera.pixelHeight, 16, RenderTextureFormat.Default);
outlineCamera.targetTexture = renderTexture;
}
UpdateMaterialsPublicProperties();
UpdateOutlineCameraFromSource();
outlineCamera.targetTexture = renderTexture;
commandBuffer.SetRenderTarget(renderTexture);
commandBuffer.Clear();
foreach (Outline outline in outlines)
{
if (!outline.IsVisible)
continue;
LayerMask l = sourceCamera.cullingMask;
if (outline != null && l == (l | (1 << outline.gameObject.layer)))
{
for (int v = 0; v < outline.SharedMaterials.Length; v++)
{
Material m = null;
if (outline.SharedMaterials[v] != null && outline.SharedMaterials[v].HasProperty("_MainTex") && outline.SharedMaterials[v].mainTexture != null)
{
foreach (Material g in materialBuffer)
{
if (g.mainTexture == outline.SharedMaterials[v].mainTexture)
{
if (outline.eraseRenderer && g.color == outlineEraseMaterial.color)
m = g;
else if (!outline.eraseRenderer && g.color == GetMaterialFromID(outline.color).color)
m = g;
}
}
if (m == null)
{
if (outline.eraseRenderer)
m = new Material(outlineEraseMaterial);
else
m = new Material(GetMaterialFromID(outline.color));
m.mainTexture = outline.SharedMaterials[v].mainTexture;
materialBuffer.Add(m);
}
}
else
{
if (outline.eraseRenderer)
m = outlineEraseMaterial;
else
m = GetMaterialFromID(outline.color);
}
if (backfaceCulling)
m.SetInt("_Culling", (int)UnityEngine.Rendering.CullMode.Back);
else
m.SetInt("_Culling", (int)UnityEngine.Rendering.CullMode.Off);
MeshFilter mL = outline.MeshFilter;
SkinnedMeshRenderer sMR = outline.SkinnedMeshRenderer;
SpriteRenderer sR = outline.SpriteRenderer;
if (mL)
{
if (mL.sharedMesh != null)
{
if (v < mL.sharedMesh.subMeshCount)
commandBuffer.DrawRenderer(outline.Renderer, m, v, 0);
}
}
else if (sMR)
{
if (sMR.sharedMesh != null)
{
if (v < sMR.sharedMesh.subMeshCount)
commandBuffer.DrawRenderer(outline.Renderer, m, v, 0);
}
}
else if (sR)
{
commandBuffer.DrawRenderer(outline.Renderer, m, v, 0);
}
}
}
}
outlineCamera.Render();
}
private void OnEnable()
{
Outline[] o = FindObjectsOfType<Outline>();
if (autoEnableOutlines)
{
foreach (Outline oL in o)
{
oL.enabled = false;
oL.enabled = true;
}
}
else
{
foreach (Outline oL in o)
{
if (oL.enabled && !outlines.Contains(oL))
outlines.Add(oL);
}
}
}
void OnDestroy()
{
if (renderTexture != null)
renderTexture.Release();
if (extraRenderTexture != null)
extraRenderTexture.Release();
DestroyMaterials();
}
[ImageEffectOpaque]
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (outlineShaderMaterial != null)
{
outlineShaderMaterial.SetTexture("_OutlineSource", renderTexture);
if (addLinesBetweenColors)
{
Graphics.Blit(source, extraRenderTexture, outlineShaderMaterial, 0);
outlineShaderMaterial.SetTexture("_OutlineSource", extraRenderTexture);
}
Graphics.Blit(source, destination, outlineShaderMaterial, 1);
}
}
private void CreateMaterialsIfNeeded()
{
if (outlineShader == null)
outlineShader = Resources.Load<Shader>("OutlineShader");
if (outlineBufferShader == null)
{
outlineBufferShader = Resources.Load<Shader>("OutlineBufferShader");
}
if (outlineShaderMaterial == null)
{
outlineShaderMaterial = new Material(outlineShader);
if (fillTexture != null)
{
outlineShaderMaterial.SetTexture("_FillTexture", fillTexture);
}
outlineShaderMaterial.hideFlags = HideFlags.HideAndDontSave;
UpdateMaterialsPublicProperties();
}
if (outlineEraseMaterial == null)
outlineEraseMaterial = CreateMaterial(new Color(0, 0, 0, 0));
if (outline1Material == null)
outline1Material = CreateMaterial(new Color(1, 0, 0, 0));
if (outline2Material == null)
outline2Material = CreateMaterial(new Color(0, 1, 0, 0));
if (outline3Material == null)
outline3Material = CreateMaterial(new Color(0, 0, 1, 0));
}
private void DestroyMaterials()
{
foreach (Material m in materialBuffer)
DestroyImmediate(m);
materialBuffer.Clear();
DestroyImmediate(outlineShaderMaterial);
DestroyImmediate(outlineEraseMaterial);
DestroyImmediate(outline1Material);
DestroyImmediate(outline2Material);
DestroyImmediate(outline3Material);
outlineShader = null;
outlineBufferShader = null;
outlineShaderMaterial = null;
outlineEraseMaterial = null;
outline1Material = null;
outline2Material = null;
outline3Material = null;
}
public void UpdateMaterialsPublicProperties()
{
if (outlineShaderMaterial)
{
float scalingFactor = 1;
if (scaleWithScreenSize)
{
// If Screen.height gets bigger, outlines gets thicker
scalingFactor = Screen.height / 360.0f;
}
// If scaling is too small (height less than 360 pixels), make sure you still render the outlines, but render them with 1 thickness
if (scaleWithScreenSize && scalingFactor < 1)
{
if (UnityEngine.XR.XRSettings.isDeviceActive && sourceCamera.stereoTargetEye != StereoTargetEyeMask.None)
{
outlineShaderMaterial.SetFloat("_LineThicknessX", (1 / 1000.0f) * (1.0f / UnityEngine.XR.XRSettings.eyeTextureWidth) * 1000.0f);
outlineShaderMaterial.SetFloat("_LineThicknessY", (1 / 1000.0f) * (1.0f / UnityEngine.XR.XRSettings.eyeTextureHeight) * 1000.0f);
}
else
{
outlineShaderMaterial.SetFloat("_LineThicknessX", (1 / 1000.0f) * (1.0f / Screen.width) * 1000.0f);
outlineShaderMaterial.SetFloat("_LineThicknessY", (1 / 1000.0f) * (1.0f / Screen.height) * 1000.0f);
}
}
else
{
if (UnityEngine.XR.XRSettings.isDeviceActive && sourceCamera.stereoTargetEye != StereoTargetEyeMask.None)
{
outlineShaderMaterial.SetFloat("_LineThicknessX", scalingFactor * (lineThickness / 1000.0f) * (1.0f / UnityEngine.XR.XRSettings.eyeTextureWidth) * 1000.0f);
outlineShaderMaterial.SetFloat("_LineThicknessY", scalingFactor * (lineThickness / 1000.0f) * (1.0f / UnityEngine.XR.XRSettings.eyeTextureHeight) * 1000.0f);
}
else
{
outlineShaderMaterial.SetFloat("_LineThicknessX", scalingFactor * (lineThickness / 1000.0f) * (1.0f / Screen.width) * 1000.0f);
outlineShaderMaterial.SetFloat("_LineThicknessY", scalingFactor * (lineThickness / 1000.0f) * (1.0f / Screen.height) * 1000.0f);
}
}
outlineShaderMaterial.SetFloat("_LineIntensity", lineIntensity);
outlineShaderMaterial.SetFloat("_FillAmount", fillAmount);
outlineShaderMaterial.SetColor("_FillColor", fillColor);
outlineShaderMaterial.SetFloat("_UseFillColor", useFillColor ? 1 : 0);
outlineShaderMaterial.SetFloat("_UseFillTexture", fillTexture != null ? 1 : 0);
outlineShaderMaterial.SetFloat("_FillTextureScaleX", fillTextureScale.x);
outlineShaderMaterial.SetFloat("_FillTextureScaleY", fillTextureScale.y);
outlineShaderMaterial.SetColor("_LineColor1", lineColor0 * lineColor0);
outlineShaderMaterial.SetColor("_LineColor2", lineColor1 * lineColor1);
outlineShaderMaterial.SetColor("_LineColor3", lineColor2 * lineColor2);
if (flipY)
outlineShaderMaterial.SetInt("_FlipY", 1);
else
outlineShaderMaterial.SetInt("_FlipY", 0);
if (!additiveRendering)
outlineShaderMaterial.SetInt("_Dark", 1);
else
outlineShaderMaterial.SetInt("_Dark", 0);
if (cornerOutlines)
outlineShaderMaterial.SetInt("_CornerOutlines", 1);
else
outlineShaderMaterial.SetInt("_CornerOutlines", 0);
Shader.SetGlobalFloat("_OutlineAlphaCutoff", alphaCutoff);
}
}
void UpdateOutlineCameraFromSource()
{
outlineCamera.CopyFrom(sourceCamera);
outlineCamera.renderingPath = RenderingPath.Forward;
outlineCamera.backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
outlineCamera.clearFlags = CameraClearFlags.SolidColor;
outlineCamera.rect = new Rect(0, 0, 1, 1);
outlineCamera.cullingMask = 0;
outlineCamera.targetTexture = renderTexture;
outlineCamera.enabled = false;
#if UNITY_5_6_OR_NEWER
outlineCamera.allowHDR = false;
#else
outlineCamera.hdr = false;
#endif
}
public void AddOutline(Outline outline)
=> outlines.Add(outline);
public void RemoveOutline(Outline outline)
=> outlines.Remove(outline);
}
}