Boat/Boats/buyancy_probe.gd

69 lines
2.5 KiB
GDScript3
Raw Normal View History

2025-03-25 00:22:03 +00:00
extends Marker3D
class_name BuyancyProbe
## How much force is applied upward
@export var float_strength: float = 10.0
@export var max_float_force:float = 500.0
2025-03-25 00:22:03 +00:00
var currentdepth:float
@export_category("Debug")
@export var show_probe :bool = false
var sphere_preview:MeshInstance3D
@onready var debug_sphere:SphereMesh = SphereMesh.new()
@export_category("Wave")
var noise: Image
var wave_speed: float = 1.0
var noise_scale: float
var height_scale: float
@onready var ocean_mat: ShaderMaterial = preload("uid://cf3lxmfb2d7c8")
@onready var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var OceanNode:Ocean = get_tree().get_first_node_in_group("Ocean")
var Oceantime:float
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
if ocean_mat != null:
update_param()
if show_probe:
sphere_preview = MeshInstance3D.new()
sphere_preview.set_name("Sphere")
add_child(sphere_preview)
debug_sphere.radius = 0.1
debug_sphere.height = 0.2
sphere_preview.mesh = debug_sphere
func _physics_process(_delta: float) -> void:
var depth = get_wave_height(global_position,Oceantime) - global_position.y
#print(depth)
if depth > 0.0 && get_parent() != null:
var parentRigid = get_parent()
var float_force = parentRigid.mass * float_strength
parentRigid.apply_force(Vector3.UP * clamp(gravity * depth * float_force,-max_float_force,max_float_force) ,global_position-parentRigid.global_position)
2025-03-25 00:22:03 +00:00
func _process(_delta: float) -> void:
if OceanNode != null:
Oceantime = OceanNode.wave_time
if show_probe && sphere_preview != null:
sphere_preview.global_position = global_position
sphere_preview.global_position.y = get_wave_height(global_position,Oceantime)
# Calculate the wave height using a sine function.
func get_wave_height(world_position: Vector3, time: float) -> 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 OceanNode.global_position.y + noise.get_pixelv(pixel_pos).r * height_scale;
func update_param():
if ocean_mat != null:
wave_speed = ocean_mat.get_shader_parameter("wave_speed")
noise = ocean_mat.get_shader_parameter("wave").noise.get_seamless_image(512, 512)
noise_scale = ocean_mat.get_shader_parameter("noise_scale")
wave_speed = ocean_mat.get_shader_parameter("wave_speed")
height_scale = ocean_mat.get_shader_parameter("height_scale")