[SOLVED] Problems encountered in shader learning

#ifdef GL_ES
precision mediump float;
#endif

uniform float u_time;

void main() {
	gl_FragColor = vec4(abs(sin(u_time)),0.0,0.0,1.0);
}

You can see this code dynamically changing in red in the browser
When I copy it to the JME Frag file, it runs in black And there’s no transition color to red and it stays black.
I wish someone would point out my mistakes

You’re relying on u_Time uniform for your animation, which needs to be set from code. In whatever environment you were using in the browser, this was done automatically, while jME by itself doesn’t set the u_Time uniform, so you’d have to either set it yourself every frame, or alternatively, you can and probably should instead use jME’s built in g_Time uniform.

2 Likes

Yep, and there is no way that “u_time” can be a uniform in JME. User defined uniforms are all prefixed with “m_”. JME defined uniforms (globals) are prefixed with “g_”.

See the whole section in the documentation starting here:

1 Like

I try to set m_Time

public class Testglsl extends SimpleApplication{

    @Override
    public void simpleInitApp() {
//    Box box= new Box(Vector3f.ZERO, 1f,1f,1f);
//   // Geometry cube = new Geometry("box", box);
//    Geometry cube = new Geometry("球体", new Sphere(10, 16, 2));
//    Material mat = new Material(assetManager,"Models/Syana/Syana.j3md");
//    cube.setMaterial(mat);
//    cube.setMaterial(assetManager.loadMaterial("Models/Syana/Syana.j3m"));



    Box box1= new Box(new Vector3f(2,2,2), 1f,1f,1f);
    Geometry cube1 = new Geometry("box", box1);
    Material mat1 = new Material(assetManager,"Models/Test/Test_1.j3md");
    mat1.setFloat("Time", 0.9f);
    cube1.setMaterial(mat1);
    
   // rootNode.attachChild(cube);
    rootNode.attachChild(cube1);
    }
    
        public static void main(String[] arguments) {
        Testglsl application = new Testglsl();
        application.start();
    }
        
}
MaterialDef Solid Color {
    //This is the complete list of user defined uniforms to be used in the
    //shaders
    MaterialParameters {
        Vector4 Color
        Float Time
    }
    Technique {
        //This is where the vertex and fragment shader files are
        //specified
        VertexShader GLSL100:   Models/Test/Test_1.vert
        FragmentShader GLSL100: Models/Test/Test_1.frag
        //This is where you specify which global uniform you need for your
        //shaders
        WorldParameters {
            WorldViewProjectionMatrix
        }
    }
    Technique FixedFunc {
    }
}
#ifdef GL_ES
precision mediump float;
#endif

uniform float m_Time;

void main() {
	gl_FragColor = vec4(abs(sin(m_Time)),-1.832,2.736,1.0);
}

The color changes when I adjust this value.
mat1.setFloat(“Time”, 0.9f);

g_Time Seems to be invalid :thinking: :thinking:

1 Like

From the docs already linked: (Emphasis added.)

JME3 Global uniforms

JME3 can expose pre-computed global uniforms to your shaders. You must specify the ones that are required for your shader in the WorldParameter’s section of the material definition file (.j3md).

So:

        WorldParameters {
            WorldViewProjectionMatrix
            Time
        }
2 Likes

Thank you for your help I saw the color transition
Desktop 2022.09.26 - 06.14.21.01

3 Likes