aboutsummaryrefslogtreecommitdiff
path: root/game/testing/Buoyancy-in-Godot-4-master/Water.gd
blob: b5e2fe73687379b132e50821f658b768b94ebeb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
extends MeshInstance3D

var material: ShaderMaterial
var noise: Image

var noise_scale: float
var wave_speed: float
var height_scale: float

var time: float

# Called when the node enters the scene tree for the first time.
func _ready():
   material = mesh.surface_get_material(0)
   noise = material.get_shader_parameter("wave").noise.get_seamless_image(512, 512)
   noise_scale = material.get_shader_parameter("noise_scale")
   wave_speed = material.get_shader_parameter("wave_speed")
   height_scale = material.get_shader_parameter("height_scale")



# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
   time += delta
   material.set_shader_parameter("wave_time", time)

func get_height(world_position: Vector3) -> float:
   var uv_x = wrapf(world_position.x / noise_scale + time * wave_speed, 0, 1)
   var uv_y = wrapf(world_position.z / noise_scale + time * wave_speed, 0, 1)

   var pixel_pos = Vector2(uv_x * noise.get_width(), uv_y * noise.get_height())
   return global_position.y + noise.get_pixelv(pixel_pos).r * height_scale;