3ds max how to convert coordinates

Hi,
Im trying to write a custom exporter in maxscript, but I cant figure out how to convert the texture coordinates to KHR_texture_transform?
Can somebody help me with this one?

image

Adding @Drigax our exporter master.

Hey @Jovce_Malakovski, welcome to the forum!

Any reason why you are writing your own exporter? The Babylon.js exporters can be invoked via maxscript, and we’re always open to feature additions and improvements.

But I’ll see if I can help answer this.

I’m going to start by breaking down each component:

UVOffset is our texture translation. In 3ds Max, texture 0,0 is measured at the bottom left of the texture, while in glTF 0,0 is at the top left.


But why is U inverted? KHR_Texture_Transform does not necessarily transform the texture, rather the extension is intended to transform the UV coordinates, so we need to kind of think in reverse and imagine that we’re moving the uv shell around as a window to the texture using these transforms:

If we don’t invert the U offset, we instead get:


We don’t invert V because our coordinate system is already inverted!

Ok, now rotation. Rotation is a bit of a mess. Mainly because of how we apply rotations between the programs. glTF applies rotation to the mesh UV coordinates, using the origin as our pivot. 3dsMax rotates the texture using the texture center as the pivot.

Here is a 45deg wAngle rotation applied in 3dsMax vs how its interpreted in glTF:

A workaround we use is to construct a transformation matrix to apply the rotation relative to the center pivot, but Currently i think our implementation is bugged as well though, looks like our matrix is missing a transformation…:


Scale:
Similarily, if we directly copy over scaling/tiling, the values are “correct”, but we run into the same change in pivot issues that need to be worked around:


The current exporter implementation tries to correct this, but it looks like some of our math is wrong…


Now, lets look at combining transformations…currently its a mess as well. mainly due to a known issue in the schema:

Basically we’re computing our texture transformations incorrectly, so a combination of rotation+scale will result in the shearing of your texture based on your scaling values, as opposed to a proper rotation of a scaled texture. I don’t know if there’s a way around this to be honest, other than using a custom shader to correctly calculate the texture values during rendering… Using identical U and V scaling gives us an incorrect transformation still using the current implementation:


In short, its hard. But its not completely impossible to get a good result for most cases.

3 Likes

Thanks @Drigax for the great answer.
I’m writing an exporter for Vray materials only, I just find out about babylon.js and I was using the max exporter to check if my values are correct.
I will see tomorrow if I can use the Max2Babylon.dll instead of trying to figure this one on my own.

Yeah, if you’d like to help us add Vray material support to the exporter plug-in, I’d greatly appreciate it! Currently we only support the built in materials, but it might be less work to contribute rather than building your own from scratch

1 Like