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.
@@ -520,17 +520,20 @@ function getCSGLib() {
520
520
  }
521
521
 
522
522
  /**
523
- * TextFogPlugin - MaterialPluginBase that applies fog to emissive color
523
+ * TextFogPlugin - MaterialPluginBase that applies fog to text materials
524
524
  *
525
- * Babylon's standard fog only affects diffuse/ambient channels.
526
- * This plugin recalculates fog blending for the final color output,
527
- * ensuring emissive text fades properly with distance fog.
525
+ * Since we disable Babylon's built-in fog (material.fogEnabled = false) to avoid
526
+ * double-fogging of diffuse colors, this plugin handles ALL fog application.
527
+ * It supplies its own uniforms and varyings to calculate fog distance and blend.
528
+ *
529
+ * This ensures text (with emissive colors) fades into fog at the same rate as
530
+ * terrain (which uses only diffuse colors with standard fog).
528
531
  */
529
532
 
530
533
 
531
534
  /**
532
- * Plugin that applies scene fog to text materials by modifying
533
- * the final fragment color before output.
535
+ * Plugin that applies scene fog to text materials uniformly.
536
+ * Handles fog for both diffuse and emissive components in one pass.
534
537
  */
535
538
  class TextFogPlugin extends materialPluginBase.MaterialPluginBase {
536
539
  /**
@@ -551,6 +554,7 @@ class TextFogPlugin extends materialPluginBase.MaterialPluginBase {
551
554
  */
552
555
  prepareDefines(defines, scene, mesh) {
553
556
  // Enable when scene has any fog mode set (1=LINEAR, 2=EXP, 3=EXP2)
557
+ // This is independent of material.fogEnabled since we handle fog ourselves
554
558
  defines['MESHWRITER_TEXT_FOG'] = scene.fogMode !== 0;
555
559
  }
556
560
 
@@ -558,10 +562,45 @@ class TextFogPlugin extends materialPluginBase.MaterialPluginBase {
558
562
  return 'TextFogPlugin';
559
563
  }
560
564
 
565
+ /**
566
+ * Define our own fog uniforms since material.fogEnabled = false
567
+ * means Babylon's built-in fog uniforms won't be available
568
+ */
561
569
  getUniforms() {
562
- // We use Babylon's built-in fog uniforms (vFogInfos, vFogColor)
563
- // which are already available in the standard material
564
- return { ubo: [] };
570
+ return {
571
+ ubo: [
572
+ { name: 'textFogInfos', size: 4, type: 'vec4' },
573
+ { name: 'textFogColor', size: 3, type: 'vec3' }
574
+ ],
575
+ vertex: '',
576
+ fragment: ''
577
+ };
578
+ }
579
+
580
+ /**
581
+ * Bind fog values from the scene to our custom uniforms
582
+ * @param {import('@babylonjs/core/Materials/uniformBuffer').UniformBuffer} uniformBuffer
583
+ * @param {import('@babylonjs/core/scene').Scene} scene
584
+ * @param {import('@babylonjs/core/Engines/engine').Engine} engine
585
+ * @param {import('@babylonjs/core/Meshes/subMesh').SubMesh} subMesh
586
+ */
587
+ bindForSubMesh(uniformBuffer, scene, engine, subMesh) {
588
+ if (scene.fogMode === 0) return;
589
+
590
+ // textFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
591
+ uniformBuffer.updateFloat4(
592
+ 'textFogInfos',
593
+ scene.fogMode,
594
+ scene.fogStart,
595
+ scene.fogEnd,
596
+ scene.fogDensity
597
+ );
598
+
599
+ // textFogColor: RGB color of the fog
600
+ uniformBuffer.updateColor3(
601
+ 'textFogColor',
602
+ scene.fogColor
603
+ );
565
604
  }
566
605
 
567
606
  /**
@@ -572,47 +611,69 @@ class TextFogPlugin extends materialPluginBase.MaterialPluginBase {
572
611
  }
573
612
 
574
613
  /**
575
- * Inject shader code to apply fog to emissive color
614
+ * Inject shader code to calculate fog distance and apply fog blending
576
615
  * @param {string} shaderType - 'vertex' or 'fragment'
577
616
  */
578
617
  getCustomCode(shaderType) {
618
+ if (shaderType === 'vertex') {
619
+ return {
620
+ // Declare our varying for fog distance
621
+ 'CUSTOM_VERTEX_DEFINITIONS': `
622
+ #ifdef MESHWRITER_TEXT_FOG
623
+ varying vec3 textFogDistance;
624
+ #endif
625
+ `,
626
+ // Calculate fog distance in view space at end of vertex shader
627
+ // finalWorld and positionUpdated are Babylon's built-in variables
628
+ 'CUSTOM_VERTEX_MAIN_END': `
629
+ #ifdef MESHWRITER_TEXT_FOG
630
+ vec4 textWorldPos = finalWorld * vec4(positionUpdated, 1.0);
631
+ textFogDistance = (view * textWorldPos).xyz;
632
+ #endif
633
+ `
634
+ };
635
+ }
636
+
579
637
  if (shaderType === 'fragment') {
580
638
  return {
581
- // This injection point runs just before gl_FragColor is finalized
582
- // At this point, standard fog has been applied to diffuse/ambient
583
- // but emissive contribution bypasses fog, so we re-apply fog
584
- // to the entire output to properly fade text into fog
639
+ // Declare uniforms and varying in fragment shader
640
+ 'CUSTOM_FRAGMENT_DEFINITIONS': `
641
+ #ifdef MESHWRITER_TEXT_FOG
642
+ uniform vec4 textFogInfos;
643
+ uniform vec3 textFogColor;
644
+ varying vec3 textFogDistance;
645
+ #endif
646
+ `,
647
+ // Apply fog to the entire fragment color before final output
648
+ // This runs just before gl_FragColor is set
585
649
  'CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR': `
586
650
  #ifdef MESHWRITER_TEXT_FOG
587
- #ifdef FOG
588
- // Recalculate fog for the full fragment color including emissive
589
- // vFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
590
- // vFogColor: fog RGB color
591
- // vFogDistance: vec3 distance from camera (set by vertex shader)
651
+ // textFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
652
+ // Fog modes: 1=LINEAR, 2=EXP, 3=EXP2
592
653
 
593
654
  float textFogFactor = 1.0;
594
- float textFogDist = length(vFogDistance);
655
+ float textFogDist = length(textFogDistance);
595
656
 
596
- if (FOGMODE_LINEAR == vFogInfos.x) {
657
+ if (textFogInfos.x == 1.0) {
597
658
  // Linear fog: factor = (end - dist) / (end - start)
598
- textFogFactor = clamp((vFogInfos.z - textFogDist) / (vFogInfos.z - vFogInfos.y), 0.0, 1.0);
599
- } else if (FOGMODE_EXP == vFogInfos.x) {
659
+ textFogFactor = clamp((textFogInfos.z - textFogDist) / (textFogInfos.z - textFogInfos.y), 0.0, 1.0);
660
+ } else if (textFogInfos.x == 2.0) {
600
661
  // Exponential fog: factor = exp(-dist * density)
601
- textFogFactor = clamp(exp(-textFogDist * vFogInfos.w), 0.0, 1.0);
602
- } else if (FOGMODE_EXP2 == vFogInfos.x) {
662
+ textFogFactor = clamp(exp(-textFogDist * textFogInfos.w), 0.0, 1.0);
663
+ } else if (textFogInfos.x == 3.0) {
603
664
  // Exponential squared fog: factor = exp(-(dist * density)^2)
604
- float fogDistDensity = textFogDist * vFogInfos.w;
665
+ float fogDistDensity = textFogDist * textFogInfos.w;
605
666
  textFogFactor = clamp(exp(-fogDistDensity * fogDistDensity), 0.0, 1.0);
606
667
  }
607
668
 
608
- // Blend the entire fragment (including emissive) toward fog color
669
+ // Blend the entire fragment (diffuse + emissive) toward fog color
609
670
  // textFogFactor: 1.0 = no fog (full color), 0.0 = full fog
610
- color.rgb = mix(vFogColor, color.rgb, textFogFactor);
611
- #endif
671
+ color.rgb = mix(textFogColor, color.rgb, textFogFactor);
612
672
  #endif
613
- `,
673
+ `
614
674
  };
615
675
  }
676
+
616
677
  return null;
617
678
  }
618
679
  }
@@ -677,15 +738,17 @@ function makeMaterial(scene, letters, emissive, ambient, specular, diffuse, opac
677
738
  // Emissive-only materials should be self-lit and not affected by fog
678
739
  if (emissiveOnly) {
679
740
  material.fogEnabled = false;
741
+ } else if (fogEnabled) {
742
+ // IMPORTANT: Disable Babylon's built-in fog when using TextFogPlugin.
743
+ // Built-in fog only affects diffuse/ambient, not emissive.
744
+ // If we left fogEnabled=true, diffuse would be fogged twice (once by Babylon,
745
+ // once by the plugin), while emissive would only be fogged once by the plugin.
746
+ // By disabling built-in fog and handling ALL fog in the plugin, we get uniform
747
+ // fog application to the entire fragment (matching terrain behavior).
748
+ material.fogEnabled = false;
749
+ material._textFogPlugin = new TextFogPlugin(material);
680
750
  } else {
681
- material.fogEnabled = fogEnabled;
682
- // Attach fog plugin to properly blend emissive color with fog
683
- // Babylon's standard fog only affects diffuse/ambient, not emissive.
684
- // The plugin re-fogs the entire fragment output so emissive fades properly.
685
- // (The slight double-fog on diffuse/ambient is negligible since text is primarily emissive)
686
- if (fogEnabled) {
687
- material._textFogPlugin = new TextFogPlugin(material);
688
- }
751
+ material.fogEnabled = false;
689
752
  }
690
753
 
691
754
  return material;
@@ -711,7 +774,8 @@ function makeFaceMaterial(scene, letters, emissive, opac, fogEnabled = true) {
711
774
  material.disableLighting = true;
712
775
  material.alpha = opac;
713
776
  material.backFaceCulling = false;
714
- material.fogEnabled = fogEnabled;
777
+ // Disable Babylon's built-in fog - TextFogPlugin handles all fog uniformly
778
+ material.fogEnabled = false;
715
779
  if (fogEnabled) {
716
780
  material._textFogPlugin = new TextFogPlugin(material);
717
781
  }