| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Fateforge
- {
- // Project the enamel and keycaps toward a common point above the cabinet.
- // Button hit regions stay rectangular, including the front lip.
- [DisallowMultipleComponent]
- public sealed class ConsoleFace : BaseMeshEffect
- {
- public RectTransform cabinet;
- public float lip;
- readonly List<UIVertex> face = new List<UIVertex>();
- readonly List<UIVertex> layers = new List<UIVertex>();
- public static void Fit(Graphic graphic, RectTransform cabinet, float lip = 0)
- {
- if (!graphic.TryGetComponent<ConsoleFace>(out var effect))
- effect = graphic.gameObject.AddComponent<ConsoleFace>();
- effect.cabinet = cabinet;
- effect.lip = lip;
- graphic.SetVerticesDirty();
- }
- public override void ModifyMesh(VertexHelper mesh)
- {
- if (!IsActive() || !cabinet) return;
- var rect = graphic.rectTransform.rect;
- if (rect.height <= lip) return;
- float axis = transform.InverseTransformPoint(cabinet.position).x;
- face.Clear();
- mesh.GetUIVertexStream(face);
- for (int i = 0; i < face.Count; i++)
- {
- var vertex = face[i];
- float far = Mathf.InverseLerp(rect.yMin, rect.yMax, vertex.position.y);
- vertex.position.x = Mathf.Lerp(vertex.position.x, axis, far * rect.height / 950f);
- vertex.position.y += lip * (1f - far);
- face[i] = vertex;
- }
- mesh.Clear();
- if (lip > 0)
- {
- layers.Clear();
- for (int step = 2; step >= 1; step--)
- {
- var shade = step == 2 ? new Color(.22f, .13f, .10f) : new Color(.50f, .32f, .21f);
- foreach (var front in face)
- {
- var side = front;
- side.position.y -= lip * step / 2f;
- side.color = (Color)front.color * shade;
- layers.Add(side);
- }
- }
- layers.AddRange(face);
- mesh.AddUIVertexTriangleStream(layers);
- }
- else mesh.AddUIVertexTriangleStream(face);
- }
- }
- }
|