Making Custom shader

Hello~

i was trying to make a custom shader.
but i was not able to draw something on screen without any errors.
shaders and sample application are very simple.

can anybody let me know what i did mistake?

sorry for poor english and thanks!

[java]
public void simpleInitApp() {
Box b = new Box(1, 1, 1);
// b.setMode(Mesh.Mode.Lines);
Geometry geom = new Geometry(“Box”, b);

    Material mat = new Material(assetManager, "MatDefs/TestMaterial1.j3md");
    mat.setColor("m_Color", ColorRGBA.White);
    geom.setMaterial(mat);

    rootNode.attachChild(geom);
}

//j3md file
MaterialDef Simple {
MaterialParameters {
Vector4 m_Color
}

Technique {
    VertexShader GLSL100 : MatDefs/TestMaterial1_Vertex.vert
    FragmentShader GLSL100 : MatDefs/TestMaterial1_Fragment.frag

    WorldParameters {
        WorldViewProjectionMatrix
    }
}

}
//vertex shader…
uniform mat4 g_WorldViewProjectionMatrix;
vec4 inPosition;

void main() {
vec4 Global_position = inPosition;
//CommonVert : End
gl_Position = g_WorldViewProjectionMatrix * Global_position;
}

//fragment shader
uniform vec4 m_Color;
void main() {

// Set the fragment color for example to gray, alpha 1.0
gl_FragColor = m_Color;  

}
[/java]

inPosition is also a engine uniform.

try
[java]
attribute vec4 inPosition;
or
in vec4 inPosition;
[/java]

1 Like

First off, change GLSL100 to GLSL110. It will make errors much more apparent.

You’ve just declared a global variable:
[java]vec4 inPosition;[/java]

2 things that need changing here
1 - inPosition from jME is a vec3
2 - It’s an attribute, i.e attribute vec3 inPosition

[java]vec4 Global_position = inPosition;

// This needs changing to the following:
vec4 Global_position = vec4(inPosition, 1.0);
[/java]

There’s no implicit casting allowed unfortunately (and GLSL110 will cause that to crash, hence why I recommend it :))

3 Likes
@wezrule said: First off, change GLSL100 to GLSL110. It will make errors much more apparent.

You’ve just declared a global variable:
[java]vec4 inPosition;[/java]

2 things that need changing here
1 - inPosition from jME is a vec3
2 - It’s an attribute, i.e attribute vec3 inPosition

[java]vec4 Global_position = inPosition;

// This needs changing to the following:
vec4 Global_position = vec4(inPosition, 1.0);
[/java]

There’s no implicit casting allowed unfortunately (and GLSL110 will cause that to crash, hence why I recommend it :))

inPosition is only a vec3 if you declare it a vec3 in your vertex shader. It can be declared and used as a 4 component vec.

1 Like
@t0neg0d said: inPosition is only a vec3 if you declare it a vec3 in your vertex shader. It can be declared and used as a 4 component vec.
just be careful though, because the 4th component may be 0
1 Like
@t0neg0d said: inPosition is only a vec3 if you declare it a vec3 in your vertex shader. It can be declared and used as a 4 component vec.

Interesting, I did not know that, thanks

@nehon said: just be careful though, because the 4th component may be 0

Also interesting :slight_smile:

@nehon said: just be careful though, because the 4th component may be 0

I haven’t had this happen (not doubting that it could, in any way, shape or form).

Just for clarification for others:

inPosition = gl_position which is a vec4.

In my experience (thus far… which is very limited compared @nehon), the 4th component has been set to 1.

I guess it can depend on hardware + drivers + … direction of the wind, so to be sure i recommend setting it to 1.0.

1 Like

inPosition is set from the VertexBuffer for Type.Position. If you set a 3 element buffer then I guess it gets auto-expanded to a vec4 if you’ve declared it that way. I don’t know how it would know to put a 1 in the fourth position, though.

1 Like

Hello Everybody!

Thanks for your help!

i knew that there was some mistakes in my code from your valuable comments.

  1. attribute vec3 inPosition
    I deleted attribute keyword because editor report that’s error. I think because that’s java editor so it cannot understand shader code.

  2. m_Color
    parameters that are declared on Material parameters are assigned to parameters which have ‘m_’ prefix + parameter name of shader.

anyway because of your comments, i was able to find my mistakes!
Thanks a lot!!

1 Like
@chitoss2 said: 1. attribute vec3 inPosition I deleted attribute keyword because editor report that's error. I think because that's java editor so it cannot understand shader code.

There’s a plugin called “GLSL Editor”. If you don’t have it installed and active, you should install and activate it.

1 Like
@sgold said: There's a plugin called "GLSL Editor". If you don't have it installed and active, you should install and activate it.

thanks for the information!
i found that GLSL editor is installed in my ide but i cannot find any option to use the editor for shader files.
can you let me know how to activate GLSL editor?

I’m not aware of anything more you need to do.

http://plugins.netbeans.org/plugin/46515/glsl-syntax-highlighter

“Supports the following file extensions: glsl, vert, tessc, tesse, geom, frag, comp, vs, tcs, tes, gs, fs.”

Maybe you just need to name the files to something like this?

@jmaasing said: http://plugins.netbeans.org/plugin/46515/glsl-syntax-highlighter

“Supports the following file extensions: glsl, vert, tessc, tesse, geom, frag, comp, vs, tcs, tes, gs, fs.”

Maybe you just need to name the files to something like this?


Is that a new one?
It looks better than the one we have?

@nehon said: Is that a new one? It looks better than the one we have?

I don’t know actually, I only know of one GLSL-syntax plugin for netbeans, I assumed it was the same.

Related idea: Now that Khronos has released a reference GLSL compiler it would be great to include that in jME and “compile on save”. If there were only more hours in a day to do jME :slight_smile:

1 Like