From 38b247487309ca89bf2d94bb321f12059861db9d Mon Sep 17 00:00:00 2001 From: Malfurious Date: Wed, 14 Sep 2022 03:50:09 -0400 Subject: Consolidate water-related assets Stray files are moved into a new directory. The water MeshInstance is made into a new scene, as I will be adding components to it. --- water/Water.gd | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 water/Water.gd (limited to 'water/Water.gd') diff --git a/water/Water.gd b/water/Water.gd new file mode 100644 index 0000000..da7666c --- /dev/null +++ b/water/Water.gd @@ -0,0 +1,113 @@ +extends MeshInstance +tool + +export var wave1_steepness = 0.5 setget _set_wave1_steepness +export var wave1_wavelength = 10.0 setget _set_wave1_wavelength +export var wave1_direction = Vector2(1.0,0.0) setget _set_wave1_direction + +export var wave2_steepness = 0.5 setget _set_wave2_steepness +export var wave2_wavelength = 10.0 setget _set_wave2_wavelength +export var wave2_direction = Vector2(1.0,0.0) setget _set_wave2_direction + +export var wave3_steepness = 0.5 setget _set_wave3_steepness +export var wave3_wavelength = 10.0 setget _set_wave3_wavelength +export var wave3_direction = Vector2(1.0,0.0) setget _set_wave3_direction + +func _set_wave_prop(idx, prop, val): + if val == null: return + if idx >= waves.size(): return + waves[idx].set(prop,val) + _update_wave(0) + +func _set_wave1_steepness(val): + wave1_steepness = val + _set_wave_prop(0,"steepness",val) + +func _set_wave1_wavelength(val): + wave1_wavelength = val + _set_wave_prop(0,"wavelength",val) + +func _set_wave1_direction(val): + wave1_direction = val + _set_wave_prop(0,"direction",val) + +func _set_wave2_steepness(val): + wave2_steepness = val + _set_wave_prop(1,"steepness",val) + +func _set_wave2_wavelength(val): + wave2_wavelength = val + _set_wave_prop(1,"wavelength",val) + +func _set_wave2_direction(val): + wave2_direction = val + _set_wave_prop(1,"direction",val) + +func _set_wave3_steepness(val): + wave3_steepness = val + _set_wave_prop(2,"steepness",val) + +func _set_wave3_wavelength(val): + wave3_wavelength = val + _set_wave_prop(2,"wavelength",val) + +func _set_wave3_direction(val): + wave3_direction = val + _set_wave_prop(2,"direction",val) + +var _time = 0.0 setget _set_time +var waves = [] +func _ready(): + waves.push_back(Wave.new(wave1_steepness,wave1_wavelength,wave1_direction,_time)) + waves.push_back(Wave.new(wave2_steepness,wave2_wavelength,wave2_direction,_time)) + waves.push_back(Wave.new(wave3_steepness,wave3_wavelength,wave3_direction,_time)) + +func _wave_to_quad(wave): + return Quat(wave.steepness,wave._k,wave._a,wave._t) + +func _update_wave(idx): + var material = self.get_surface_material(0) + material.set_shader_param("wave" + str(idx+1), _wave_to_quad(waves[idx])) + material.set_shader_param("wave" + str(idx+1) + "_dir", waves[idx].direction) + +func _set_time(val): + _time = val + for idx in waves.size(): + waves[idx]._time = _time + _update_wave(idx) + +func _physics_process(delta): + self._time += delta + #self._time = 0.0 + +func gertsner_wave(pos, idx): + if idx >= waves.size(): return 0.0 + var wave = waves[idx] + var k = wave._k + var a = wave._a + var t = wave._t + + var d = wave.direction.normalized() + var f = k * (d.dot(Vector2(pos.x,pos.z)) - t) + + var tmp = a * cos(f) + var new_pos = Vector3(d.x * tmp, a * sin(f), d.y * tmp) + + return new_pos + +func sample_wave(pos): + var new_pos = Vector3(pos.x,0,pos.z) + for idx in waves.size(): + new_pos += gertsner_wave(pos, idx) + return new_pos + +func height(pos): + var old_pos = pos + var y = old_pos.y + for _i in range(2): + var new_pos = sample_wave(old_pos) + y = new_pos.y + new_pos = old_pos - (new_pos - old_pos) + old_pos = new_pos + y += self.global_transform.origin.y + return y -- cgit v1.2.3