Shader Code does not work

I’ve got the following shader (it is supposed to calculate the distance and make it a color):

uniform vec3 g_CameraPosition;

float base(float dist, float max) {
    if(dist > max) {
        return 1;
    }
    return (dist / max);
}

void main() {
    float dist = base(distance(g_CameraPosition, inPosition), 500);
    outColor = vec4(dist, dist, dist, 0);
}

All the inputs/outputs seem to be working, but the color of the box I apply it to is still white. This means base returns 1.
However, when I calculate it in Java, it returns a distance of 10. Calculating the color = 10/500 = 0.02 (black).

Why does it show white then?
Is something wrong with my shader or my shader node?
If you need more code, just ask.

Well just try distance/10 as color and then it should just be White at that point.
You need to verify that the distance is correct and then add each Part additionally.

Maybe your distance is off since it doesnt translate the vertex to World space by multiplying with the Projection Matrix?

Or is that already inPosition?

InputMappings {
    inPosition = Global.position.xyz
    inMax = MatParam.Max
}

I tried to cheat out of using a seperate Vertex Shader by doing this.

Update: setting outColor to anything else also doesn’t work, so it’s probably a problem in my nodes.
Thanks for the help anyway.

Here is everything

  • Fragment (updated)
//uniform vec3 g_CameraPosition;

//float base(float dist, float max) {
    //if(dist > max) {
        //return 1;
    //}
    //return (dist / max);
//}

void main() {
    //float dist = base(distance(g_CameraPosition, inPosition), 500);
    //outColor = vec4(dist, dist, dist, 1);
    outColor = vec4(0.0,0.0,1.0,1.0);
}
  • Shader Node
ShaderNodeDefinitions{ 
    ShaderNodeDefinition DistanceFrag {      
        Type: Fragment

        Shader GLSL100: Shaders/data/dist.frag
        
        Documentation{
            I like cheese.
        }
        Input {
            float inMax
            vec3 inPosition;
        }
        Output {
            vec4 outColor
        }
    }
}
  • Material Definition
MaterialDef Simple {
    MaterialParameters {
        Float Max
    }
    Technique {
        WorldParameters {
            WorldViewProjectionMatrix
        }
        VertexShaderNodes {
            ShaderNode CommonVert {
                Definition : CommonVert : Common/MatDefs/ShaderNodes/Common/CommonVert.j3sn
                InputMappings {
                    worldViewProjectionMatrix = WorldParam.WorldViewProjectionMatrix
                    modelPosition = Global.position.xyz
                }
                OutputMappings {
                    Global.position = projPosition
                }
            }
        }

        FragmentShaderNodes {
            ShaderNode DistanceFrag {
                Definition : DistanceFrag : Shaders/data/distancefrag.j3sn
                InputMappings {
                    inPosition = Global.position.xyz
                    inMax = MatParam.Max
                }
                OutputMappings {
                    Global.color = outColor
                }
            }
        }
    }
}
  • Application:
package test.shader;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/**
 *
 * @author MisterCavespider
 */
public class ShaderDistanceSimple extends SimpleApplication {

    @Override
    public void simpleInitApp() {
        flyCam.setMoveSpeed(65);
        
        Geometry box = new Geometry("box", new Box(1, 1, 1));
        
        Material mat = new Material(assetManager, "Materials/distance.j3md");
        mat.setFloat("Max", 30);
        
        box.setMaterial(mat);
        rootNode.attachChild(box);
        
        float dist = cam.getLocation().distance(box.getLocalTranslation());
        System.out.println(dist);
    }
    
}

It’s either a typo, or I did something wrong somewhere.
I don’t know.

ADD:
and this is how it looks like when it’s interpreted/compiled:

1    #version 150 core
2    #define FRAGMENT_SHADER 1
3    
4    uniform float m_Max;
5    
6    
7    //uniform vec3 g_CameraPosition;
8    
9    //float base(float dist, float max) {
10        //if(dist > max) {
11            //return 1;
12        //}
13        //return (dist / max);
14    //}
15    
16    out vec4 Global_position;
17    out vec4 Global_color;
18    
19    void main(){
20        Global_position = vec4(1.0);
21        Global_color = vec4(1.0);
22    
23        //TheFrag : Begin
24        vec3 TheFrag_inPosition = Global_position.xyz;
25        vec4 TheFrag_outColor;
26    
27        //float dist = base(distance(g_CameraPosition, TheFrag_inPosition), 500);
28        //TheFrag_outColor = vec4(dist, dist, dist, 1);
29        TheFrag_outColor = vec4(0.0, 1.0, 0.5, 1.0):::::;
30        Global_color = TheFrag_outColor;
31        //TheFrag : End
32    }

Your Fragment Shader and the compiled version don’t match, but that’s a minor issue.

The real issue is that the Fragment Shader seems to be missing something like outColor = Global_color; (but it seems that happens later, maybe you need a filter?)
Maybe it’s a bug in the ShaderNodes?

What do you see in the sdk when you open it? It should preview the materials

it gives a nice visual. It seems normal to me (the shader node “Color” ouputs outColor correctly).

And to make the whole thread even more complicated: I can’t seem to make a single test case of shaders using nodes to apply any color at all.
I use:

outColor = vec4(0.0, 0.5, 1.0, 1.0);

and

outPosition = inMatrix * vec4(inPosition, 1.0);

Color is always white.

I found the solution (partially)!
If I remove any associations with position from the fragment shader, it suddenly works.

However, this brings me to another problem: the material isn’t updated. The distance works (it is rendered darker), but only for the set up situation. When I move the camera (using flyCam), the color is still just as dark.
Any smart way to update this? / Am I doing something wrong agian?