using System.Numerics; using Microsoft.UI.Composition; using Windows.UI; namespace XFEEDToolBox.Controls; /// Retained sky at infinity. It rotates with the camera, never follows /// map translation/zoom, and has no hit targets or continuously running animation. internal sealed class GalaxySkybox { private readonly ContainerVisual root; private readonly SpriteVisual background; private readonly ShapeVisual dots,clouds; private readonly (Vector3 Direction,CompositionSpriteShape Shape)[] stars; private readonly (Vector3 Direction,CompositionEllipseGeometry Geometry,CompositionSpriteShape Shape)[] nebulae; private double lastYaw=double.NaN,lastPitch,lastWidth,lastHeight; internal Visual Visual=>root; internal int Nodes=>stars.Length+nebulae.Length+3; internal long Rebuilds { get; private set; } internal GalaxySkybox(Compositor compositor) { root=compositor.CreateContainerVisual();background=compositor.CreateSpriteVisual(); var night=compositor.CreateLinearGradientBrush();night.StartPoint=new(0,0);night.EndPoint=new(1,1); night.ColorStops.Add(compositor.CreateColorGradientStop(0,Color.FromArgb(255,3,7,17))); night.ColorStops.Add(compositor.CreateColorGradientStop(.55f,Color.FromArgb(255,8,15,29))); night.ColorStops.Add(compositor.CreateColorGradientStop(1,Color.FromArgb(255,4,9,20)));background.Brush=night; clouds=compositor.CreateShapeVisual();dots=compositor.CreateShapeVisual(); root.Children.InsertAtBottom(background);root.Children.InsertAtTop(clouds);root.Children.InsertAtTop(dots); stars=new (Vector3,CompositionSpriteShape)[900]; var brushes=new[]{compositor.CreateColorBrush(Color.FromArgb(150,138,171,207)),compositor.CreateColorBrush(Color.FromArgb(180,216,223,243)),compositor.CreateColorBrush(Color.FromArgb(125,242,203,171))}; for(var i=0;iwidth||py>height){shape.Scale=Vector2.Zero;continue;} shape.Offset=new((float)px,(float)py);shape.Scale=Vector2.One; } foreach(var (direction,geometry,shape) in nebulae) { var (x,y,z)=View(direction); if(z<.2){shape.Scale=Vector2.Zero;continue;} var extent=Math.Min(1.2,.6/z);geometry.Radius=new((float)(focal*extent),(float)(focal*extent*.22)); shape.Offset=new((float)(width/2+x/z*focal),(float)(height/2-y/z*focal)); var tangent=new Vector3(-direction.Z,0,direction.X);var (tx,ty,tz)=View(tangent); shape.RotationAngleInDegrees=(float)(Math.Atan2(-(ty*z-y*tz),tx*z-x*tz)*180/Math.PI);shape.Scale=Vector2.One; } (double X,double Y,double Z) View(Vector3 p) {var depth=-p.X*sy+p.Z*cy;return(p.X*cy+p.Z*sy,p.Y*cp-depth*sp,p.Y*sp+depth*cp);} } }