add ambient occlusion shader to mini view

This commit is contained in:
Nitwel 2024-05-26 22:22:26 +02:00
parent c0cd7c01df
commit a2e423ece9
12 changed files with 124 additions and 48 deletions

View File

@ -1,5 +1,7 @@
extends Node3D extends Node3D
const RoomMaterial = preload ("res://content/system/house/room/room_next.tres")
const Room = preload ("./room/room.tscn") const Room = preload ("./room/room.tscn")
const RoomType = preload ("./room/room.gd") const RoomType = preload ("./room/room.gd")
const Doors = preload ("./doors/doors.gd") const Doors = preload ("./doors/doors.gd")
@ -213,6 +215,7 @@ func fix_reference():
align_reference.disabled = false align_reference.disabled = false
align_reference.visible = true align_reference.visible = true
align_reference.update_initial_positions() align_reference.update_initial_positions()
RoomMaterial.set_shader_parameter("show_border", true)
func save_reference(): func save_reference():
if fixing_reference: if fixing_reference:
@ -235,6 +238,7 @@ func save_reference():
align_reference.update_initial_positions() align_reference.update_initial_positions()
Store.house.save_local() Store.house.save_local()
RoomMaterial.set_shader_parameter("show_border", false)
func save_all_entities(): func save_all_entities():
Store.house.state.entities.clear() Store.house.state.entities.clear()

View File

@ -21,6 +21,8 @@ var editable: bool = false:
update() update()
var _show_border: bool = false
func _ready(): func _ready():
Update.props(self, ["editable"]) Update.props(self, ["editable"])

View File

@ -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);
}

View File

@ -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/system/house/room/room.gd" id="1_fccq0"]
[ext_resource type="Script" path="res://content/functions/clickable.gd" id="1_ugebq"] [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://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/view.gd" id="6_g066t"]
[ext_resource type="Script" path="res://content/system/house/room/states/edit.gd" id="7_ap14h"] [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="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"] [sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_ap613"]
plane = Plane(0, -1, 0, 0) plane = Plane(0, -1, 0, 0)
@ -26,10 +36,10 @@ shape = SubResource("WorldBoundaryShape3D_08sv0")
script = ExtResource("1_ugebq") script = ExtResource("1_ugebq")
[node name="WallMesh" type="MeshInstance3D" parent="."] [node name="WallMesh" type="MeshInstance3D" parent="."]
material_override = ExtResource("3_w4vos") material_override = SubResource("ShaderMaterial_rqpss")
[node name="CeilingMesh" type="MeshInstance3D" parent="."] [node name="CeilingMesh" type="MeshInstance3D" parent="."]
material_override = ExtResource("3_w4vos") material_override = SubResource("ShaderMaterial_1hn7n")
[node name="WallCollision" type="StaticBody3D" parent="."] [node name="WallCollision" type="StaticBody3D" parent="."]
collision_layer = 24 collision_layer = 24

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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="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] [resource]
render_priority = 0 render_priority = 0
shader = ExtResource("1_sbr3e") 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")

View File

@ -1,15 +1,16 @@
shader_type spatial; 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 vec4 data[100];
uniform int data_size: hint_range(0, 100, 1); uniform int data_size: hint_range(0, 100, 1);
uniform float alpha: hint_range(0.0, 1.0, 0.1) = 0.3; uniform float alpha: hint_range(0.0, 1.0, 0.1) = 0.3;
uniform sampler2D color_gradient; 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); uniform vec4 edge_color : source_color = vec4(0.0, 0.0, 0.0, 1.0);
varying vec3 color; varying vec3 color;
varying vec2 aspect_ratio;
float simple_weight(int index, vec3 world_pos, float p) { float simple_weight(int index, vec3 world_pos, float p) {
return 1.0 / pow(distance(data[index].xyz, world_pos), 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); 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() { void vertex() {
color = vec3(0.9, 0.9, 0.9); color = vec3(0.9, 0.9, 0.9);
aspect_ratio = CUSTOM0.rg;
if(data_size > 0) { if(data_size > 0) {
float distances[100]; float distances[100];
@ -37,9 +33,6 @@ void vertex() {
float closest_dist = -1.0; float closest_dist = -1.0;
int closest_index = 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); closest_dist = distance(data[0].xyz, VERTEX);
// Inverse distance weighting using Shepard's method // Inverse distance weighting using Shepard's method
@ -67,17 +60,26 @@ void vertex() {
} }
void fragment() { 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; ROUGHNESS = roughness;
//SPECULAR = 0.5 * inversesqrt(ALPHA);
DEPTH = FRAGCOORD.z + 0.0001; 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);
}
} }

View File

@ -89,7 +89,7 @@ func _ready():
tween.set_parallel(true) tween.set_parallel(true)
if small.value: if small.value:
var aabb=App.house.get_level_aabb(0) var aabb=App.house.get_rooms_aabb()
var height=aabb.size.y var height=aabb.size.y
aabb.position.y=- 0.03 aabb.position.y=- 0.03

View File

@ -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/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/functions/movable.gd" id="2_x7oed"]
[ext_resource type="Script" path="res://content/system/miniature/entity_select.gd" id="3_tgpny"] [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"] [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 radius = 0.3
height = 1.75 height = 1.75
[sub_resource type="QuadMesh" id="QuadMesh_4asi8"]
[node name="Miniature" type="Node3D"] [node name="Miniature" type="Node3D"]
script = ExtResource("1_b53yn") 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) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.757576, 0)
gizmo_extents = 0.1 gizmo_extents = 0.1
script = ExtResource("3_tgpny") 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")

View File

@ -42,6 +42,7 @@ static func generate_wall_mesh_with_doors(corners, height, doors):
var corner = corners[i] var corner = corners[i]
var next_corner = corners[(i + 1) % corners.size()] 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 forward = Vector3(next_corner.x - corner.x, 0, next_corner.y - corner.y).normalized()
var points := PackedVector2Array() 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 triangles: PackedInt32Array = cdt.get_all_triangles()
var points_3d = PackedVector3Array() var points_3d = PackedVector3Array()
var points_uv = PackedVector2Array()
for k in range(points.size()): 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 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 corner = corners[i]
var next_corner = corners[(i + 1) % corners.size()] 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 forward = Vector3(next_corner.x - corner.x, 0, next_corner.y - corner.y).normalized()
var points := PackedVector2Array() 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 triangles: PackedInt32Array = cdt.get_all_triangles()
var points_3d = PackedVector3Array() var points_3d = PackedVector3Array()
var points_uv = PackedVector2Array()
for k in range(points.size()): 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_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 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) 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() var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES) st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_custom_format(0, SurfaceTool.CUSTOM_RG_FLOAT)
for i in range(points.size()): 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)) st.add_vertex(Vector3(points[i].x, points[i].y, points[i].z))
for i in range(triangles.size()): for i in range(triangles.size()):
@ -393,8 +403,11 @@ static func _create_mesh_2d(points: PackedVector2Array, triangles: PackedInt32Ar
var st = SurfaceTool.new() var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_TRIANGLES) st.begin(Mesh.PRIMITIVE_TRIANGLES)
st.set_custom_format(0, SurfaceTool.CUSTOM_RG_FLOAT)
for i in range(points.size()): 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)) st.add_vertex(Vector3(points[i].x, 0, points[i].y))
for i in range(triangles.size()): for i in range(triangles.size()):