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