using System.Collections.Generic; using UnityEngine; namespace Auralis { public sealed class RoomArchitectureCutout:MonoBehaviour { public SpriteRenderer Source{get;private set;} public Material Material{get;private set;} public Texture2D Mask{get;private set;} public static RoomArchitectureCutout Attach(SpriteRenderer source,IReadOnlyList banks) { var mask=Resources.Load("GameArt/"+source.sprite.texture.name+"ArchitectureLayerV1"); if(!mask)return null; var cut=source.gameObject.AddComponent();cut.Source=source;cut.Mask=mask; cut.Material=new Material(Resources.Load("Materials/RoomArchitectureMask")); cut.Material.SetTexture("_ArchitectureMask",mask);source.sharedMaterial=cut.Material; // Only these expanded rooms open above their old painting. Blend // its upper 123 source pixels into the existing rear scenery while // retaining the cutout, source registration and original lighting. string sheet=source.sprite.texture.name; if(sheet=="ObservatoryNorth"||sheet=="ArchiveMemoryMorning")cut.Material.SetFloat("_UpperFadeHeight",.12f); if(sheet=="ArchiveMemoryEvening") { // Solid original walls hide the unrelated second library below // the canvas edge; only 41 top pixels join the taller rear vault. // Source registration and all supports remain unchanged. cut.Material.SetTexture("_ArchitectureMask",Texture2D.whiteTexture); cut.Material.SetFloat("_UpperFadeHeight",.04f); } // The garden already partitions the original into fixed front/back // crops. Keep those source UVs and apply the same cutout to each crop. foreach(var garden in source.transform.parent.GetComponentsInChildren(true)) if(garden.Original==source)foreach(var piece in garden.Pieces)piece.sharedMaterial=cut.Material; var bands=new List(); foreach(var bank in banks) { if(bands.Count==16)break; var center=bank.transform.TransformPoint(bank.offset);var size=Vector3.Scale(bank.size,bank.transform.lossyScale); var b=new Bounds(center,new Vector3(Mathf.Abs(size.x),Mathf.Abs(size.y),.1f));if(b.size.x<=0)continue; Vector2 lo=cut.Uv(new Vector2(b.min.x,b.max.y-.07f)),hi=cut.Uv(new Vector2(b.max.x,b.max.y+.035f)); bands.Add(new Vector4(lo.x,lo.y,hi.x,hi.y)); } // Narrative murals and decorated glass are physical objects, not distant sky. // Explicit source rectangles protect their complete illustrations from masks. foreach(var rect in StoryPanels(source.sprite.texture.name)) if(bands.Count<16)bands.Add(new Vector4(rect.x/1536,(1024-rect.yMax)/1024,rect.xMax/1536,(1024-rect.y)/1024)); cut.Material.SetInt("_BandCount",bands.Count);if(bands.Count>0)cut.Material.SetVectorArray("_ProtectedBands",bands);return cut; } static Rect[] StoryPanels(string key) { switch(key) { case "ArchiveMemoryGlobe":return new[]{new Rect(552,0,414,710)}; case "ArchiveMemoryReading":return new[]{new Rect(585,316,365,282)}; case "ArchiveMemoryMorning":return new[]{new Rect(320,0,420,344)}; case "ArchiveMemoryCrossing":return new[]{new Rect(564,0,412,672)}; case "ArchiveMemoryStoreEntry":return new[]{new Rect(277,0,249,254),new Rect(1006,0,267,254)}; case "ArchiveMemoryConnected":return new[]{new Rect(554,0,420,290),new Rect(540,348,470,289)}; default:return System.Array.Empty(); } } Vector2 Uv(Vector2 world) { var sprite=Source.sprite;var p=(Vector2)Source.transform.InverseTransformPoint(world)*sprite.pixelsPerUnit+sprite.pivot+sprite.rect.position; return new Vector2(p.x/sprite.texture.width,p.y/sprite.texture.height); } void OnDestroy(){if(Material)Destroy(Material);} } }