Skip to content

Commit

Permalink
#5768: Adjust IFace::getShiftScaleRotation() method to take the textu…
Browse files Browse the repository at this point in the history
…re dimensions into account
  • Loading branch information
codereader committed Oct 12, 2021
1 parent b25e18f commit d844e42
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 38 deletions.
28 changes: 1 addition & 27 deletions radiantcore/brush/Face.cpp
Expand Up @@ -465,33 +465,7 @@ void Face::SetTexdef(const TextureProjection& projection)

ShiftScaleRotation Face::getShiftScaleRotation() const
{
auto ssr = _texdef.getShiftScaleRotation();

// These values are going to show up in the Surface Inspector, so
// we need to make some adjustments:

// We want the shift values appear in pixels of the editor image,
// so scale up the UV values by the editor image dimensions
ssr.shift[0] *= _shader.getWidth();
ssr.shift[1] *= _shader.getHeight();

// We only need to display shift values in the range of the texture dimensions
ssr.shift[0] = float_mod(ssr.shift[0], _shader.getWidth());
ssr.shift[1] = float_mod(ssr.shift[1], _shader.getHeight());

// Surface Inspector wants to display values such that scale == 1.0 means:
// a 512-unit wide face can display the full 512px of the editor image.
// The corresponding texture matrix transform features a scale value like 1/512
// to scale the 512 XYZ coord down to 1.0 in UV space.
// Now, getFakeTexCoords() yields the reciprocal 1/scale (=> 512), to have larger scale
// values correspond to a higher "texture zoom" factor (is more intuitive that way):
// => 1024 in getFakeTexcoords() means a 512 editor image appears twice as large visually,
// even though the UV coords shrunk only span half the range.
// We divide by the image dims to receive the 1.0-like values we want to see in the entry box.
ssr.scale[0] /= _shader.getWidth();
ssr.scale[1] /= _shader.getHeight();

return ssr;
return _texdef.getShiftScaleRotation(_shader.getWidth(), _shader.getHeight());
}

void Face::setShiftScaleRotation(const ShiftScaleRotation& ssr)
Expand Down
25 changes: 19 additions & 6 deletions radiantcore/brush/TextureMatrix.cpp
Expand Up @@ -62,17 +62,30 @@ void TextureMatrix::addScale(std::size_t width, std::size_t height)
_coords[1][2] /= height;
}

ShiftScaleRotation TextureMatrix::getShiftScaleRotation() const
ShiftScaleRotation TextureMatrix::getShiftScaleRotation(std::size_t width, std::size_t height) const
{
ShiftScaleRotation ssr;

ssr.scale[0] = 1.0 / Vector2(_coords[0][0], _coords[1][0]).getLength();
ssr.scale[1] = 1.0 / Vector2(_coords[0][1], _coords[1][1]).getLength();
// These values are going to show up in the Surface Inspector, which takes the image
// dimensions into account. We stretch UV space using the image dimensions.

ssr.rotate = -radians_to_degrees(arctangent_yx(_coords[1][0], _coords[0][0]));
// Surface Inspector wants to display values such that scale == 1.0 means:
// a 512-unit wide face can display the full 512px of the editor image.
// The corresponding texture matrix transform features a scale value like 1/512
// to scale the 512 XYZ coord down to 1.0 in UV space.
ssr.scale[0] = 1.0 / Vector2(_coords[0][0] * width, _coords[1][0] * height).getLength();
ssr.scale[1] = 1.0 / Vector2(_coords[0][1] * width, _coords[1][1] * height).getLength();

ssr.shift[0] = -_coords[0][2];
ssr.shift[1] = _coords[1][2];
ssr.rotate = -radians_to_degrees(arctangent_yx(_coords[1][0] * height, _coords[0][0] * width));

// We want the shift values appear in pixels of the editor image,
// so scale up the UV values by the editor image dimensions
ssr.shift[0] = -_coords[0][2] * width;
ssr.shift[1] = _coords[1][2] * height;

// We only need to display shift values in the range of the texture dimensions
ssr.shift[0] = float_mod(ssr.shift[0], width);
ssr.shift[1] = float_mod(ssr.shift[1], height);

// determine whether or not an axis is flipped using a 2d cross-product
auto cross = Vector2(_coords[0][0], _coords[0][1]).crossProduct(Vector2(_coords[1][0], _coords[1][1]));
Expand Down
3 changes: 1 addition & 2 deletions radiantcore/brush/TextureMatrix.h
Expand Up @@ -60,8 +60,7 @@ class TextureMatrix final

// compute a fake shift scale rot representation from the texture matrix
// these shift scale rot values are to be understood in the local axis base
// Note: this code looks similar to Texdef_fromTransform, but the algorithm is slightly different.
ShiftScaleRotation getShiftScaleRotation() const;
ShiftScaleRotation getShiftScaleRotation(std::size_t width, std::size_t height) const;

// All texture-projection translation (shift) values are congruent modulo the dimensions of the texture.
// This function normalises shift values to the smallest positive congruent values.
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/brush/TextureProjection.cpp
Expand Up @@ -67,9 +67,9 @@ void TextureProjection::setTransformFromMatrix4(const Matrix4& transform)
setTransform(getTextureMatrixFromMatrix4(transform));
}

ShiftScaleRotation TextureProjection::getShiftScaleRotation() const
ShiftScaleRotation TextureProjection::getShiftScaleRotation(std::size_t width, std::size_t height) const
{
return _matrix.getShiftScaleRotation();
return _matrix.getShiftScaleRotation(width, height);
}

void TextureProjection::setFromShiftScaleRotate(const ShiftScaleRotation& ssr)
Expand Down
3 changes: 2 additions & 1 deletion radiantcore/brush/TextureProjection.h
Expand Up @@ -37,7 +37,8 @@ class TextureProjection final

void setTransform(const Matrix3& transform);

ShiftScaleRotation getShiftScaleRotation() const;
// Returns the Shift/Scale/Rotation values scaled to the given image dimensions
ShiftScaleRotation getShiftScaleRotation(std::size_t width, std::size_t height) const;
void setFromShiftScaleRotate(const ShiftScaleRotation& ssr);

Matrix3 getMatrix() const;
Expand Down

0 comments on commit d844e42

Please sign in to comment.