Coding shader to use r,g colors on the screen as (u,v) coordinates?
Asked Answered
L

6

0

Hi! I'm currently working on a project where I'm using Godot's 2D engine to program a Wolfenstein 3D style first-person renderer. I have a Node2D set up to draw this:

This should be enough data to get (u,v) texture mapping, if i can just convert red to u and green to v.

I'm new to shaders, though, and my code doesn't seem to be working:

shader_type canvas_item;

uniform sampler2D text; //texture

void fragment() {
	vec4 color = texture(TEXTURE, UV); //get color of pixel
	vec2 uv = vec2(color.r, color.g) / 255.0; //use red as u and green as v
							//divide by 255 to get a value between 0 and 1(?)
	COLOR = texture(text, uv); //set color of pixel to corresponding pixel on texture
}

Results in a solid color being used for the entire node, specifically the color in the top left of the texture:


Anyone know what's going wrong here? Thanks :]

Liquorish answered 8/3, 2023 at 18:25 Comment(0)
L
0

I ended up using another ColorRect below my FirstPersonDraw on the scene tree which draws a rectangle over the whole screen.
This probably isn't the best implementation, but I'm satisfied with this project for now. I think I'll pick it back up when I make the switch to Godot 4; it would benefit from the more robust shader tools.

Liquorish answered 10/3, 2023 at 17:30 Comment(0)
J
0

Not 100% sure but ... colors are floats in [0,1] , not in [0,255] I think. So you would get always the same pixel of the texture, and removing the division by 255 may fix it ?

And nice hack to get this done by the way 🙂

Jordanna answered 8/3, 2023 at 18:44 Comment(0)
L
0

Jordanna Unfortunately just results in a different solid color being used:

And thanks! It's been pretty frustrating working on this but it feels super cool when I get something to work.

Liquorish answered 8/3, 2023 at 18:51 Comment(0)
S
0

If that's a full screen shader, you have to use SCREEN_TEXTURE and SCREEN_UV

Stylet answered 8/3, 2023 at 19:58 Comment(0)
L
0

Stylet This was the right way to go for sure! The funny thing is I already tried this and thought it didn't work because it gave me solid colors again, only split perfectly down the middle.

But this time I had the thought to experiment a bit more and hide my ceiling and floor nodes, showing the tilemap underneath, and this gave me:

So apparently the FirstPersonDraw node I've attached my shader to is acting as a mask and operating on the layers beneath it, rather than on itself. To test, I threw a sprite of an r,g field on the canvas and found it applied the texture perfectly : D
So that part's working, thank you! (^^) I'm betting the mask problem is as simple as finding some property buried in my node settings or repositioning some things in my node tree...

Liquorish answered 8/3, 2023 at 22:6 Comment(0)
S
0

I usually use ColorRects for fullscreen shaders.

Stylet answered 8/3, 2023 at 22:58 Comment(0)
L
0

I ended up using another ColorRect below my FirstPersonDraw on the scene tree which draws a rectangle over the whole screen.
This probably isn't the best implementation, but I'm satisfied with this project for now. I think I'll pick it back up when I make the switch to Godot 4; it would benefit from the more robust shader tools.

Liquorish answered 10/3, 2023 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.