I am working on a custom shader, and I need to define a varying vec3 that represents the world position of each vertex without factoring in the world translation or world rotation (so I only want to know the world scale of the model in the .vert shader), but I am having trouble figuring out how to get this value.
A bit more info on what exactly I need to do this for:
I need this vec3 in order to generate a 3D noise value - and I cannot use the vertex coordinates once they have been fully transformed into world sapce, otherwise the return value of the noise equation fluctuates for every pixel as the model is translated or rotated with java code. I also cannot use the modelSpacePos before applying the WorldTransformMatrix, because many of my models using this shader were created by different artists at different scales in blender, and thus the Identity scale of each mesh varies, which also leads to inconsistent visual appearances between different models using the shader.
That is true, I could just send over the world scale as a uniform when the spatial is loaded.
Although that could add an extra step for someone else to make a mistake while loading in a spatial using the shader, so it would be more desirable to do all of the code in the shader
This works for most of my shaders that use noise, but for this unique case, I do not want the full world position, I only want to know the world scale.
So I’m hoping to learn how I can figure out which part of the WorldMatrix contains the scale. I am not very good with matrix math, but from my understanding, the value mat4 g_WorldMatrix contains the location, rotation, and scale of the model, and can be multiplied by the inPosition value to put the vertices into world space, equivalent to the method TransformWorld(inPosition) - but I do not know which part of the this g_WorldMatrix value contains the vec3 that represents only the scale.
Same here haha, I only took one calculus course in school, and I was a business major at the time, so I despised learning matrix math, and I immediately forgot everything about matrices after passing the class with the bare minimum grade. Now here I am working on game dev, wishing I had paid more attention and taken more math courses
Yes I noticed that the only thing I can find is related to Unity, when I google “how to get the world scale in glsl shader” .
But I do not know what the equivalent code would be in JME unfortunately.
Hmmm I think that could work, although I believe that would still return different values if the model is rotated. So I think I would still need to invert the rotation to get a consistent value during rotation.
Yes there is a .getScale() method for the java Transform object. Is there a way to get a Transform in the .vert shader though? As far as I have found, I can only get the full world matrix in the shader.
Sending the value as a uniform ened up working okay for this situation, I just finished changing my code and got things working.
In this situation, the shader is used exclusively by the NPCs and players in my game, who conveniently all share a parent class - so I was able pass the value as a uniform in a parent method, so now the worldScale uniform will be assigned automatically for any newly loaded spatials in the parent.
It would still be useful to know how to get the WorldScale from the world matrix in the shader, but it seems as though there isn’t any simple copy/paste solution out there to learn from like I was hoping for Doing it all in the shader would definitely be the cleaner approach
Scroll down to the matrix section. See if you can figure out how to translate m01 to a GLSL matrix element reference.
Why you can do this is a 3D math problem. A matrix3 transform is the three axes vectors in the new coordinate space. So taking the length of any of those axes will show you the scale of x, y, or z respectively.