reze-engine 0.3.12 → 0.3.14

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/src/engine.ts CHANGED
@@ -6,9 +6,21 @@ import { PmxLoader } from "./pmx-loader"
6
6
  export type EngineOptions = {
7
7
  ambientColor?: Vec3
8
8
  bloomIntensity?: number
9
+ bloomThreshold?: number
9
10
  rimLightIntensity?: number
10
11
  cameraDistance?: number
11
12
  cameraTarget?: Vec3
13
+ cameraFov?: number
14
+ }
15
+
16
+ export const DEFAULT_ENGINE_OPTIONS: Required<EngineOptions> = {
17
+ ambientColor: new Vec3(1.0, 1.0, 1.0),
18
+ bloomIntensity: 0.12,
19
+ bloomThreshold: 0.5,
20
+ rimLightIntensity: 0.45,
21
+ cameraDistance: 26.6,
22
+ cameraTarget: new Vec3(0, 12.5, 0),
23
+ cameraFov: Math.PI / 4,
12
24
  }
13
25
 
14
26
  export interface EngineStats {
@@ -42,8 +54,9 @@ export class Engine {
42
54
  private camera!: Camera
43
55
  private cameraUniformBuffer!: GPUBuffer
44
56
  private cameraMatrixData = new Float32Array(36)
45
- private cameraDistance: number = 26.6
46
- private cameraTarget: Vec3 = new Vec3(0, 12.5, 0)
57
+ private cameraDistance!: number
58
+ private cameraTarget!: Vec3
59
+ private cameraFov!: number
47
60
  private lightUniformBuffer!: GPUBuffer
48
61
  private lightData = new Float32Array(4)
49
62
  private vertexBuffer!: GPUBuffer
@@ -72,18 +85,8 @@ export class Engine {
72
85
  private readonly STENCIL_EYE_VALUE = 1
73
86
  private readonly BLOOM_DOWNSCALE_FACTOR = 2
74
87
 
75
- // Default values
76
- private static readonly DEFAULT_BLOOM_THRESHOLD = 0.01
77
- private static readonly DEFAULT_BLOOM_INTENSITY = 0.12
78
- private static readonly DEFAULT_RIM_LIGHT_INTENSITY = 0.45
79
- private static readonly DEFAULT_CAMERA_DISTANCE = 26.6
80
- private static readonly DEFAULT_CAMERA_TARGET = new Vec3(0, 12.5, 0)
81
- private static readonly TRANSPARENCY_EPSILON = 0.001
82
- private static readonly STATS_FPS_UPDATE_INTERVAL_MS = 1000
83
- private static readonly STATS_FRAME_TIME_ROUNDING = 100
84
-
85
88
  // Ambient light settings
86
- private ambientColor: Vec3 = new Vec3(1.0, 1.0, 1.0)
89
+ private ambientColor!: Vec3
87
90
  // Bloom post-processing textures
88
91
  private sceneRenderTexture!: GPUTexture
89
92
  private sceneRenderTextureView!: GPUTextureView // Cached view (recreated on resize)
@@ -104,10 +107,10 @@ export class Engine {
104
107
  private bloomBlurVBindGroup?: GPUBindGroup
105
108
  private bloomComposeBindGroup?: GPUBindGroup
106
109
  // Bloom settings
107
- private bloomThreshold: number = Engine.DEFAULT_BLOOM_THRESHOLD
108
- private bloomIntensity: number = Engine.DEFAULT_BLOOM_INTENSITY
110
+ private bloomThreshold!: number
111
+ private bloomIntensity!: number
109
112
  // Rim light settings
110
- private rimLightIntensity: number = Engine.DEFAULT_RIM_LIGHT_INTENSITY
113
+ private rimLightIntensity!: number
111
114
 
112
115
  private currentModel: Model | null = null
113
116
  private modelDir: string = ""
@@ -132,11 +135,13 @@ export class Engine {
132
135
  constructor(canvas: HTMLCanvasElement, options?: EngineOptions) {
133
136
  this.canvas = canvas
134
137
  if (options) {
135
- this.ambientColor = options.ambientColor ?? new Vec3(1.0, 1.0, 1.0)
136
- this.bloomIntensity = options.bloomIntensity ?? Engine.DEFAULT_BLOOM_INTENSITY
137
- this.rimLightIntensity = options.rimLightIntensity ?? Engine.DEFAULT_RIM_LIGHT_INTENSITY
138
- this.cameraDistance = options.cameraDistance ?? Engine.DEFAULT_CAMERA_DISTANCE
139
- this.cameraTarget = options.cameraTarget ?? Engine.DEFAULT_CAMERA_TARGET
138
+ this.ambientColor = options.ambientColor ?? DEFAULT_ENGINE_OPTIONS.ambientColor!
139
+ this.bloomIntensity = options.bloomIntensity ?? DEFAULT_ENGINE_OPTIONS.bloomIntensity
140
+ this.bloomThreshold = options.bloomThreshold ?? DEFAULT_ENGINE_OPTIONS.bloomThreshold
141
+ this.rimLightIntensity = options.rimLightIntensity ?? DEFAULT_ENGINE_OPTIONS.rimLightIntensity
142
+ this.cameraDistance = options.cameraDistance ?? DEFAULT_ENGINE_OPTIONS.cameraDistance
143
+ this.cameraTarget = options.cameraTarget ?? DEFAULT_ENGINE_OPTIONS.cameraTarget
144
+ this.cameraFov = options.cameraFov ?? DEFAULT_ENGINE_OPTIONS.cameraFov
140
145
  }
141
146
  }
142
147
 
@@ -1052,7 +1057,7 @@ export class Engine {
1052
1057
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1053
1058
  })
1054
1059
 
1055
- this.camera = new Camera(Math.PI, Math.PI / 2.5, this.cameraDistance, this.cameraTarget)
1060
+ this.camera = new Camera(Math.PI, Math.PI / 2.5, this.cameraDistance, this.cameraTarget, this.cameraFov)
1056
1061
 
1057
1062
  this.camera.aspect = this.canvas.width / this.canvas.height
1058
1063
  this.camera.attachControl(this.canvas)
@@ -1285,7 +1290,7 @@ export class Engine {
1285
1290
  if (!diffuseTexture) throw new Error(`Material "${mat.name}" has no diffuse texture`)
1286
1291
 
1287
1292
  const materialAlpha = mat.diffuse[3]
1288
- const isTransparent = materialAlpha < 1.0 - Engine.TRANSPARENCY_EPSILON
1293
+ const isTransparent = materialAlpha < 1.0 - 0.001
1289
1294
 
1290
1295
  const materialUniformBuffer = this.createMaterialUniformBuffer(mat.name, materialAlpha, 0.0)
1291
1296
 
@@ -1739,17 +1744,15 @@ export class Engine {
1739
1744
  this.frameTimeSum -= avg
1740
1745
  this.frameTimeCount = maxSamples
1741
1746
  }
1742
- this.stats.frameTime =
1743
- Math.round((this.frameTimeSum / this.frameTimeCount) * Engine.STATS_FRAME_TIME_ROUNDING) /
1744
- Engine.STATS_FRAME_TIME_ROUNDING
1747
+ this.stats.frameTime = Math.round((this.frameTimeSum / this.frameTimeCount) * 100) / 100
1745
1748
 
1746
1749
  // FPS tracking
1747
1750
  const now = performance.now()
1748
1751
  this.framesSinceLastUpdate++
1749
1752
  const elapsed = now - this.lastFpsUpdate
1750
1753
 
1751
- if (elapsed >= Engine.STATS_FPS_UPDATE_INTERVAL_MS) {
1752
- this.stats.fps = Math.round((this.framesSinceLastUpdate / elapsed) * Engine.STATS_FPS_UPDATE_INTERVAL_MS)
1754
+ if (elapsed >= 1000) {
1755
+ this.stats.fps = Math.round((this.framesSinceLastUpdate / elapsed) * 1000)
1753
1756
  this.framesSinceLastUpdate = 0
1754
1757
  this.lastFpsUpdate = now
1755
1758
  }