floor meshing

This commit is contained in:
veclav talica 2024-08-02 03:48:26 +03:00
parent 77892c47e4
commit d9cf3be232
4 changed files with 87 additions and 18 deletions

BIN
Art/Stone_11-128x128.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://eempmja460l5"
path="res://.godot/imported/Stone_11-128x128.png-74289798a2555c4732a5c0fbe1b7230c.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://Art/Stone_11-128x128.png"
dest_files=["res://.godot/imported/Stone_11-128x128.png-74289798a2555c4732a5c0fbe1b7230c.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
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=1

View File

@ -17,6 +17,28 @@ func _generate_geometry(space: Space, looked_from_inside: bool) -> Node3D:
var geometry := MeshInstance3D.new() var geometry := MeshInstance3D.new()
geometry.name = name geometry.name = name
var mesh := ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, _generate_wall_arrays(space, looked_from_inside))
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, _generate_floor_arrays(space))
var wall_material := StandardMaterial3D.new()
wall_material.albedo_texture = wall_texture
mesh.surface_set_material(0, wall_material)
var floor_material := StandardMaterial3D.new()
floor_material.albedo_texture = floor_texture
mesh.surface_set_material(1, floor_material)
geometry.mesh = mesh
for child in get_children():
if child is SpaceRegion:
geometry.add_child(child._generate_geometry(space, not looked_from_inside))
return geometry
func _generate_wall_arrays(space: Space, looked_from_inside: bool) -> Array:
var polygon = self.polygon var polygon = self.polygon
var polygon_pairs = Array() var polygon_pairs = Array()
for i in range(-1, polygon.size() - 1): for i in range(-1, polygon.size() - 1):
@ -31,22 +53,8 @@ func _generate_geometry(space: Space, looked_from_inside: bool) -> Node3D:
arrays.resize(Mesh.ARRAY_MAX) arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices arrays[Mesh.ARRAY_VERTEX] = vertices
arrays[Mesh.ARRAY_TEX_UV] = uvs arrays[Mesh.ARRAY_TEX_UV] = uvs
var mesh := ArrayMesh.new() return arrays
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
var new_material := preload("res://Scenes/RegionMaterial.tres")
new_material.albedo_texture = wall_texture
mesh.surface_set_material(0, new_material)
geometry.mesh = mesh
for child in get_children():
if child is SpaceRegion:
geometry.add_child(child._generate_geometry(space, not looked_from_inside))
return geometry
func _push_line(space: Space, func _push_line(space: Space,
@ -79,3 +87,26 @@ func _push_line(space: Space,
uvs.append(Vector2(1, 0)) uvs.append(Vector2(1, 0))
uvs.append(Vector2(0, 1)) uvs.append(Vector2(0, 1))
uvs.append(Vector2(0, 0)) uvs.append(Vector2(0, 0))
func _generate_floor_arrays(space: Space) -> Array:
var triangulation := Geometry2D.triangulate_polygon(self.polygon)
var vertices := PackedVector3Array()
var uvs := PackedVector2Array()
vertices.resize(triangulation.size())
uvs.resize(triangulation.size())
for i in range(triangulation.size()):
vertices[i] = Vector3(
self.polygon[triangulation[i]].x * space.unit_scale,
elevation * space.unit_scale,
self.polygon[triangulation[i]].y * space.unit_scale,
)
uvs[i] = self.polygon[triangulation[i]] * space.unit_scale
var arrays := Array()
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices
arrays[Mesh.ARRAY_TEX_UV] = uvs
return arrays

View File

@ -1,10 +1,11 @@
[gd_scene load_steps=7 format=3 uid="uid://deff7lt34nj0h"] [gd_scene load_steps=8 format=3 uid="uid://deff7lt34nj0h"]
[ext_resource type="Script" path="res://Scripts/Space/Space.gd" id="1_ckp7u"] [ext_resource type="Script" path="res://Scripts/Space/Space.gd" id="1_ckp7u"]
[ext_resource type="Script" path="res://Scripts/Space/SpaceRegion.gd" id="2_s3h7s"] [ext_resource type="Script" path="res://Scripts/Space/SpaceRegion.gd" id="2_s3h7s"]
[ext_resource type="Texture2D" uid="uid://h0skytm8grcu" path="res://icon.svg" id="3_7vff1"] [ext_resource type="Texture2D" uid="uid://h0skytm8grcu" path="res://icon.svg" id="3_7vff1"]
[ext_resource type="Texture2D" uid="uid://icmmuqquxhq3" path="res://Art/Stone_06-128x128.png" id="3_ondo6"] [ext_resource type="Texture2D" uid="uid://icmmuqquxhq3" path="res://Art/Stone_06-128x128.png" id="3_ondo6"]
[ext_resource type="Texture2D" uid="uid://buoupwtftxjse" path="res://Art/Stone_05-128x128.png" id="3_r08k8"] [ext_resource type="Texture2D" uid="uid://buoupwtftxjse" path="res://Art/Stone_05-128x128.png" id="3_r08k8"]
[ext_resource type="Texture2D" uid="uid://eempmja460l5" path="res://Art/Stone_11-128x128.png" id="4_mnswr"]
[ext_resource type="Script" path="res://Scripts/Space/SpaceSpawn.gd" id="4_rhl23"] [ext_resource type="Script" path="res://Scripts/Space/SpaceSpawn.gd" id="4_rhl23"]
[node name="Dungeon" type="Node2D"] [node name="Dungeon" type="Node2D"]
@ -15,7 +16,7 @@ position = Vector2(-30, -8)
polygon = PackedVector2Array(30, 72, 262, 32, 497, 192, 286, 328, 94, 264) polygon = PackedVector2Array(30, 72, 262, 32, 497, 192, 286, 328, 94, 264)
script = ExtResource("2_s3h7s") script = ExtResource("2_s3h7s")
wall_texture = ExtResource("3_ondo6") wall_texture = ExtResource("3_ondo6")
floor_texture = ExtResource("3_r08k8") floor_texture = ExtResource("4_mnswr")
[node name="Column" type="Polygon2D" parent="Room"] [node name="Column" type="Polygon2D" parent="Room"]
position = Vector2(78, -8) position = Vector2(78, -8)