Beispiel #1
0
        public ShadowRenderingRequest(TrueShadow shadow)
        {
            this.shadow = shadow;

            var casterScale = 1f;

            try
            {
                casterScale = shadow.Graphic.canvas.scaleFactor;
            }
            catch (NullReferenceException) { }

            shadowSize   = shadow.Size * casterScale;
            shadowOffset = shadow.Offset.Rotate(-shadow.RectTransform.eulerAngles.z) * casterScale;
            rect         = shadow.RectTransform.rect;
            dimensions   = rect.size * casterScale;

            // Tiled type cannot be batched by similar size
            int dimensionHash = shadow.Graphic is Image image && image.type == Image.Type.Tiled
                                ? dimensions.GetHashCode()
                                : HashUtils.CombineHashCodes(
                Mathf.CeilToInt(dimensions.x / DIMENSIONS_HASH_STEP) * DIMENSIONS_HASH_STEP,
                Mathf.CeilToInt(dimensions.y / DIMENSIONS_HASH_STEP) * DIMENSIONS_HASH_STEP
                );

            hash = HashUtils.CombineHashCodes(
                Mathf.CeilToInt(shadowSize * 100),
                dimensionHash,
                shadow.ContentHash
                );
        }
        void Reset()
        {
            shadow = FindTrueShadow();
            if (shadow)
            {
                normalSize     = shadow.Size;
                normalDistance = shadow.OffsetDistance;
                normalColor    = shadow.Color;

                // Clicked UI remain selected, which is unwanted. Selected state is probably most useful on console
                // and keyboard nav, the later is rather hard to detect
                bool selectedIsNormal = Input.mousePresent || Input.touchSupported;
                autoDeselect = selectedIsNormal;

                hoverSize    = Round(Min(normalSize * 1.75f, normalSize + 20f));
                selectedSize = selectedIsNormal ? normalSize : hoverSize;
                clickedSize  = Round(Min(normalSize * 1.25f, normalSize + 15f));

                hoverDistance    = Round(Min(normalDistance * 1.5f, normalDistance + 20f));
                selectedDistance = selectedIsNormal ? normalDistance : hoverDistance;
                clickedDistance  = Round(Min(normalDistance * 1.25f, normalDistance + 15f));


                hoverColor    = Color.Lerp(normalColor, FADED_COLOR, .15f);
                selectedColor = selectedIsNormal ? normalColor : hoverColor;
                clickedColor  = Color.Lerp(normalColor, FADED_COLOR, .25f);
            }
        }
        void OnEnable()
        {
            shadow     = FindTrueShadow();
            selectable = GetComponent <Selectable>();

            targetSize     = normalSize = shadow.Size;
            targetDistance = normalDistance = shadow.OffsetDistance;
            targetColor    = normalColor = shadow.Color;

            shadow.Size           = targetSize = normalSize;
            shadow.OffsetDistance = targetDistance = normalDistance;
        }
        void AddSortEntry(TrueShadow shadow)
        {
            var entry    = new SortEntry(shadow);
            var group    = new SortGroup(entry);
            var oldIndex = sortGroups.IndexOf(group);

            if (oldIndex > -1)
            {
                sortGroups[oldIndex].Add(entry);
            }
            else
            {
                sortGroups.Add(group);
            }
        }
        public static void Initialize(TrueShadow shadow, ref ShadowRenderer renderer)
        {
            if (renderer && renderer.shadow == shadow)
            {
                renderer.gameObject.SetActive(true);
                return;
            }

            var obj = new GameObject($"{shadow.gameObject.name}'s Shadow")
            {
#if LETAI_TRUESHADOW_DEBUG
                hideFlags = DebugSettings.Instance.showObjects
                            ? HideFlags.DontSave
                            : HideFlags.HideAndDontSave
#else
                hideFlags = HideFlags.HideAndDontSave
#endif
            };

#if LETAI_TRUESHADOW_DEBUG && UNITY_EDITOR
            UnityEditor.SceneVisibilityManager.instance.DisablePicking(obj, true);
#endif

            shadow.SetHierachyDirty();

            var rt = obj.AddComponent <RectTransform>();
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.zero;

            var graphic = obj.AddComponent <RawImage>();
            graphic.raycastTarget = false;
            graphic.color         = shadow.Color;

            renderer         = obj.AddComponent <ShadowRenderer>();
            renderer.shadow  = shadow;
            renderer.rt      = rt;
            renderer.graphic = graphic;

            // renderer.RecreateGraphic(shadow.Baked ? GraphicType.Image : GraphicType.RawImage);

            renderer.UpdateMaterial();

            renderer.CanvasRenderer = obj.GetComponent <CanvasRenderer>();
            renderer.CanvasRenderer.SetColor(shadow.IgnoreCasterColor ? Color.white : shadow.CanvasRenderer.GetColor());
            renderer.CanvasRenderer.SetAlpha(shadow.CanvasRenderer.GetAlpha());

            renderer.ReLayout();
        }
        internal ShadowSettingSnapshot(TrueShadow shadow)
        {
            this.shadow = shadow;
            canvas      = shadow.Graphic.canvas;
            canvasRt    = (RectTransform)canvas.transform;

            var meshBound = shadow.SpriteMesh.bounds;

            shouldAntialiasImprint = canvas.renderMode != RenderMode.ScreenSpaceOverlay;

            canvasScale = canvas.scaleFactor;

            var canvasRelativeRotation = Quaternion.Inverse(canvasRt.rotation) * shadow.RectTransform.rotation;

            canvasRelativeOffset = shadow.Offset.Rotate(-canvasRelativeRotation.eulerAngles.z) * canvasScale;

            dimensions = (Vector2)meshBound.size * canvasScale;
            size       = shadow.Size * canvasScale;

            CalcHash();
        }
 public SortEntry(TrueShadow shadow)
 {
     this.shadow       = shadow;
     shadowTransform   = shadow.transform;
     rendererTransform = shadow.shadowRenderer.transform;
 }
 public void UnRegister(TrueShadow shadow)
 {
     shadows.Remove(shadow);
 }
 public void Register(TrueShadow shadow)
 {
     shadows.AddUnique(shadow);
 }
        public static void Initialize(TrueShadow shadow, ref ShadowRenderer renderer)
        {
            if (renderer && renderer.shadow == shadow)
            {
                renderer.gameObject.SetActive(true);
                return;
            }

            var obj = new GameObject($"{shadow.gameObject.name}'s Shadow")
            {
#if LETAI_TRUESHADOW_DEBUG
                hideFlags = DebugSettings.Instance.showObjects
                            ? HideFlags.DontSave
                            : HideFlags.HideAndDontSave
#else
                hideFlags = HideFlags.HideAndDontSave
#endif
            };

            shadow.SetHierachyDirty();

            var rt = obj.AddComponent <RectTransform>();

            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.zero;

            Graphic graphic;
            var     type = shadow.Baked ? GraphicType.Image : GraphicType.RawImage;

            switch (type)
            {
            case GraphicType.Image:
                var image = obj.AddComponent <Image>();
                image.useSpriteMesh = true;

                graphic = image;
                break;

            case GraphicType.RawImage:
                graphic = obj.AddComponent <RawImage>();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            graphic.raycastTarget = false;
            graphic.color         = shadow.Color;

            renderer         = obj.AddComponent <ShadowRenderer>();
            renderer.shadow  = shadow;
            renderer.rt      = rt;
            renderer.graphic = graphic;

            // renderer.RecreateGraphic(shadow.Baked ? GraphicType.Image : GraphicType.RawImage);

            renderer.UpdateMaterial();

            renderer.CanvasRenderer = obj.GetComponent <CanvasRenderer>();
            renderer.CanvasRenderer.SetColor(shadow.IgnoreCasterColor ? Color.white : shadow.CanvasRenderer.GetColor());
            renderer.CanvasRenderer.SetAlpha(shadow.CanvasRenderer.GetAlpha());

            renderer.ReLayout();
        }