Is there a way to get UV texture hit coordiantes from a 3D Raycast?

DastweeperDastweeper Posts: 6Member
in 3D

Similar to Unity's RaycastHit.TextureCoord, is something like that in Godot? And if so, how would you get it?

I'm trying to get the exact pixel color of a texture clicked on from a mouse pointer, and if texture coordinate data were in raycasts in Godot that would make me happier than a pig locked inside an apple pie factory on Labor Day Weekend.

Thankee kindly!

Best Answer

  • cyberealitycybereality Posts: 927
    edited May 27 Accepted Answer

    The same code should work, you just make another viewport with your outline texture and place it somewhere further down the tree so it doesn't render. And use "get_node()" instead of "get_viewport()" so you can find the "fake" cubemap. I believe that should work.

Answers

  • cyberealitycybereality Posts: 927Moderator
    edited May 27

    You can read the screen pixels directly though the viewport. You don't need to mess with uv textures.

    See this (variables x and y are your mouse positions)

    var view_texture = get_viewport().get_texture().get_data()
    view_texture.flip_y()
    view_texture.lock()
    var color = view_texture.get_pixel(x, y)
    view_texture.unlock()
    print("color is ", color)
    
  • DastweeperDastweeper Posts: 6Member

    @cybereality said:
    You can read the screen pixels directly though the viewport. You don't need to mess with uv textures.

    See this (variables x and y are your mouse positions)

    var view_texture = get_viewport().get_texture().get_data()
    view_texture.flip_y()
    view_texture.lock()
    var color = view_texture.get_pixel(x, y)
    view_texture.unlock()
    print("color is ", color)
    

    Thanks so much for the help! I should have specified though: I need to get the color of a texture that isn't being rendered. The UV coordinates or ability to have the solution ignore certain objects that are rendered to screen is necessary.

    What? Why am I doing that? Well, as briefly as possible: I'm doing a hidden object game with hundreds of little objects mapped to the inside of a cubemap. The highly detailed art that makes up the scene has two versions: one is the "real" cubemap the 3D camera can look around and see, and the other is an image alpha with outlines of all 100+ objects that I'm hoping through clever shader magicks to use to detect whether the player has clicked on something or not. As a bonus, I have encoded information about what object it is through the RGB values of the image.

    I got this working in Unity, so I know it's possible, I'm super hoping it can happen here. Genuinely appreciate your time and help!

  • cyberealitycybereality Posts: 927Moderator
    edited May 27 Accepted Answer

    The same code should work, you just make another viewport with your outline texture and place it somewhere further down the tree so it doesn't render. And use "get_node()" instead of "get_viewport()" so you can find the "fake" cubemap. I believe that should work.

  • MegalomaniakMegalomaniak Posts: 2,645Admin
    edited May 27

    @Dastweeper said:

    What? Why am I doing that? Well, as briefly as possible: ...

    Always nice to see someone who understands the xy problem.

  • DastweeperDastweeper Posts: 6Member

    @Megalomaniak said:

    @Dastweeper said:

    What? Why am I doing that? Well, as briefly as possible: ...

    Always nice to see someone who understands the xy problem.

    If you folks are kind enough to spend your time offering helpful advice, the least I can do is try to format my question efficiently and use it right. Thanks for a great first impression of this community!

  • cyberealitycybereality Posts: 927Moderator

    So were you able to get it working in the end?

  • DastweeperDastweeper Posts: 6Member

    I'm trying it out right now. I suspect it will, I'll let you know!

  • cyberealitycybereality Posts: 927Moderator

    Okay, cool. Let me know how that goes.

  • DastweeperDastweeper Posts: 6Member

    So I almost got it working. Very odd behavior: when I go to click, nothing happens, but then I get the first debug "Ding" you can see in the pic below.

    Then the program freezes and nothing else is output to console. Any ideas? Is something happening with .lock?

  • cyberealitycybereality Posts: 927Moderator

    I got it working but it's a little complicated, I hope I can explain well. You need a second "fake" ViewportContainer and a "fake" Viewport as the child. Then make a Camera as a child of the Viewport. Check "Own World" on the "fake" Viewport and create a World right under. Set the Environment directly under to your "fake" Panorama. The "fake" ViewportContainer should be above the real one in the scene list so it is behind.

    On the top of the script for your mouse do this to save the node name:

    onready var fake = get_node("../../FakeContainer/FakeViewport")
    

    Then in the click script use this line:

    var view_texture = fake.get_texture().get_data()
    

    It seems to work for me. The only issue I foresee is syncing the cameras up when the player looks around. I imagine it's just a matter of setting the "fake" Camera to have the transform of the real Camera each frame, but I haven't tried it. Hope that sets you in the right direction.

  • DastweeperDastweeper Posts: 6Member

    Really appreciate you continuing to assist, it means a lot!

    I don't think I have it set up quite right. It's throwing errors that lead me to believe that get_node isn't coming back with anything.

    As of right now, same behavior, plus those bottom two errors.

Leave a Comment

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