using System.Collections.Generic; using UnityEngine; namespace Auralis { // Split the actual original into disjoint render pieces. Existing Polygon/Box // surfaces are READ here, never resized or replaced. Retained source pixels // occur once; Kiln C replaces the near tread volumes with separate stone faces. public sealed class DungeonEarlyDescentLayers : MonoBehaviour { public SpriteRenderer Original { get; private set; } public readonly List TreadRects=new(); public readonly List BackgroundRects=new(); public readonly List ReplacedSourceRects=new(); public readonly List PreservedRailRects=new(); public readonly List MaterialPieces=new(); public readonly List LadderPieces=new(); public float SourceScale=>scale; public int SourcePixelArea { get; private set; } // Original pixels still rendered. Removed pixels are accounted separately; // replacement atlas samples are not passed off as preserved painting pixels. public int RegisteredArea { get; private set; } public int ReplacedSourcePixelArea { get; private set; } // Sum of sampled crop areas, including repeated atlas regions, not unique texels. public float MaterialSampledPixelArea { get; private set; } public float MaterialWorldArea { get; private set; } public const float KilnMaterialScale=.0085f; // The natural deck/front join is the physical edge; the extra rear face // uses plain masonry grain and never advertises another standing edge. public static readonly Rect KilnFrontRegion=new(851,334,435,83); public static readonly Rect KilnDeckRegion=new(49,546,400,52); public const float KilnContactRow=347; public Texture2D KilnSourceTexture { get; private set; } Mesh mesh;Material material; Texture2D kilnMaterial; DungeonEarlyArchitecture owner;Vector2 anchor,origin;float scale; readonly struct TreadCut { public readonly Rect Area; public readonly bool Deck; public readonly float ContactY; public TreadCut(Rect area,bool deck=false,float? contactY=null){Area=area;Deck=deck;ContactY=contactY??area.yMin;} } public static void Create(Transform parent,SpriteRenderer original,string family,string room,Vector2 sourceAnchor,Vector2 worldAnchor,float sourceScale) { if(!GraphicsRolloutMode.Enabled||!original||!original.sprite)return; var go=new GameObject("Einzeln registrierte historische Stufen · "+family);go.transform.SetParent(parent,false); var c=go.AddComponent();c.Original=original;c.anchor=sourceAnchor;c.origin=worldAnchor;c.scale=sourceScale; // Room-level context also owns the existing later-created door frames // and optional ordinary platforms, which are siblings of this descent. c.owner=DungeonEarlyArchitecture.AttachDescent(parent.parent?parent.parent:parent,family,room); c.Build(parent); } Vector2 Pixel(Vector2 world)=>new(anchor.x+(world.x-origin.x)/scale,anchor.y-(world.y-origin.y)/scale); Vector2 World(Vector2 pixel)=>origin+new Vector2(pixel.x-anchor.x,anchor.y-pixel.y)*scale; void Build(Transform parent) { var texture=Original.sprite.texture;var cuts=new List(); if(owner.Family=="sooty-bell-kiln"&&owner.RoomId=="C") { KilnSourceTexture=Resources.Load("GameArt/DungeonEarlyKilnKit"); if(KilnSourceTexture)BuildKilnMaterial(); } foreach(var p in parent.GetComponentsInChildren()) { var polygon=p.GetComponent();if(!polygon||polygon.pathCount==0)continue; var path=polygon.GetPath(0);int edgeCount=path.Length/2; for(int i=0;i.04f||Mathf.Abs(a.x-b.x)<.5f)continue; float width=Mathf.Abs(a.x-b.x),depth=18; if(kilnMaterial&&width*scale<2) { // Each short tread front ends at its lower neighbour. A full // gallery cornice would otherwise overlap the following step. float lower=0; if(i>0)lower=Mathf.Max(lower,Pixel(polygon.transform.TransformPoint(path[i-1]+polygon.offset)).y-a.y); if(i+2.04f)depth=Mathf.Min(depth,lower); } if(kilnMaterial) { float nearDeck=width*scale<2?3:2; cuts.Add(new TreadCut(new Rect(Mathf.Min(a.x,b.x),a.y-nearDeck,width,depth+nearDeck),false,a.y)); // The full old stone projects farther back than the atlas's // small bevel. Complete that depth with a separate deck, // not another bright ledge or a stretched bevel. if(width*scale<2) cuts.Add(new TreadCut(new Rect(Mathf.Min(a.x,b.x),a.y-12,width,12-nearDeck),true)); // The last painted stair has a rear slab over the start of // the middle landing. Its front remains the flat y398 bank. else if(Mathf.Abs(a.y-398)<.1f&&Mathf.Min(a.x,b.x)<1083) cuts.Add(new TreadCut(new Rect(1082,386,23,10),true)); } else cuts.Add(new TreadCut(new Rect(Mathf.Min(a.x,b.x),a.y,width,depth))); } } foreach(var p in parent.GetComponentsInChildren()) { if(p.gameObject.layer!=6||!p.GetComponent()||p.isTrigger)continue; var left=Pixel(new Vector2(p.bounds.min.x,p.bounds.max.y)); var right=Pixel(new Vector2(p.bounds.max.x,p.bounds.max.y)); cuts.Add(kilnMaterial?new TreadCut(new Rect(left.x,left.y-2,right.x-left.x,20),false,left.y) :new TreadCut(new Rect(left.x,left.y,right.x-left.x,25))); } var remaining=new List{new(0,0,texture.width,texture.height)}; foreach(var raw in cuts) { var area=raw.Area; var cut=Rect.MinMaxRect(Mathf.Round(area.xMin),Mathf.Round(area.yMin),Mathf.Round(area.xMax),Mathf.Round(area.yMax)); var visibleCuts=new List{cut}; if(kilnMaterial) foreach(var rail in PreservedRailRects)visibleCuts=Subtract(visibleCuts,rail); foreach(var visibleCut in visibleCuts) { var next=new List(); foreach(var r in remaining) { float l=Mathf.Max(r.xMin,visibleCut.xMin),t=Mathf.Max(r.yMin,visibleCut.yMin),right=Mathf.Min(r.xMax,visibleCut.xMax),bottom=Mathf.Min(r.yMax,visibleCut.yMax); if(right<=l||bottom<=t){next.Add(r);continue;} Add(next,r.xMin,r.yMin,r.width,t-r.yMin);Add(next,r.xMin,bottom,r.width,r.yMax-bottom); Add(next,r.xMin,t,l-r.xMin,bottom-t);Add(next,right,t,r.xMax-right,bottom-t); var s=new Rect(l,t,right-l,bottom-t); // Only a fragment containing the physical front line owns // contact. A projected rear face or lower fascia does not. bool actualTop=kilnMaterial?!raw.Deck&&t<=raw.ContactY+.01f&&bottom>raw.ContactY+.01f :Mathf.Abs(t-cut.yMin)<.01f; if(kilnMaterial) { ReplacedSourceRects.Add(s); ReplacedSourcePixelArea+=Mathf.RoundToInt(s.width*s.height); KilnFace(s,cut,raw.ContactY,actualTop,raw.Deck); } else { TreadRects.Add(s); owner.Slice("Originale einzelne Trittfront",texture,s,new Vector2(l,t),World(new Vector2(l,t)),scale,-2,Original.color,actualTop); } } remaining=next; } } BackgroundRects.AddRange(remaining);SourcePixelArea=texture.width*texture.height; foreach(var r in TreadRects)RegisteredArea+=Mathf.RoundToInt(r.width*r.height); foreach(var r in remaining)RegisteredArea+=Mathf.RoundToInt(r.width*r.height); var vertices=new List();var uv=new List();var triangles=new List(); foreach(var r in remaining) { int n=vertices.Count; vertices.Add(World(new Vector2(r.xMin,r.yMin)));vertices.Add(World(new Vector2(r.xMax,r.yMin))); vertices.Add(World(new Vector2(r.xMin,r.yMax)));vertices.Add(World(new Vector2(r.xMax,r.yMax))); uv.Add(new(r.xMin/texture.width,1-r.yMin/texture.height));uv.Add(new(r.xMax/texture.width,1-r.yMin/texture.height)); uv.Add(new(r.xMin/texture.width,1-r.yMax/texture.height));uv.Add(new(r.xMax/texture.width,1-r.yMax/texture.height)); triangles.Add(n);triangles.Add(n+1);triangles.Add(n+2);triangles.Add(n+1);triangles.Add(n+3);triangles.Add(n+2); } mesh=new Mesh{name="Unveränderte Hinterraumpixel zwischen originalen Tritten"};mesh.SetVertices(vertices);mesh.SetUVs(0,uv);mesh.SetTriangles(triangles,0);mesh.RecalculateBounds(); var colors=new Color[vertices.Count];for(int i=0;i().sharedMesh=mesh; var mr=gameObject.AddComponent();mr.sharedMaterial=material;mr.sortingOrder=Original.sortingOrder; // Mesh vertices use original world coordinates. All descent parents have // identity transforms; explicitly keep this render node at world zero. transform.position=Vector3.zero;Original.enabled=false; } static void Add(List r,float x,float y,float w,float h){if(w>.001f&&h>.001f)r.Add(new Rect(x,y,w,h));} static List Subtract(List regions,Rect hole) { var result=new List(); foreach(var r in regions) { float l=Mathf.Max(r.xMin,hole.xMin),t=Mathf.Max(r.yMin,hole.yMin),right=Mathf.Min(r.xMax,hole.xMax),bottom=Mathf.Min(r.yMax,hole.yMax); if(right<=l||bottom<=t){result.Add(r);continue;} Add(result,r.xMin,r.yMin,r.width,t-r.yMin);Add(result,r.xMin,bottom,r.width,r.yMax-bottom); Add(result,r.xMin,t,l-r.xMin,bottom-t);Add(result,right,t,r.xMax-right,bottom-t); } return result; } void BuildKilnMaterial() { // A local material response, at the original atlas resolution. Lift // charcoal shadows into muted grey-brown without clipping the grain; // source art, alpha, dimensions, filtering and texel density stay intact. var pixels=KilnSourceTexture.GetPixels32(); for(int i=0;i.001f) { float columns=Mathf.Min(remaining,region.width-phase); if(columns<=.001f){phase=0;continue;} var crop=new Rect(region.x+phase,top,columns,rows); var color=deck?new Color(.98f,.95f,.90f):new Color(.89f,.86f,.80f); var sourceAnchor=new Vector2(crop.x,walkEdge?KilnContactRow:crop.y); var worldAnchor=walkEdge?new Vector2(at.x,World(new Vector2(source.xMin,contactY)).y):at; var piece=owner.Slice(deck?"Glockenofen C · rückwärtige Trittoberseite":"Glockenofen C · Trittvolumen mit physischer Vorderkante",kilnMaterial,crop, sourceAnchor,worldAnchor,KilnMaterialScale,-2,color,walkEdge); MaterialPieces.Add(piece); MaterialSampledPixelArea+=columns*rows; MaterialWorldArea+=columns*rows*KilnMaterialScale*KilnMaterialScale; at.x+=columns*KilnMaterialScale;remaining-=columns;phase=0; } } public static void ReplaceShortLadder(ClimbableLadder ladder,Vector2 bottom,Vector2 top,Transform parent) { if(!GraphicsRolloutMode.Enabled||!ladder)return; var owner=parent.GetComponentInParent();if(!owner)return; foreach(var sr in ladder.GetComponentsInChildren())sr.enabled=false; foreach(var lr in ladder.GetComponentsInChildren())lr.enabled=false; // Original copper atlas: rail centres161/333.5 and rung594. All pieces // share the same .8/172.5 scale; neither rail nor rung is stretched. const float s=.8f/172.5f;float height=top.y-bottom.y; var texture=Resources.Load("GameArt/DungeonMillArchitectureKit");var copper=new Color(.66f,.61f,.52f); var registered=parent.GetComponentInChildren(); for(int rail=0;rail<2;rail++) { float x=rail==0?145:317,w=rail==0?32:33,y=top.y,remaining=height; while(remaining>.0001f) { float h=Mathf.Min(30*s,remaining); var p=owner.Slice("Wartungsleiter · gleichmäßiger Kupferholm",texture,new Rect(x,548,w,h/s),new Vector2(rail==0?161:333.5f,548),new Vector2(top.x+(rail==0?-.4f:.4f),y),s,6,copper); if(registered)registered.LadderPieces.Add(p);remaining-=h;y-=h; } } for(float y=bottom.y+.12f;y