Buyancy update

This commit is contained in:
Lucas 2025-03-22 00:03:08 +01:00
parent 1ca99b9897
commit e197c8fa1f
10 changed files with 238 additions and 50 deletions

View file

@ -1,40 +1,126 @@
extends RigidBody3D
@export_group("Buyancy")
@export var float_force := 40
@export var water_drag:= 0.05
@export var water_angular_drag := 0.05
@export var WaterNode:MeshInstance3D
@export var ripple_texture: Texture2D
@export var upforce_max := 500.0
@export_group("Inputs")
@export var acceleration := 10 # Forward force
@export var turn_speed := 2.0 # Rotation speed
@export var max_speed := 60.0 # Limit speed
## Slow down naturally
@export var water_resistance := 0.98
@onready var gravity:float = ProjectSettings.get_setting("physics/3d/default_gravity")
const upforce_max := 500.0
const water_height :=0.0
###### Buyancy ########
var cells_scale: Vector2 = Vector2(1.0, 1.0) # Same as `CellsScale`
var max_ripple_height: float = 1.0 # Same as `MaxRippleHeight`
var Wantedheight: float = 1.0 # Same as `MaxRippleHeight`
var ripple: Vector2 # Same as `MaxRippleHeight`
var noise: FastNoiseLite
var InputForce:Vector3
var water_height :=0.0
var inWater := false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var material
if WaterNode != null:
material = WaterNode.mesh.surface_get_material(0)
noise = load("res://mat/wavenoise.tres").noise
if material != null:
cells_scale = material.get_shader_parameter("CellsScale")
ripple = material.get_shader_parameter("RippleTimeScale")
max_ripple_height = material.get_shader_parameter("MaxRippleHeight")
ripple_texture = load("res://mat/wavenoise.tres")
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
# Get input directions
var forward_input = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
var turn_input = Input.get_action_strength("turn_right") - Input.get_action_strength("turn_left")
# Apply forward force in the boat's local forward direction
var forward_vector = global_transform.basis.x # Local forward direction
var direction = forward_vector * forward_input * acceleration
InputForce = direction
print (InputForce)
apply_central_force(InputForce)
func _physics_process(_delta: float) -> void:
water_height = get_water_height(global_position)
var currentDepth = water_height - global_position.y
if currentDepth > 0:
inWater = true
var force_to_apply:Vector3 = Vector3.UP * clamp(float_force * gravity * currentDepth,0.0,upforce_max)
apply_central_force(force_to_apply)
var water_tilt = get_water_normal()
# Get current rotation
var current_rotation = $untitled.rotation
# Smoothly interpolate rotation to match water tilt
current_rotation.x = lerp_angle(current_rotation.x, water_tilt.x, 0.25) # Smooth pitch
current_rotation.z = lerp_angle(current_rotation.z, water_tilt.z, 0.25) # Smooth roll
$untitled.rotation = current_rotation # Apply the new rotation
else:
inWater = false
func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
# Change comportement based on if in water or not
if inWater:
state.linear_velocity *= 1 - water_drag
state.angular_velocity *= 1 - water_angular_drag
func get_water_height(_position:Vector3) -> float:
var wave_speed = 15.0
# Step 1: Compute noise-based wave height
var uv = Vector2(_position.x, _position.z) * cells_scale # Matches shader scaling
# Step 2: Apply time-based animation for moving waves
var time_offset = Time.get_ticks_msec() * 0.001 * wave_speed
var noise_value = noise.get_noise_2d(uv.x + time_offset, uv.y + time_offset) # Noise-based wave
# Step 3: Scale and return final water height
return 0.15 + (noise_value * max_ripple_height)
func get_water_normal() -> Vector3:
var offset = 1.0 # Distance for sampling points (adjust as needed)
# Sample water height at three nearby points
var p1 = global_position + Vector3(offset, 0, 0) # Right side
var p2 = global_position + Vector3(0, 0, offset) # Forward side
var h1 = get_water_height(p1) # Height at right side
var h2 = get_water_height(p2) # Height at forward side
var h_center = get_water_height(global_position) # Center height
# Calculate tilt angles
var pitch = atan2(h2 - h_center, offset) # Forward/backward tilt (X-axis)
var roll = atan2(h1 - h_center, offset) # Left/right tilt (Z-axis)
return Vector3(pitch, 0, roll) # Euler angles (X: pitch, Y: unused, Z: roll)

Binary file not shown.

34
mat/TestNoise.tres Normal file
View file

@ -0,0 +1,34 @@
[gd_resource type="ShaderMaterial" load_steps=4 format=3 uid="uid://bf7eocrgp1de1"]
[ext_resource type="Texture2D" uid="uid://dn01vy45t0mnc" path="res://mat/wavenoise.tres" id="1_855jd"]
[sub_resource type="VisualShaderNodeTexture" id="VisualShaderNodeTexture_3pykj"]
texture = ExtResource("1_855jd")
[sub_resource type="VisualShader" id="VisualShader_u1il5"]
code = "shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_lambert, specular_schlick_ggx;
uniform sampler2D tex_frg_2;
void fragment() {
// Texture2D:2
vec4 n_out2p0 = texture(tex_frg_2, UV);
// Output:0
ALBEDO = vec3(n_out2p0.xyz);
}
"
graph_offset = Vector2(-309.528, 27.2265)
nodes/fragment/2/node = SubResource("VisualShaderNodeTexture_3pykj")
nodes/fragment/2/position = Vector2(-400, 100)
nodes/fragment/connections = PackedInt32Array(2, 0, 0, 0)
[resource]
render_priority = 0
shader = SubResource("VisualShader_u1il5")

View file

@ -1,11 +1,11 @@
[gd_scene load_steps=11 format=3 uid="uid://cjjrdfywoxwgr"]
[ext_resource type="Material" uid="uid://br11m0qg2yhid" path="res://mat/waterMat.tres" id="1_x7q1n"]
[ext_resource type="Script" uid="uid://dialqdlhpqqsg" path="res://mat/water.gd" id="2_0sebh"]
[ext_resource type="PackedScene" uid="uid://cpykugh40l23q" path="res://kaykit/medieval/building_windmill_herited.tscn" id="2_cdryl"]
[ext_resource type="PackedScene" uid="uid://j4cu75dnf3u8" path="res://kaykit/medieval/building_well_red.fbx" id="4_wio4u"]
[ext_resource type="PackedScene" uid="uid://chcjbpt2tr5k3" path="res://kaykit/medieval/building_watermill_herited.tscn" id="4_x7q1n"]
[ext_resource type="PackedScene" uid="uid://babgqvkugifk1" path="res://boat/boat.tscn" id="5_wio4u"]
[ext_resource type="Texture2D" uid="uid://dn01vy45t0mnc" path="res://mat/wavenoise.tres" id="6_0sebh"]
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_cdryl"]
sky_top_color = Color(0.308708, 0.682951, 0.784164, 1)
@ -40,7 +40,6 @@ environment = SubResource("Environment_x7q1n")
[node name="Water" type="MeshInstance3D" parent="."]
mesh = SubResource("PlaneMesh_h5gxe")
script = ExtResource("2_0sebh")
[node name="Env" type="Node3D" parent="."]
@ -56,5 +55,8 @@ transform = Transform3D(0.676092, 0, -0.736817, 0, 1, 0, 0.736817, 0, 0.676092,
[node name="OmniLight3D" type="OmniLight3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.50488, 1.99753, 0.0326443)
[node name="Boat" parent="." instance=ExtResource("5_wio4u")]
[node name="Boat" parent="." node_paths=PackedStringArray("WaterNode") instance=ExtResource("5_wio4u")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.208335, 0.773113, 1.74591)
can_sleep = false
WaterNode = NodePath("../Water")
ripple_texture = ExtResource("6_0sebh")

View file

@ -0,0 +1,60 @@
[gd_scene load_steps=11 format=3 uid="uid://cjjrdfywoxwgr"]
[ext_resource type="Material" uid="uid://br11m0qg2yhid" path="res://mat/waterMat.tres" id="1_x7q1n"]
[ext_resource type="Script" uid="uid://dialqdlhpqqsg" path="res://mat/water.gd" id="2_0sebh"]
[ext_resource type="PackedScene" uid="uid://cpykugh40l23q" path="res://kaykit/medieval/building_windmill_herited.tscn" id="2_cdryl"]
[ext_resource type="PackedScene" uid="uid://j4cu75dnf3u8" path="res://kaykit/medieval/building_well_red.fbx" id="4_wio4u"]
[ext_resource type="PackedScene" uid="uid://chcjbpt2tr5k3" path="res://kaykit/medieval/building_watermill_herited.tscn" id="4_x7q1n"]
[ext_resource type="PackedScene" uid="uid://babgqvkugifk1" path="res://boat/boat.tscn" id="5_wio4u"]
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_cdryl"]
sky_top_color = Color(0.308708, 0.682951, 0.784164, 1)
sky_horizon_color = Color(0.780183, 0.865994, 0.882297, 1)
ground_horizon_color = Color(0.780183, 0.865994, 0.882297, 1)
[sub_resource type="Sky" id="Sky_h5gxe"]
sky_material = SubResource("ProceduralSkyMaterial_cdryl")
[sub_resource type="Environment" id="Environment_x7q1n"]
background_mode = 2
sky = SubResource("Sky_h5gxe")
tonemap_mode = 2
glow_enabled = true
[sub_resource type="PlaneMesh" id="PlaneMesh_h5gxe"]
material = ExtResource("1_x7q1n")
size = Vector2(50, 50)
subdivide_width = 200
subdivide_depth = 200
[node name="Main" type="Node3D"]
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-0.866025, -0.474162, 0.158653, 0, 0.317305, 0.948324, -0.5, 0.821272, -0.274794, 0, 1.39043, 0)
light_specular = 0.1
shadow_enabled = true
shadow_opacity = 0.9
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_x7q1n")
[node name="Water" type="MeshInstance3D" parent="."]
mesh = SubResource("PlaneMesh_h5gxe")
script = ExtResource("2_0sebh")
[node name="Env" type="Node3D" parent="."]
[node name="building_windmill_red" parent="Env" instance=ExtResource("2_cdryl")]
transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, 0.309549, -0.0858696, -3.25239)
[node name="building_well_red" parent="Env" instance=ExtResource("4_wio4u")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 5.48994, 1.19209e-07, -0.244412)
[node name="building_watermill_red2" parent="Env" instance=ExtResource("4_x7q1n")]
transform = Transform3D(0.676092, 0, -0.736817, 0, 1, 0, 0.736817, 0, 0.676092, -4.20475, 0, -1.22925)
[node name="OmniLight3D" type="OmniLight3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.50488, 1.99753, 0.0326443)
[node name="Boat" parent="." instance=ExtResource("5_wio4u")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.208335, 0.773113, 1.74591)

View file

@ -1,21 +0,0 @@
extends MeshInstance3D
var material: ShaderMaterial
var noise: FastNoiseLite = preload("res://mat/watershader.tres::FastNoiseLite_gokfr")
var noise_UVscale:Vector2
var wave_speed:float
var height_scale:float
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
material = mesh.surface_get_material(0)
if material:
noise_UVscale = material.get_shader_parameter("RippleTimeScale")
print(noise_UVscale)
FastNoiseLite
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

View file

@ -5,7 +5,7 @@
[resource]
render_priority = 0
shader = ExtResource("1_337u2")
shader_parameter/Waves = false
shader_parameter/Waves = true
shader_parameter/CellsScale = Vector2(15, 15)
shader_parameter/RippleTimeScale = Vector2(0.05, 0.08)
shader_parameter/MaxRippleHeight = 0.15

View file

@ -1,4 +1,6 @@
[gd_resource type="VisualShader" load_steps=76 format=3 uid="uid://dju4bgl0vd6f5"]
[gd_resource type="VisualShader" load_steps=74 format=3 uid="uid://dju4bgl0vd6f5"]
[ext_resource type="Texture2D" uid="uid://dn01vy45t0mnc" path="res://mat/wavenoise.tres" id="1_vh75p"]
[sub_resource type="VisualShaderNodeUVFunc" id="VisualShaderNodeUVFunc_obdjg"]
linked_parent_graph_frame = 49
@ -185,20 +187,9 @@ size = Vector2(2124, 1224)
title = "Noise"
attached_nodes = PackedInt32Array(15, 17, 10, 42, 44, 45, 48, 47, 46, 8, 16, 43, 41, 22, 11, 7, 5, 62)
[sub_resource type="FastNoiseLite" id="FastNoiseLite_gokfr"]
noise_type = 2
seed = 1615
fractal_type = 0
cellular_distance_function = 1
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_5fl1t"]
resource_name = "FoamNoise2"
seamless = true
noise = SubResource("FastNoiseLite_gokfr")
[sub_resource type="VisualShaderNodeTexture" id="VisualShaderNodeTexture_tk76b"]
linked_parent_graph_frame = 49
texture = SubResource("NoiseTexture2D_5fl1t")
texture = ExtResource("1_vh75p")
texture_type = 1
[sub_resource type="VisualShaderNodeFloatParameter" id="VisualShaderNodeFloatParameter_obdjg"]
@ -335,13 +326,8 @@ input_name = "time"
[sub_resource type="VisualShaderNodeUVFunc" id="VisualShaderNodeUVFunc_vh75p"]
default_input_values = [1, Vector2(0.1, 0.1), 2, Vector2(0, 0)]
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_dt65u"]
resource_name = "FoamNoise2"
seamless = true
noise = SubResource("FastNoiseLite_gokfr")
[sub_resource type="VisualShaderNodeTexture" id="VisualShaderNodeTexture_0o2nh"]
texture = SubResource("NoiseTexture2D_dt65u")
texture = ExtResource("1_vh75p")
[sub_resource type="VisualShaderNodeInput" id="VisualShaderNodeInput_0o2nh"]
input_name = "vertex"
@ -679,7 +665,7 @@ void fragment() {
}
"
graph_offset = Vector2(-540.049, 106.316)
graph_offset = Vector2(-106.871, 138.902)
modes/diffuse = 3
modes/specular = 1
flags/fog_disabled = true

12
mat/wavenoise.tres Normal file
View file

@ -0,0 +1,12 @@
[gd_resource type="NoiseTexture2D" load_steps=2 format=3 uid="uid://dn01vy45t0mnc"]
[sub_resource type="FastNoiseLite" id="FastNoiseLite_gokfr"]
noise_type = 2
seed = 1615
fractal_type = 0
cellular_distance_function = 1
[resource]
resource_name = "FoamNoise2"
seamless = true
noise = SubResource("FastNoiseLite_gokfr")

View file

@ -11,6 +11,7 @@ config_version=5
[application]
config/name="SpilledClone"
config/version="0.01"
run/main_scene="uid://cjjrdfywoxwgr"
config/features=PackedStringArray("4.4", "Forward Plus")
boot_splash/bg_color=Color(0, 0, 0, 1)
@ -21,5 +22,33 @@ config/icon="res://icon.svg"
window/size/viewport_width=576
window/size/viewport_height=324
window/stretch/mode="viewport"
window/stretch/aspect="expand"
window/per_pixel_transparency/allowed=true
window/vsync/vsync_mode=0
[input]
move_forward={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":5,"axis_value":1.0,"script":null)
]
}
move_backward={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":4,"axis_value":1.0,"script":null)
]
}
turn_right={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":1.0,"script":null)
]
}
turn_left={
"deadzone": 0.2,
"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":0,"axis_value":-1.0,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null)
]
}