From 03d271e87cc5fca08814d0b7248dd770f5b6c364 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 8 Feb 2024 23:46:03 +0100 Subject: [PATCH] Added water shader --- client/Main.tscn | 46 ++++++++++++++++--- .../{grass_shader.gdshader => Grass.gdshader} | 0 ...grass_material.tres => GrassMaterial.tres} | 2 +- client/world/water/Water.gdshader | 36 +++++++++++++++ client/world/water/Water.tres | 30 ++++++++++++ client/world/water/Water.tscn | 11 +++++ 6 files changed, 118 insertions(+), 7 deletions(-) rename client/world/grass/{grass_shader.gdshader => Grass.gdshader} (100%) rename client/world/grass/{grass_material.tres => GrassMaterial.tres} (81%) create mode 100644 client/world/water/Water.gdshader create mode 100644 client/world/water/Water.tres create mode 100644 client/world/water/Water.tscn diff --git a/client/Main.tscn b/client/Main.tscn index 9373018..1e328b1 100644 --- a/client/Main.tscn +++ b/client/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=25 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=26 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://Main.gd" id="1_cw3ws"] [ext_resource type="Script" path="res://network/Client.gd" id="2_8hxcx"] @@ -18,9 +18,10 @@ [ext_resource type="PackedScene" uid="uid://cb2t7bvvf3gwh" path="res://enemy/slime/Slime.tscn" id="15_hgl78"] [ext_resource type="Script" path="res://world/PlayerManager.gd" id="16_dp6bj"] [ext_resource type="PackedScene" uid="uid://dagn5bf7ou3sd" path="res://ui/UI.tscn" id="17_43qhq"] -[ext_resource type="Material" uid="uid://bdsblfaxbipaa" path="res://world/grass/grass_material.tres" id="18_tja64"] +[ext_resource type="Material" uid="uid://bdsblfaxbipaa" path="res://world/grass/GrassMaterial.tres" id="18_tja64"] [ext_resource type="Script" path="res://camera/Camera.gd" id="18_wogcj"] [ext_resource type="MultiMesh" uid="uid://dog5aq5n2q025" path="res://assets/grass/grass.multimesh" id="19_ae26a"] +[ext_resource type="PackedScene" uid="uid://cm0rho6adv2p7" path="res://world/water/Water.tscn" id="20_bmo3k"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_he5c5"] diffuse_mode = 3 @@ -137,6 +138,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7.00829, 1.28057, -1.95247) [node name="Slime3" parent="World/Enemies" instance=ExtResource("15_hgl78")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.53558, 1.28057, -0.306177) +[node name="Grass" type="MultiMeshInstance3D" parent="World"] +material_override = ExtResource("18_tja64") +cast_shadow = 0 +multimesh = ExtResource("19_ae26a") + [node name="Floor" type="MeshInstance3D" parent="World"] mesh = SubResource("PlaneMesh_lb6xk") @@ -146,10 +152,38 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.5, 0) [node name="CollisionShape3D" type="CollisionShape3D" parent="World/Floor/StaticBody3D"] shape = SubResource("BoxShape3D_yg2lt") -[node name="Grass" type="MultiMeshInstance3D" parent="World"] -material_override = ExtResource("18_tja64") -cast_shadow = 0 -multimesh = ExtResource("19_ae26a") +[node name="Ocean" type="Node3D" parent="World"] + +[node name="Water" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +skeleton = NodePath("../..") + +[node name="Water2" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 25, 0, 25) +skeleton = NodePath("../..") + +[node name="Water3" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 25) +skeleton = NodePath("../..") + +[node name="Water4" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -25, 0, 25) +skeleton = NodePath("../..") + +[node name="Water5" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -25, 0, 0) +skeleton = NodePath("../..") + +[node name="Water6" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -25, 0, -25) +skeleton = NodePath("../..") + +[node name="Water7" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -25) +skeleton = NodePath("../..") + +[node name="Water8" parent="World/Ocean" instance=ExtResource("20_bmo3k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 25, 0, -25) +skeleton = NodePath("../..") [node name="Players" type="Node3D" parent="."] unique_name_in_owner = true diff --git a/client/world/grass/grass_shader.gdshader b/client/world/grass/Grass.gdshader similarity index 100% rename from client/world/grass/grass_shader.gdshader rename to client/world/grass/Grass.gdshader diff --git a/client/world/grass/grass_material.tres b/client/world/grass/GrassMaterial.tres similarity index 81% rename from client/world/grass/grass_material.tres rename to client/world/grass/GrassMaterial.tres index 888446a..d085fb8 100644 --- a/client/world/grass/grass_material.tres +++ b/client/world/grass/GrassMaterial.tres @@ -1,6 +1,6 @@ [gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://bdsblfaxbipaa"] -[ext_resource type="Shader" path="res://world/grass/grass_shader.gdshader" id="1_7qi6v"] +[ext_resource type="Shader" path="res://world/grass/Grass.gdshader" id="1_7qi6v"] [resource] render_priority = 0 diff --git a/client/world/water/Water.gdshader b/client/world/water/Water.gdshader new file mode 100644 index 0000000..3494fe3 --- /dev/null +++ b/client/world/water/Water.gdshader @@ -0,0 +1,36 @@ +shader_type spatial; +render_mode cull_disabled; +render_mode specular_toon; +// render_mode wireframe; + +uniform vec3 color : source_color = vec3(0.055, 0.286, 0.62); +uniform vec3 fresnel_color : source_color = vec3(0.141, 0.447, 0.902); +uniform float fresnel_reduction = 5.0; +uniform float roughness : hint_range(0.0, 1.0) = 0.02; +uniform sampler2D height_map; +uniform sampler2D normal_map; +uniform float height_scale = 0.0; +uniform float noise_scale = 0.5; + +varying vec2 noise_position; + +float get_fresnel(float power, vec3 normal, vec3 view) { + return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0)), power); +} + +void vertex() { + noise_position = VERTEX.xz * noise_scale + 0.5; + float height = texture(height_map, noise_position).x - 0.5; + VERTEX.y += height * height_scale; +} + +void fragment() { + // NORMAL_MAP = texture(normal_map, UV).rgb; + NORMAL_MAP = texture(normal_map, noise_position).xyz; + + float fresnel = get_fresnel(fresnel_reduction, NORMAL, VIEW); + RIM = 0.2; + METALLIC = 0.0; + ROUGHNESS = roughness * fresnel; + ALBEDO = mix(color, fresnel_color, fresnel); +} \ No newline at end of file diff --git a/client/world/water/Water.tres b/client/world/water/Water.tres new file mode 100644 index 0000000..2165d1b --- /dev/null +++ b/client/world/water/Water.tres @@ -0,0 +1,30 @@ +[gd_resource type="ShaderMaterial" load_steps=6 format=3 uid="uid://c6kk0k4jvuh4q"] + +[ext_resource type="Shader" path="res://world/water/Water.gdshader" id="1_uj7oj"] + +[sub_resource type="FastNoiseLite" id="FastNoiseLite_f06x4"] + +[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_7t3c8"] +noise = SubResource("FastNoiseLite_f06x4") + +[sub_resource type="FastNoiseLite" id="FastNoiseLite_d0hd3"] +noise_type = 3 +fractal_type = 2 + +[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_rcc52"] +seamless = true +as_normal_map = true +bump_strength = 1.5 +noise = SubResource("FastNoiseLite_d0hd3") + +[resource] +render_priority = 0 +shader = ExtResource("1_uj7oj") +shader_parameter/color = Color(0.054902, 0.286275, 0.619608, 1) +shader_parameter/fresnel_color = Color(0.141176, 0.447059, 0.901961, 1) +shader_parameter/fresnel_reduction = 5.0 +shader_parameter/roughness = 0.02 +shader_parameter/height_scale = 0.0 +shader_parameter/noise_scale = 0.5 +shader_parameter/height_map = SubResource("NoiseTexture2D_7t3c8") +shader_parameter/normal_map = SubResource("NoiseTexture2D_rcc52") diff --git a/client/world/water/Water.tscn b/client/world/water/Water.tscn new file mode 100644 index 0000000..d612582 --- /dev/null +++ b/client/world/water/Water.tscn @@ -0,0 +1,11 @@ +[gd_scene load_steps=3 format=3 uid="uid://cm0rho6adv2p7"] + +[ext_resource type="Material" uid="uid://c6kk0k4jvuh4q" path="res://world/water/Water.tres" id="1_yiqgh"] + +[sub_resource type="PlaneMesh" id="PlaneMesh_cybb0"] +material = ExtResource("1_yiqgh") +size = Vector2(25, 25) + +[node name="Water" type="MeshInstance3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 25, 0, 0) +mesh = SubResource("PlaneMesh_cybb0")