meshwriter-cudu 3.0.2 → 3.0.4

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.
Files changed (33) hide show
  1. package/dist/meshwriter.cjs.js +112 -39
  2. package/dist/meshwriter.cjs.js.map +1 -1
  3. package/dist/meshwriter.esm.js +112 -39
  4. package/dist/meshwriter.esm.js.map +1 -1
  5. package/dist/meshwriter.min.js +1 -1
  6. package/dist/meshwriter.min.js.map +1 -1
  7. package/dist/meshwriter.umd.js +112 -39
  8. package/dist/meshwriter.umd.js.map +1 -1
  9. package/dist/src/fogPlugin.d.ts +35 -4
  10. package/fonts/baked/atkinson-hyperlegible-next-200.js +1973 -0
  11. package/fonts/baked/atkinson-hyperlegible-next-200.json +1 -1
  12. package/fonts/baked/atkinson-hyperlegible-next-300.js +1995 -0
  13. package/fonts/baked/atkinson-hyperlegible-next-300.json +1 -1
  14. package/fonts/baked/atkinson-hyperlegible-next-400.js +1949 -0
  15. package/fonts/baked/atkinson-hyperlegible-next-400.json +1 -1
  16. package/fonts/baked/atkinson-hyperlegible-next-500.js +1981 -0
  17. package/fonts/baked/atkinson-hyperlegible-next-500.json +1 -1
  18. package/fonts/baked/atkinson-hyperlegible-next-600.js +1982 -0
  19. package/fonts/baked/atkinson-hyperlegible-next-600.json +1 -1
  20. package/fonts/baked/atkinson-hyperlegible-next-700.js +1974 -0
  21. package/fonts/baked/atkinson-hyperlegible-next-700.json +1 -1
  22. package/fonts/baked/atkinson-hyperlegible-next-800.js +1977 -0
  23. package/fonts/baked/atkinson-hyperlegible-next-800.json +1 -1
  24. package/fonts/baked/manifest.json +10 -28
  25. package/package.json +3 -2
  26. package/src/fogPlugin.js +100 -30
  27. package/src/material.js +12 -9
  28. package/fonts/baked/atkinson-hyperlegible-next-250.json +0 -1
  29. package/fonts/baked/atkinson-hyperlegible-next-350.json +0 -1
  30. package/fonts/baked/atkinson-hyperlegible-next-450.json +0 -1
  31. package/fonts/baked/atkinson-hyperlegible-next-550.json +0 -1
  32. package/fonts/baked/atkinson-hyperlegible-next-650.json +0 -1
  33. package/fonts/baked/atkinson-hyperlegible-next-750.json +0 -1
@@ -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,53 @@ 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
+ *
565
+ * - ubo: Used when Uniform Buffer Objects are enabled (Safari)
566
+ * - fragment: Used when UBOs are disabled (Chrome) - provides standalone declarations
567
+ *
568
+ * Babylon.js chooses between these based on engine capabilities.
569
+ */
557
570
  getUniforms() {
558
- // We use Babylon's built-in fog uniforms (vFogInfos, vFogColor)
559
- // which are already available in the standard material
560
- return { ubo: [] };
571
+ return {
572
+ ubo: [
573
+ { name: 'textFogInfos', size: 4, type: 'vec4' },
574
+ { name: 'textFogColor', size: 3, type: 'vec3' }
575
+ ],
576
+ vertex: '',
577
+ fragment: `
578
+ uniform vec4 textFogInfos;
579
+ uniform vec3 textFogColor;
580
+ `
581
+ };
582
+ }
583
+
584
+ /**
585
+ * Bind fog values from the scene to our custom uniforms
586
+ * @param {import('@babylonjs/core/Materials/uniformBuffer').UniformBuffer} uniformBuffer
587
+ * @param {import('@babylonjs/core/scene').Scene} scene
588
+ * @param {import('@babylonjs/core/Engines/engine').Engine} engine
589
+ * @param {import('@babylonjs/core/Meshes/subMesh').SubMesh} subMesh
590
+ */
591
+ bindForSubMesh(uniformBuffer, scene, engine, subMesh) {
592
+ if (scene.fogMode === 0) return;
593
+
594
+ // textFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
595
+ uniformBuffer.updateFloat4(
596
+ 'textFogInfos',
597
+ scene.fogMode,
598
+ scene.fogStart,
599
+ scene.fogEnd,
600
+ scene.fogDensity
601
+ );
602
+
603
+ // textFogColor: RGB color of the fog
604
+ uniformBuffer.updateColor3(
605
+ 'textFogColor',
606
+ scene.fogColor
607
+ );
561
608
  }
562
609
 
563
610
  /**
@@ -568,47 +615,70 @@ class TextFogPlugin extends MaterialPluginBase {
568
615
  }
569
616
 
570
617
  /**
571
- * Inject shader code to apply fog to emissive color
618
+ * Inject shader code to calculate fog distance and apply fog blending
572
619
  * @param {string} shaderType - 'vertex' or 'fragment'
573
620
  */
574
621
  getCustomCode(shaderType) {
622
+ if (shaderType === 'vertex') {
623
+ return {
624
+ // Declare our varying for fog distance
625
+ 'CUSTOM_VERTEX_DEFINITIONS': `
626
+ #ifdef MESHWRITER_TEXT_FOG
627
+ varying vec3 textFogDistance;
628
+ #endif
629
+ `,
630
+ // Calculate fog distance in view space at end of vertex shader
631
+ // finalWorld and positionUpdated are Babylon's built-in variables
632
+ 'CUSTOM_VERTEX_MAIN_END': `
633
+ #ifdef MESHWRITER_TEXT_FOG
634
+ vec4 textWorldPos = finalWorld * vec4(positionUpdated, 1.0);
635
+ textFogDistance = (view * textWorldPos).xyz;
636
+ #endif
637
+ `
638
+ };
639
+ }
640
+
575
641
  if (shaderType === 'fragment') {
576
642
  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
643
+ // Declare varying in fragment shader
644
+ // Note: textFogInfos and textFogColor uniforms come from the Material UBO
645
+ // (defined in getUniforms). Don't redeclare them here - Safari's strict
646
+ // GLSL compiler rejects duplicate declarations.
647
+ 'CUSTOM_FRAGMENT_DEFINITIONS': `
648
+ #ifdef MESHWRITER_TEXT_FOG
649
+ varying vec3 textFogDistance;
650
+ #endif
651
+ `,
652
+ // Apply fog to the entire fragment color before final output
653
+ // This runs just before gl_FragColor is set
581
654
  'CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR': `
582
655
  #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)
656
+ // textFogInfos: x=fogMode, y=fogStart, z=fogEnd, w=fogDensity
657
+ // Fog modes: 1=LINEAR, 2=EXP, 3=EXP2
588
658
 
589
659
  float textFogFactor = 1.0;
590
- float textFogDist = length(vFogDistance);
660
+ float textFogDist = length(textFogDistance);
591
661
 
592
- if (FOGMODE_LINEAR == vFogInfos.x) {
662
+ if (textFogInfos.x == 1.0) {
593
663
  // 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) {
664
+ textFogFactor = clamp((textFogInfos.z - textFogDist) / (textFogInfos.z - textFogInfos.y), 0.0, 1.0);
665
+ } else if (textFogInfos.x == 2.0) {
596
666
  // Exponential fog: factor = exp(-dist * density)
597
- textFogFactor = clamp(exp(-textFogDist * vFogInfos.w), 0.0, 1.0);
598
- } else if (FOGMODE_EXP2 == vFogInfos.x) {
667
+ textFogFactor = clamp(exp(-textFogDist * textFogInfos.w), 0.0, 1.0);
668
+ } else if (textFogInfos.x == 3.0) {
599
669
  // Exponential squared fog: factor = exp(-(dist * density)^2)
600
- float fogDistDensity = textFogDist * vFogInfos.w;
670
+ float fogDistDensity = textFogDist * textFogInfos.w;
601
671
  textFogFactor = clamp(exp(-fogDistDensity * fogDistDensity), 0.0, 1.0);
602
672
  }
603
673
 
604
- // Blend the entire fragment (including emissive) toward fog color
674
+ // Blend the entire fragment (diffuse + emissive) toward fog color
605
675
  // textFogFactor: 1.0 = no fog (full color), 0.0 = full fog
606
- color.rgb = mix(vFogColor, color.rgb, textFogFactor);
607
- #endif
676
+ color.rgb = mix(textFogColor, color.rgb, textFogFactor);
608
677
  #endif
609
- `,
678
+ `
610
679
  };
611
680
  }
681
+
612
682
  return null;
613
683
  }
614
684
  }
@@ -673,15 +743,17 @@ function makeMaterial(scene, letters, emissive, ambient, specular, diffuse, opac
673
743
  // Emissive-only materials should be self-lit and not affected by fog
674
744
  if (emissiveOnly) {
675
745
  material.fogEnabled = false;
746
+ } else if (fogEnabled) {
747
+ // IMPORTANT: Disable Babylon's built-in fog when using TextFogPlugin.
748
+ // Built-in fog only affects diffuse/ambient, not emissive.
749
+ // If we left fogEnabled=true, diffuse would be fogged twice (once by Babylon,
750
+ // once by the plugin), while emissive would only be fogged once by the plugin.
751
+ // By disabling built-in fog and handling ALL fog in the plugin, we get uniform
752
+ // fog application to the entire fragment (matching terrain behavior).
753
+ material.fogEnabled = false;
754
+ material._textFogPlugin = new TextFogPlugin(material);
676
755
  } 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
- }
756
+ material.fogEnabled = false;
685
757
  }
686
758
 
687
759
  return material;
@@ -707,7 +779,8 @@ function makeFaceMaterial(scene, letters, emissive, opac, fogEnabled = true) {
707
779
  material.disableLighting = true;
708
780
  material.alpha = opac;
709
781
  material.backFaceCulling = false;
710
- material.fogEnabled = fogEnabled;
782
+ // Disable Babylon's built-in fog - TextFogPlugin handles all fog uniformly
783
+ material.fogEnabled = false;
711
784
  if (fogEnabled) {
712
785
  material._textFogPlugin = new TextFogPlugin(material);
713
786
  }