Skip to content

Commit

Permalink
Merge pull request #323 from boku-ilen/Godot4
Browse files Browse the repository at this point in the history
Port to Godot 4.0
  • Loading branch information
MathiasBaumgartinger authored Jul 31, 2023
2 parents cd58554 + d61247f commit 7e68f94
Show file tree
Hide file tree
Showing 2,694 changed files with 49,336 additions and 95,766 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Godot-specific ignores
.godot/
.import/
export.cfg
export_presets.cfg
Expand Down
11 changes: 5 additions & 6 deletions Buildings/BuildingBase.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tool
extends Spatial
extends Node3D


#
Expand All @@ -10,7 +9,7 @@ extends Spatial


var height
var footprint: PoolVector2Array
var footprint: PackedVector2Array
var holes: Array

func set_footprint(new_footprint):
Expand Down Expand Up @@ -44,13 +43,13 @@ func set_offset(offset_x: int, offset_y: int):
footprint[i].y = -footprint[i].y


# Build this building by calling "build" on all children.
# Build this building by calling "build" checked all children.
func build():
# To stack the floors on top of each other, the total height must be remembered
# To stack the floors checked top of each other, the total height must be remembered
var next_floor_height_offset = 0

for child in get_children():
child.translation.y += next_floor_height_offset
child.position.y += next_floor_height_offset

if child.has_method("build"):
child.build(footprint)
Expand Down
8 changes: 4 additions & 4 deletions Buildings/BuildingBase.tscn
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://b06qlp5itcqts"]

[ext_resource path="res://Buildings/BuildingBase.gd" type="Script" id=1]
[ext_resource type="Script" path="res://Buildings/BuildingBase.gd" id="1"]

[node name="BuildingBase" type="Spatial"]
script = ExtResource( 1 )
[node name="BuildingBase" type="Node3D"]
script = ExtResource("1")
18 changes: 9 additions & 9 deletions Buildings/Components/FlatRoof.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tool
extends Spatial
extends Node3D


#
Expand All @@ -11,18 +10,18 @@ var color


func _ready():
$MeshInstance.material_override = preload("res://Buildings/Components/FlatRoof.tres")
$MeshInstance3D.material_override = preload("res://Buildings/Components/FlatRoof.tres")


func set_color(new_color):
color = new_color


func build(footprint: PoolVector2Array):
func build(footprint: PackedVector2Array):
# Convert the footprint to a polygon
var polygon_indices = Geometry.triangulate_polygon(footprint)
var polygon_indices = Geometry2D.triangulate_polygon(footprint)

if polygon_indices.empty():
if polygon_indices.is_empty():
# The triangualtion was unsuccessful
return

Expand All @@ -31,12 +30,13 @@ func build(footprint: PoolVector2Array):

for index in polygon_indices:
var vertex_2d = footprint[index]
st.add_color(color)
st.add_uv(vertex_2d * 0.1)
st.set_color(color)
st.set_uv(vertex_2d * 0.1)
st.add_vertex(Vector3(vertex_2d.x, 0, vertex_2d.y))

st.generate_normals()
st.generate_tangents()

# Apply
var mesh = st.commit()
get_node("MeshInstance").mesh = mesh
get_node("MeshInstance3D").mesh = mesh
21 changes: 11 additions & 10 deletions Buildings/Components/FlatRoof.tres
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[gd_resource type="ShaderMaterial" load_steps=3 format=2]
[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://bk6oqvoa2n7bp"]

[ext_resource path="res://Resources/Textures/Buildings/roof/flat_roof_white.jpg" type="Texture" id=1]
[ext_resource path="res://Buildings/Components/RoofShader.gdshader" type="Shader" id=2]
[ext_resource type="Texture2D" uid="uid://c8swkj5p24pcg" path="res://Resources/Textures/Buildings/roof/flat_roof_white.jpg" id="1"]
[ext_resource type="Shader" path="res://Buildings/Components/RoofShader.gdshader" id="2"]

[resource]
resource_local_to_scene = true
shader = ExtResource( 2 )
shader_param/specular = 0.5
shader_param/metallic = 1.0
shader_param/roughness = 0.7
shader_param/point_size = 1.0
shader_param/normal_scale = null
shader_param/texture_albedo = ExtResource( 1 )
render_priority = 0
shader = ExtResource("2")
shader_parameter/specular = 0.5
shader_parameter/metallic = 1.0
shader_parameter/roughness = 0.7
shader_parameter/point_size = 1.0
shader_parameter/normal_scale = null
shader_parameter/texture_albedo = ExtResource("1")
11 changes: 6 additions & 5 deletions Buildings/Components/FlatRoof.tscn
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=2 format=3 uid="uid://dnsskjxroy78b"]

[ext_resource path="res://Buildings/Components/FlatRoof.gd" type="Script" id=1]
[ext_resource type="Script" path="res://Buildings/Components/FlatRoof.gd" id="1"]

[node name="FlatRoof" type="Spatial"]
script = ExtResource( 1 )
[node name="FlatRoof" type="Node3D"]
script = ExtResource("1")

[node name="MeshInstance" type="MeshInstance" parent="."]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
layers = 4
cast_shadow = 0
51 changes: 27 additions & 24 deletions Buildings/Components/PlainWalls.gd
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
tool
extends Spatial
extends Node3D


#
# Plain (flat) walls for a building.
#


export(bool) var wind_counterclockwise = true
@export var wind_counterclockwise: bool = true

var height = 2.5
var texture_scale = 2.5 # Size of the texture in meters - likely identical to the height

var color


func _ready():
$MeshInstance3D.material_override = preload("res://Buildings/Components/PlainWalls.tres")


func set_color(new_color):
color = new_color


func set_lights_enabled(enabled):
$MeshInstance.material_override.set_shader_param("lights_on", enabled)
$MeshInstance3D.material_override.set_shader_parameter("lights_on", enabled)


func set_window_shading(enabled: bool):
$MeshInstance.material_override.set_shader_param("window_shading", enabled)
$MeshInstance3D.material_override.set_shader_parameter("window_shading", enabled)


func build(footprint: PoolVector2Array):
func build(footprint: PackedVector2Array):
var st = SurfaceTool.new()

st.begin(Mesh.PRIMITIVE_TRIANGLES)

# Generate flat normals
st.set_smooth_group(-1)

# We want to essentially extrude the footprint, to create walls from lines.
# Each two subsequent vertices should become a wall.
# Sine each wall is a rectangle, each wall is composed of two triangles.
Expand All @@ -56,58 +62,55 @@ func build(footprint: PoolVector2Array):
var distance_to_next_point = max(0.1, point_3d.distance_to(next_point_3d)) # to prevent division by 0

if (wind_counterclockwise):
var tangent_plane = Plane(next_point_3d, point_up_3d, point_3d)
st.add_tangent(tangent_plane)
st.add_color(color)
st.set_color(color)

# First triangle of the wall
st.add_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.set_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.add_vertex(next_point_3d)

st.add_uv(Vector2(0.0, height) / texture_scale)
st.set_uv(Vector2(0.0, height) / texture_scale)
st.add_vertex(point_up_3d)

st.add_uv(Vector2(0.0, 0.0))
st.set_uv(Vector2(0.0, 0.0))
st.add_vertex(point_3d)

# Second triangle of the wall
st.add_uv(Vector2(distance_to_next_point, height) / texture_scale)
st.set_uv(Vector2(distance_to_next_point, height) / texture_scale)
st.add_vertex(next_point_up_3d)

st.add_uv(Vector2(0.0, height) / texture_scale)
st.set_uv(Vector2(0.0, height) / texture_scale)
st.add_vertex(point_up_3d)

st.add_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.set_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.add_vertex(next_point_3d)
else:
var tangent_plane = Plane(point_3d, point_up_3d, next_point_3d)
st.add_tangent(tangent_plane)
st.add_color(color)

# First triangle of the wall
st.add_uv(Vector2(0.0, 0.0))
st.set_uv(Vector2(0.0, 0.0))
st.add_vertex(point_3d)

st.add_uv(Vector2(0.0, height) / texture_scale)
st.set_uv(Vector2(0.0, height) / texture_scale)
st.add_vertex(point_up_3d)

st.add_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.set_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.add_vertex(next_point_3d)

# Second triangle of the wall
st.add_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.set_uv(Vector2(distance_to_next_point, 0.0) / texture_scale)
st.add_vertex(next_point_3d)

st.add_uv(Vector2(0.0, height) / texture_scale)
st.set_uv(Vector2(0.0, height) / texture_scale)
st.add_vertex(point_up_3d)

st.add_uv(Vector2(distance_to_next_point, height) / texture_scale)
st.set_uv(Vector2(distance_to_next_point, height) / texture_scale)
st.add_vertex(next_point_up_3d)

st.generate_normals()
st.generate_tangents()

# Apply
var mesh = st.commit()
$MeshInstance.mesh = mesh
$MeshInstance3D.mesh = mesh


71 changes: 36 additions & 35 deletions Buildings/Components/PlainWalls.tres
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
[gd_resource type="ShaderMaterial" load_steps=6 format=2]
[gd_resource type="ShaderMaterial" load_steps=6 format=3 uid="uid://b26nu6gpqlovf"]

[ext_resource path="res://Resources/Textures/Buildings/facade/plaster_white.jpg" type="Texture" id=1]
[ext_resource path="res://Resources/Textures/Buildings/facade/normalmap_plaster.jpg" type="Texture" id=2]
[ext_resource path="res://Resources/Textures/Buildings/facade/plaster_metallic.jpg" type="Texture" id=3]
[ext_resource path="res://Resources/Textures/Buildings/facade/plaster_roughness.jpg" type="Texture" id=4]
[ext_resource type="Texture2D" uid="uid://b8otwpf8100d" path="res://Resources/Textures/Buildings/facade/plaster_white.jpg" id="1"]
[ext_resource type="Texture2D" uid="uid://c26k818ryw8sd" path="res://Resources/Textures/Buildings/facade/normalmap_plaster.jpg" id="2"]
[ext_resource type="Texture2D" uid="uid://cdbi4nnlw1jtw" path="res://Resources/Textures/Buildings/facade/plaster_metallic.jpg" id="3"]
[ext_resource type="Texture2D" uid="uid://2ing3u6wp6lx" path="res://Resources/Textures/Buildings/facade/plaster_roughness.jpg" id="4"]

[sub_resource type="Shader" id=1]
[sub_resource type="Shader" id="1"]
code = "shader_type spatial;

uniform sampler2D texture_albedo : hint_albedo;
uniform sampler2D texture_albedo : source_color;
uniform float specular;
uniform float metallic;
uniform float roughness : hint_range(0,1);
uniform float point_size : hint_range(0,128);
uniform sampler2D texture_metallic : hint_white;
uniform sampler2D texture_metallic : hint_default_white;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_white;
uniform sampler2D texture_roughness : hint_default_white;
uniform vec4 roughness_texture_channel;
uniform sampler2D texture_emission : hint_black_albedo;
uniform vec4 emission : hint_color;
uniform sampler2D texture_emission : hint_default_black;
uniform vec4 emission : source_color;
uniform float emission_energy;
uniform sampler2D texture_normal : hint_normal;
uniform float normal_scale : hint_range(-16,16);
Expand All @@ -35,7 +35,7 @@ varying vec3 worldpos;

void vertex() {
UV=UV*uv1_scale.xy+uv1_offset.xy;
worldpos = (WORLD_MATRIX * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
worldpos = (MODEL_MATRIX * vec4(0.0, 0.0, 0.0, 1.0)).xyz;
}

float rand(vec3 co){
Expand All @@ -54,8 +54,8 @@ void fragment() {
float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
ROUGHNESS = roughness_tex * roughness;
SPECULAR = specular;
NORMALMAP = texture(texture_normal,base_uv).rgb;
NORMALMAP_DEPTH = normal_scale;
NORMAL_MAP = texture(texture_normal,base_uv).rgb;
NORMAL_MAP_DEPTH = normal_scale;

if (lights_on) {
int random_seed = int(rand(round(worldpos)) * rand(vec3(floor(UV), 0.0)) * 100000.0);
Expand All @@ -70,24 +70,25 @@ void fragment() {

[resource]
resource_local_to_scene = true
shader = SubResource( 1 )
shader_param/specular = 0.5
shader_param/metallic = 1.0
shader_param/roughness = 1.0
shader_param/point_size = 1.0
shader_param/metallic_texture_channel = Plane( 1, 0, 0, 0 )
shader_param/roughness_texture_channel = Plane( 1, 0, 0, 0 )
shader_param/emission = Color( 1, 0.866667, 0.67451, 1 )
shader_param/emission_energy = 6.0
shader_param/normal_scale = 3.0
shader_param/uv1_scale = Vector3( 1, 1, 1 )
shader_param/uv1_offset = Vector3( 0, 0, 0 )
shader_param/uv2_scale = Vector3( 1, 1, 1 )
shader_param/uv2_offset = Vector3( 0, 0, 0 )
shader_param/lights_on = false
shader_param/window_shading = true
shader_param/texture_albedo = ExtResource( 1 )
shader_param/texture_metallic = ExtResource( 3 )
shader_param/texture_roughness = ExtResource( 4 )
shader_param/texture_emission = ExtResource( 3 )
shader_param/texture_normal = ExtResource( 2 )
render_priority = 0
shader = SubResource("1")
shader_parameter/specular = 0.5
shader_parameter/metallic = 1.0
shader_parameter/roughness = 1.0
shader_parameter/point_size = 1.0
shader_parameter/metallic_texture_channel = Plane(1, 0, 0, 0)
shader_parameter/roughness_texture_channel = Plane(1, 0, 0, 0)
shader_parameter/emission = Color(1, 0.866667, 0.67451, 1)
shader_parameter/emission_energy = 6.0
shader_parameter/normal_scale = 3.0
shader_parameter/uv1_scale = Vector3(1, 1, 1)
shader_parameter/uv1_offset = Vector3(0, 0, 0)
shader_parameter/uv2_scale = Vector3(1, 1, 1)
shader_parameter/uv2_offset = Vector3(0, 0, 0)
shader_parameter/lights_on = false
shader_parameter/window_shading = true
shader_parameter/texture_albedo = ExtResource("1")
shader_parameter/texture_metallic = ExtResource("3")
shader_parameter/texture_roughness = ExtResource("4")
shader_parameter/texture_emission = ExtResource("3")
shader_parameter/texture_normal = ExtResource("2")
13 changes: 6 additions & 7 deletions Buildings/Components/PlainWalls.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=2 format=3 uid="uid://ba4dacxh7qiw0"]

[ext_resource path="res://Buildings/Components/PlainWalls.gd" type="Script" id=1]
[ext_resource path="res://Buildings/Components/PlainWalls.tres" type="Material" id=2]
[ext_resource type="Script" path="res://Buildings/Components/PlainWalls.gd" id="1"]

[node name="PlainWalls" type="Spatial"]
script = ExtResource( 1 )
[node name="PlainWalls" type="Node3D"]
script = ExtResource("1")

[node name="MeshInstance" type="MeshInstance" parent="."]
material_override = ExtResource( 2 )
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
layers = 4
Loading

0 comments on commit 7e68f94

Please sign in to comment.