File size: 9,850 Bytes
1829efb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { useEffect } from 'react'
import { useControls, button, folder } from 'leva'
import { useDebugStore } from '../store/debug'
import { useButterflyStore } from '../modules/butterfly/store'
import { FOLDER_ORDER, PARAM_SPECS, DEFAULT_PARAMS, type ButterflyParams } from '../modules/butterfly/params'

function clearLocalStorageAndReload() {
  window.localStorage.clear()
  window.location.reload()
}

type ButterflySpec = (typeof PARAM_SPECS)[keyof typeof PARAM_SPECS]
// Leva schema composition is intentionally dynamic because butterfly params are data-driven.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type AnySchema = any

function specToLeva(spec: ButterflySpec) {
  const out: Record<string, unknown> = { value: spec.value }
  if ('min' in spec) {
    out.min = spec.min
    out.max = spec.max
    out.step = spec.step
  }
  if ('label' in spec && spec.label) out.label = spec.label
  return out
}

function createButterflyLevaSchema() {
  const root: Record<string, AnySchema> = {}
  for (const [key, spec] of Object.entries(PARAM_SPECS)) {
    if (spec.folder === null) root[key] = specToLeva(spec)
  }
  for (const f of FOLDER_ORDER) {
    const entries: Record<string, AnySchema> = {}
    for (const [key, spec] of Object.entries(PARAM_SPECS)) {
      if (spec.folder === f) entries[key] = specToLeva(spec)
    }
    if (Object.keys(entries).length > 0) {
      root[f] = folder(entries, { collapsed: false })
    }
  }
  return root
}

const BUTTERFLY_LEVA_SCHEMA = createButterflyLevaSchema()

function dumpParams() {
  const debug = useDebugStore.getState()
  const dof = {
    dofEnabled: debug.dofEnabled,
    focalDistance: debug.focalDistance,
    apertureAngle: debug.apertureAngle,
    falloff: debug.falloff,
    sharpRange: debug.sharpRange,
    falloffRate: debug.falloffRate,
  }

  const post = {
    bloomEnabled: debug.bloomEnabled,
    bloomIntensity: debug.bloomIntensity,
    bloomThreshold: debug.bloomThreshold,
    chromaticEnabled: debug.chromaticEnabled,
    chromaticOffset: debug.chromaticOffset,
    motionBlurEnabled: debug.motionBlurEnabled,
    motionBlurStrength: debug.motionBlurStrength,
  }

  const lighting = {
    environmentIntensity: debug.environmentIntensity,
    sunIntensity: debug.sunIntensity,
    sunColor: debug.sunColor,
  }

  const out: Record<string, unknown> = { dof, post, lighting }
  out.viewerQuality = debug.viewerQuality
  out.worldRenderMode = debug.worldRenderMode
  out.objectRenderMode = debug.objectRenderMode

  if (debug.butterfliesEnabled) {
    const bf = useButterflyStore.getState()
    const butterfly: Record<string, unknown> = {}
    for (const k of Object.keys(DEFAULT_PARAMS) as Array<keyof ButterflyParams>) {
      butterfly[k] = bf[k]
    }
    out.butterfly = butterfly
  }
  out.flyMouseSensitivity = debug.flyMouseSensitivity

  const json = JSON.stringify(out, null, 2)
  // eslint-disable-next-line no-console
  console.log('[debug params]\n' + json)
  if (typeof navigator !== 'undefined' && navigator.clipboard) {
    navigator.clipboard.writeText(json).catch(() => {})
  }
}

export function DebugPanel() {
  const showOrigin = useDebugStore((s) => s.showOrigin)
  const setShowOrigin = useDebugStore((s) => s.setShowOrigin)
  const butterfliesEnabled = useDebugStore((s) => s.butterfliesEnabled)
  const setButterfliesEnabled = useDebugStore((s) => s.setButterfliesEnabled)
  const controllerMode = useDebugStore((s) => s.controllerMode)
  const setControllerMode = useDebugStore((s) => s.setControllerMode)
  const flyMouseSensitivity = useDebugStore((s) => s.flyMouseSensitivity)
  const dofEnabled = useDebugStore((s) => s.dofEnabled)
  const focalDistance = useDebugStore((s) => s.focalDistance)
  const apertureAngle = useDebugStore((s) => s.apertureAngle)
  const falloff = useDebugStore((s) => s.falloff)
  const sharpRange = useDebugStore((s) => s.sharpRange)
  const falloffRate = useDebugStore((s) => s.falloffRate)
  const bloomEnabled = useDebugStore((s) => s.bloomEnabled)
  const bloomIntensity = useDebugStore((s) => s.bloomIntensity)
  const bloomThreshold = useDebugStore((s) => s.bloomThreshold)
  const chromaticEnabled = useDebugStore((s) => s.chromaticEnabled)
  const chromaticOffset = useDebugStore((s) => s.chromaticOffset)
  const motionBlurEnabled = useDebugStore((s) => s.motionBlurEnabled)
  const motionBlurStrength = useDebugStore((s) => s.motionBlurStrength)
  const environmentIntensity = useDebugStore((s) => s.environmentIntensity)
  const sunIntensity = useDebugStore((s) => s.sunIntensity)
  const sunColor = useDebugStore((s) => s.sunColor)
  const setDofEnabled = useDebugStore((s) => s.setDofEnabled)
  const setFocalDistance = useDebugStore((s) => s.setFocalDistance)
  const setApertureAngle = useDebugStore((s) => s.setApertureAngle)
  const setFalloff = useDebugStore((s) => s.setFalloff)
  const setSharpRange = useDebugStore((s) => s.setSharpRange)
  const setFalloffRate = useDebugStore((s) => s.setFalloffRate)
  const setBloomEnabled = useDebugStore((s) => s.setBloomEnabled)
  const setBloomIntensity = useDebugStore((s) => s.setBloomIntensity)
  const setBloomThreshold = useDebugStore((s) => s.setBloomThreshold)
  const setChromaticEnabled = useDebugStore((s) => s.setChromaticEnabled)
  const setChromaticOffset = useDebugStore((s) => s.setChromaticOffset)
  const setMotionBlurEnabled = useDebugStore((s) => s.setMotionBlurEnabled)
  const setMotionBlurStrength = useDebugStore((s) => s.setMotionBlurStrength)
  const setEnvironmentIntensity = useDebugStore((s) => s.setEnvironmentIntensity)
  const setSunIntensity = useDebugStore((s) => s.setSunIntensity)
  const setSunColor = useDebugStore((s) => s.setSunColor)
  const setFlyMouseSensitivity = useDebugStore((s) => s.setFlyMouseSensitivity)

  useControls({
    'Clear Local Storage + Reload': button(clearLocalStorageAndReload),
    'Dump Params (copy JSON)': button(dumpParams),
    Scene: folder({
      showOrigin: {
        value: showOrigin,
        label: 'Show Origin',
        onChange: setShowOrigin,
      },
      butterfliesEnabled: {
        value: butterfliesEnabled,
        label: 'Butterflies',
        onChange: setButterfliesEnabled,
      },
    }),
    'Splat DoF': folder({
      dofEnabled: {
        value: dofEnabled,
        label: 'Enabled',
        onChange: setDofEnabled,
      },
      focalDistance: {
        value: focalDistance,
        min: 0.1,
        max: 50,
        step: 0.1,
        label: 'Focal Distance',
        onChange: setFocalDistance,
      },
      apertureAngle: {
        value: apertureAngle,
        min: 0,
        max: 0.3,
        step: 0.001,
        label: 'Aperture (rad)',
        onChange: setApertureAngle,
      },
      falloff: {
        value: falloff,
        min: 0,
        max: 1,
        step: 0.01,
        label: 'Falloff (0=disk)',
        onChange: setFalloff,
      },
      sharpRange: {
        value: sharpRange,
        min: 0,
        max: 20,
        step: 0.1,
        label: 'Sharp Range',
        onChange: setSharpRange,
      },
      falloffRate: {
        value: falloffRate,
        min: 0.01,
        max: 2,
        step: 0.01,
        label: 'Exp Rate',
        onChange: setFalloffRate,
      },
    }),
    'Post FX': folder({
      bloomEnabled: {
        value: bloomEnabled,
        label: 'Bloom',
        onChange: setBloomEnabled,
      },
      bloomIntensity: {
        value: bloomIntensity,
        min: 0,
        max: 3,
        step: 0.01,
        label: 'Bloom Intensity',
        onChange: setBloomIntensity,
      },
      bloomThreshold: {
        value: bloomThreshold,
        min: 0,
        max: 5,
        step: 0.01,
        label: 'Bloom Threshold',
        onChange: setBloomThreshold,
      },
      chromaticEnabled: {
        value: chromaticEnabled,
        label: 'Chromatic Aberr.',
        onChange: setChromaticEnabled,
      },
      chromaticOffset: {
        value: chromaticOffset,
        min: 0,
        max: 0.01,
        step: 0.0001,
        label: 'Chromatic Offset',
        onChange: setChromaticOffset,
      },
      motionBlurEnabled: {
        value: motionBlurEnabled,
        label: 'Motion Blur',
        onChange: setMotionBlurEnabled,
      },
      motionBlurStrength: {
        value: motionBlurStrength,
        min: 0,
        max: 2,
        step: 0.01,
        label: 'Motion Blur Str.',
        onChange: setMotionBlurStrength,
      },
    }),
    Lighting: folder({
      environmentIntensity: {
        value: environmentIntensity,
        min: 0,
        max: 5,
        step: 0.01,
        label: 'Env Strength',
        onChange: setEnvironmentIntensity,
      },
      sunIntensity: {
        value: sunIntensity,
        min: 0,
        max: 10,
        step: 0.01,
        label: 'Sun Strength',
        onChange: setSunIntensity,
      },
      sunColor: {
        value: sunColor,
        label: 'Sun Color',
        onChange: setSunColor,
      },
    }),
    controllerMode: {
      value: controllerMode,
      options: { Fly: 'fly', FPS: 'fps' },
      label: 'Controller',
      onChange: (v: string) => setControllerMode(v as typeof controllerMode),
    },
    flyMouseSensitivity: {
      value: flyMouseSensitivity,
      min: 0.0005,
      max: 0.02,
      step: 0.0005,
      label: 'Fly Mouse Sens.',
      onChange: setFlyMouseSensitivity,
    },
  })

  return butterfliesEnabled ? <ButterflyLevaBridge /> : null
}

function ButterflyLevaBridge() {
  const values = useControls(BUTTERFLY_LEVA_SCHEMA) as unknown as ButterflyParams

  useEffect(() => {
    const state = useButterflyStore.getState()
    const keys = Object.keys(DEFAULT_PARAMS) as Array<keyof ButterflyParams>
    for (const k of keys) {
      const v = values[k]
      if (v !== undefined && v !== state[k]) state.setParam(k, v)
    }
  }, [values])

  return null
}