Give LocalTranslation to a frag shader?


I can build a material instance, give it to one or more geometry instances. I can change each of the geometries individually (setLocalTranslation (x, y, z)).

At the moment I can get any material-specific or texture-specific information in the “.frag” shader. If I were to give a copy of the material to every geometry, then I could give the respective material of a geometry the position of each geometry, but this is unattractive. I would want to stick with one instance of the material.

Can I and if so how, give geometry-specific information - like the LocalTranslation here - to the “.frag” shader?

This is a strange request. What are you Actually trying to do?

1 Like

I’m thinking about my own point:

I have several tiles with the same material. The tiles form my self-made map. Each part is of course shifted individually. Now I want to calculate a sphere that blends a mark / rune on the floor (better: over the texture of the floor) …

My problem is this:

You already have access to the world position of the object so I’m just wondering why local position relative to the parent is needed. I don’t understand why whatever you are doing can’t be done in either model space or world space.

Correction of my question:

How do I ask the world position of the object in the .frag shader?

I don’t have time to look up the shader-specific syntax at the moment but in concept its:
World * vec3(0.0, 0.0, 0.0)

I know there are JME provided functions for getting the world transformation of a point. The world transform of 0, 0,0 is the world position of the object.

1 Like

Sorry for wasting time on things that are probably obvious.
I really look at the most basic basics.
Is “World” a method or a variable in shader…

It’s the world transform… I don’t have time to look into shader source code to find this for you.

Edit: note that your j3md will have to have “World” is one of its bindings… a lot of them already do but some only have WorldView.

Thank you so much. Maybe someone else will answer :).

may I query “attribute vec3 inPosition” from the .vert shader in the .frag shader?

do I have to mask it somehow (since attributes in .frag
are not allowed)?

Looking for inPosition in the .vert will show you how the transform is done. It my be somethingWorldView(…) and if so then you’ll want somethingWorld(…) instead.

You don’t need inPositon in the .frag shader. inPosition is the position of the model vertext. You want the model position, ie: its own origin… which by definition is (0,0,0).

transformed into world space.

I think this will be a lot more obvious if you look at the shader source code. You must have some.

Info: my shader is:

Using TextureArray was a great recommendation from you!

1 Like

You can post your vert shader here and it should be pretty obvious how the transform is done if you can’t find it.

gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);

But I’m still thinking about how the answer before will help me …

vec3 worldPos = g_WorldMatrix * vec4(0.0, 0.0, 0.0, 1.0);


uniform mat4 g_WorldViewProjectionMatrix;
vec4 worldPos = g_WorldViewProjectionMatrix * vec4(0.0, 0.0, 0.0, 1.0);

compiles first.

I now have to think about why that calculates exactly the position of the object in the 3d space …

Thank you, need to try more now.

NOT WorldViewProjection. That will give you the wrong value.

Just g_WorldMatrix will give you the right value. When that doesn’t compile right you can look back through what I said about bindings in the j3md.

Curious… what do you think this is doing?

Sorry, g_WorldMatrix compiles too.

I found it strange too.

No, I mean I know exactly what it’s doing. I was wondering what YOU think it’s doing.

modelRelativePos → World → worldPos
worldPos → View → cameraViewRelativePos
cameraViewRelativePos → Projection → “screen space” Pos (really view projection space pos where x,y -1 to 1, etc.)

modelPos → WorldViewProjection → “screen space” pos all in one go.

But sometimes it’s good to be able to get the parts along the way… like world position.

1 Like