From a2e423ece963ba2b81cb5a5a8d526094ddaca519 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Sun, 26 May 2024 22:22:26 +0200 Subject: [PATCH] add ambient occlusion shader to mini view --- app/content/system/house/house.gd | 4 ++ app/content/system/house/room/room.gd | 2 + app/content/system/house/room/room.gdshader | 6 +++ app/content/system/house/room/room.tscn | 18 ++++++-- .../system/house/room/room_next.gdshader | 37 +++++++++++++++ app/content/system/house/room/room_next.tres | 9 ++++ app/content/system/house/room/wall.tres | 8 ---- app/content/system/miniature/mini_wall.tres | 9 +--- .../miniature/mini_wall_shader.gdshader | 46 ++++++++++--------- app/content/system/miniature/miniature.gd | 2 +- app/content/system/miniature/miniature.tscn | 10 +++- app/lib/utils/mesh/construct_room_mesh.gd | 21 +++++++-- 12 files changed, 124 insertions(+), 48 deletions(-) create mode 100644 app/content/system/house/room/room.gdshader create mode 100644 app/content/system/house/room/room_next.gdshader create mode 100644 app/content/system/house/room/room_next.tres delete mode 100644 app/content/system/house/room/wall.tres diff --git a/app/content/system/house/house.gd b/app/content/system/house/house.gd index 61d81a2..9565177 100644 --- a/app/content/system/house/house.gd +++ b/app/content/system/house/house.gd @@ -1,5 +1,7 @@ extends Node3D +const RoomMaterial = preload ("res://content/system/house/room/room_next.tres") + const Room = preload ("./room/room.tscn") const RoomType = preload ("./room/room.gd") const Doors = preload ("./doors/doors.gd") @@ -213,6 +215,7 @@ func fix_reference(): align_reference.disabled = false align_reference.visible = true align_reference.update_initial_positions() + RoomMaterial.set_shader_parameter("show_border", true) func save_reference(): if fixing_reference: @@ -235,6 +238,7 @@ func save_reference(): align_reference.update_initial_positions() Store.house.save_local() + RoomMaterial.set_shader_parameter("show_border", false) func save_all_entities(): Store.house.state.entities.clear() diff --git a/app/content/system/house/room/room.gd b/app/content/system/house/room/room.gd index c5355ff..9ce0bbb 100644 --- a/app/content/system/house/room/room.gd +++ b/app/content/system/house/room/room.gd @@ -21,6 +21,8 @@ var editable: bool = false: update() +var _show_border: bool = false + func _ready(): Update.props(self, ["editable"]) diff --git a/app/content/system/house/room/room.gdshader b/app/content/system/house/room/room.gdshader new file mode 100644 index 0000000..f31ef88 --- /dev/null +++ b/app/content/system/house/room/room.gdshader @@ -0,0 +1,6 @@ +shader_type spatial; +render_mode blend_sub, depth_draw_always, cull_disabled, diffuse_burley, specular_schlick_ggx, unshaded; + +void fragment() { + ALBEDO = vec3(1.0); +} diff --git a/app/content/system/house/room/room.tscn b/app/content/system/house/room/room.tscn index 6c46e60..9ade218 100644 --- a/app/content/system/house/room/room.tscn +++ b/app/content/system/house/room/room.tscn @@ -1,14 +1,24 @@ -[gd_scene load_steps=9 format=3 uid="uid://bswgmclohuqui"] +[gd_scene load_steps=12 format=3 uid="uid://bswgmclohuqui"] [ext_resource type="Script" path="res://content/system/house/room/room.gd" id="1_fccq0"] [ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_ugebq"] -[ext_resource type="Material" uid="uid://bbx6fv7jq50tr" path="res://content/system/house/room/wall.tres" id="3_w4vos"] +[ext_resource type="Material" uid="uid://xehjkoeofqc" path="res://content/system/house/room/room_next.tres" id="3_4q8ik"] +[ext_resource type="Shader" path="res://content/system/house/room/room.gdshader" id="3_lwge0"] [ext_resource type="Script" path="res://lib/utils/state_machine/state_machine.gd" id="4_nbbo6"] [ext_resource type="Script" path="res://content/system/house/room/states/view.gd" id="6_g066t"] [ext_resource type="Script" path="res://content/system/house/room/states/edit.gd" id="7_ap14h"] [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_08sv0"] +[sub_resource type="ShaderMaterial" id="ShaderMaterial_rqpss"] +render_priority = -100 +next_pass = ExtResource("3_4q8ik") +shader = ExtResource("3_lwge0") + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_1hn7n"] +render_priority = -100 +shader = ExtResource("3_lwge0") + [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ap613"] plane = Plane(0, -1, 0, 0) @@ -26,10 +36,10 @@ shape = SubResource("WorldBoundaryShape3D_08sv0") script = ExtResource("1_ugebq") [node name="WallMesh" type="MeshInstance3D" parent="."] -material_override = ExtResource("3_w4vos") +material_override = SubResource("ShaderMaterial_rqpss") [node name="CeilingMesh" type="MeshInstance3D" parent="."] -material_override = ExtResource("3_w4vos") +material_override = SubResource("ShaderMaterial_1hn7n") [node name="WallCollision" type="StaticBody3D" parent="."] collision_layer = 24 diff --git a/app/content/system/house/room/room_next.gdshader b/app/content/system/house/room/room_next.gdshader new file mode 100644 index 0000000..f3e0c10 --- /dev/null +++ b/app/content/system/house/room/room_next.gdshader @@ -0,0 +1,37 @@ +shader_type spatial; +render_mode blend_add,depth_draw_always, cull_disabled, diffuse_burley, specular_schlick_ggx, unshaded; + +uniform vec3 border_color: source_color = vec3(0.0); +uniform bool show_border = false; + +varying vec2 aspect_ratio; + +void vertex() { + aspect_ratio = CUSTOM0.rg; +} + +void fragment() { + ALBEDO = border_color; + ALPHA = 0.0; + + vec2 pos = vec2(UV.x * aspect_ratio.x, UV.y * aspect_ratio.y); + float border = 0.05; + + if( show_border ) { + if (pos.x <= border) { + ALPHA = mix(ALPHA, 1.0, 1.0 - pos.x / border); + } + + if (pos.x >= aspect_ratio.x - border) { + ALPHA = mix(ALPHA, 1.0, (pos.x - (aspect_ratio.x - border)) / border); + } + + if (pos.y <= border) { + ALPHA = mix(ALPHA, 1.0, 1.0 - pos.y / border); + } + + if (pos.y >= aspect_ratio.y - border) { + ALPHA = mix(ALPHA, 1.0, (pos.y - (aspect_ratio.y - border)) / border); + } + } +} diff --git a/app/content/system/house/room/room_next.tres b/app/content/system/house/room/room_next.tres new file mode 100644 index 0000000..fdf16ea --- /dev/null +++ b/app/content/system/house/room/room_next.tres @@ -0,0 +1,9 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://xehjkoeofqc"] + +[ext_resource type="Shader" path="res://content/system/house/room/room_next.gdshader" id="1_hwtkw"] + +[resource] +render_priority = -99 +shader = ExtResource("1_hwtkw") +shader_parameter/border_color = Color(0, 0, 0.564706, 1) +shader_parameter/show_border = false diff --git a/app/content/system/house/room/wall.tres b/app/content/system/house/room/wall.tres deleted file mode 100644 index 16a7c42..0000000 --- a/app/content/system/house/room/wall.tres +++ /dev/null @@ -1,8 +0,0 @@ -[gd_resource type="StandardMaterial3D" format=3 uid="uid://bbx6fv7jq50tr"] - -[resource] -render_priority = -100 -blend_mode = 2 -cull_mode = 2 -depth_draw_mode = 1 -shading_mode = 0 diff --git a/app/content/system/miniature/mini_wall.tres b/app/content/system/miniature/mini_wall.tres index 91512fe..51d8df9 100644 --- a/app/content/system/miniature/mini_wall.tres +++ b/app/content/system/miniature/mini_wall.tres @@ -1,14 +1,7 @@ -[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://bcfcough6ucvc"] +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://bcfcough6ucvc"] [ext_resource type="Shader" path="res://content/system/miniature/mini_wall_shader.gdshader" id="1_sbr3e"] -[ext_resource type="Texture2D" uid="uid://bbuq4wn7e5o2q" path="res://content/system/miniature/temp_gradient.tres" id="2_3lwi8"] [resource] render_priority = 0 shader = ExtResource("1_sbr3e") -shader_parameter/data = PackedFloat32Array() -shader_parameter/data_size = 0 -shader_parameter/alpha = 1.0 -shader_parameter/roughness = 0.15 -shader_parameter/edge_color = Color(0, 0, 0, 1) -shader_parameter/color_gradient = ExtResource("2_3lwi8") diff --git a/app/content/system/miniature/mini_wall_shader.gdshader b/app/content/system/miniature/mini_wall_shader.gdshader index facd69b..9a3c220 100644 --- a/app/content/system/miniature/mini_wall_shader.gdshader +++ b/app/content/system/miniature/mini_wall_shader.gdshader @@ -1,15 +1,16 @@ shader_type spatial; -render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx, shadows_disabled; +render_mode blend_mix, cull_back, diffuse_burley, specular_schlick_ggx; uniform vec4 data[100]; uniform int data_size: hint_range(0, 100, 1); uniform float alpha: hint_range(0.0, 1.0, 0.1) = 0.3; uniform sampler2D color_gradient; -uniform float roughness : hint_range(0.0, 1.0) = 0.15; +uniform float roughness : hint_range(0.0, 1.0) = 0.85; uniform vec4 edge_color : source_color = vec4(0.0, 0.0, 0.0, 1.0); varying vec3 color; +varying vec2 aspect_ratio; float simple_weight(int index, vec3 world_pos, float p) { return 1.0 / pow(distance(data[index].xyz, world_pos), p); @@ -20,14 +21,9 @@ float sphere_weight(int index, vec3 world_pos, float r) { return pow(max(0, r - dist) / (r * dist), 2); } -float SchlickFresnel(float u) { - float m = 1.0 - u; - float m2 = m * m; - return m2 * m2 * m; -} - void vertex() { color = vec3(0.9, 0.9, 0.9); + aspect_ratio = CUSTOM0.rg; if(data_size > 0) { float distances[100]; @@ -37,9 +33,6 @@ void vertex() { float closest_dist = -1.0; int closest_index = 0; - // Calculate Global Coordinates - //vec3 world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz; - closest_dist = distance(data[0].xyz, VERTEX); // Inverse distance weighting using Shepard's method @@ -67,17 +60,26 @@ void vertex() { } void fragment() { - ALBEDO = vec3(color.xyz); - //ALPHA = alpha; - - float VdotN = dot(VIEW, NORMAL); - float fresnel = clamp(SchlickFresnel(VdotN), 0.0, 1.0); - - // apply glass look - //float a = mix(0.001, 1.0, ALPHA); - //ALPHA = mix(fresnel * edge_color.a, 1.0, a); - //ALBEDO = mix(edge_color.rgb * edge_color.a, ALBEDO, a); ROUGHNESS = roughness; - //SPECULAR = 0.5 * inversesqrt(ALPHA); DEPTH = FRAGCOORD.z + 0.0001; + ALBEDO = vec3(color.xyz); + + vec2 pos = vec2(UV.x * aspect_ratio.x, UV.y * aspect_ratio.y); + float border = 0.05; + + if (pos.x <= border) { + ALBEDO = mix(vec3(0.0), ALBEDO, pos.x / border); + } + + if (pos.x >= aspect_ratio.x - border) { + ALBEDO = mix(vec3(0.0), ALBEDO, 1.0 - (pos.x - (aspect_ratio.x - border)) / border); + } + + if (pos.y <= border) { + ALBEDO = mix(vec3(0.0), ALBEDO, pos.y / border); + } + + if (pos.y >= aspect_ratio.y - border) { + ALBEDO = mix(vec3(0.0), ALBEDO, 1.0 - (pos.y - (aspect_ratio.y - border)) / border); + } } \ No newline at end of file diff --git a/app/content/system/miniature/miniature.gd b/app/content/system/miniature/miniature.gd index ca3fc90..6aa8dfb 100644 --- a/app/content/system/miniature/miniature.gd +++ b/app/content/system/miniature/miniature.gd @@ -89,7 +89,7 @@ func _ready(): tween.set_parallel(true) if small.value: - var aabb=App.house.get_level_aabb(0) + var aabb=App.house.get_rooms_aabb() var height=aabb.size.y aabb.position.y=- 0.03 diff --git a/app/content/system/miniature/miniature.tscn b/app/content/system/miniature/miniature.tscn index cf3f350..213d16e 100644 --- a/app/content/system/miniature/miniature.tscn +++ b/app/content/system/miniature/miniature.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=7 format=3 uid="uid://ds60i5n211hi3"] +[gd_scene load_steps=9 format=3 uid="uid://ds60i5n211hi3"] [ext_resource type="Script" path="res://content/system/miniature/miniature.gd" id="1_b53yn"] [ext_resource type="Script" path="res://content/functions/movable.gd" id="2_x7oed"] [ext_resource type="Script" path="res://content/system/miniature/entity_select.gd" id="3_tgpny"] +[ext_resource type="Material" uid="uid://bcfcough6ucvc" path="res://content/system/miniature/mini_wall.tres" id="4_be66c"] [sub_resource type="BoxShape3D" id="BoxShape3D_bckw3"] @@ -15,6 +16,8 @@ albedo_color = Color(0.404, 0.22, 0.627, 0.5) radius = 0.3 height = 1.75 +[sub_resource type="QuadMesh" id="QuadMesh_4asi8"] + [node name="Miniature" type="Node3D"] script = ExtResource("1_b53yn") @@ -43,3 +46,8 @@ mesh = SubResource("CapsuleMesh_f3avi") transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.757576, 0) gizmo_extents = 0.1 script = ExtResource("3_tgpny") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.92, 0, 0) +material_override = ExtResource("4_be66c") +mesh = SubResource("QuadMesh_4asi8") diff --git a/app/lib/utils/mesh/construct_room_mesh.gd b/app/lib/utils/mesh/construct_room_mesh.gd index 20b9049..e2e3206 100644 --- a/app/lib/utils/mesh/construct_room_mesh.gd +++ b/app/lib/utils/mesh/construct_room_mesh.gd @@ -42,6 +42,7 @@ static func generate_wall_mesh_with_doors(corners, height, doors): var corner = corners[i] var next_corner = corners[(i + 1) % corners.size()] + var width = corner.distance_to(next_corner) var forward = Vector3(next_corner.x - corner.x, 0, next_corner.y - corner.y).normalized() var points := PackedVector2Array() @@ -98,11 +99,14 @@ static func generate_wall_mesh_with_doors(corners, height, doors): var triangles: PackedInt32Array = cdt.get_all_triangles() var points_3d = PackedVector3Array() + var points_uv = PackedVector2Array() for k in range(points.size()): - points_3d.append(Vector3(corner.x, 0, corner.y) + points[k].x * forward + Vector3(0, points[k].y, 0)) + var point = Vector3(corner.x, 0, corner.y) + points[k].x * forward + Vector3(0, points[k].y, 0) + points_3d.append(point) + points_uv.append(Vector2(points[k].x / width, points[k].y / height)) - mesh = _create_mesh_3d(points_3d, triangles, mesh) + mesh = _create_mesh_3d(points_3d, triangles, points_uv, width, height, mesh) return mesh @@ -144,6 +148,7 @@ static func generate_wall_mesh_with_doors_grid(corners, height, doors, grid:=0.1 var corner = corners[i] var next_corner = corners[(i + 1) % corners.size()] + var width = corner.distance_to(next_corner) var forward = Vector3(next_corner.x - corner.x, 0, next_corner.y - corner.y).normalized() var points := PackedVector2Array() @@ -226,11 +231,13 @@ static func generate_wall_mesh_with_doors_grid(corners, height, doors, grid:=0.1 var triangles: PackedInt32Array = cdt.get_all_triangles() var points_3d = PackedVector3Array() + var points_uv = PackedVector2Array() for k in range(points.size()): points_3d.append(Vector3(corner.x, 0, corner.y) + points[k].x * forward + Vector3(0, points[k].y, 0)) + points_uv.append(Vector2(points[k].x / width, points[k].y / height)) - mesh = _create_mesh_3d(points_3d, triangles, mesh) + mesh = _create_mesh_3d(points_3d, triangles, points_uv, width, height, mesh) return mesh @@ -369,12 +376,15 @@ static func generate_ceiling_mesh_grid(corners, grid: Vector2=Vector2(0.1, 0.1)) return _create_mesh_2d(points, triangles) -static func _create_mesh_3d(points: PackedVector3Array, triangles: PackedInt32Array, existing=null): +static func _create_mesh_3d(points: PackedVector3Array, triangles: PackedInt32Array, points_uv, width: float, height: float, existing=null): var st = SurfaceTool.new() st.begin(Mesh.PRIMITIVE_TRIANGLES) + st.set_custom_format(0, SurfaceTool.CUSTOM_RG_FLOAT) for i in range(points.size()): + st.set_uv(points_uv[i]) + st.set_custom(0, Color(width, height, 0, 0)) st.add_vertex(Vector3(points[i].x, points[i].y, points[i].z)) for i in range(triangles.size()): @@ -393,8 +403,11 @@ static func _create_mesh_2d(points: PackedVector2Array, triangles: PackedInt32Ar var st = SurfaceTool.new() st.begin(Mesh.PRIMITIVE_TRIANGLES) + st.set_custom_format(0, SurfaceTool.CUSTOM_RG_FLOAT) for i in range(points.size()): + st.set_uv(Vector2(0.5, 0.5)) + st.set_custom(0, Color(1.0, 1.0, 0, 0)) st.add_vertex(Vector3(points[i].x, 0, points[i].y)) for i in range(triangles.size()):