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