Surface size reference in shader

Hi everyone :),

I’m trying to pick up doing some stuff in jme3 again, specifically i’m trying to create a sky shader (including clouds, stars etc.). As it was kinda hard grasping the concept of thinking pixel based, it’s going pretty good now. The problem I’m facing is that I’m doing all my calculations on specificied values, for example when I want a linear gradient (black->red) along the x-axis on a sphere with a radius of 10wu, placed on the origin, I would need to do something like this in the frag shader:

[java]
float red = (10.0 + vertex.x) / 20.0;
gl_FragColor = vec4(red, 0.0, 0.0, 1.0);
[/java]

Using that code, red will always be between 0.0 and 1.0. However when increasing the size of the sphere, red can go below 0 and higher then 1. I’m pretty sure this would also happen when moving the origin of the object, although I haven’t tried something like that yet.

Basically my question is, is there any way to get the lowest and highest values of a vertex position inside a frag shader? If not, how would one make a shader that fits on multiple objects that differ in size?

Any links to well explained shader material is also appreciated :slight_smile:

Thanks :slight_smile:

The easiest way is to pass a size value into the shader and use co-ordinates in model space.

<cite>@zarch said:</cite> The easiest way is to pass a size value into the shader and use co-ordinates in model space.
Alright, I guess I was hoping there was some predefined variable for this already, seems that is not the case :-(

I just found it a little hard to believe that you can’t re-use the same material for multiple similar objects with only different scales

You can, you need to set the model up for it though. UV mapping is the usual way.

@reveance said: Alright, I guess I was hoping there was some predefined variable for this already, seems that is not the case :-(

I just found it a little hard to believe that you can’t re-use the same material for multiple similar objects with only different scales

Sure you can. You didn’t say scale before, you said size and radius. If you create a sphere of 10 radius then the sphere is physically 20 units wide. This is not scale. The shader only sees a bunch of triangles stretched over a large space.

If you instead made a sphere of 0.5 radius and then scaled it… you can get the scale in the shader because that’s part of the world transform. I’ve found no better way to extract the scale from a world matrix other than to take the length of one of the columns: float scale = length( g_WorldMatrix[0].xyz );

Also, “reuse the same material” may be a bit of a misnomer in this case because everything about the material would be shared except the sphere size value and any other object-specific stuff you set. The shader itself would not be duplicated in this case.

Edit: or as zarch says, embed the information into the mesh using the uv coordinates or something.

1 Like
<cite>@pspeed said:</cite> Sure you can. You didn't say scale before, you said size and radius. If you create a sphere of 10 radius then the sphere is physically 20 units wide. This is not scale. The shader only sees a bunch of triangles stretched over a large space.

If you instead made a sphere of 0.5 radius and then scaled it… you can get the scale in the shader because that’s part of the world transform. I’ve found no better way to extract the scale from a world matrix other than to take the length of one of the columns: float scale = length( g_WorldMatrix[0].xyz );

Also, “reuse the same material” may be a bit of a misnomer in this case because everything about the material would be shared except the sphere size value and any other object-specific stuff you set. The shader itself would not be duplicated in this case.

Edit: or as zarch says, embed the information into the mesh using the uv coordinates or something.


Oh alright, I thought scaling an object by 2 was being treated the same way as creating an object that is twice as big, my fault.

Then I guess there’s no other way to scale materials based on the size of the object, other than passing it to the shader as a uniform.