Compare commits

..

4 Commits

8 changed files with 172 additions and 70 deletions

BIN
local_player.scn (Stored with Git LFS)

Binary file not shown.

BIN
map_editor.scn (Stored with Git LFS)

Binary file not shown.

BIN
map_editor/camera_boundary_mesh.res (Stored with Git LFS)

Binary file not shown.

View File

@ -1,18 +1,19 @@
class_name MapEditorMenu extends VBoxContainer class_name MapEditorMenu extends VBoxContainer
## ##
## ## [code]edit_action[/code] has been activated for use when the player interacts with the map
## editor.
## ##
signal edit_activated(edit_action: Callable) signal edit_activated(edit_action: Callable)
## ##
## ## Emits [signal edit_activated].
## ##
func activate_editor(edit_action: Callable) -> void: func activate_editor(edit_action: Callable) -> void:
edit_activated.emit(edit_action) edit_activated.emit(edit_action)
## ##
## ## Resets the state of the menu.
## ##
func reset() -> void: func reset() -> void:
pass pass

BIN
map_editor/menus_theme.res (Stored with Git LFS)

Binary file not shown.

BIN
map_editor/tile_selector_button_group.res (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -1,25 +1,82 @@
class_name MeshGrid extends Node3D class_name MeshGrid extends Node3D
const _CELL_COUNT := 8 const _CHUNK_SIZE := 32
class _Chunk: const _GRID_ORIGIN := Vector2(0.5, 0.5)
var multimesh_instances: Array[_MultimeshInstance] = []
var meshes: Array[Mesh] = [] ##
## Baked block of meshes making up a segment of the grid.
##
class Chunk:
var _multimesh_instances: Array[MultiMeshInstance] = []
var _meshes: Array[Mesh] = []
func _init() -> void: func _init() -> void:
self.meshes.resize(_CELL_COUNT * _CELL_COUNT) _meshes.resize(_CHUNK_SIZE * _CHUNK_SIZE)
class _MultimeshInstance: ##
## Invalidates the mesh block, re-baking its contents from the current mesh data set.
##
func invalidate(mesh_grid: MeshGrid, coordinate: Vector2i) -> void:
# TODO: Once this is all lowered into native code, look for ways to parallelize the loops.
for multimesh_instance in _multimesh_instances:
RenderingServer.free_rid(multimesh_instance._instance_rid)
RenderingServer.free_rid(multimesh_instance._multimesh_rid)
_multimesh_instances.clear()
# Normalize mesh instance data for the chunk.
var transforms_by_mesh := {}
var grid_size := mesh_grid.size
for i in _meshes.size():
var mesh := _meshes[i]
if mesh != null:
if not(mesh in transforms_by_mesh):
transforms_by_mesh[mesh] = []
transforms_by_mesh[mesh].append(Transform3D(Basis.IDENTITY, Vector3(
(float(coordinate.x * _CHUNK_SIZE) + (i % _CHUNK_SIZE)) -
(float(grid_size.x) * _GRID_ORIGIN.x), 0.0,
(float(coordinate.y * _CHUNK_SIZE) + (i / _CHUNK_SIZE)) -
(float(grid_size.y) * _GRID_ORIGIN.y))))
# (Re)-bake into multimesh instances for the chunk.
var scenario_rid := mesh_grid.get_world_3d().scenario
var global_transform := mesh_grid.global_transform
for chunk_mesh in transforms_by_mesh:
var multimesh_instance := MultiMeshInstance.new(
scenario_rid, chunk_mesh.get_rid(), transforms_by_mesh[chunk_mesh])
multimesh_instance.set_offset(global_transform)
_multimesh_instances.append(multimesh_instance)
##
## Sets the mesh location in the chunk at the relative [code]coordinatess[/code] to
## [code]mesh[/code].
##
## *Note* that [method Chunk.invalidate] must be called at some point after to visually update
## the chunk.
##
func set_mesh(coordinates: Vector2i, mesh: Mesh) -> void:
_meshes[(_CHUNK_SIZE * coordinates.y) + coordinates.x] = mesh
##
## Specialized multi-mesh instance convenience for use within the mesh grid and its chunks.
##
class MultiMeshInstance:
var _instance_rid := RID() var _instance_rid := RID()
var _multimesh_rid := RID() var _multimesh_rid := RID()
func _init(scenario_rid: RID, mesh: Mesh, transforms: Array) -> void: func _init(scenario_rid: RID, mesh_rid: RID, transforms: Array) -> void:
_multimesh_rid = RenderingServer.multimesh_create() _multimesh_rid = RenderingServer.multimesh_create()
_instance_rid = RenderingServer.instance_create2(_multimesh_rid, scenario_rid) _instance_rid = RenderingServer.instance_create2(_multimesh_rid, scenario_rid)
RenderingServer.multimesh_set_mesh(_multimesh_rid, mesh.get_rid()) RenderingServer.multimesh_set_mesh(_multimesh_rid, mesh_rid)
var transform_count := transforms.size() var transform_count := transforms.size()
@ -29,88 +86,118 @@ class _MultimeshInstance:
for i in transform_count: for i in transform_count:
RenderingServer.multimesh_instance_set_transform(_multimesh_rid, i, transforms[i]) RenderingServer.multimesh_instance_set_transform(_multimesh_rid, i, transforms[i])
var _chunks: Array[_Chunk] = [] ##
## Sets the parent transform of all mesh instances under it to [code]offset[/code].
##
func set_offset(offset: Transform3D) -> void:
RenderingServer.instance_set_transform(_instance_rid, offset)
var _grid_origin := Vector2(0.5, 0.5) var _chunks: Array[Chunk] = []
var _chunks_size := Vector2i.ZERO
## ##
## The number of horizontal and vertical cells in the current grid. ## Size of the mesh grid (in engine units).
##
## Setting this value will result in the current grid data being completely overwritten to return
## it to a sensible default.
## ##
@export @export
var size: Vector2i: var size: Vector2i:
set(value): set(value):
self._chunks.resize(int(ceil((value.x * value.y) / float(_CELL_COUNT)))) var chunk_size_factor := float(_CHUNK_SIZE)
for i in self._chunks.size(): _chunks.resize(int(ceilf(value.x / chunk_size_factor) * ceilf(value.y / chunk_size_factor)))
self._chunks[i] = _Chunk.new()
for i in _chunks.size():
_chunks[i] = Chunk.new()
_chunks_size = Vector2i((value / float(_CHUNK_SIZE)).ceil())
size = value size = value
func _get_chunk(chunk_coordinate: Vector2i) -> Chunk:
return _chunks[(_chunks_size.x * chunk_coordinate.y) + chunk_coordinate.x]
func _notification(what: int) -> void: func _notification(what: int) -> void:
match what: match what:
NOTIFICATION_TRANSFORM_CHANGED: NOTIFICATION_TRANSFORM_CHANGED:
for chunk in _chunks: for chunk in _chunks:
for multimesh_instance in chunk.multimesh_instances: for multimesh_instance in chunk._multimesh_instances:
RenderingServer.instance_set_transform( multimesh_instance.set_offset_transform(global_transform)
multimesh_instance._instance_rid, multimesh_instance.global_transform)
## ##
## Sets the cell at [code]coordinate[/code] to display [code]mesh[/code]. ## Clears the entire mesh grid to only contain [code]mesh[/code].
## ##
## Note that this function assumes [code]coordinate[/code] is within the bounds of the grid ## [code]null[/code] may be past to [code]mesh[/code] for clearing the mesh grid to nothing.
## coordinate space.
## ##
## Note that changes to a cell result in the chunk it resides in being completely regenerated as ## For clearing a specific region of the mesh grid, see [method fill_mesh].
## part of its modification, and therefore has an implicitly higher overhead to other setter
## operations defined by [MeshGrid].
## ##
func set_mesh(coordinate: Vector2i, mesh: Mesh) -> void: func clear_mesh(mesh: Mesh) -> void:
assert(Rect2i(Vector2i.ZERO, size).has_point(coordinate), "coordinate must be within grid") for y in size.y:
for x in size.x:
var coordinate := Vector2i(x, y)
# TODO: Once this is all lowered into native code, look for ways to parallelize the loops. _get_chunk(coordinate / _CHUNK_SIZE).set_mesh(coordinate % _CHUNK_SIZE, mesh)
var chunk_coordinate := coordinate / _CELL_COUNT
var cell_coordinate := coordinate % _CELL_COUNT
var chunk := _chunks[(size.x * chunk_coordinate.y) + chunk_coordinate.x]
var chunk_meshes := chunk.meshes
chunk_meshes[(_CELL_COUNT * cell_coordinate.y) + cell_coordinate.x] = mesh for y in _chunks_size.y:
for x in _chunks_size.x:
var chunk_coordinate := Vector2i(x, y)
# Invalide any existing baked multimeshes in the chunk. _get_chunk(chunk_coordinate).invalidate(self, chunk_coordinate)
for multimesh_instance in chunk.multimesh_instances:
RenderingServer.free_rid(multimesh_instance._instance_rid)
RenderingServer.free_rid(multimesh_instance._multimesh_rid)
chunk.multimesh_instances.clear() ##
## Clears the region of the mesh grid at [code]area[/code] to only contain [code]mesh[/code].
##
## [code]null[/code] may be past to [code]mesh[/code] for filling the region to nothing.
##
## *Note* that [code]area[/code] *must* be within the area of the of the mesh grid, where
## [code]Vector2i.ZERO[/code] is the top-left and [member size] [code]- 1[/code] is the bottom-
## right.
##
## For clearing the whole of the mesh grid, see [method clear_mesh].
##
func fill_mesh(area: Rect2i, mesh: Mesh) -> void:
assert(get_area().encloses(area), "area must be within grid")
# Normalize mesh instance data for the chunk. var filled_chunks := BitMap.new()
var mesh_transform_sets := {}
for i in chunk_meshes.size(): filled_chunks.resize(_chunks_size)
var chunk_mesh := chunk_meshes[i]
if chunk_mesh != null: for y in range(area.position.y, area.end.y):
if not(chunk_mesh in mesh_transform_sets): for x in range(area.position.x, area.end.x):
mesh_transform_sets[chunk_mesh] = [] var coordinate := Vector2i(x, y)
var chunk_coordinate := coordinate / _CHUNK_SIZE
mesh_transform_sets[chunk_mesh].push_back(Transform3D(Basis.IDENTITY, Vector3( _get_chunk(chunk_coordinate).set_mesh(coordinate % _CHUNK_SIZE, mesh)
(float(chunk_coordinate.x * _CELL_COUNT) + (i % _CELL_COUNT)) - filled_chunks.set_bitv(chunk_coordinate, true)
(float(size.x) * _grid_origin.x), 0.0,
(float(chunk_coordinate.y * _CELL_COUNT) + (i / _CELL_COUNT)) -
(float(size.y) * _grid_origin.y))))
# Bake into multimesh instances for the chunk. for y in _chunks_size.y:
var scenario_rid := get_world_3d().scenario for x in _chunks_size.x:
if filled_chunks.get_bit(x, y):
var chunk_coordinate := Vector2i(x, y)
for chunk_mesh in mesh_transform_sets: _get_chunk(chunk_coordinate).invalidate(self, chunk_coordinate)
var multimesh_instance :=\
_MultimeshInstance.new(scenario_rid, chunk_mesh, mesh_transform_sets[chunk_mesh])
RenderingServer.instance_set_transform(multimesh_instance._instance_rid, global_transform) func get_area() -> Rect2i:
return Rect2i(Vector2i.ZERO, size)
chunk.multimesh_instances.push_back(multimesh_instance) ##
## Plots a single mesh at [code]coordinates[/code] to be [code]mesh[/code], overwriting whatever it
## previously contained.
##
## [code]null[/code] may be past to [code]mesh[/code] for clearing the mesh grid to nothing.
##
## *Note* that [code]coordinates[/code] *must* be within the area of the of the mesh grid, where
## [code]Vector2i.ZERO[/code] is the top-left and [member size] [code]- 1[/code] is the bottom-
## right.
##
## For bulk-setting many meshes at once, see [method fill_mesh] and [method clear_mesh].
##
func plot_mesh(coordinates: Vector2i, mesh: Mesh) -> void:
assert(get_area().has_point(coordinates), "coordinate must be within grid")
var chunk_coordinates := coordinates / _CHUNK_SIZE
var chunk := _get_chunk(chunk_coordinates)
chunk.set_mesh(coordinates % _CHUNK_SIZE, mesh)
chunk.invalidate(self, chunk_coordinates)
## ##
## Returns [code]world_position[/code] converted into a coordinate aligned with the [MeshGrid]. ## Returns [code]world_position[/code] converted into a coordinate aligned with the [MeshGrid].
@ -119,4 +206,4 @@ func set_mesh(coordinate: Vector2i, mesh: Mesh) -> void:
## coordinates outside of the [MeshGrid] bounds as well. ## coordinates outside of the [MeshGrid] bounds as well.
## ##
func world_to_grid(world_position: Vector2) -> Vector2i: func world_to_grid(world_position: Vector2) -> Vector2i:
return Vector2i((world_position + (Vector2(size) * _grid_origin)).floor()) return Vector2i((world_position + (Vector2(size) * _GRID_ORIGIN)).floor())

View File

@ -14,6 +14,7 @@ config/name="Protectorate"
run/main_scene="res://map_editor.scn" run/main_scene="res://map_editor.scn"
config/use_custom_user_dir=true config/use_custom_user_dir=true
config/features=PackedStringArray("4.0", "Forward Plus") config/features=PackedStringArray("4.0", "Forward Plus")
boot_splash/show_image=false
config/icon="res://icon.png" config/icon="res://icon.png"
[autoload] [autoload]
@ -23,6 +24,7 @@ LocalPlayer="*res://local_player.scn"
[debug] [debug]
gdscript/warnings/integer_division=0
gdscript/warnings/assert_always_true=0 gdscript/warnings/assert_always_true=0
gdscript/warnings/assert_always_false=0 gdscript/warnings/assert_always_false=0
@ -80,6 +82,15 @@ editor_paint={
] ]
} }
[layer_names]
3d_render/layer_1="Physical"
3d_render/layer_2="Interface"
[physics]
common/physics_ticks_per_second=30
[rendering] [rendering]
lights_and_shadows/directional_shadow/soft_shadow_filter_quality=3 lights_and_shadows/directional_shadow/soft_shadow_filter_quality=3