Specification: Texture Application - noodlecollie/crowbar GitHub Wiki

Textures are projected onto brush faces by use of a texture plane, which has multiple attributes that define how the projection takes place. In laymen's terms, this can be thought of as if someone were to shine a light perpendicularly through a pane of multicoloured glass and onto a sheet of paper; moving the glass (and the light, so that it remains perpendicular) alters the shape of the colours projected onto the paper. If the paper is at an angle relative to the glass, the projection of the light will appear distorted.

In the editor, the 'pane of glass' corresponds to the texture plane, a mathematical plane whose orientation is independent of the brush's face. By modifying the translation, rotation and scale of the texture plane, the projection of the texture onto the face can be altered.

Specification

Attribute Type Description Implemented?
Texture Identifier ID The unique identifier of the texture to be applied. If it is not found, the 'missing' texture will be displayed.
Axis Space Enum Specifies what type of co-ordinate space the texture plane is specified in. The default is World, which means that the texture U and V axes are relative to the world and not to any specific brush - if the face the texture is applied to is repositioned in the world, the texture's translation on the face will appear to change. A value of Parent means that the axes are specified relative to its brush's parent - moving the brush itself will modify how the texture looks on the face, but moving the parent will not. Finally, a value of Object means that the axes are always defined relative to the brush whose face they are applied to - moving the brush will not alter the appearance of the texture.
Translation 2D Vector Specifies the translation of the texture, in texels. Specifically, it corresponds to the position of the texture plane origin on the texture itself: a translation of (0,0) means that the origin lies in the top left of the texture, whereas a translation of (5, 10) would mean that the origin lies at the top-left corner of the texture pixel (5,10). From the texture's point of view, a positive X-translation moves the texture left and a positive Y-translation moves it up.
Rotation Float Specifies the anticlockwise rotation of the texture around the origin of the texture plane, in degrees. In 3D space, this can be thought of as rotating the texture plane around its normal axis.
Scale 2D Vector Specifies the scale of the texture, in plane units per texel. Larger scale values mean more plane units must cover a single texture pixel, making the texture look stretched; smaller values will make the texture look squashed. Note that the plane units are relative to the co-ordinate system specified by Axis Space.
U Axis 3D Vector Vector specifying the U axis of the texture plane. This vector is taken to be in the co-ordinate system specified by Axis Space. The magnitude of a unit in the U axis corresponds to the length of this vector: if the U axis were (3, 0, 0) and the texture scale in U was 1, one texture pixel would cover 3 units on X in the given axis space.
V Axis 3D Vector Vector specifying the V axis of the texture plane. This vector is taken to be in the co-ordinate system specified by Axis Space. The magnitude of a unit in the V axis corresponds to the length of this vector: if the V axis were (0, 5, 0) and the texture scale in V was 1, one texture pixel would cover 5 units on Y in the given axis space.
Align to Face Bool If true, the centroid of the chosen face will be used as the texture plane origin. The specified Axis Space will still be used.

###Further Notes

  • If the U axis or the V axis are null vectors or are not linearly independent, the 'invalid projection' texture should be displayed.
  • Translation is applied first, then scaling, then rotation.

Method

  1. The texture plane is defined in 3D space by the above attributes. The plane's U and V axes are set as the given U and V, and the origin set to (0, 0, 0).

  2. The translation is applied to the origin. The X translation value should be negated, and the Y translation value left as-is, because of the inverted Y axis when comparing plane space to texture image space.

  3. The scale should be applied to the U and V axes and to the origin position. The U and V axes should be divided by their respective scales, while the origin should be componentwise-multiplied by the scale values.

  4. The U and V axes and the origin value should be rotated by the rotation value around the 3D cross product UxV.

  5. TODO: Rest of the process.

Resources

Matrices

Centre of Texel to Texture Co-ordinate

Input

Name Type Description
width Integer Width of the texture, in pixels.
height Integer Height of the texture, in pixels.

Pseudocode

Matrix4x4(
  1/width, 0, 0, 1/(2 * width),
  0, -1/height, 0, (1 - (1/(2 * height))),
  0, 0, 1, 0,
  0, 0, 0, 1
);

Upper Left Corner of Texel to Texture Co-ordinate

Input

Name Type Description
width Integer Width of the texture, in pixels.
height Integer Height of the texture, in pixels.

Pseudocode

QMatrix4x4(
  1/width, 0, 0, 0,
  0, -1/height, 0, 1,
  0, 0, 1, 0,
  0, 0, 0, 1
);