diff --git a/Materials/Water.tres b/Materials/Water.tres index 02ea8aa..4381732 100644 --- a/Materials/Water.tres +++ b/Materials/Water.tres @@ -13,9 +13,10 @@ noise = SubResource("FastNoiseLite_qwrmt") render_priority = 0 shader = ExtResource("1_33bm8") shader_parameter/color = Vector3(0.1, 0.4, 0.8) -shader_parameter/foamHeight = 0.085 -shader_parameter/amplitudes = PackedFloat32Array(0.3, 0.2, 0.1, 0.01, 0.015) -shader_parameter/waveSpeeds = PackedFloat32Array(0.5, 2, 5, 10, 9) -shader_parameter/xAmplifiers = PackedFloat32Array(1, 2, 1, 10, 0) -shader_parameter/yAmplifiers = PackedFloat32Array(0, 0.5, 2, 10, 0) +shader_parameter/reflectiveness = 10.0 +shader_parameter/foamHeight = 0.515 +shader_parameter/amplitudes = PackedFloat32Array(0.1, 0.02, 0.02, 0.01, 0.005, 0.2) +shader_parameter/waveSpeeds = PackedFloat32Array(0.5, 2, 2.2, 5, 10, 0.1) +shader_parameter/xAmplifiers = PackedFloat32Array(1, 3, -3, 15, 25, 0.1) +shader_parameter/yAmplifiers = PackedFloat32Array(0, 3, 2, -10, -20, 0.2) shader_parameter/foamTex = SubResource("NoiseTexture2D_roaec") diff --git a/Scenes/WaterPlane.tscn b/Scenes/WaterPlane.tscn index 152d2d6..6d87ac4 100644 --- a/Scenes/WaterPlane.tscn +++ b/Scenes/WaterPlane.tscn @@ -1,17 +1,57 @@ -[gd_scene load_steps=3 format=3 uid="uid://cvgligyy2psi8"] +[gd_scene load_steps=9 format=3 uid="uid://cvgligyy2psi8"] [ext_resource type="Material" uid="uid://b1f46mvfaphsh" path="res://Materials/Water.tres" id="1_p35v0"] +[ext_resource type="Shader" path="res://Shaders/Water2.gdshader" id="2_07p2y"] +[ext_resource type="Texture2D" uid="uid://251fcnsgqb0y" path="res://noise.png" id="3_rslsq"] [sub_resource type="PlaneMesh" id="PlaneMesh_gqyhx"] -size = Vector2(5, 5) -subdivide_width = 50 -subdivide_depth = 50 +lightmap_size_hint = Vector2i(52, 52) +material = ExtResource("1_p35v0") +size = Vector2(10, 10) +subdivide_width = 200 +subdivide_depth = 200 + +[sub_resource type="FastNoiseLite" id="FastNoiseLite_qwrmt"] +frequency = 0.0126 +fractal_type = 2 + +[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_lju3n"] +seamless = true +noise = SubResource("FastNoiseLite_qwrmt") + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_0tjqc"] +render_priority = 0 +shader = ExtResource("2_07p2y") +shader_parameter/color = Vector3(0.1, 0.4, 0.8) +shader_parameter/foamHeight = 6.035 +shader_parameter/amplitude = 0.2 +shader_parameter/frequency = 1.0 +shader_parameter/waveCount = 32 +shader_parameter/amplitudeScaleFactor = 0.82 +shader_parameter/frequencyScaleFactor = 1.3 +shader_parameter/foamTex = SubResource("NoiseTexture2D_lju3n") +shader_parameter/dirs = ExtResource("3_rslsq") + +[sub_resource type="PlaneMesh" id="PlaneMesh_w7hsm"] +material = SubResource("ShaderMaterial_0tjqc") +size = Vector2(15, 15) +subdivide_width = 200 +subdivide_depth = 200 [node name="WaterPlane" type="Node3D"] [node name="MeshInstance3D" type="MeshInstance3D" parent="."] mesh = SubResource("PlaneMesh_gqyhx") -surface_material_override/0 = ExtResource("1_p35v0") + +[node name="MeshInstance3D2" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 11, 0, 0) +visible = false +mesh = SubResource("PlaneMesh_w7hsm") [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] -transform = Transform3D(1, 0, 0, 0, 0.765573, 0.643349, 0, -0.643349, 0.765573, 0, 0.851548, 0) +transform = Transform3D(1, 0, 0, 0, 0.765573, 0.643349, 0, -0.643349, 0.765573, 0, 3.56427, 0) + +[node name="OmniLight3D" type="OmniLight3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.734879, 0) +light_energy = 16.0 +omni_range = 457.052 diff --git a/Shaders/Water.gdshader b/Shaders/Water.gdshader index 6018415..ff92bd9 100644 --- a/Shaders/Water.gdshader +++ b/Shaders/Water.gdshader @@ -1,6 +1,8 @@ shader_type spatial; +render_mode world_vertex_coords; uniform vec3 color = vec3(0.1, 0.4, 0.8); +uniform float reflectiveness = 10.0; uniform float foamHeight = 0.2; uniform sampler2D foamTex; @@ -15,28 +17,30 @@ varying float yOffset; float calcHeight(float x, float y){ float offset = 0.0; for (int i = 0; i < WAVE_COUNT; i++){ - offset += amplitudes[i] * sin((waveSpeeds[i] * TIME) + (xAmplifiers[i] * (x)) + (yAmplifiers[i] * (y))); + offset += amplitudes[i] * pow(E, sin((waveSpeeds[i] * TIME) + (xAmplifiers[i] * (x)) + (yAmplifiers[i] * (y)))); } return offset; } void vertex() { yOffset = VERTEX.y; - vec3 b = vec3(VERTEX.x, VERTEX.y + calcHeight(NODE_POSITION_WORLD.x + VERTEX.x + 0.01, NODE_POSITION_WORLD.z + VERTEX.z), VERTEX.z); - vec3 c = vec3(VERTEX.x, VERTEX.y + calcHeight(NODE_POSITION_WORLD.x + VERTEX.x, NODE_POSITION_WORLD.z + VERTEX.z + 0.1), VERTEX.z); - VERTEX.y += calcHeight(NODE_POSITION_WORLD.x + VERTEX.x, NODE_POSITION_WORLD.z + VERTEX.z); + vec3 b = vec3(VERTEX.x, VERTEX.y + calcHeight(VERTEX.x + 0.01, VERTEX.z), VERTEX.z); + vec3 c = vec3(VERTEX.x, VERTEX.y + calcHeight(VERTEX.x, VERTEX.z + 0.01), VERTEX.z); + VERTEX.y += calcHeight(VERTEX.x, VERTEX.z); NORMAL = normalize(cross(b - VERTEX, c - VERTEX)); yOffset += VERTEX.y; } void fragment() { - ALBEDO = color * clamp(1.0 + yOffset, 0.2, 2); + ALBEDO = color * clamp(0.8 + yOffset, 0.2, 2); float strength = yOffset * texture(foamTex, UV).r; if (strength >= foamHeight) ALBEDO = ALBEDO + vec3(1, 1, 1) * (strength + foamHeight); - ALPHA = 0.9; + //ALPHA = 0.9; + //ALBEDO = (NORMAL + 1.0) / 2.0; } //void light() { - // Called for every pixel for every light affecting the material. - // Uncomment to replace the default light processing function with this one. + //DIFFUSE_LIGHT += clamp(dot(NORMAL, LIGHT), 0.0, 1.0) * ATTENUATION * LIGHT_COLOR; + //vec3 halfVector = normalize(normalize(VIEW) + normalize(LIGHT)); + //SPECULAR_LIGHT += clamp(pow(dot(NORMAL, halfVector), reflectiveness), 0.0, 1.0) * LIGHT_COLOR; //} diff --git a/Shaders/Water2.gdshader b/Shaders/Water2.gdshader new file mode 100644 index 0000000..1b5ee1c --- /dev/null +++ b/Shaders/Water2.gdshader @@ -0,0 +1,56 @@ +shader_type spatial; +render_mode world_vertex_coords; + +uniform vec3 color = vec3(0.1, 0.4, 0.8); +uniform float foamHeight = 0.2; +uniform sampler2D foamTex; +uniform float amplitude = 0.2; +uniform float frequency = 1.0; +uniform int waveCount = 5; +uniform float amplitudeScaleFactor = 0.82; +uniform float frequencyScaleFactor = 1.18; +uniform sampler2D dirs; + +varying float yOffset; + +float random(float x, float y){ + return (texture(dirs, vec2(x / 32.0, y / 2.0)).r + 1.0) / 2.0; +} + +float calcHeight(float x, float y){ + float amp = 1.0; + float freq = 1.0; + float offset = 0.0; + for (int i = 0; i < waveCount; i++){ + vec2 dir = vec2(random(float(i), 0.0), random(float(i), 1.0)); + float s = sin(frequency * freq * dot(dir, vec2(x, y)) + TIME); + offset += amplitude * amp * pow(E, s); + //offset += amplitude * amp * pow(E, sin(frequency * freq * (TIME + (random(float(i), 0.0) * x) + (random(float(i), 1.0) * y)))); + amp *= amplitudeScaleFactor; + freq *= frequencyScaleFactor; + } + return offset; +} + +void vertex() { + yOffset = VERTEX.y; + vec3 b = vec3(VERTEX.x, VERTEX.y + calcHeight(VERTEX.x + 0.01, VERTEX.z), VERTEX.z); + vec3 c = vec3(VERTEX.x, VERTEX.y + calcHeight(VERTEX.x, VERTEX.z + 0.01), VERTEX.z); + VERTEX.y += calcHeight(VERTEX.x, VERTEX.z); + NORMAL = normalize(cross(b - VERTEX, c - VERTEX)); + yOffset += VERTEX.y; +} + +void fragment() { + ALBEDO = color; + //SPECULAR = 1.0; + float strength = yOffset * texture(foamTex, UV).r; + //if (strength >= foamHeight) ALBEDO = ALBEDO + vec3(1, 1, 1) * (strength + foamHeight); + //ALPHA = 0.9; + ALBEDO = NORMAL; +} + +//void light() { + // Called for every pixel for every light affecting the material. + // Uncomment to replace the default light processing function with this one. +//} diff --git a/noise.png b/noise.png new file mode 100644 index 0000000..471ac08 Binary files /dev/null and b/noise.png differ diff --git a/noise.png.import b/noise.png.import new file mode 100644 index 0000000..282407a --- /dev/null +++ b/noise.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://251fcnsgqb0y" +path.s3tc="res://.godot/imported/noise.png-a5cb495e1c9f6281c22a3b0a0f68704b.s3tc.ctex" +metadata={ +"imported_formats": ["s3tc_bptc"], +"vram_texture": true +} + +[deps] + +source_file="res://noise.png" +dest_files=["res://.godot/imported/noise.png-a5cb495e1c9f6281c22a3b0a0f68704b.s3tc.ctex"] + +[params] + +compress/mode=2 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=true +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=0 diff --git a/project.godot b/project.godot index 15e2264..561eeef 100644 --- a/project.godot +++ b/project.godot @@ -20,7 +20,7 @@ project/assembly_name="water shader" [editor_plugins] -enabled=PackedStringArray("res://addons/godot-xr-tools/addons/godot-xr-tools/plugin.cfg") +enabled=PackedStringArray() [physics]