| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | [RequireComponent(typeof(Camera))] |
| | 9 | | public class BIWOutline : MonoBehaviour |
| | 10 | | { |
| | 11 | |
|
| | 12 | | [SerializeField] Material OutlineMaterial = null; |
| | 13 | |
|
| | 14 | | private Camera builderCamera; |
| | 15 | |
|
| | 16 | | private Camera outlineCamera; |
| | 17 | | private Canvas outlineCanvas; |
| | 18 | | private RawImage outlineRawImage; |
| | 19 | |
|
| | 20 | | private int lastScreenWidth = 0; |
| | 21 | | private int lastScreenHeight = 0; |
| | 22 | |
|
| | 23 | | private void Awake() |
| | 24 | | { |
| 6 | 25 | | builderCamera = GetComponent<Camera>(); |
| | 26 | |
|
| 6 | 27 | | outlineCamera = new GameObject("BuilderOutlineCamera").AddComponent<Camera>(); |
| 6 | 28 | | outlineCamera.CopyFrom(builderCamera); |
| 6 | 29 | | outlineCamera.clearFlags = CameraClearFlags.SolidColor; |
| 6 | 30 | | outlineCamera.backgroundColor = Color.clear; |
| 6 | 31 | | outlineCamera.cullingMask = BIWSettings.SELECTION_LAYER; |
| 6 | 32 | | outlineCamera.depth = builderCamera.depth - 1; |
| 6 | 33 | | outlineCamera.transform.SetParent(builderCamera.transform); |
| | 34 | |
|
| 6 | 35 | | outlineCanvas = new GameObject("BuilderOutlineCanvas").AddComponent<Canvas>(); |
| 6 | 36 | | outlineCanvas.renderMode = RenderMode.ScreenSpaceCamera; |
| 6 | 37 | | outlineCanvas.worldCamera = builderCamera; |
| 6 | 38 | | outlineCanvas.planeDistance = 1; |
| | 39 | |
|
| 6 | 40 | | outlineRawImage = new GameObject("BuilderOutlineRawImage").AddComponent<RawImage>(); |
| 6 | 41 | | outlineRawImage.transform.SetParent(outlineCanvas.transform); |
| 6 | 42 | | outlineRawImage.transform.ResetLocalTRS(); |
| | 43 | |
|
| 6 | 44 | | outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect.hei |
| 6 | 45 | | outlineRawImage.raycastTarget = false; |
| 6 | 46 | | outlineRawImage.material = OutlineMaterial; |
| 6 | 47 | | } |
| | 48 | |
|
| 12 | 49 | | private void OnDestroy() { Dispose(); } |
| | 50 | |
|
| | 51 | | public void Dispose() |
| | 52 | | { |
| 12 | 53 | | if (outlineCanvas != null) |
| 6 | 54 | | Destroy(outlineCanvas.gameObject); |
| 12 | 55 | | if (outlineRawImage != null) |
| 6 | 56 | | Destroy(outlineRawImage.gameObject); |
| 12 | 57 | | if (outlineCamera != null) |
| 6 | 58 | | Destroy(outlineCamera.gameObject); |
| 12 | 59 | | } |
| | 60 | |
|
| | 61 | | public void Activate() |
| | 62 | | { |
| 7 | 63 | | outlineCamera.gameObject.SetActive(true); |
| 7 | 64 | | outlineCanvas.gameObject.SetActive(true); |
| 7 | 65 | | outlineRawImage.gameObject.SetActive(true); |
| 7 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Deactivate() |
| | 69 | | { |
| 1 | 70 | | outlineCamera.gameObject.SetActive(false); |
| 1 | 71 | | outlineCanvas.gameObject.SetActive(false); |
| 1 | 72 | | outlineRawImage.gameObject.SetActive(false); |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | public void SetOutlineMaterial(Material mat) |
| | 76 | | { |
| 6 | 77 | | OutlineMaterial = mat; |
| 6 | 78 | | outlineRawImage.material = OutlineMaterial; |
| 6 | 79 | | } |
| | 80 | |
|
| | 81 | | private void OnResize() |
| | 82 | | { |
| 0 | 83 | | outlineRawImage.gameObject.SetActive(false); // Hack: force Unity to refresh the texture |
| | 84 | |
|
| 0 | 85 | | RenderTexture currentRenderTexture = outlineCamera.targetTexture; |
| 0 | 86 | | if (currentRenderTexture != null) |
| | 87 | | { |
| 0 | 88 | | currentRenderTexture.Release(); |
| 0 | 89 | | Object.Destroy(currentRenderTexture); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | RenderTexture renderTexture = new RenderTexture(Screen.width, Screen.height, 24); |
| 0 | 93 | | renderTexture.antiAliasing = 4; |
| 0 | 94 | | renderTexture.depth = 16; |
| 0 | 95 | | renderTexture.autoGenerateMips = true; |
| 0 | 96 | | renderTexture.useMipMap = true; |
| | 97 | |
|
| 0 | 98 | | outlineCamera.targetTexture = renderTexture; |
| 0 | 99 | | OutlineMaterial.mainTexture = outlineCamera.targetTexture; |
| 0 | 100 | | outlineRawImage.rectTransform.sizeDelta = new Vector2(outlineCanvas.pixelRect.width, outlineCanvas.pixelRect.hei |
| 0 | 101 | | outlineRawImage.gameObject.SetActive(true); |
| 0 | 102 | | } |
| | 103 | |
|
| 0 | 104 | | private void OnPreviewModeChanged(bool isPreview) { outlineCanvas.gameObject.SetActive(!isPreview); } |
| | 105 | |
|
| | 106 | | private void Update() |
| | 107 | | { |
| 0 | 108 | | if ((Screen.width > 0 && Screen.height > 0) && (lastScreenWidth != Screen.width || lastScreenHeight != Screen.he |
| | 109 | | { |
| 0 | 110 | | lastScreenWidth = Screen.width; |
| 0 | 111 | | lastScreenHeight = Screen.height; |
| 0 | 112 | | OnResize(); |
| | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | } |