From d844e42815c7421c58f91ad262c4692e6c92a6de Mon Sep 17 00:00:00 2001 From: codereader Date: Tue, 12 Oct 2021 20:32:26 +0200 Subject: [PATCH] #5768: Adjust IFace::getShiftScaleRotation() method to take the texture dimensions into account --- radiantcore/brush/Face.cpp | 28 +------------------------ radiantcore/brush/TextureMatrix.cpp | 25 ++++++++++++++++------ radiantcore/brush/TextureMatrix.h | 3 +-- radiantcore/brush/TextureProjection.cpp | 4 ++-- radiantcore/brush/TextureProjection.h | 3 ++- 5 files changed, 25 insertions(+), 38 deletions(-) diff --git a/radiantcore/brush/Face.cpp b/radiantcore/brush/Face.cpp index 609fefa33b..90ea7378eb 100644 --- a/radiantcore/brush/Face.cpp +++ b/radiantcore/brush/Face.cpp @@ -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) diff --git a/radiantcore/brush/TextureMatrix.cpp b/radiantcore/brush/TextureMatrix.cpp index 7c9050eb36..a6eb95d5f4 100644 --- a/radiantcore/brush/TextureMatrix.cpp +++ b/radiantcore/brush/TextureMatrix.cpp @@ -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])); diff --git a/radiantcore/brush/TextureMatrix.h b/radiantcore/brush/TextureMatrix.h index 6776ecccc8..46105a257f 100644 --- a/radiantcore/brush/TextureMatrix.h +++ b/radiantcore/brush/TextureMatrix.h @@ -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. diff --git a/radiantcore/brush/TextureProjection.cpp b/radiantcore/brush/TextureProjection.cpp index ddda95a0ad..59147601c6 100644 --- a/radiantcore/brush/TextureProjection.cpp +++ b/radiantcore/brush/TextureProjection.cpp @@ -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) diff --git a/radiantcore/brush/TextureProjection.h b/radiantcore/brush/TextureProjection.h index fea1c86fae..c415272068 100644 --- a/radiantcore/brush/TextureProjection.h +++ b/radiantcore/brush/TextureProjection.h @@ -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;