66 */
77
88public final class PerformanceRenderingSystem : RenderingSystem {
9+ let improveLegibility : Bool
10+
911 public override func setup( context: ECSContext ) {
10- context. recordPerformance ( )
12+ context. setRecordPerformance ( true )
13+ }
14+
15+ public override func teardown( context: ECSContext ) {
16+ context. setRecordPerformance ( false )
1117 }
1218
13- lazy var text : Text = Text ( string: " Accumulating Statistics... \n Loading Resources: \( game. resourceManager. currentlyLoading) " , pointSize: 20 , color: . green)
19+ lazy var text : Text = Text (
20+ string: " Accumulating Statistics... " ,
21+ pointSize: improveLegibility ? 14 : 10 ,
22+ style: . bold,
23+ color: . gray
24+ )
1425 func rebuildText( ) -> Bool {
1526 let performance = context. performance!
1627 let systemsFrameTime = performance. systemsFrameTime
@@ -33,50 +44,73 @@ public final class PerformanceRenderingSystem: RenderingSystem {
3344 string += " \n \( String ( format: " %02d%% " , Int ( ( renderingSystemsFrameTime / totalSystemTime) * 100 ) ) ) Rendering Systems "
3445 string += " \n \( String ( format: " %02d%% " , Int ( ( systemsFrameTime / totalSystemTime) * 100 ) ) ) Systems \n "
3546 string += " \n Loading: \( game. resourceManager. currentlyLoading. count) "
36- string += " \n Loaded: "
47+ if game. resourceManager. currentlyLoading. isEmpty == false {
48+ string += " \n \t "
49+ string += game. resourceManager. currentlyLoading. joined ( separator: " , \n \t " )
50+ }
51+ string += " \n Loaded: \n \t "
3752 var loadedThingsCount = 0
3853 func loadedThingsLine( ) {
39- if loadedThingsCount > 3 {
54+ if loadedThingsCount >= 3 {
4055 loadedThingsCount = 0
41- string += " , \n "
56+ string += " , \n \t "
4257 } else {
4358 string += " , "
4459 }
4560 }
46- if context. entities. isEmpty == false {
47- string += " \( context. entities. count) Entities "
48- loadedThingsCount += 1
49- }
50- loadedThingsLine ( )
61+
62+ string += " \( context. entities. count) Entities "
63+ loadedThingsCount += 1
64+
5165 if game. resourceManager. cache. geometries. isEmpty == false || game. resourceManager. cache. skinnedGeometries. isEmpty == false {
66+ loadedThingsLine ( )
5267 string += " \( game. resourceManager. cache. geometries. count + game. resourceManager. cache. skinnedGeometries. count) Geometry "
5368 loadedThingsCount += 1
5469 }
55- loadedThingsLine ( )
70+
71+ if game. resourceManager. cache. collisionMeshes. isEmpty == false {
72+ loadedThingsLine ( )
73+ string += " \( game. resourceManager. cache. collisionMeshes. count) Collision "
74+ loadedThingsCount += 1
75+ }
76+
5677 if game. resourceManager. cache. textures. isEmpty == false {
78+ loadedThingsLine ( )
5779 string += " \( game. resourceManager. cache. textures. count) Texture "
5880 loadedThingsCount += 1
5981 }
60- loadedThingsLine ( )
82+
6183 if game. resourceManager. cache. skeletons. isEmpty == false {
84+ loadedThingsLine ( )
6285 string += " \( game. resourceManager. cache. skeletons. count) Skeleton "
6386 loadedThingsCount += 1
6487 }
65- loadedThingsLine ( )
88+
6689 if game. resourceManager. cache. skeletalAnimations. isEmpty == false {
67- string += " \( game. resourceManager. cache. skeletalAnimations. count) SkeleltalAnimation "
90+ loadedThingsLine ( )
91+ string += " \( game. resourceManager. cache. skeletalAnimations. count) SkelAnim "
92+ loadedThingsCount += 1
93+ }
94+
95+ if game. resourceManager. cache. objectAnimation3Ds. isEmpty == false {
96+ loadedThingsLine ( )
97+ string += " \( game. resourceManager. cache. objectAnimation3Ds. count) ObjAnim3D "
6898 loadedThingsCount += 1
6999 }
70- loadedThingsLine ( )
100+
71101 if game. resourceManager. cache. tileSets. isEmpty == false {
102+ loadedThingsLine ( )
72103 string += " \( game. resourceManager. cache. tileSets. count) TileSet "
73104 loadedThingsCount += 1
74105 }
75- loadedThingsLine ( )
106+
76107 if game. resourceManager. cache. tileMaps. isEmpty == false {
108+ loadedThingsLine ( )
77109 string += " \( game. resourceManager. cache. tileMaps. count) TileMap "
78110 loadedThingsCount += 1
79111 }
112+
113+
80114 string += " \n "
81115
82116 for statistic in performance. averageSortedStatistics ( ) {
@@ -91,30 +125,98 @@ public final class PerformanceRenderingSystem: RenderingSystem {
91125
92126 let tickDuration : Float = 3
93127 var cumulativeTime : Float = 0
94- public override func render( context: ECSContext , into view: View , withTimePassed deltaTime: Float ) {
95- cumulativeTime += deltaTime
96- if cumulativeTime > tickDuration {
97- let didRebuild = rebuildText ( )
128+
129+ public override func shouldRender( context: ECSContext , into view: GameView , withTimePassed deltaTime: Float ) -> Bool {
130+ return context. performance != nil && text. isReady
131+ }
132+
133+ public override func render( context: ECSContext , into view: GameView , withTimePassed deltaTime: Float ) {
134+ self . cumulativeTime += deltaTime
135+ if self . cumulativeTime > self . tickDuration {
136+ let didRebuild = self . rebuildText ( )
98137 if didRebuild {
99- cumulativeTime = 0
138+ self . cumulativeTime = 0
100139 }
101140 }
102141
103- // guard text.isReady else { return }
104- //
105- // let x: Float = .maximum(10, view.safeAreaInsets.leading)
106- // let y: Float = .maximum(10, view.safeAreaInsets.top)
107- // let position = Position2(x, y)
108- //
109- // var canvas: Canvas = Canvas(window: window)
110- // canvas.insert(
111- // Rect(size: text.size).inset(by: Insets(-4)),
112- // color: Color(0, 0, 0, 0.6),
113- // at: position
114- // )
115- // canvas.insert(text, at: position)
116- // renderTarget.insert(canvas)
117- }
142+ let x : Float = . maximum( 10 , view. marginInsets. leading)
143+ let y : Float = . maximum( 10 , view. marginInsets. top)
144+ let position = Position2 ( x, y)
118145
146+ var canvas : Canvas = Canvas ( view: view)
147+
148+ if improveLegibility { // Rectangle
149+ let color = Color ( red: 0.1 , green: 0.1 , blue: 0.1 , alpha: 0.5 )
150+ let rect = Rect ( size: text. size) . inset ( by: Insets ( - 4 ) )
151+ let position = Position3 (
152+ position. x + rect. position. x,
153+ position. y + rect. position. y,
154+ 0
155+ )
156+ let scale = Size3 ( rect. size. width, rect. size. height, 1 )
157+ let rotation = Quaternion ( . zero, axis: . forward)
158+ let transform = Transform3 ( position: position, rotation: rotation, scale: scale)
159+
160+ let material = Material ( color: color)
161+ let flags = DrawCommand . Flags (
162+ cull: . disabled,
163+ depthTest: . always,
164+ depthWrite: . disabled,
165+ primitive: . triangle,
166+ winding: . clockwise,
167+ blendMode: . subtract
168+ )
169+ let command = DrawCommand (
170+ resource: . geometry( . rectOriginTopLeft) ,
171+ transforms: [ transform] ,
172+ material: material,
173+ vsh: . standard,
174+ fsh: . materialColor,
175+ flags: flags
176+ )
177+ canvas. insert ( command)
178+ }
179+
180+ do { // Text
181+ if text. string. isEmpty == false {
182+ text. interfaceScale = canvas. interfaceScale
183+
184+ let position = Position3 ( position. x, position. y, 0 )
185+ let rotation = Quaternion ( . zero, axis: . forward)
186+ let transform = Transform3 ( position: position, rotation: rotation, scale: . one)
187+
188+ let material = Material ( texture: text. texture, sampleFilter: text. sampleFilter, tintColor: text. color. withAlpha ( improveLegibility ? 1 : 0.6 ) )
189+
190+ let flags = DrawCommand . Flags (
191+ cull: . disabled,
192+ depthTest: . always,
193+ depthWrite: . disabled,
194+ primitive: . triangle,
195+ winding: . clockwise,
196+ blendMode: . add
197+ )
198+ let command = DrawCommand (
199+ resource: . geometry( text. geometry) ,
200+ transforms: [ transform] ,
201+ material: material,
202+ vsh: . standard,
203+ fsh: . textureSampleTintColor,
204+ flags: flags
205+ )
206+ canvas. insert ( command)
207+ }
208+ }
209+
210+ view. insert ( canvas)
211+ }
212+
213+ public init ( improveLegibility: Bool ) {
214+ self . improveLegibility = improveLegibility
215+ }
216+
217+ public required init ( ) {
218+ self . improveLegibility = false
219+ }
220+
119221 public override class func sortOrder( ) -> RenderingSystemSortOrder ? { . performance }
120222}
0 commit comments