Questions about Fog of War Shaders

it seems that jme has its own fuzzing scheme but I haven’t tried it yet。
The problem I’m currently having is how should I blur the contents of the vertex shader sampler2D in the shader?

Is it possible to get the data of the vertex shader’s sampler2D in the fragment shader?(How do all this in a shader?)

I was hoping to do some blurring to deal with the angles of these meshes

#import "Common/ShaderLib/GLSLCompat.glsllib"
#import "Common/ShaderLib/Parallax.glsllib"
#import "Common/ShaderLib/Optics.glsllib"
#ifndef VERTEX_LIGHTING
    #import "Common/ShaderLib/BlinnPhongLighting.glsllib"
    #import "Common/ShaderLib/Lighting.glsllib"
#endif

// fog - jayfella
#ifdef USE_FOG
#import "Common/ShaderLib/MaterialFog.glsllib"
varying float fog_distance;
uniform vec4 m_FogColor;

#ifdef FOG_LINEAR
uniform vec2 m_LinearFog;
#endif

#ifdef FOG_EXP
uniform float m_ExpFog;
#endif

#ifdef FOG_EXPSQ
uniform float m_ExpSqFog;
#endif

#endif // end fog

varying vec2 texCoord;
#ifdef SEPARATE_TEXCOORD
  varying vec2 texCoord2;
#endif

varying vec3 AmbientSum;
varying vec4 DiffuseSum;
varying vec3 SpecularSum;

#ifndef VERTEX_LIGHTING
  uniform vec4 g_LightDirection;
  //varying vec3 vPosition;
  varying vec3 vViewDir;
  varying vec4 vLightDir;
  varying vec3 lightVec;
#else
  varying vec2 vertexLightValues;
#endif

#ifdef DIFFUSEMAP
  uniform sampler2D m_DiffuseMap;
#endif

#ifdef SPECULARMAP
  uniform sampler2D m_SpecularMap;
#endif

#ifdef PARALLAXMAP
  uniform sampler2D m_ParallaxMap;  
#endif
#if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING) 
    uniform float m_ParallaxHeight;
    varying vec3 vViewDirPrlx;
#endif

#ifdef LIGHTMAP
  uniform sampler2D m_LightMap;
#endif
  
#ifdef NORMALMAP
  uniform sampler2D m_NormalMap;   
#else
  varying vec3 vNormal;
#endif

#ifdef ALPHAMAP
  uniform sampler2D m_AlphaMap;
#endif

#ifdef COLORRAMP
  uniform sampler2D m_ColorRamp;
#endif

uniform float m_AlphaDiscardThreshold;

#ifndef VERTEX_LIGHTING
    uniform float m_Shininess;
    #ifdef USE_REFLECTION 
        uniform float m_ReflectionPower;
        uniform float m_ReflectionIntensity;
        varying vec4 refVec;

        uniform ENVMAP m_EnvMap;
    #endif
#endif

uniform sampler2D m_FowMap; // 待模糊的纹理
varying vec2 texCoord3; // 纹理坐标

const int kernelSize = 4; // 高斯核大小
uniform float kernel[kernelSize]; // 高斯核权重

void main(){
    vec2 newTexCoord;
     
    #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING) 
     
       #ifdef STEEP_PARALLAX
           #ifdef NORMALMAP_PARALLAX
               //parallax map is stored in the alpha channel of the normal map         
               newTexCoord = steepParallaxOffset(m_NormalMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
           #else
               //parallax map is a texture
               newTexCoord = steepParallaxOffset(m_ParallaxMap, vViewDirPrlx, texCoord, m_ParallaxHeight);         
           #endif
       #else
           #ifdef NORMALMAP_PARALLAX
               //parallax map is stored in the alpha channel of the normal map         
               newTexCoord = classicParallaxOffset(m_NormalMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
           #else
               //parallax map is a texture
               newTexCoord = classicParallaxOffset(m_ParallaxMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
           #endif
       #endif
    #else
       newTexCoord = texCoord;    
    #endif
    
   #ifdef DIFFUSEMAP
      vec4 diffuseColor = texture2D(m_DiffuseMap, newTexCoord);
    #else
      vec4 diffuseColor = vec4(1.0);
    #endif

    float alpha = DiffuseSum.a * diffuseColor.a;
    #ifdef ALPHAMAP
       alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
    #endif
    #ifdef DISCARD_ALPHA
        if(alpha < m_AlphaDiscardThreshold){
            discard;
        }
    #endif



    // ***********************
    // Read from textures
    // ***********************
    #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
      vec4 normalHeight = texture2D(m_NormalMap, newTexCoord);
      //Note the -2.0 and -1.0. We invert the green channel of the normal map, 
      //as it's compliant with normal maps generated with blender.
      //see http://hub.jmonkeyengine.org/forum/topic/parallax-mapping-fundamental-bug/#post-256898
      //for more explanation.
      vec3 normal = normalize((normalHeight.xyz * vec3(2.0,-2.0,2.0) - vec3(1.0,-1.0,1.0)));
      #ifdef LATC
        normal.z = sqrt(1.0 - (normal.x * normal.x) - (normal.y * normal.y));
      #endif      
    #elif !defined(VERTEX_LIGHTING)
      vec3 normal = vNormal;
      #if !defined(LOW_QUALITY) && !defined(V_TANGENT)
         normal = normalize(normal);
      #endif
    #endif

    #ifdef SPECULARMAP
      vec4 specularColor = texture2D(m_SpecularMap, newTexCoord);
    #else
      vec4 specularColor = vec4(1.0);
    #endif

    #ifdef LIGHTMAP
       vec3 lightMapColor;
       #ifdef SEPARATE_TEXCOORD
          lightMapColor = texture2D(m_LightMap, texCoord2).rgb;
       #else
          lightMapColor = texture2D(m_LightMap, texCoord).rgb;
       #endif
       specularColor.rgb *= lightMapColor;
       diffuseColor.rgb  *= lightMapColor;
    #endif

    #ifdef VERTEX_LIGHTING
       vec2 light = vertexLightValues.xy;
       #ifdef COLORRAMP
            diffuseColor.rgb  *= texture2D(m_ColorRamp, vec2(light.x, 0.0)).rgb;
            specularColor.rgb *= texture2D(m_ColorRamp, vec2(light.y, 0.0)).rgb;
            light.xy = vec2(1.0);
       #endif

       gl_FragColor.rgb =  AmbientSum     * diffuseColor.rgb + 
                           DiffuseSum.rgb * diffuseColor.rgb  * vec3(light.x) +
                           SpecularSum    * specularColor.rgb * vec3(light.y);
    #else
       vec4 lightDir = vLightDir;
       lightDir.xyz = normalize(lightDir.xyz);
       vec3 viewDir = normalize(vViewDir);
       float spotFallOff = 1.0;

       #if __VERSION__ >= 110
        // allow use of control flow
        if(g_LightDirection.w != 0.0){
       #endif
          spotFallOff =  computeSpotFalloff(g_LightDirection, lightVec);
       #if __VERSION__ >= 110
          if(spotFallOff <= 0.0){
              gl_FragColor.rgb = AmbientSum * diffuseColor.rgb;
              gl_FragColor.a   = alpha;
              return;
          }
         }        
       #endif

       vec2   light = computeLighting(normal, viewDir, lightDir.xyz, lightDir.w * spotFallOff, m_Shininess) ;
       #ifdef COLORRAMP
            diffuseColor.rgb  *= texture2D(m_ColorRamp, vec2(light.x, 0.0)).rgb;
            specularColor.rgb *= texture2D(m_ColorRamp, vec2(light.y, 0.0)).rgb;
            light.xy = vec2(1.0);
       #endif

       // Workaround, since it is not possible to modify varying variables
       vec4 SpecularSum2 = vec4(SpecularSum, 1.0);
       #ifdef USE_REFLECTION
            vec4 refColor = Optics_GetEnvColor(m_EnvMap, refVec.xyz);

            // Interpolate light specularity toward reflection color
            // Multiply result by specular map
            specularColor = mix(SpecularSum2 * light.y, refColor, refVec.w) * specularColor;

            SpecularSum2 = vec4(1.0);
            light.y = 1.0;
       #endif

       gl_FragColor.rgb =  AmbientSum       * diffuseColor.rgb  +
                           DiffuseSum.rgb   * diffuseColor.rgb  * vec3(light.x) +
                           SpecularSum2.rgb * specularColor.rgb * vec3(light.y);
    #endif


    // add fog after the lighting because shadows will cause the fog to darken
    // which just results in the geometry looking like it's changed color
    #ifdef USE_FOG
        #ifdef FOG_LINEAR
            gl_FragColor = getFogLinear(gl_FragColor, m_FogColor, m_LinearFog.x, m_LinearFog.y, fog_distance);
        #endif
        #ifdef FOG_EXP
            gl_FragColor = getFogExp(gl_FragColor, m_FogColor, m_ExpFog, fog_distance);
        #endif
        #ifdef FOG_EXPSQ
            gl_FragColor = getFogExpSquare(gl_FragColor, m_FogColor, m_ExpSqFog, fog_distance);
        #endif
    #endif // end fog


    gl_FragColor.a = alpha;
    
    vec2 texOffset = 1.0 / textureSize(m_FowMap, 0); // 纹理像素偏移量

    vec3 result = vec3(0.0); // 最终模糊结果

    // 对纹理进行多次采样并加权平均
    for (int i = 0; i < kernelSize; ++i) {
        for (int j = 0; j < kernelSize; ++j) {
            vec2 offset = vec2(i - (kernelSize / 2), j - (kernelSize / 2)) * texOffset;
            result += texture2D(m_FowMap, texCoord3 + offset).rgb * kernel[i] * kernel[j];
        }
    }

    // 输出模糊后的颜色
    gl_FragColor = vec4(result, 1.0);
}

I made some changes but it didn’t work

Things I can’t tell from what you’ve posted:
-what your fog-of-war texture size is
-what size ‘things’ you are writing to it.

It looks like you are writing big giant squares to a texture that has very high resolution.

In which case, your problem would be solved by writing single pixels to a texture whose resolution matches what you are currently writing big chunks to.

1 Like

Firstly, thank you for your reply

Use this code for initialisation
initializeFowImag(512, 512);

    /**
     * 初始化战争迷雾
     *
     * @param imageSizeW
     * @param imageSizeH
     */
    public void initializeFowImag(int imageSizeW, int imageSizeH) {
        // Create the raw FOW overlay image

        ByteBuffer data = BufferUtils.createByteBuffer(imageSizeW * imageSizeH * 4); // square image, four bytes per color
        Image image = new Image(Image.Format.ABGR8, imageSizeW, imageSizeH, data, ColorSpace.Linear);
        // Create a raster that we can draw to
        raster = ImageRaster.create(image);
        // Create the texture to set to the FowMap on materials
        Texture2D fowMap = new Texture2D(image);

        // Set the texture for any material that will take it.
        // This is a JME trick that keeps us from having to remember to set it
        // on every material.
        simpleApp.getRootNode().addMatParamOverride(new MatParamOverride(VarType.Texture2D, "FowMap", fowMap));

        // Put some test data into the fog of war texture
        for( int i = 0; i <imageSizeW; i++ ) {
            for( int j = 0; j <imageSizeH; j++ ) { 
                MapArrays.add(0); 
                IsVisible.add(0);
                visionColor.add(new ColorRGBA(0f, 0f, 0f, 1f));
            }
        }

        
    }
    #ifdef HAS_FOW
        // Get the x,z location of the vertex in world space
        vec2 worldXz = (TransformWorld(modelSpacePos)).xz;
    
        // Change the world space coordinate to texture space 0..1
        vec2 fowUv = worldXz/4; // use your actual values here in format like 0.0
        fowUv /= vec2(512, 512);

        // Sample the texture
        vec4 fow = texture2D(m_FowMap, fowUv);
        fowColor=fow;
        
        // adjust lighting by the fog of war value
        DiffuseSum *= fow;
        AmbientSum *= fow.rgb;
        SpecularSum *= fow.rgb;
        //DiffuseSum = vec4(fowUv.x, fowUv.y, 0.0, 1.0);
        //AmbientSum = vec3(fowUv.x, fowUv.y, 0.0);
        //DiffuseSum = fow;
    #endif

The size of the Map grid is 2048/8
That is, the total grid length is 2048 individual grid lengths are 8

public static MapGrid3D mapGrid3D = new MapGrid3D(2048, 2048, 8f);

I use this code to control the colour of the grid


    public static ArrayList setFowImag(int givenX, int givenY, ImageRaster raster) {
        long s = Instant.now().toEpochMilli();
        int radius = 128;
        
        float scale = (float)raster.getWidth() / mapGrid3D.getXLength(); // 计算缩放比例
        
            // 缩放给定点坐标
        int scaledX = (int)(givenX * scale);
        int scaledY = (int)(givenY * scale);
            // 缩放半径
        int scaledRadius = (int)(radius * scale);
        
        // 计算给定点所在的格子cellsize
        int gridX = (int)(scaledX / mapGrid3D.getCellsize());
        int gridY = (int)(scaledY / mapGrid3D.getCellsize());
    // 遍历半径范围内的格子
        ArrayList<Integer> VisionRange = new ArrayList<>();
        for (int x = Math.max(0, gridX - scaledRadius); x <= Math.min(raster.getHeight() - 1, gridX + scaledRadius); x++) {
            for (int y = Math.max(0, gridY - scaledRadius); y <= Math.min(raster.getWidth() - 1, gridY + scaledRadius); y++) {
                // 计算格子的实际坐标(在纹理网格中的位置坐标)
                int currentGridX = (int)(x * mapGrid3D.getCellsize());
                int currentGridY = (int)(y * mapGrid3D.getCellsize());

                // 计算给定点到当前格子的距离
                double distanceToGivenPoint = Math.sqrt(Math.pow(scaledX - currentGridX, 2) + Math.pow(scaledY - currentGridY, 2));

                // 如果距离小于或等于缩放后的半径,输出坐标
                if (distanceToGivenPoint <= scaledRadius) {
                    for (int i = 0; i <= mapGrid3D.getCellsize(); i++) {
                        for (int j = 0; j <= mapGrid3D.getCellsize(); j++) {

                            if (currentGridX + j >= 0 && currentGridX + j < raster.getWidth() && currentGridY + i >= 0 && currentGridY + i < raster.getHeight()) {
                                int q = currentGridX + j;
                                int w = currentGridY + i;
                                int index =  w * (int) raster.getWidth() + q;

           
                      //   raster.setPixel(q, w, color);

                                VisionRange.add(index);
                                
                            }
                        }
                    }

                }
            }
        }
        System.err.println(" 耗时"+(Instant.now().toEpochMilli()-s)/1000D+"秒");
        
        return VisionRange;
        
    }
    public static ArrayList setFowImag(int givenX, int givenY, ImageRaster raster) {
        long s = Instant.now().toEpochMilli();
        int radius = 128;
        
        float scale = (float)raster.getWidth() / mapGrid3D.getXLength(); // 计算缩放比例
        
            // 缩放给定点坐标
        int scaledX = (int)(givenX * scale);
        int scaledY = (int)(givenY * scale);
            // 缩放半径
        int scaledRadius = (int)(radius * scale);
        
        // 计算给定点所在的格子cellsize
                int gridX = (int)(scaledX);
                int gridY = (int)(scaledY);
    // 遍历半径范围内的格子
        ArrayList<Integer> VisionRange = new ArrayList<>();
        for (int x = Math.max(0, gridX - scaledRadius); x <= Math.min(raster.getHeight() - 1, gridX + scaledRadius); x++) {
            for (int y = Math.max(0, gridY - scaledRadius); y <= Math.min(raster.getWidth() - 1, gridY + scaledRadius); y++) {
                // 计算格子的实际坐标(在纹理网格中的位置坐标)
                int currentGridX = (int)(x);
                int currentGridY = (int)(y);

                // 计算给定点到当前格子的距离
                double distanceToGivenPoint = Math.sqrt(Math.pow(scaledX - currentGridX, 2) + Math.pow(scaledY - currentGridY, 2));

                // 如果距离小于或等于缩放后的半径,输出坐标
                if (distanceToGivenPoint <= scaledRadius) {
                    for (int i = 0; i <= mapGrid3D.getCellsize(); i++) {
                        for (int j = 0; j <= mapGrid3D.getCellsize(); j++) {

                            if (currentGridX + j >= 0 && currentGridX + j < raster.getWidth() && currentGridY + i >= 0 && currentGridY + i < raster.getHeight()) {
                                int q = currentGridX + j;
                                int w = currentGridY + i;
                                int index =  w * (int) raster.getWidth() + q;

                        // 设置像素的颜色为插值后的颜色
//                         raster.setPixel(q, w, color);

                                VisionRange.add(index);
                                
                            }
                        }
                    }

                }
            }
        }
        System.err.println(" 耗时"+(Instant.now().toEpochMilli()-s)/1000D+"秒");
        
        return VisionRange;
        
    }
                int gridX = (int)(scaledX);
                int gridY = (int)(scaledY);
                int currentGridX = (int)(x);
                int currentGridY = (int)(y);

You seem to be right I changed the four values and now it looks like this


The jaggedness isn’t so noticeable anymore

I think it may also need to be blurred properly :face_with_raised_eyebrow:

What about pass an array of Vector3f(x, y, radius) to the fragment shader, and calculate uv distance to each point?

Notice: the x,y coordinate and radius should be normalized to [0, 1].

#define EDGE_BLUR 0.03
#define MAX_ENTITY 100
uniform vec3 u_Entity[MAX_ENTITY];
uniform int u_EntityCount;

varying vec2 v_TexCoord;

float fog(vec2 pos) {
    float color = 1.0;
    for (int i = 0; i < u_EntityCount; i++) {
        vec3 entity = u_Entity[i];
        float r = entity.z;
        float dist = length(pos - entity.xy);
        float inCircle = smoothstep(r-EDGE_BLUR, r, dist);
        color *= inCircle;
    }
    return 1.0-color;
}

void main(void) {
    vec2 uv = v_TexCoord.xy;

    vec3 color = vec3(fog(uv));

    gl_FragColor = vec4(color, 1.0);
}

Also, you can use model space vertex position to calculate the fog, and do not normalized the entity (x, y, radius) to texture coordination, switch is more pixel accurate.

1 Like

After some additional knowledge, I made the following changes to the code

#import "Common/ShaderLib/GLSLCompat.glsllib"
#import "Common/ShaderLib/Instancing.glsllib"
#import "Common/ShaderLib/Skinning.glsllib"
#import "Common/ShaderLib/Lighting.glsllib"
#import "Common/ShaderLib/MorphAnim.glsllib"

#ifdef VERTEX_LIGHTING
    #import "Common/ShaderLib/BlinnPhongLighting.glsllib"    
#endif

varying vec4 fowColor;

// fog - jayfella
#ifdef USE_FOG
varying float fog_distance;
uniform vec3 g_CameraPosition;
#endif

uniform vec4 m_Ambient;
uniform vec4 m_Diffuse;
uniform vec4 m_Specular;
uniform float m_Shininess;

uniform vec4 g_LightColor;
uniform vec4 g_LightPosition;
uniform vec4 g_AmbientLightColor;

varying vec2 texCoord;
#ifdef SEPARATE_TEXCOORD
  varying vec2 texCoord2;
  attribute vec2 inTexCoord2;
#endif

varying vec3 AmbientSum;
varying vec4 DiffuseSum;
varying vec3 SpecularSum;

attribute vec3 inPosition;
attribute vec2 inTexCoord;
attribute vec3 inNormal;

varying vec3 lightVec;

#ifdef VERTEX_COLOR
  attribute vec4 inColor;
#endif

#ifdef HAS_FOW
    uniform sampler2D m_FowMap;
    varying vec2 fogTexCoord;
#endif

#ifndef VERTEX_LIGHTING
  attribute vec4 inTangent;

  #ifndef NORMALMAP
    varying vec3 vNormal;
  #endif  
  varying vec3 vViewDir;
  varying vec4 vLightDir;
#else
  varying vec2 vertexLightValues;
  uniform vec4 g_LightDirection;
#endif

#if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING) 
    varying vec3 vViewDirPrlx;
#endif

#ifdef USE_REFLECTION
    uniform vec3 g_CameraPosition;

    uniform vec3 m_FresnelParams;
    varying vec4 refVec;

    /**
     * Input:
     * attribute inPosition
     * attribute inNormal
     * uniform g_WorldMatrix
     * uniform g_CameraPosition
     *
     * Output:
     * varying refVec
     */
    void computeRef(in vec4 modelSpacePos){
        // vec3 worldPos = (g_WorldMatrix * modelSpacePos).xyz;
        vec3 worldPos = TransformWorld(modelSpacePos).xyz;

        vec3 I = normalize( g_CameraPosition - worldPos  ).xyz;
        // vec3 N = normalize( (g_WorldMatrix * vec4(inNormal, 0.0)).xyz );
        vec3 N = normalize( TransformWorld(vec4(inNormal, 0.0)).xyz );

        refVec.xyz = reflect(I, N);
        refVec.w   = m_FresnelParams.x + m_FresnelParams.y * pow(1.0 + dot(I, N), m_FresnelParams.z);
    }
#endif

void main(){
   vec4 modelSpacePos = vec4(inPosition, 1.0);
   vec3 modelSpaceNorm = inNormal;
   
   #ifndef VERTEX_LIGHTING
        vec3 modelSpaceTan  = inTangent.xyz;
   #endif

   #ifdef NUM_MORPH_TARGETS
        #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
           Morph_Compute(modelSpacePos, modelSpaceNorm, modelSpaceTan);
        #else
           Morph_Compute(modelSpacePos, modelSpaceNorm);
        #endif
   #endif

   #ifdef NUM_BONES
        #ifndef VERTEX_LIGHTING
        Skinning_Compute(modelSpacePos, modelSpaceNorm, modelSpaceTan);
        #else
        Skinning_Compute(modelSpacePos, modelSpaceNorm);
        #endif
   #endif

   gl_Position = TransformWorldViewProjection(modelSpacePos);// g_WorldViewProjectionMatrix * modelSpacePos;
   texCoord = inTexCoord;
   #ifdef SEPARATE_TEXCOORD
      texCoord2 = inTexCoord2;
   #endif

   vec3 wvPosition = TransformWorldView(modelSpacePos).xyz;// (g_WorldViewMatrix * modelSpacePos).xyz;
   vec3 wvNormal  = normalize(TransformNormal(modelSpaceNorm));//normalize(g_NormalMatrix * modelSpaceNorm);
   vec3 viewDir = normalize(-wvPosition);
  
   vec4 wvLightPos = (g_ViewMatrix * vec4(g_LightPosition.xyz,clamp(g_LightColor.w,0.0,1.0)));
   wvLightPos.w = g_LightPosition.w;
   vec4 lightColor = g_LightColor;

   #if (defined(NORMALMAP) || defined(PARALLAXMAP)) && !defined(VERTEX_LIGHTING)
     vec3 wvTangent = normalize(TransformNormal(modelSpaceTan));
     vec3 wvBinormal = cross(wvNormal, wvTangent);
     mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal);
   #endif
 
   #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
     vViewDir  = -wvPosition * tbnMat;    
     #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) 
         vViewDirPrlx = vViewDir;
     #endif
     lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec);
     vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz;
   #elif !defined(VERTEX_LIGHTING)
     vNormal = wvNormal;
     vViewDir = viewDir;
     #if defined(PARALLAXMAP)
        vViewDirPrlx  =  -wvPosition * tbnMat;
     #endif
     lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec);
   #endif

   #ifdef MATERIAL_COLORS
      AmbientSum  = (m_Ambient  * g_AmbientLightColor).rgb;
      DiffuseSum  =  m_Diffuse  * vec4(lightColor.rgb, 1.0);
      SpecularSum = (m_Specular * lightColor).rgb;
    #else
      // Defaults: Ambient and diffuse are white, specular is black.
      AmbientSum  = g_AmbientLightColor.rgb;
      DiffuseSum  =  vec4(lightColor.rgb, 1.0);
      SpecularSum = vec3(0.0);
    #endif

    #ifdef HAS_FOW
   // Get the x,z location of the vertex in world space
    vec2 worldXz = (TransformWorld(modelSpacePos)).xz;
    
    // Change the world space coordinate to texture space 0..1
    vec2 fowUv = worldXz / 4.0; // Adjust scale as needed
    fowUv /= vec2(512.0, 512.0);

    // Sample the texture
    vec4 fow = texture2D(m_FowMap, fowUv);

    // Apply Gaussian blur to the sampled color
    float blurAmount = 0.01; // Adjust as needed
    vec4 blurredFowColor = vec4(0.0);
    for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
            vec2 offset = vec2(i, j) * blurAmount;
            blurredFowColor += texture2D(m_FowMap, fowUv + offset);
        }
    }
    blurredFowColor /= 25.0; // Adjust normalization factor

    // Apply the blurred color to the output
    fowColor = blurredFowColor;
    
    // Adjust lighting by the fog of war value
    DiffuseSum *= fowColor;
    AmbientSum *= fowColor.rgb;
    SpecularSum *= fowColor.rgb;
    #endif
    
    #ifdef VERTEX_COLOR
      AmbientSum *= inColor.rgb;
      DiffuseSum *= inColor;
    #endif
    
    //DiffuseSum = vec4(1.0, 0.0, 0.0, 1.0);

    #ifdef VERTEX_LIGHTING
        float spotFallOff = 1.0;
        vec4 vLightDir;
        lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec);
        #if __VERSION__ >= 110
            // allow use of control flow
        if(lightColor.w > 1.0){
        #endif           
           spotFallOff = computeSpotFalloff(g_LightDirection, lightVec);
        #if __VERSION__ >= 110           
        }
        #endif
        
        vertexLightValues = computeLighting(wvNormal, viewDir, vLightDir.xyz, vLightDir.w * spotFallOff, m_Shininess);
    #endif

    #ifdef USE_REFLECTION 
        computeRef(modelSpacePos);
    #endif

    #ifdef USE_FOG
    fog_distance = distance(g_CameraPosition, (TransformWorld(modelSpacePos)).xyz);
    #endif
}
    #ifdef HAS_FOW
   // Get the x,z location of the vertex in world space
    vec2 worldXz = (TransformWorld(modelSpacePos)).xz;
    
    // Change the world space coordinate to texture space 0..1
    vec2 fowUv = worldXz / 4.0; // Adjust scale as needed
    fowUv /= vec2(512.0, 512.0);

    // Sample the texture
    vec4 fow = texture2D(m_FowMap, fowUv);

    // Apply Gaussian blur to the sampled color
    float blurAmount = 0.01; // Adjust as needed
    vec4 blurredFowColor = vec4(0.0);
    for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
            vec2 offset = vec2(i, j) * blurAmount;
            blurredFowColor += texture2D(m_FowMap, fowUv + offset);
        }
    }
    blurredFowColor /= 25.0; // Adjust normalization factor

    // Apply the blurred color to the output
    fowColor = blurredFowColor;
    
    // Adjust lighting by the fog of war value
    DiffuseSum *= fowColor;
    AmbientSum *= fowColor.rgb;
    SpecularSum *= fowColor.rgb;
    #endif

Shaders are a bit gruelling to learn I’ve spent the last 7 hours doing some hard exploring and so far it looks like this


barely see the jaggedness problem anymore

2 Likes