prithivMLmods commited on
Commit
461780e
·
verified ·
1 Parent(s): 66af498

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -96
app.py CHANGED
@@ -127,20 +127,9 @@ ELEVATION_MAP = {
127
  }
128
 
129
  def snap_to_nearest(value, options):
130
- """Snap a value to the nearest option in a list."""
131
  return min(options, key=lambda x: abs(x - value))
132
 
133
  def build_lighting_prompt(azimuth: float, elevation: float) -> str:
134
- """
135
- Build a lighting prompt from azimuth and elevation values.
136
-
137
- Args:
138
- azimuth: Horizontal rotation in degrees (0-360)
139
- elevation: Vertical angle in degrees (-90 to 90)
140
-
141
- Returns:
142
- Formatted prompt string for the LoRA
143
- """
144
  azimuth_snapped = snap_to_nearest(azimuth, list(AZIMUTH_MAP.keys()))
145
  elevation_snapped = snap_to_nearest(elevation, list(ELEVATION_MAP.keys()))
146
 
@@ -161,9 +150,6 @@ def infer_lighting_edit(
161
  height: int = 1024,
162
  width: int = 1024,
163
  ):
164
- """
165
- Edit the lighting of an image using Qwen Image Edit 2511 with multi-angle lighting LoRA.
166
- """
167
  global loaded
168
  progress = gr.Progress(track_tqdm=True)
169
 
@@ -199,9 +185,7 @@ def infer_lighting_edit(
199
  def update_dimensions_on_upload(image):
200
  if image is None:
201
  return 1024, 1024
202
-
203
  original_width, original_height = image.size
204
-
205
  if original_width > original_height:
206
  new_width = 1024
207
  aspect_ratio = original_height / original_width
@@ -210,18 +194,11 @@ def update_dimensions_on_upload(image):
210
  new_height = 1024
211
  aspect_ratio = original_width / original_height
212
  new_width = int(new_height * aspect_ratio)
213
-
214
  new_width = (new_width // 8) * 8
215
  new_height = (new_height // 8) * 8
216
-
217
  return new_width, new_height
218
 
219
  class LightingControl3D(gr.HTML):
220
- """
221
- A 3D lighting control component using Three.js.
222
- Outputs: { azimuth: number, elevation: number }
223
- Accepts imageUrl prop to display user's uploaded image on the plane.
224
- """
225
  def __init__(self, value=None, imageUrl=None, **kwargs):
226
  if value is None:
227
  value = {"azimuth": 0, "elevation": 0}
@@ -237,14 +214,12 @@ class LightingControl3D(gr.HTML):
237
  const wrapper = element.querySelector('#lighting-control-wrapper');
238
  const promptOverlay = element.querySelector('#prompt-overlay');
239
 
240
- // Wait for THREE to load
241
  const initScene = () => {
242
  if (typeof THREE === 'undefined') {
243
  setTimeout(initScene, 100);
244
  return;
245
  }
246
 
247
- // Scene setup
248
  const scene = new THREE.Scene();
249
  scene.background = new THREE.Color(0x1a1a1a);
250
 
@@ -259,10 +234,8 @@ class LightingControl3D(gr.HTML):
259
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;
260
  wrapper.insertBefore(renderer.domElement, promptOverlay);
261
 
262
- // Lighting for the scene
263
  scene.add(new THREE.AmbientLight(0xffffff, 0.1));
264
 
265
- // Ground plane for shadows
266
  const ground = new THREE.Mesh(
267
  new THREE.PlaneGeometry(10, 10),
268
  new THREE.ShadowMaterial({ opacity: 0.3 })
@@ -272,20 +245,16 @@ class LightingControl3D(gr.HTML):
272
  ground.receiveShadow = true;
273
  scene.add(ground);
274
 
275
- // Grid
276
  scene.add(new THREE.GridHelper(8, 16, 0x333333, 0x222222));
277
 
278
- // Constants
279
  const CENTER = new THREE.Vector3(0, 0.75, 0);
280
  const BASE_DISTANCE = 2.5;
281
  const AZIMUTH_RADIUS = 2.4;
282
  const ELEVATION_RADIUS = 1.8;
283
 
284
- // State
285
  let azimuthAngle = props.value?.azimuth || 0;
286
  let elevationAngle = props.value?.elevation || 0;
287
 
288
- // Mappings
289
  const azimuthSteps = [0, 45, 90, 135, 180, 225, 270, 315];
290
  const elevationSteps = [-90, 0, 90];
291
  const azimuthNames = {
@@ -299,7 +268,6 @@ class LightingControl3D(gr.HTML):
299
  return steps.reduce((prev, curr) => Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
300
  }
301
 
302
- // Create placeholder texture (smiley face)
303
  function createPlaceholderTexture() {
304
  const canvas = document.createElement('canvas');
305
  canvas.width = 256;
@@ -324,7 +292,6 @@ class LightingControl3D(gr.HTML):
324
  return new THREE.CanvasTexture(canvas);
325
  }
326
 
327
- // Target image plane
328
  let currentTexture = createPlaceholderTexture();
329
  const planeMaterial = new THREE.MeshStandardMaterial({ map: currentTexture, side: THREE.DoubleSide, roughness: 0.5, metalness: 0 });
330
  let targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 1.2), planeMaterial);
@@ -332,13 +299,10 @@ class LightingControl3D(gr.HTML):
332
  targetPlane.receiveShadow = true;
333
  scene.add(targetPlane);
334
 
335
- // Function to update texture from image URL
336
  function updateTextureFromUrl(url) {
337
  if (!url) {
338
- // Reset to placeholder
339
  planeMaterial.map = createPlaceholderTexture();
340
  planeMaterial.needsUpdate = true;
341
- // Reset plane to square
342
  scene.remove(targetPlane);
343
  targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 1.2), planeMaterial);
344
  targetPlane.position.copy(CENTER);
@@ -355,7 +319,6 @@ class LightingControl3D(gr.HTML):
355
  planeMaterial.map = texture;
356
  planeMaterial.needsUpdate = true;
357
 
358
- // Adjust plane aspect ratio to match image
359
  const img = texture.image;
360
  if (img && img.width && img.height) {
361
  const aspect = img.width / img.height;
@@ -382,63 +345,51 @@ class LightingControl3D(gr.HTML):
382
  });
383
  }
384
 
385
- // Check for initial imageUrl
386
  if (props.imageUrl) {
387
  updateTextureFromUrl(props.imageUrl);
388
  }
389
 
390
- // --- NEW LIGHT MODEL: STUDIO SQUARE WITH RAYS ---
391
  const lightGroup = new THREE.Group();
392
 
393
- // 1. Studio Housing (Black/Dark Grey)
394
- const housingGeo = new THREE.BoxGeometry(0.7, 0.7, 0.2);
395
  const housingMat = new THREE.MeshStandardMaterial({
396
- color: 0x222222,
397
- roughness: 0.3,
398
- metalness: 0.7
399
  });
400
  const housing = new THREE.Mesh(housingGeo, housingMat);
401
 
402
- // 2. Light Surface (Bright White Square)
403
- const faceGeo = new THREE.PlaneGeometry(0.65, 0.65);
404
- const faceMat = new THREE.MeshStandardMaterial({
405
- color: 0xffffff,
406
  emissive: 0xffffff,
407
- emissiveIntensity: 3.0,
408
- roughness: 0.0
409
  });
410
- const face = new THREE.Mesh(faceGeo, faceMat);
411
- face.position.z = 0.105; // Slightly in front of housing
412
- housing.add(face);
413
-
414
- // 3. Light Rays (Volumetric Beam Effect)
415
- // Using a transparent cylinder to simulate a beam
416
- const beamHeight = 4.0;
417
- // Cylinder top (0.5) is light size, bottom (1.2) is spread
418
- const beamGeo = new THREE.CylinderGeometry(0.5, 1.2, beamHeight, 32, 1, true);
419
 
420
- // Shift geometry so narrow end starts at 0 and extends positive
421
- beamGeo.translate(0, -beamHeight / 2, 0);
422
- beamGeo.rotateX(-Math.PI / 2); // Rotate to point along +Z axis
423
 
 
424
  const beamMat = new THREE.MeshBasicMaterial({
425
- color: 0xffffff,
426
  transparent: true,
427
- opacity: 0.06,
428
  side: THREE.DoubleSide,
429
  depthWrite: false,
430
  blending: THREE.AdditiveBlending
431
  });
 
432
 
433
- const beam = new THREE.Mesh(beamGeo, beamMat);
434
- beam.position.z = 0.12; // Start just in front of the face
435
- housing.add(beam);
436
-
437
  lightGroup.add(housing);
438
 
439
- // Actual Light Source
440
- const spotLight = new THREE.SpotLight(0xffffff, 15, 20, Math.PI / 3, 0.5, 1);
441
- spotLight.position.set(0, 0, 0.1);
442
  spotLight.castShadow = true;
443
  spotLight.shadow.mapSize.width = 1024;
444
  spotLight.shadow.mapSize.height = 1024;
@@ -454,9 +405,6 @@ class LightingControl3D(gr.HTML):
454
 
455
  scene.add(lightGroup);
456
 
457
- // --- CONTROLS: COLORS UPDATED ---
458
-
459
- // Azimuth ring (YELLOW)
460
  const azimuthRing = new THREE.Mesh(
461
  new THREE.TorusGeometry(AZIMUTH_RADIUS, 0.04, 16, 64),
462
  new THREE.MeshStandardMaterial({ color: 0xffff00, emissive: 0xffff00, emissiveIntensity: 0.3 })
@@ -465,7 +413,6 @@ class LightingControl3D(gr.HTML):
465
  azimuthRing.position.y = 0.05;
466
  scene.add(azimuthRing);
467
 
468
- // Azimuth Handle (YELLOW)
469
  const azimuthHandle = new THREE.Mesh(
470
  new THREE.SphereGeometry(0.18, 16, 16),
471
  new THREE.MeshStandardMaterial({ color: 0xffff00, emissive: 0xffff00, emissiveIntensity: 0.5 })
@@ -473,7 +420,6 @@ class LightingControl3D(gr.HTML):
473
  azimuthHandle.userData.type = 'azimuth';
474
  scene.add(azimuthHandle);
475
 
476
- // Elevation arc (BLUE)
477
  const arcPoints = [];
478
  for (let i = 0; i <= 32; i++) {
479
  const angle = THREE.MathUtils.degToRad(-90 + (180 * i / 32));
@@ -486,7 +432,6 @@ class LightingControl3D(gr.HTML):
486
  );
487
  scene.add(elevationArc);
488
 
489
- // Elevation Handle (BLUE)
490
  const elevationHandle = new THREE.Mesh(
491
  new THREE.SphereGeometry(0.18, 16, 16),
492
  new THREE.MeshStandardMaterial({ color: 0x0000ff, emissive: 0x0000ff, emissiveIntensity: 0.5 })
@@ -494,13 +439,12 @@ class LightingControl3D(gr.HTML):
494
  elevationHandle.userData.type = 'elevation';
495
  scene.add(elevationHandle);
496
 
497
- // --- REFRESH BUTTON ---
498
  const refreshBtn = document.createElement('button');
499
  refreshBtn.innerHTML = 'Reset View';
500
  refreshBtn.style.position = 'absolute';
501
  refreshBtn.style.top = '15px';
502
  refreshBtn.style.right = '15px';
503
- refreshBtn.style.background = '#e63e00'; // Theme primary color
504
  refreshBtn.style.color = '#fff';
505
  refreshBtn.style.border = 'none';
506
  refreshBtn.style.padding = '8px 16px';
@@ -540,7 +484,6 @@ class LightingControl3D(gr.HTML):
540
  azimuthHandle.position.set(AZIMUTH_RADIUS * Math.sin(azRad), 0.05, AZIMUTH_RADIUS * Math.cos(azRad));
541
  elevationHandle.position.set(-0.8, ELEVATION_RADIUS * Math.sin(elRad) + CENTER.y, ELEVATION_RADIUS * Math.cos(elRad));
542
 
543
- // Update prompt
544
  const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
545
  const elSnap = snapToNearest(elevationAngle, elevationSteps);
546
  let prompt = 'Light source from';
@@ -560,7 +503,6 @@ class LightingControl3D(gr.HTML):
560
  trigger('change', props.value);
561
  }
562
 
563
- // Raycasting
564
  const raycaster = new THREE.Raycaster();
565
  const mouse = new THREE.Vector2();
566
  let isDragging = false;
@@ -633,7 +575,6 @@ class LightingControl3D(gr.HTML):
633
  dragTarget.material.emissiveIntensity = 0.5;
634
  dragTarget.scale.setScalar(1);
635
 
636
- // Snap and animate
637
  const targetAz = snapToNearest(azimuthAngle, azimuthSteps);
638
  const targetEl = snapToNearest(elevationAngle, elevationSteps);
639
 
@@ -666,7 +607,6 @@ class LightingControl3D(gr.HTML):
666
 
667
  canvas.addEventListener('mouseup', onMouseUp);
668
  canvas.addEventListener('mouseleave', onMouseUp);
669
- // Touch support for mobile
670
  canvas.addEventListener('touchstart', (e) => {
671
  e.preventDefault();
672
  const touch = e.touches[0];
@@ -724,24 +664,20 @@ class LightingControl3D(gr.HTML):
724
  onMouseUp();
725
  }, { passive: false });
726
 
727
- // Initial update
728
  updatePositions();
729
 
730
- // Render loop
731
  function render() {
732
  requestAnimationFrame(render);
733
  renderer.render(scene, camera);
734
  }
735
  render();
736
 
737
- // Handle resize
738
  new ResizeObserver(() => {
739
  camera.aspect = wrapper.clientWidth / wrapper.clientHeight;
740
  camera.updateProjectionMatrix();
741
  renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
742
  }).observe(wrapper);
743
 
744
- // Store update functions for external calls
745
  wrapper._updateFromProps = (newVal) => {
746
  if (newVal && typeof newVal === 'object') {
747
  azimuthAngle = newVal.azimuth ?? azimuthAngle;
@@ -752,16 +688,13 @@ class LightingControl3D(gr.HTML):
752
 
753
  wrapper._updateTexture = updateTextureFromUrl;
754
 
755
- // Watch for prop changes (imageUrl and value)
756
  let lastImageUrl = props.imageUrl;
757
  let lastValue = JSON.stringify(props.value);
758
  setInterval(() => {
759
- // Check imageUrl changes
760
  if (props.imageUrl !== lastImageUrl) {
761
  lastImageUrl = props.imageUrl;
762
  updateTextureFromUrl(props.imageUrl);
763
  }
764
- // Check value changes (from sliders)
765
  const currentValue = JSON.stringify(props.value);
766
  if (currentValue !== lastValue) {
767
  lastValue = currentValue;
@@ -802,7 +735,7 @@ with gr.Blocks(css=css) as demo:
802
  image = gr.Image(label="Input Image", type="pil", height=300)
803
 
804
  gr.Markdown("### 3D Lighting Control")
805
- #gr.Markdown("*Drag the colored handles: 🟡 Azimuth (Direction), 🔵 Elevation (Height)*")
806
 
807
  lighting_3d = LightingControl3D(
808
  value={"azimuth": 0, "elevation": 0},
@@ -841,7 +774,7 @@ with gr.Blocks(css=css) as demo:
841
  with gr.Column(scale=1):
842
  result = gr.Image(label="Output Image", height=500)
843
 
844
- with gr.Accordion("Advanced Settings", open=True):
845
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
846
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
847
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
@@ -850,12 +783,10 @@ with gr.Blocks(css=css) as demo:
850
  width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
851
 
852
  def update_prompt_from_sliders(azimuth, elevation):
853
- """Update prompt preview when sliders change."""
854
  prompt = build_lighting_prompt(azimuth, elevation)
855
  return prompt
856
 
857
  def sync_3d_to_sliders(lighting_value):
858
- """Sync 3D control changes to sliders."""
859
  if lighting_value and isinstance(lighting_value, dict):
860
  az = lighting_value.get('azimuth', 0)
861
  el = lighting_value.get('elevation', 0)
@@ -864,11 +795,9 @@ with gr.Blocks(css=css) as demo:
864
  return gr.update(), gr.update(), gr.update()
865
 
866
  def sync_sliders_to_3d(azimuth, elevation):
867
- """Sync slider changes to 3D control."""
868
  return {"azimuth": azimuth, "elevation": elevation}
869
 
870
  def update_3d_image(image):
871
- """Update the 3D component with the uploaded image."""
872
  if image is None:
873
  return gr.update(imageUrl=None)
874
 
 
127
  }
128
 
129
  def snap_to_nearest(value, options):
 
130
  return min(options, key=lambda x: abs(x - value))
131
 
132
  def build_lighting_prompt(azimuth: float, elevation: float) -> str:
 
 
 
 
 
 
 
 
 
 
133
  azimuth_snapped = snap_to_nearest(azimuth, list(AZIMUTH_MAP.keys()))
134
  elevation_snapped = snap_to_nearest(elevation, list(ELEVATION_MAP.keys()))
135
 
 
150
  height: int = 1024,
151
  width: int = 1024,
152
  ):
 
 
 
153
  global loaded
154
  progress = gr.Progress(track_tqdm=True)
155
 
 
185
  def update_dimensions_on_upload(image):
186
  if image is None:
187
  return 1024, 1024
 
188
  original_width, original_height = image.size
 
189
  if original_width > original_height:
190
  new_width = 1024
191
  aspect_ratio = original_height / original_width
 
194
  new_height = 1024
195
  aspect_ratio = original_width / original_height
196
  new_width = int(new_height * aspect_ratio)
 
197
  new_width = (new_width // 8) * 8
198
  new_height = (new_height // 8) * 8
 
199
  return new_width, new_height
200
 
201
  class LightingControl3D(gr.HTML):
 
 
 
 
 
202
  def __init__(self, value=None, imageUrl=None, **kwargs):
203
  if value is None:
204
  value = {"azimuth": 0, "elevation": 0}
 
214
  const wrapper = element.querySelector('#lighting-control-wrapper');
215
  const promptOverlay = element.querySelector('#prompt-overlay');
216
 
 
217
  const initScene = () => {
218
  if (typeof THREE === 'undefined') {
219
  setTimeout(initScene, 100);
220
  return;
221
  }
222
 
 
223
  const scene = new THREE.Scene();
224
  scene.background = new THREE.Color(0x1a1a1a);
225
 
 
234
  renderer.shadowMap.type = THREE.PCFSoftShadowMap;
235
  wrapper.insertBefore(renderer.domElement, promptOverlay);
236
 
 
237
  scene.add(new THREE.AmbientLight(0xffffff, 0.1));
238
 
 
239
  const ground = new THREE.Mesh(
240
  new THREE.PlaneGeometry(10, 10),
241
  new THREE.ShadowMaterial({ opacity: 0.3 })
 
245
  ground.receiveShadow = true;
246
  scene.add(ground);
247
 
 
248
  scene.add(new THREE.GridHelper(8, 16, 0x333333, 0x222222));
249
 
 
250
  const CENTER = new THREE.Vector3(0, 0.75, 0);
251
  const BASE_DISTANCE = 2.5;
252
  const AZIMUTH_RADIUS = 2.4;
253
  const ELEVATION_RADIUS = 1.8;
254
 
 
255
  let azimuthAngle = props.value?.azimuth || 0;
256
  let elevationAngle = props.value?.elevation || 0;
257
 
 
258
  const azimuthSteps = [0, 45, 90, 135, 180, 225, 270, 315];
259
  const elevationSteps = [-90, 0, 90];
260
  const azimuthNames = {
 
268
  return steps.reduce((prev, curr) => Math.abs(curr - value) < Math.abs(prev - value) ? curr : prev);
269
  }
270
 
 
271
  function createPlaceholderTexture() {
272
  const canvas = document.createElement('canvas');
273
  canvas.width = 256;
 
292
  return new THREE.CanvasTexture(canvas);
293
  }
294
 
 
295
  let currentTexture = createPlaceholderTexture();
296
  const planeMaterial = new THREE.MeshStandardMaterial({ map: currentTexture, side: THREE.DoubleSide, roughness: 0.5, metalness: 0 });
297
  let targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 1.2), planeMaterial);
 
299
  targetPlane.receiveShadow = true;
300
  scene.add(targetPlane);
301
 
 
302
  function updateTextureFromUrl(url) {
303
  if (!url) {
 
304
  planeMaterial.map = createPlaceholderTexture();
305
  planeMaterial.needsUpdate = true;
 
306
  scene.remove(targetPlane);
307
  targetPlane = new THREE.Mesh(new THREE.PlaneGeometry(1.2, 1.2), planeMaterial);
308
  targetPlane.position.copy(CENTER);
 
319
  planeMaterial.map = texture;
320
  planeMaterial.needsUpdate = true;
321
 
 
322
  const img = texture.image;
323
  if (img && img.width && img.height) {
324
  const aspect = img.width / img.height;
 
345
  });
346
  }
347
 
 
348
  if (props.imageUrl) {
349
  updateTextureFromUrl(props.imageUrl);
350
  }
351
 
 
352
  const lightGroup = new THREE.Group();
353
 
354
+ const housingGeo = new THREE.BoxGeometry(0.8, 0.8, 0.2);
 
355
  const housingMat = new THREE.MeshStandardMaterial({
356
+ color: 0x111111,
357
+ roughness: 0.7,
358
+ metalness: 0.5
359
  });
360
  const housing = new THREE.Mesh(housingGeo, housingMat);
361
 
362
+ const panelGeo = new THREE.PlaneGeometry(0.75, 0.75);
363
+ const panelMat = new THREE.MeshStandardMaterial({
364
+ color: 0xffffff,
 
365
  emissive: 0xffffff,
366
+ emissiveIntensity: 2.0,
367
+ roughness: 0.1
368
  });
369
+ const panel = new THREE.Mesh(panelGeo, panelMat);
370
+ panel.position.z = 0.101;
 
 
 
 
 
 
 
371
 
372
+ housing.add(panel);
 
 
373
 
374
+ const beamGeo = new THREE.ConeGeometry(1.2, 5, 32, 1, true);
375
  const beamMat = new THREE.MeshBasicMaterial({
376
+ color: 0xffffee,
377
  transparent: true,
378
+ opacity: 0.08,
379
  side: THREE.DoubleSide,
380
  depthWrite: false,
381
  blending: THREE.AdditiveBlending
382
  });
383
+ const lightBeam = new THREE.Mesh(beamGeo, beamMat);
384
 
385
+ lightBeam.rotation.x = Math.PI / 2;
386
+ lightBeam.position.z = 2.5;
387
+ housing.add(lightBeam);
388
+
389
  lightGroup.add(housing);
390
 
391
+ const spotLight = new THREE.SpotLight(0xffffff, 10, 10, Math.PI / 3, 1, 1);
392
+ spotLight.position.set(0, 0, 0.1);
 
393
  spotLight.castShadow = true;
394
  spotLight.shadow.mapSize.width = 1024;
395
  spotLight.shadow.mapSize.height = 1024;
 
405
 
406
  scene.add(lightGroup);
407
 
 
 
 
408
  const azimuthRing = new THREE.Mesh(
409
  new THREE.TorusGeometry(AZIMUTH_RADIUS, 0.04, 16, 64),
410
  new THREE.MeshStandardMaterial({ color: 0xffff00, emissive: 0xffff00, emissiveIntensity: 0.3 })
 
413
  azimuthRing.position.y = 0.05;
414
  scene.add(azimuthRing);
415
 
 
416
  const azimuthHandle = new THREE.Mesh(
417
  new THREE.SphereGeometry(0.18, 16, 16),
418
  new THREE.MeshStandardMaterial({ color: 0xffff00, emissive: 0xffff00, emissiveIntensity: 0.5 })
 
420
  azimuthHandle.userData.type = 'azimuth';
421
  scene.add(azimuthHandle);
422
 
 
423
  const arcPoints = [];
424
  for (let i = 0; i <= 32; i++) {
425
  const angle = THREE.MathUtils.degToRad(-90 + (180 * i / 32));
 
432
  );
433
  scene.add(elevationArc);
434
 
 
435
  const elevationHandle = new THREE.Mesh(
436
  new THREE.SphereGeometry(0.18, 16, 16),
437
  new THREE.MeshStandardMaterial({ color: 0x0000ff, emissive: 0x0000ff, emissiveIntensity: 0.5 })
 
439
  elevationHandle.userData.type = 'elevation';
440
  scene.add(elevationHandle);
441
 
 
442
  const refreshBtn = document.createElement('button');
443
  refreshBtn.innerHTML = 'Reset View';
444
  refreshBtn.style.position = 'absolute';
445
  refreshBtn.style.top = '15px';
446
  refreshBtn.style.right = '15px';
447
+ refreshBtn.style.background = '#e63e00';
448
  refreshBtn.style.color = '#fff';
449
  refreshBtn.style.border = 'none';
450
  refreshBtn.style.padding = '8px 16px';
 
484
  azimuthHandle.position.set(AZIMUTH_RADIUS * Math.sin(azRad), 0.05, AZIMUTH_RADIUS * Math.cos(azRad));
485
  elevationHandle.position.set(-0.8, ELEVATION_RADIUS * Math.sin(elRad) + CENTER.y, ELEVATION_RADIUS * Math.cos(elRad));
486
 
 
487
  const azSnap = snapToNearest(azimuthAngle, azimuthSteps);
488
  const elSnap = snapToNearest(elevationAngle, elevationSteps);
489
  let prompt = 'Light source from';
 
503
  trigger('change', props.value);
504
  }
505
 
 
506
  const raycaster = new THREE.Raycaster();
507
  const mouse = new THREE.Vector2();
508
  let isDragging = false;
 
575
  dragTarget.material.emissiveIntensity = 0.5;
576
  dragTarget.scale.setScalar(1);
577
 
 
578
  const targetAz = snapToNearest(azimuthAngle, azimuthSteps);
579
  const targetEl = snapToNearest(elevationAngle, elevationSteps);
580
 
 
607
 
608
  canvas.addEventListener('mouseup', onMouseUp);
609
  canvas.addEventListener('mouseleave', onMouseUp);
 
610
  canvas.addEventListener('touchstart', (e) => {
611
  e.preventDefault();
612
  const touch = e.touches[0];
 
664
  onMouseUp();
665
  }, { passive: false });
666
 
 
667
  updatePositions();
668
 
 
669
  function render() {
670
  requestAnimationFrame(render);
671
  renderer.render(scene, camera);
672
  }
673
  render();
674
 
 
675
  new ResizeObserver(() => {
676
  camera.aspect = wrapper.clientWidth / wrapper.clientHeight;
677
  camera.updateProjectionMatrix();
678
  renderer.setSize(wrapper.clientWidth, wrapper.clientHeight);
679
  }).observe(wrapper);
680
 
 
681
  wrapper._updateFromProps = (newVal) => {
682
  if (newVal && typeof newVal === 'object') {
683
  azimuthAngle = newVal.azimuth ?? azimuthAngle;
 
688
 
689
  wrapper._updateTexture = updateTextureFromUrl;
690
 
 
691
  let lastImageUrl = props.imageUrl;
692
  let lastValue = JSON.stringify(props.value);
693
  setInterval(() => {
 
694
  if (props.imageUrl !== lastImageUrl) {
695
  lastImageUrl = props.imageUrl;
696
  updateTextureFromUrl(props.imageUrl);
697
  }
 
698
  const currentValue = JSON.stringify(props.value);
699
  if (currentValue !== lastValue) {
700
  lastValue = currentValue;
 
735
  image = gr.Image(label="Input Image", type="pil", height=300)
736
 
737
  gr.Markdown("### 3D Lighting Control")
738
+ gr.Markdown("*Drag the colored handles: 🟡 Azimuth (Direction), 🔵 Elevation (Height)*")
739
 
740
  lighting_3d = LightingControl3D(
741
  value={"azimuth": 0, "elevation": 0},
 
774
  with gr.Column(scale=1):
775
  result = gr.Image(label="Output Image", height=500)
776
 
777
+ with gr.Accordion("Advanced Settings", open=False):
778
  seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
779
  randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
780
  guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
 
783
  width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
784
 
785
  def update_prompt_from_sliders(azimuth, elevation):
 
786
  prompt = build_lighting_prompt(azimuth, elevation)
787
  return prompt
788
 
789
  def sync_3d_to_sliders(lighting_value):
 
790
  if lighting_value and isinstance(lighting_value, dict):
791
  az = lighting_value.get('azimuth', 0)
792
  el = lighting_value.get('elevation', 0)
 
795
  return gr.update(), gr.update(), gr.update()
796
 
797
  def sync_sliders_to_3d(azimuth, elevation):
 
798
  return {"azimuth": azimuth, "elevation": elevation}
799
 
800
  def update_3d_image(image):
 
801
  if image is None:
802
  return gr.update(imageUrl=None)
803