## [Tutorial] uvq coordinates, or: The Magic Q

### [Tutorial] uvq coordinates, or: The Magic Q

I'm not sure if this is common knowledge (and only I missed it), but I'd like to share what enlightened me yesterday:

Texture coordinates are not so obvious to understand, especially because SketchUp gives us a third "Q" coordinate but it doesn't allow us to set the third coordinate (correct me if I'm wrong). The U and V coordinates alone didn't match to the start/end of the image. We then used to divide U/Q and V/Q and Q/Q=1 and got values that made sense.
However further calculations/transformations are difficult since a projected texture appears as a quadrilateral (which would need 4×4 matrix calculation) and it's hard to keep projectively distorted textures aligned if they where not projected from the same plane.
UVQ2XYZ.png

While a projectively distorted texture appears as a quadrilateral (unregular quad) on a face in model space, when we change into texture space it's all the opposite and the texture image is a square (for texturing it doesn't matter how many pixels wide/high an image actually is). In fact, texture coordinates with Q=1 are the projection of 3 dimensional texture coordinates into the plane of the face. And Q relates to the distance how far a texture coordinate is "behind" the face, in texture space.
Every quadrilateral can be seen as a projection of a perfect rectangle of some unknown rotation in 3d space. As long as we know the Q coordinate (= the distance how far a texture coordinate is in texture space), we do not necessarily need to calculate with quadrilaterals.

That gives us three conclusions:

1. We have access to the projective distortion of a texture and can even calculate the transformation matrix (by using SketchUp's own Geom::Transformation with a 3×3 matrix, see here). This allows us even to find where features/pixels of a texture are in model space!
2. To quickly find out whether a texture is projectively distorted (not whether it has the "projected" property set for copy-painting), we can check whether the Q coordinate is different from 1. SketchUp's TextureWriter then bakes the distortion into an image because other softwares would maybe only support UV without Q. All other sorts of skewing/scaling (=affine transformations) produce a Q=1 and export all fine as one image file.
3. (As long as the Face.position_material method ignores the third value) Depending on the use case, we should only divide the U, V, Q values by Q at the very end before applying the texture to a face.
2
Last edited by Aerilius on Tue Aug 14, 2012 6:46 pm, edited 1 time in total.

Aerilius
PluginStore Author

### Re: [Tutorial] uvq coordinates, or: The Magic Q

Aerilius wrote: And Q relates to the distance how far a texture coordinate is "behind" the face, in texture space.

By what unit? For U and V 0.0 to 1.0 relates to the width and height of the texture, but what does Z represent?

Aerilius wrote:(As long as the Face.position_material method ignores the third value)

So you tried this?

Aerilius wrote:We should only divide the U, V, Q values by Q at the very end before applying the texture to a face.

So if you are going to rotate a texture, apply the rotation to the raw UVQ data, then divide by Q?
0
Thomas Thomassen — SketchUp Monkey & Coding addict

thomthom
PluginStore Author

### Re: [Tutorial] uvq coordinates, or: The Magic Q

thomthom wrote:
Aerilius wrote:(As long as the Face.position_material method ignores the third value)

So you tried this?

I had tried it some time ago, but didn't observe an effect. I have to try it again with this new knowledge later this evening. It's possible that I just did it wrong (like giving for point/uvq pairs where the q value don't give a planar face in uvq space). So no offense to the Ruby API in the case it works indeed.

thomthom wrote:So if you are going to rotate a texture, apply the rotation to the raw UVQ data, then divide by Q??

Probably depends on what you want to rotate, (more likely) the projection as it is, or the texture image "before" it is projectively distorted (I updated my description to be more relative).
0

Aerilius
PluginStore Author

### Re: [Tutorial] uvq coordinates, or: The Magic Q

Hi. I have a question. Heretofore I have seen stuff on UV coordinates, and somewhere I thought I saw the term W used. Is this Q in Sketchup the equivalent of the W from wherever?
0
I take the slow, deliberate approach in my aimless wandering.

mitcorb
Top SketchUcator

### Re: [Tutorial] uvq coordinates, or: The Magic Q

I've read UVW somewhere as well. We have to find out more.

For UVW mapping Wikipedia says no more than: "the third dimension allows texture maps to wrap in complex ways onto irregular surfaces"

A Google search shows UVQ is mostly related with SketchUp, and that the names of these coordinates are not very standardized (just letters in the alphabet before x,y,z), for example there are also Q,R,S,T.

There's an interesting explanation on gamedev.stackexchange, that the W allows to optionally apply a transformation matrix before rendering a texture to screen (for example for animating textures without expensive moving of the texture on every entity).
0

Aerilius
PluginStore Author