meshwriter-cudu 3.0.2 → 3.0.3

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.
@@ -468,17 +468,20 @@
468
468
  }
469
469
 
470
470
  /**
471
- * TextFogPlugin - MaterialPluginBase that applies fog to emissive color
471
+ * TextFogPlugin - MaterialPluginBase that applies fog to text materials
472
472
  *
473
- * Babylon's standard fog only affects diffuse/ambient channels.
474
- * This plugin recalculates fog blending for the final color output,
475
- * ensuring emissive text fades properly with distance fog.
473
+ * Since we disable Babylon's built-in fog (material.fogEnabled = false) to avoid
474
+ * double-fogging of diffuse colors, this plugin handles ALL fog application.
475
+ * It supplies its own uniforms and varyings to calculate fog distance and blend.
476
+ *
477
+ * This ensures text (with emissive colors) fades into fog at the same rate as
478
+ * terrain (which uses only diffuse colors with standard fog).
476
479
  */
477
480
 
478
481
 
479
482
  /**
480
- * Plugin that applies scene fog to text materials by modifying
481
- * the final fragment color before output.
483
+ * Plugin that applies scene fog to text materials uniformly.
484
+ * Handles fog for both diffuse and emissive components in one pass.
482
485
  */
483
486
  class TextFogPlugin extends materialPluginBase.MaterialPluginBase {
484
487
  /**
@@ -499,6 +502,7 @@
499
502
  */
500
503
  prepareDefines(defines, scene, mesh) {
501
504
  // Enable when scene has any fog mode set (1=LINEAR, 2=EXP, 3=EXP2)
505
+ // This is independent of material.fogEnabled since we handle fog ourselves
502
506
  defines['MESHWRITER_TEXT_FOG'] = scene.fogMode !== 0;
503
507
  }
504
508
 
@@ -506,10 +510,45 @@
506
510
  return 'TextFogPlugin';
507
511
  }
508
512
 
513
+ /**
514
+ * Define our own fog uniforms since material.fogEnabled = false
515
+ * means Babylon's built-in fog uniforms won't be available
516
+ */
509
517
  getUniforms() {
510
- // We use Babylon's built-in fog uniforms (vFogInfos, vFogColor)
511
- // which are already available in the standard material
512
- return { ubo: [] };
518
+ return {
519
+ ubo: [
520
+ { name: 'textFogInfos', size: 4, type: 'vec4' },
521
+ { name: 'textFogColor', size: 3, type: 'vec3' }
522
+ ],
523
+ vertex: '',
524
+ fragment: ''
525
+ };
526
+ }
527
+
528
+ /**
529
+ * Bind fog values from the scene to our custom uniforms
530
+ * @param {import('@babylonjs/core/Materials/uniformBuffer').UniformBuffer} uniformBuffer
531
+ * @param {import('@babylonjs/core/scene').Scene} scene
532
+ * @param {import('@babylonjs/core/Engines/engine').Engine} engine
533
+ * @param {import('@babylonjs/core/Meshes/subMesh').SubMesh} subMesh
534
+ */
535
+ bindForSubMesh(uniformBuffer, scene, engine, subMesh) {
536
+ if (scene.fogMode === 0) return;
537
+
538
+ // textFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
539
+ uniformBuffer.updateFloat4(
540
+ 'textFogInfos',
541
+ scene.fogMode,
542
+ scene.fogStart,
543
+ scene.fogEnd,
544
+ scene.fogDensity
545
+ );
546
+
547
+ // textFogColor: RGB color of the fog
548
+ uniformBuffer.updateColor3(
549
+ 'textFogColor',
550
+ scene.fogColor
551
+ );
513
552
  }
514
553
 
515
554
  /**
@@ -520,47 +559,69 @@
520
559
  }
521
560
 
522
561
  /**
523
- * Inject shader code to apply fog to emissive color
562
+ * Inject shader code to calculate fog distance and apply fog blending
524
563
  * @param {string} shaderType - 'vertex' or 'fragment'
525
564
  */
526
565
  getCustomCode(shaderType) {
566
+ if (shaderType === 'vertex') {
567
+ return {
568
+ // Declare our varying for fog distance
569
+ 'CUSTOM_VERTEX_DEFINITIONS': `
570
+ #ifdef MESHWRITER_TEXT_FOG
571
+ varying vec3 textFogDistance;
572
+ #endif
573
+ `,
574
+ // Calculate fog distance in view space at end of vertex shader
575
+ // finalWorld and positionUpdated are Babylon's built-in variables
576
+ 'CUSTOM_VERTEX_MAIN_END': `
577
+ #ifdef MESHWRITER_TEXT_FOG
578
+ vec4 textWorldPos = finalWorld * vec4(positionUpdated, 1.0);
579
+ textFogDistance = (view * textWorldPos).xyz;
580
+ #endif
581
+ `
582
+ };
583
+ }
584
+
527
585
  if (shaderType === 'fragment') {
528
586
  return {
529
- // This injection point runs just before gl_FragColor is finalized
530
- // At this point, standard fog has been applied to diffuse/ambient
531
- // but emissive contribution bypasses fog, so we re-apply fog
532
- // to the entire output to properly fade text into fog
587
+ // Declare uniforms and varying in fragment shader
588
+ 'CUSTOM_FRAGMENT_DEFINITIONS': `
589
+ #ifdef MESHWRITER_TEXT_FOG
590
+ uniform vec4 textFogInfos;
591
+ uniform vec3 textFogColor;
592
+ varying vec3 textFogDistance;
593
+ #endif
594
+ `,
595
+ // Apply fog to the entire fragment color before final output
596
+ // This runs just before gl_FragColor is set
533
597
  'CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR': `
534
598
  #ifdef MESHWRITER_TEXT_FOG
535
- #ifdef FOG
536
- // Recalculate fog for the full fragment color including emissive
537
- // vFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
538
- // vFogColor: fog RGB color
539
- // vFogDistance: vec3 distance from camera (set by vertex shader)
599
+ // textFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
600
+ // Fog modes: 1=LINEAR, 2=EXP, 3=EXP2
540
601
 
541
602
  float textFogFactor = 1.0;
542
- float textFogDist = length(vFogDistance);
603
+ float textFogDist = length(textFogDistance);
543
604
 
544
- if (FOGMODE_LINEAR == vFogInfos.x) {
605
+ if (textFogInfos.x == 1.0) {
545
606
  // Linear fog: factor = (end - dist) / (end - start)
546
- textFogFactor = clamp((vFogInfos.z - textFogDist) / (vFogInfos.z - vFogInfos.y), 0.0, 1.0);
547
- } else if (FOGMODE_EXP == vFogInfos.x) {
607
+ textFogFactor = clamp((textFogInfos.z - textFogDist) / (textFogInfos.z - textFogInfos.y), 0.0, 1.0);
608
+ } else if (textFogInfos.x == 2.0) {
548
609
  // Exponential fog: factor = exp(-dist * density)
549
- textFogFactor = clamp(exp(-textFogDist * vFogInfos.w), 0.0, 1.0);
550
- } else if (FOGMODE_EXP2 == vFogInfos.x) {
610
+ textFogFactor = clamp(exp(-textFogDist * textFogInfos.w), 0.0, 1.0);
611
+ } else if (textFogInfos.x == 3.0) {
551
612
  // Exponential squared fog: factor = exp(-(dist * density)^2)
552
- float fogDistDensity = textFogDist * vFogInfos.w;
613
+ float fogDistDensity = textFogDist * textFogInfos.w;
553
614
  textFogFactor = clamp(exp(-fogDistDensity * fogDistDensity), 0.0, 1.0);
554
615
  }
555
616
 
556
- // Blend the entire fragment (including emissive) toward fog color
617
+ // Blend the entire fragment (diffuse + emissive) toward fog color
557
618
  // textFogFactor: 1.0 = no fog (full color), 0.0 = full fog
558
- color.rgb = mix(vFogColor, color.rgb, textFogFactor);
619
+ color.rgb = mix(textFogColor, color.rgb, textFogFactor);
559
620
  #endif
560
- #endif
561
- `,
621
+ `
562
622
  };
563
623
  }
624
+
564
625
  return null;
565
626
  }
566
627
  }
@@ -625,15 +686,17 @@
625
686
  // Emissive-only materials should be self-lit and not affected by fog
626
687
  if (emissiveOnly) {
627
688
  material.fogEnabled = false;
689
+ } else if (fogEnabled) {
690
+ // IMPORTANT: Disable Babylon's built-in fog when using TextFogPlugin.
691
+ // Built-in fog only affects diffuse/ambient, not emissive.
692
+ // If we left fogEnabled=true, diffuse would be fogged twice (once by Babylon,
693
+ // once by the plugin), while emissive would only be fogged once by the plugin.
694
+ // By disabling built-in fog and handling ALL fog in the plugin, we get uniform
695
+ // fog application to the entire fragment (matching terrain behavior).
696
+ material.fogEnabled = false;
697
+ material._textFogPlugin = new TextFogPlugin(material);
628
698
  } else {
629
- material.fogEnabled = fogEnabled;
630
- // Attach fog plugin to properly blend emissive color with fog
631
- // Babylon's standard fog only affects diffuse/ambient, not emissive.
632
- // The plugin re-fogs the entire fragment output so emissive fades properly.
633
- // (The slight double-fog on diffuse/ambient is negligible since text is primarily emissive)
634
- if (fogEnabled) {
635
- material._textFogPlugin = new TextFogPlugin(material);
636
- }
699
+ material.fogEnabled = false;
637
700
  }
638
701
 
639
702
  return material;
@@ -659,7 +722,8 @@
659
722
  material.disableLighting = true;
660
723
  material.alpha = opac;
661
724
  material.backFaceCulling = false;
662
- material.fogEnabled = fogEnabled;
725
+ // Disable Babylon's built-in fog - TextFogPlugin handles all fog uniformly
726
+ material.fogEnabled = false;
663
727
  if (fogEnabled) {
664
728
  material._textFogPlugin = new TextFogPlugin(material);
665
729
  }