Download

Transform material UV using a Matrix

I am looking for a way to set a matrix as a parameter value of a material instance, and use it to modify the per vertex UV of a mesh.

In GLSL vertex shader, it would look like this:


layout(location = 0) in vec3 position;
uniform mat4 transform;
out vec2 uv;

void main()
{
    uv = vec2(transform * vec4(position, 1.0));
}

It’s trivial, but I can’t find any matrix related tools in the UE materials (beside some functions that emulate 3x3 matrix).
I could use vector math, but it would be way slower than a single matrix multiply.
I am using 4.9.2.

Matrix math is just series of dot products on actual hardware so it won’t be slower if you do it by hand. What kind of transformations you are storing for those matrices? If you just need scale and bias then its lot cheaper to do without matrix.

It’s a complex matrix with a bunch of rotations too. Usually a matrix multiply is a single instruction on GPU, which is why I said it’s faster than a bunch of vector math. It’s also cleaner.
But if UE don’t give access to those in materials, I’ll just do the same math manually. I was mostly curious here.

It’s not single instruction. All modern gpu’s are using scalar architechture so its more like dozen instruction. Matrix would be simpler tought. You can make Custom node that does native matrix multiplication. Only problem is that UE4 only support scalar and vector parameters. So you would need still reconstruct matrix.

Ok, thank you.
And for the second part of this question, is it possible to do this computation per vertex, and interpolate the result to the pixels?
I am not sure how UE handle the shader stages. Is it clever enough to do this optimization, or does everything that end up in “Base Color” is done in the pixel shader?
Maybe I shouldn’t bother with these things?

Only way you can move calculations to vertex shader Customized UV’s.

Even that name is uv’s it should work for other purposes too. This is also recommend way to modify uv’s for mobile content because precision issues.

That’s exactly what I needed, thank you again.