Rotate a Texture2D/Image?

jollyswagmanjollyswagman Posts: 2Member

Does Godot feature any way to rotate a Texture2D/Image? I am trying to procedurally create images, which requires rotating sub-images to the desired angle and drawing them all to the same canvas to compose the final product. None of the methods of Image (or Texture2D) seem to do this.

Please tell me I'm missing something! And if not, any suggestions for how I can do this? I wouldn't know how to write my own rotation function.

Thank you very much! I've been really enjoying working with Godot.

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 1,677
    edited August 25 Accepted Answer

    I don't know of any way to rotate a Image or Texture2D node directly, but you could probably use a Viewport to achieve a similar result.

    In theory all you need to do is place all of the textures as Sprite nodes that are children of a Viewport node, set the Viewport node's size to the desired size, set a Camera2D node so the contents of the Viewport are visible, and then use the Viewport output as the final product.

    That way to rotate, scale, and position the sub-images, you can just change the properties of the Sprite nodes and let Godot handle manipulating the images within the Viewport.

    The Viewport demos in the Godot demo repository kinda show how this could be done. There is also this page on the documentation that uses a Viewport as a texture for a procedural planet, which might help as a reference.

    Hopefully this helps a bit. It should totally be possible to use rotated textures, it just might require using Viewport nodes instead of rotating the image/texture directly.
    (Side note: Welcome to the forums!)

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,677Admin
    edited August 25 Accepted Answer

    I don't know of any way to rotate a Image or Texture2D node directly, but you could probably use a Viewport to achieve a similar result.

    In theory all you need to do is place all of the textures as Sprite nodes that are children of a Viewport node, set the Viewport node's size to the desired size, set a Camera2D node so the contents of the Viewport are visible, and then use the Viewport output as the final product.

    That way to rotate, scale, and position the sub-images, you can just change the properties of the Sprite nodes and let Godot handle manipulating the images within the Viewport.

    The Viewport demos in the Godot demo repository kinda show how this could be done. There is also this page on the documentation that uses a Viewport as a texture for a procedural planet, which might help as a reference.

    Hopefully this helps a bit. It should totally be possible to use rotated textures, it just might require using Viewport nodes instead of rotating the image/texture directly.
    (Side note: Welcome to the forums!)

  • MegalomaniakMegalomaniak Posts: 1,627Admin

    Another thing you could try is using a shader and manipulating the texture coordinates to achieve the rotations.

  • jollyswagmanjollyswagman Posts: 2Member

    Thank you! I know nothing about shaders, so I went the Viewport route. Didn't have much trouble (knock on wood). A few notes in case anyone else is wondering how to do this:

    1) Strictly speaking, no camera node is needed to do this, though it may be handy when using the GUI (I wasn't).
    2) Viewport textures are served upside-down, so they have to be vertically flipped after you get them.
    3) Viewports have a gray background by default, but you can set their 'tranparent_bg' property to 'true' to get transparency, which you'll presumably want.

    Thanks again!

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file