#Difference between reading from a NoiseTexture2D inside gdscript and gdshader

1 messages · Page 1 of 1 (latest)

wind lion
#

I have a gdscript


@export var wave_pattern : NoiseTexture2D

func _physics_process(_delta: float) -> void:
    position.y = wave_pattern.noise.get_noise_2d(position.x / 100.0, position.z / 100.0) * 10.0

and a shader with the following code

render_mode world_vertex_coords;

uniform sampler2D wave;

void vertex() {
    float height = texture(wave, VERTEX.xz / 100.0).r;
    VERTEX.y = height * 10;
}

both the wave_pattern and wave have the same NoiseTexture2D assigned to them with just a simple FastNoiseLite perlin noise texture

however if I attach the gdscript to a Node3D and use the shader as a material for a plane and overlap them, then let the game run the heigths don't match up

side note: get_noise_2d seems to always return 0 no matter the noise behind it at 0 0
and it seems to not care if I set the NoiseTexture2D to be normalized and returns values between -1 and 1 anyway (I haven't accounted for it in this example but I have tried to negate this and it doesn't seem to be fixing my issue)

This indicator should be at the water surface

woven flint
#

one is using y and the other is using z -- I don't think shader vertices use a different frame of reference than normal scene, ie, y is still up/down?

#

(though I could be wrong ofc!)

#

(I mean as input to the displacement -- in the gdscript it's position.y / 100.0 as input)

wind lion
#

silly me
but no It doesn't seem to fix the issue (I think I just messed it up at some point when testing)

#

imma edit the original post

woven flint
#

oh, in your gdscript you use position not global_position

#

so you're not using world coords there

wind lion
#

oh

#

yeah I'm a bit new and didn't know that the position is relative to the scene (I'm guessing)

#

makes sense

woven flint
#

relative to the parent (in a sense: even worse! 😁)

wind lion
#

nvm

#

not even that seems to fix it

#

wait

#

even with

extends Node3D

@export var wave_pattern : NoiseTexture2D

func _physics_process(_delta: float) -> void:
    position.y = wave_pattern.noise.get_noise_2d(1, 1) * 10.0

and

shader_type spatial;
render_mode world_vertex_coords;

uniform sampler2D wave;

void vertex() {
    float height = texture(wave, vec2(1,1)).r;
    VERTEX.y = height * 10;
}

it still doesn't work

#

and I'm using get_noise_2d(1,1) here cause of the 0 0 thing I mentioned in the original post

#

and yes I tried the normalization thing again with
position.y = (wave_pattern.noise.get_noise_2d(1, 1) + 1) / 2 * 10.0

#

and I know the it atleast works seperately because now my waves are non existant and the bobber (the thing thats supposed to be at the water surface) is at the same y-level no matter where I place it

woven flint
#

fwiw you're still using position.y not global_position.y in that example

#

oh really dumb question -- are these the exact same noise texture?

#

(you said so earlier, but worth double checking now 😄 )

wind lion
#

yes its literally the same resource and
there isn't actually a difference in position.y and global_position.y in this example because I have every other transform set to 0
and It doesnt fix it if I do use global_position.y

woven flint
#

what is this shader being applied to? Have all of its transformations been zeroed out? (not that they have to be, but you're trying to coordinate the positions of two separate things, you know?)

#

(you said: plane. But like, has it been rotated?)

#

(because if so, maybe we're getting some weird readings off of that... 😄 )

wind lion
#

be warned tho that its messy

woven flint
#

yeah, that looks normal. 🤔

woven flint
#

This isn't a solution; https://forum.godotengine.org/t/noise-texture-not-appearing/86907 suggests you should also say repeat_enable (actually, I guess it might be a solution; dunno!)

wind lion
#

nope! That doesn't seem to do anything

wind lion
#

I thought that maybe not accessing the noise directly would work and instead just using
position.y = wave_pattern.get_image().get_pixel(position.x/100.0, position.z/100.0).r * 10
but it appears that NoiseTexture2D.get_image() always returns null

#

recreating the settings of the image and then using get_pixel() seems to get the same incorrect results

global_position.y = img.get_pixel(position.x / 100, position.z / 100).r * 10.0```