minecraft-renderer 0.1.45 → 0.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "minecraft-renderer",
3
- "version": "0.1.45",
3
+ "version": "0.1.46",
4
4
  "description": "The most Modular Minecraft world renderer with Three.js WebGL backend",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -4,6 +4,7 @@
4
4
  */
5
5
 
6
6
  import { proxy } from 'valtio'
7
+ import { defaultPerformanceInstabilityFactors } from '../performanceMonitor'
7
8
  import type {
8
9
  GraphicsBackendConfig,
9
10
  RendererReactiveState,
@@ -117,6 +118,7 @@ export const getDefaultRendererState = (): {
117
118
  heightmaps: new Map<string, Int16Array>(),
118
119
  allChunksLoaded: false,
119
120
  mesherWork: false,
121
+ instabilityFactors: defaultPerformanceInstabilityFactors(),
120
122
  intersectMedia: null
121
123
  },
122
124
  renderer: '...',
@@ -9,3 +9,4 @@ export * from './types'
9
9
  export * from './config'
10
10
  export * from './playerState'
11
11
  export * from './appViewer'
12
+ export * from '../performanceMonitor'
@@ -23,6 +23,7 @@ export interface SoundSystem {
23
23
  }
24
24
 
25
25
  import type { MenuBackgroundOptions } from '../three/menuBackground/types'
26
+ import type { PerformanceInstabilityFactors } from '../performanceMonitor'
26
27
 
27
28
  /** Graphics backend configuration */
28
29
  export interface GraphicsBackendConfig {
@@ -76,6 +77,8 @@ export interface RendererReactiveState {
76
77
  heightmaps: Map<string, Int16Array>
77
78
  allChunksLoaded: boolean
78
79
  mesherWork: boolean
80
+ /** Low-FPS / render instability factors (see `performanceMonitor`). */
81
+ instabilityFactors: PerformanceInstabilityFactors
79
82
  intersectMedia: any | null
80
83
  }
81
84
  renderer: string
@@ -0,0 +1,77 @@
1
+ //@ts-nocheck
2
+ import {
3
+ CONSTANT_LONG_RENDER_FRACTION,
4
+ CONSTANT_LONG_RENDER_MIN_SAMPLES,
5
+ FAST_SCENE_WITHOUT_ENTITIES_MS,
6
+ HIGH_TEXTURE_COUNT,
7
+ LONG_RENDER_TIME_MS,
8
+ LOW_FPS_THRESHOLD,
9
+ RENDER_TIME_HISTORY_SIZE,
10
+ SLOW_ENTITIES_RENDER_MS,
11
+ } from './constants'
12
+ import type { FramePerformanceSample, PerformanceInstabilityFactors } from './types'
13
+
14
+ /**
15
+ * Tracks render/FPS signals and writes instability factors into reactive state
16
+ * (alongside `mesherWork`).
17
+ */
18
+ export class PerformanceMonitor {
19
+ private readonly renderTimeHistory: number[] = []
20
+
21
+ constructor(private readonly factors: PerformanceInstabilityFactors) {}
22
+
23
+ onFrame(sample: FramePerformanceSample): void {
24
+ this.pushRenderTime(sample.totalMs)
25
+ this.recompute(sample)
26
+ }
27
+
28
+ private pushRenderTime(ms: number): void {
29
+ this.renderTimeHistory.push(ms)
30
+ if (this.renderTimeHistory.length > RENDER_TIME_HISTORY_SIZE) {
31
+ this.renderTimeHistory.shift()
32
+ }
33
+ }
34
+
35
+ private recompute(sample: FramePerformanceSample): void {
36
+ const lowFps = sample.fps > 0 && sample.fps <= LOW_FPS_THRESHOLD
37
+ const sceneWithoutEntitiesMs = Math.max(0, sample.totalMs - sample.entitiesMs)
38
+
39
+ const longRenderTime = sample.totalMs >= LONG_RENDER_TIME_MS
40
+
41
+ const historyLen = this.renderTimeHistory.length
42
+ const longFrames = this.renderTimeHistory.filter(t => t >= LONG_RENDER_TIME_MS).length
43
+ const constantLongRenderTime =
44
+ historyLen >= CONSTANT_LONG_RENDER_MIN_SAMPLES &&
45
+ longFrames / historyLen >= CONSTANT_LONG_RENDER_FRACTION
46
+
47
+ const tooManyTextures = sample.loadedTextureCount >= HIGH_TEXTURE_COUNT
48
+
49
+ const tooManyEntities =
50
+ lowFps &&
51
+ sample.entitiesMs >= SLOW_ENTITIES_RENDER_MS &&
52
+ sceneWithoutEntitiesMs <= FAST_SCENE_WITHOUT_ENTITIES_MS
53
+
54
+ const hasKnownCause =
55
+ longRenderTime ||
56
+ constantLongRenderTime ||
57
+ tooManyEntities ||
58
+ tooManyTextures
59
+
60
+ const unknownReason = lowFps && !hasKnownCause
61
+
62
+ this.factors.longRenderTime = longRenderTime
63
+ this.factors.constantLongRenderTime = constantLongRenderTime
64
+ this.factors.tooManyEntities = tooManyEntities
65
+ this.factors.tooManyTextures = tooManyTextures
66
+ this.factors.unknownReason = unknownReason
67
+ }
68
+
69
+ reset(): void {
70
+ this.renderTimeHistory.length = 0
71
+ this.factors.longRenderTime = false
72
+ this.factors.constantLongRenderTime = false
73
+ this.factors.tooManyEntities = false
74
+ this.factors.tooManyTextures = false
75
+ this.factors.unknownReason = false
76
+ }
77
+ }
@@ -0,0 +1,24 @@
1
+ //@ts-nocheck
2
+ /** Recent frame exceeded this → `longRenderTime`. */
3
+ export const LONG_RENDER_TIME_MS = 30
4
+
5
+ /** Scene pass without entities faster than this → candidate for entity bottleneck. */
6
+ export const FAST_SCENE_WITHOUT_ENTITIES_MS = 20
7
+
8
+ /** Entity pass slower than this (with low FPS) → `tooManyEntities`. */
9
+ export const SLOW_ENTITIES_RENDER_MS = 8
10
+
11
+ /** FPS at or below this is treated as low performance. */
12
+ export const LOW_FPS_THRESHOLD = 45
13
+
14
+ /** Loaded WebGL textures at or above this → `tooManyTextures` (labels, signs, iOS). */
15
+ export const HIGH_TEXTURE_COUNT = 100
16
+
17
+ /** Ring buffer length for sustained render-time analysis. */
18
+ export const RENDER_TIME_HISTORY_SIZE = 24
19
+
20
+ /** Fraction of recent frames over `LONG_RENDER_TIME_MS` → `constantLongRenderTime`. */
21
+ export const CONSTANT_LONG_RENDER_FRACTION = 0.65
22
+
23
+ /** Minimum frames in history before `constantLongRenderTime` can trigger. */
24
+ export const CONSTANT_LONG_RENDER_MIN_SAMPLES = 8
@@ -0,0 +1,16 @@
1
+ //@ts-nocheck
2
+ import type { PerformanceInstabilityFactors } from './types'
3
+
4
+ const FACTOR_CODES: Array<{ key: keyof PerformanceInstabilityFactors, code: string }> = [
5
+ { key: 'longRenderTime', code: 'LR' },
6
+ { key: 'constantLongRenderTime', code: 'CLR' },
7
+ { key: 'tooManyEntities', code: 'ENT' },
8
+ { key: 'tooManyTextures', code: 'TEX' },
9
+ { key: 'unknownReason', code: 'UNK' },
10
+ ]
11
+
12
+ /** Compact debug overlay fragment, e.g. `LR+ENT` or empty string. */
13
+ export function formatPerformanceFactorsDebug(factors: PerformanceInstabilityFactors): string {
14
+ const active = FACTOR_CODES.filter(({ key }) => factors[key]).map(({ code }) => code)
15
+ return active.length > 0 ? active.join('+') : ''
16
+ }
@@ -0,0 +1,10 @@
1
+ //@ts-nocheck
2
+ export type { FramePerformanceSample, PerformanceInstabilityFactors } from './types'
3
+ export { defaultPerformanceInstabilityFactors } from './types'
4
+ export {
5
+ LONG_RENDER_TIME_MS,
6
+ LOW_FPS_THRESHOLD,
7
+ HIGH_TEXTURE_COUNT,
8
+ } from './constants'
9
+ export { PerformanceMonitor } from './PerformanceMonitor'
10
+ export { formatPerformanceFactorsDebug } from './formatPerformanceFactorsDebug'
@@ -0,0 +1,27 @@
1
+ //@ts-nocheck
2
+ /** Low-FPS / instability factors written to reactive renderer state. */
3
+ export interface PerformanceInstabilityFactors {
4
+ longRenderTime: boolean
5
+ constantLongRenderTime: boolean
6
+ tooManyEntities: boolean
7
+ tooManyTextures: boolean
8
+ unknownReason: boolean
9
+ }
10
+
11
+ export const defaultPerformanceInstabilityFactors = (): PerformanceInstabilityFactors => ({
12
+ longRenderTime: false,
13
+ constantLongRenderTime: false,
14
+ tooManyEntities: false,
15
+ tooManyTextures: false,
16
+ unknownReason: false,
17
+ })
18
+
19
+ export interface FramePerformanceSample {
20
+ /** Full `WorldRendererThree.render()` duration in ms. */
21
+ totalMs: number
22
+ /** Time spent in `entities.render()` this frame (0 if skipped). */
23
+ entitiesMs: number
24
+ loadedTextureCount: number
25
+ /** FPS from the last completed 1s window (0 before first sample). */
26
+ fps: number
27
+ }
@@ -36,6 +36,7 @@ import { downloadWorldGeometry } from './worldGeometryExport'
36
36
  import { ChunkMeshManager } from './chunkMeshManager'
37
37
  import type { RendererModuleManifest, RegisteredModule, RendererModuleController } from './rendererModuleSystem'
38
38
  import { BUILTIN_MODULES } from './modules/index'
39
+ import { formatPerformanceFactorsDebug, PerformanceMonitor } from '../performanceMonitor'
39
40
 
40
41
  type SectionKey = string
41
42
 
@@ -57,6 +58,7 @@ export class WorldRendererThree extends WorldRendererCommon {
57
58
  ambientLight = new THREE.AmbientLight(0xcc_cc_cc)
58
59
  directionalLight = new THREE.DirectionalLight(0xff_ff_ff, 0.5)
59
60
  entities = new Entities(this, (globalThis as any).mcData)
61
+ performanceMonitor!: PerformanceMonitor
60
62
  cameraGroupVr?: THREE.Object3D
61
63
  material = new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true, alphaTest: 0.1 })
62
64
  itemsTexture!: THREE.Texture
@@ -154,6 +156,8 @@ export class WorldRendererThree extends WorldRendererCommon {
154
156
  if (!displayOptions.resourcesManager) throw new Error('resourcesManager is required in displayOptions')
155
157
  super(displayOptions.resourcesManager, displayOptions, initOptions)
156
158
 
159
+ this.performanceMonitor = new PerformanceMonitor(this.reactiveState.world.instabilityFactors)
160
+
157
161
  this.renderer = renderer
158
162
  displayOptions.rendererState.renderer = WorldRendererThree.getRendererInfo(renderer) ?? '...'
159
163
 
@@ -713,7 +717,10 @@ export class WorldRendererThree extends WorldRendererCommon {
713
717
  text += `B: ${formatCompact(this.blocksRendered)} `
714
718
  text += `MEM: ${this.chunkMeshManager.getEstimatedMemoryUsage().total} `
715
719
  const poolStats = this.chunkMeshManager.getStats()
716
- text += `POOL: ${poolStats.activeCount}/${poolStats.poolSize}`
720
+ text += `POOL: ${poolStats.activeCount}/${poolStats.poolSize} `
721
+ const pf = formatPerformanceFactorsDebug(this.reactiveState.world.instabilityFactors)
722
+ if (pf) text += `PF: ${pf} `
723
+ // entities can be seen in F3
717
724
  pane.updateText(text)
718
725
  this.backendInfoReport = text
719
726
  }
@@ -1201,8 +1208,11 @@ export class WorldRendererThree extends WorldRendererCommon {
1201
1208
  this.camera.updateProjectionMatrix()
1202
1209
  }
1203
1210
 
1211
+ let entitiesRenderMs = 0
1204
1212
  if (!this.reactiveDebugParams.disableEntities) {
1213
+ const entitiesStart = performance.now()
1205
1214
  this.entities.render()
1215
+ entitiesRenderMs = performance.now() - entitiesStart
1206
1216
  }
1207
1217
 
1208
1218
  // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
@@ -1243,6 +1253,13 @@ export class WorldRendererThree extends WorldRendererCommon {
1243
1253
  this.renderTimeAvgCount++
1244
1254
  this.renderTimeAvg = ((this.renderTimeAvg * (this.renderTimeAvgCount - 1)) + totalTime) / this.renderTimeAvgCount
1245
1255
  this.renderTimeMax = Math.max(this.renderTimeMax, totalTime)
1256
+
1257
+ this.performanceMonitor.onFrame({
1258
+ totalMs: totalTime,
1259
+ entitiesMs: entitiesRenderMs,
1260
+ loadedTextureCount: this.renderer.info.memory.textures,
1261
+ fps: this.lastFps,
1262
+ })
1246
1263
  }
1247
1264
 
1248
1265
  renderHead(position: Vec3, rotation: number, isWall: boolean, blockEntity) {
@@ -1450,6 +1467,7 @@ export class WorldRendererThree extends WorldRendererCommon {
1450
1467
  }
1451
1468
 
1452
1469
  destroy(): void {
1470
+ this.performanceMonitor?.reset()
1453
1471
  this.pendingSectionUpdates.clear()
1454
1472
  this.pendingSectionBufferStartTimes.clear()
1455
1473
  this.chunkMeshManager.dispose()