Refactor map editor
This commit is contained in:
		
							parent
							
								
									f3e630849b
								
							
						
					
					
						commit
						32700ee9d4
					
				
							
								
								
									
										
											BIN
										
									
								
								map_editor.scn
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								map_editor.scn
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								map_editor/camera_boundary_mesh.res
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								map_editor/camera_boundary_mesh.res
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								map_editor/edit_mode_button_group.res
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								map_editor/edit_mode_button_group.res
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -1,143 +0,0 @@ | ||||
| class_name MapEditorController extends Node | ||||
| 
 | ||||
| ## | ||||
| ## Default elevation height for terrain. | ||||
| ## | ||||
| const DEFAULT_ELEVATION := 0.5 | ||||
| 
 | ||||
| var _brush_selected := 0 | ||||
| 
 | ||||
| @export | ||||
| var _registered_brushes: Array[Image] = [] | ||||
| 
 | ||||
| @export | ||||
| var _registered_paints: Array[TerrainPaint] = [] | ||||
| 
 | ||||
| var _paint_selected_erase := 0 | ||||
| 
 | ||||
| var _paint_selected_blue := 0 | ||||
| 
 | ||||
| var _paint_selected_green := 0 | ||||
| 
 | ||||
| var _paint_selected_red := 0 | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| var smooth_elevation := DEFAULT_ELEVATION | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func disable_controls() -> void: | ||||
| 	set_process(false) | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func enable_controls() -> void: | ||||
| 	set_process(true) | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_registered_brushes() -> Array[Image]: | ||||
| 	return _registered_brushes | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_registered_paints() -> Array[TerrainPaint]: | ||||
| 	return _registered_paints | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_selected_brush() -> int: | ||||
| 	return _brush_selected | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_selected_paint_blue() -> int: | ||||
| 	return _paint_selected_blue | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_selected_paint_erase() -> int: | ||||
| 	return _paint_selected_erase | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_selected_paint_green() -> int: | ||||
| 	return _paint_selected_green | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_selected_paint_red() -> int: | ||||
| 	return _paint_selected_red | ||||
| 
 | ||||
| ## | ||||
| ## Registers [code]brush_mask[/code] as a usable brush option in the editor. | ||||
| ## | ||||
| func register_brush(brush_mask: Image) -> void: | ||||
| 	_registered_brushes.append(brush_mask) | ||||
| 
 | ||||
| ## | ||||
| ## Registers [code]terrain_paint[/code] as a usable paint option in the editor. | ||||
| ## | ||||
| func register_paint(terrain_paint: TerrainPaint) -> void: | ||||
| 	_registered_paints.append(terrain_paint) | ||||
| 
 | ||||
| ## | ||||
| ## Resets the editor settings to their initial values. | ||||
| ## | ||||
| func reset() -> void: | ||||
| 	select_brush(0) | ||||
| 	select_paint_erase(0) | ||||
| 	select_paint_red(0) | ||||
| 	select_paint_green(0) | ||||
| 	select_paint_blue(0) | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func select_brush(selected_index: int) -> void: | ||||
| 	assert((selected_index >= 0) or (selected_index < _registered_brushes.size())) | ||||
| 
 | ||||
| 	_brush_selected = selected_index | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func select_paint_blue(selected_index: int) -> void: | ||||
| 	assert((selected_index >= 0) or (selected_index < _registered_paints.size())) | ||||
| 
 | ||||
| 	_paint_selected_blue = selected_index | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func select_paint_erase(selected_index: int) -> void: | ||||
| 	assert((selected_index >= 0) or (selected_index < _registered_paints.size())) | ||||
| 
 | ||||
| 	_paint_selected_erase = selected_index | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func select_paint_green(selected_index: int) -> void: | ||||
| 	assert((selected_index >= 0) or (selected_index < _registered_paints.size())) | ||||
| 
 | ||||
| 	_paint_selected_green = selected_index | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func select_paint_red(selected_index: int) -> void: | ||||
| 	assert((selected_index >= 0) or (selected_index < _registered_paints.size())) | ||||
| 
 | ||||
| 	_paint_selected_red = selected_index | ||||
| @ -1,27 +1,33 @@ | ||||
| class_name DynamicTerrainInstance3D extends TerrainInstance3D | ||||
| class_name MapEditorTerrainCanvas extends Node | ||||
| 
 | ||||
| ## | ||||
| ## Blank sample value. | ||||
| ## | ||||
| const BLANK := Color(0.0, 0.0, 0.0, 0.0) | ||||
| 
 | ||||
| var _editable_image: Image | ||||
| var _editable_image := Image.create(1, 1, false, Image.FORMAT_RGBAF) | ||||
| 
 | ||||
| var _editable_texture: ImageTexture | ||||
| var _editable_texture := ImageTexture.create_from_image(_editable_image) | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| var size := Vector2i.ONE: | ||||
| 	set(value): | ||||
| 		size = Vector2(maxi(size.x, value.x), maxi(size.y, value.y)) | ||||
| 
 | ||||
| 		_editable_image.resize(size.x, size.y) | ||||
| 		_editable_texture.set_image(_editable_image) | ||||
| 
 | ||||
| func _get_brush_area(point: Vector2i, mask_size: Vector2i, mask_scale: float) -> Rect2i: | ||||
| 	# Convert worldspace point to image-space coordinates for mask. | ||||
| 	var scaled_mask_size := Vector2i(mask_size * mask_scale) | ||||
| 
 | ||||
| 	return Rect2i((point + Vector2i(get_size() * 0.5)) - Vector2i(scaled_mask_size * 0.5), scaled_mask_size) | ||||
| 	return Rect2i((point + Vector2i(size * 0.5)) - | ||||
| 		Vector2i(scaled_mask_size * 0.5), scaled_mask_size) | ||||
| 
 | ||||
| func _init() -> void: | ||||
| 	super._init() | ||||
| 
 | ||||
| 	_editable_image = Image.create(1, 1, false, Image.FORMAT_RGBAF) | ||||
| 	_editable_texture = ImageTexture.create_from_image(_editable_image) | ||||
| 
 | ||||
| 	super.resize(Vector2i.ONE) | ||||
| func _get_editable_area() -> Rect2i: | ||||
| 	return Rect2i(Vector2i.ZERO, size).grow(-1) | ||||
| 
 | ||||
| ## | ||||
| ## Clears the terrain map to the value of [code]clear_elevation[/code]. | ||||
| @ -30,17 +36,23 @@ func clear(clear_elevation: float) -> void: | ||||
| 	_editable_image.fill(Color(0.0, 0.0, 0.0, clampf(clear_elevation, 0.0, 1.0))) | ||||
| 	_editable_texture.update(_editable_image) | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## | ||||
| func get_texture() -> void: | ||||
| 	return _editable_texture | ||||
| 
 | ||||
| ## | ||||
| ## Oscillates the height of terrain toward a value of [member height_scale] (multiplied by | ||||
| ## [code]level[/code]) around the area of [code]point[/code] determined by [code]mask[/code] | ||||
| ## multiplied by [code]mask_scale[/code] at a rate of [code]intensity[/code]. | ||||
| ## | ||||
| func oscillate(point: Vector2i, level: float, mask: Image, mask_scale: float, intensity: float) -> void: | ||||
| func oscillate(level: float, point: Vector2i, mask: Image, mask_scale: float, intensity: float) -> void: | ||||
| 	var mask_size := mask.get_size() | ||||
| 	var brush_source := _get_brush_area(point, mask_size, mask_scale) | ||||
| 
 | ||||
| 	if brush_source.has_area(): | ||||
| 		var brush_target := Rect2i(Vector2i.ZERO, get_size()).intersection(brush_source) | ||||
| 		var brush_target := _get_editable_area().intersection(brush_source) | ||||
| 
 | ||||
| 		if brush_target.has_area(): | ||||
| 			var mask_ratio := mask_size / brush_source.size | ||||
| @ -64,12 +76,12 @@ func oscillate(point: Vector2i, level: float, mask: Image, mask_scale: float, in | ||||
| ## [code]intensity[/code] around the area of [code]point[/code] determined by [code]mask[/code] | ||||
| ## multiplied by [code]mask_scale[/code] at a rate of [code]intensity[/code]. | ||||
| ## | ||||
| func paint(point: Vector2i, color: Color, mask: Image, mask_scale: float, intensity: float) -> void: | ||||
| func paint(color: Color, point: Vector2i, mask: Image, mask_scale: float, intensity: float) -> void: | ||||
| 	var mask_size := mask.get_size() | ||||
| 	var brush_source := _get_brush_area(point, mask_size, mask_scale) | ||||
| 
 | ||||
| 	if brush_source.has_area(): | ||||
| 		var brush_target := Rect2i(Vector2i.ZERO, get_size()).intersection(brush_source) | ||||
| 		var brush_target := _get_editable_area().intersection(brush_source) | ||||
| 
 | ||||
| 		if brush_target.has_area(): | ||||
| 			var mask_ratio := mask_size / brush_source.size | ||||
| @ -92,23 +104,6 @@ func paint(point: Vector2i, color: Color, mask: Image, mask_scale: float, intens | ||||
| 
 | ||||
| 		_editable_texture.update(_editable_image) | ||||
| 
 | ||||
| ## | ||||
| ## Attempts to resize the terrain geometry and map to be equal to [code]size[/code] (in-engine | ||||
| ## units), with [code]Vector2i.ONE[/code] as the minimum size. | ||||
| ## | ||||
| ## In the process of growing the terrain map, existing edge data will be copied to fill it out. | ||||
| ## | ||||
| func resize(size: Vector2i) -> void: | ||||
| 	super.resize(size) | ||||
| 
 | ||||
| 	var actual_size := get_size() | ||||
| 
 | ||||
| 	_editable_image.resize(actual_size.x, actual_size.y) | ||||
| 
 | ||||
| 	_editable_texture = ImageTexture.create_from_image(_editable_image) | ||||
| 
 | ||||
| 	update_terrain(_editable_texture) | ||||
| 
 | ||||
| ## | ||||
| ## Samples the color value in the editable terrain at the worldspace [code]point[/code], returning | ||||
| ## its respective [Color] value. | ||||
| @ -117,7 +112,6 @@ func resize(size: Vector2i) -> void: | ||||
| ## instead. | ||||
| ## | ||||
| func sample(point: Vector2i) -> Color: | ||||
| 	var size := get_size() | ||||
| 	var image_point := point + Vector2i(size * 0.5) | ||||
| 
 | ||||
| 	if Rect2i(Vector2i.ZERO, size).has_point(image_point): | ||||
							
								
								
									
										
											BIN
										
									
								
								map_editor/menus_theme.res
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								map_editor/menus_theme.res
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| @ -9,20 +9,15 @@ | ||||
| config_version=5 | ||||
| 
 | ||||
| _global_script_classes=[{ | ||||
| "base": "TerrainInstance3D", | ||||
| "class": &"DynamicTerrainInstance3D", | ||||
| "language": &"GDScript", | ||||
| "path": "res://terrain/dynamic_terrain_instance_3d.gd" | ||||
| }, { | ||||
| "base": "HFlowContainer", | ||||
| "class": &"ItemSelection", | ||||
| "language": &"GDScript", | ||||
| "path": "res://user_interface/button_selection.gd" | ||||
| }, { | ||||
| "base": "Node", | ||||
| "class": &"MapEditorController", | ||||
| "class": &"MapEditorTerrainCanvas", | ||||
| "language": &"GDScript", | ||||
| "path": "res://map_editor/map_editor_controller.gd" | ||||
| "path": "res://map_editor/map_editor_terrain_canvas.gd" | ||||
| }, { | ||||
| "base": "Node3D", | ||||
| "class": &"PlayerController", | ||||
| @ -45,9 +40,8 @@ _global_script_classes=[{ | ||||
| "path": "res://terrain/paints/terrain_paint.gd" | ||||
| }] | ||||
| _global_script_class_icons={ | ||||
| "DynamicTerrainInstance3D": "", | ||||
| "ItemSelection": "", | ||||
| "MapEditorController": "", | ||||
| "MapEditorTerrainCanvas": "", | ||||
| "PlayerController": "", | ||||
| "Settings": "", | ||||
| "TerrainInstance3D": "", | ||||
|  | ||||
| @ -4,7 +4,7 @@ const int MAP_COUNT = 4; | ||||
| 
 | ||||
| uniform sampler2D[MAP_COUNT] ALBEDO_MAPS; | ||||
| 
 | ||||
| uniform float MAX_HEIGHT = 100.0; | ||||
| uniform float HEIGHT_SCALE = 100.0; | ||||
| 
 | ||||
| uniform sampler2D[MAP_COUNT] NORMAL_MAPS; | ||||
| 
 | ||||
| @ -39,7 +39,7 @@ void fragment() { | ||||
| } | ||||
| 
 | ||||
| float height(vec2 uv) { | ||||
| 	return MAX_HEIGHT * texture(TERRAIN_MAP, uv).a; | ||||
| 	return HEIGHT_SCALE * texture(TERRAIN_MAP, uv).a; | ||||
| } | ||||
| 
 | ||||
| void vertex() { | ||||
|  | ||||
| @ -1,87 +1,79 @@ | ||||
| @tool | ||||
| class_name TerrainInstance3D extends GeometryInstance3D | ||||
| 
 | ||||
| ## | ||||
| ## Identifier constant for a paint slot channel. | ||||
| ## | ||||
| const _DETAIL := 2 | ||||
| 
 | ||||
| const _SHADER := preload("res://terrain/terrain.gdshader") | ||||
| 
 | ||||
| enum PaintSlot { | ||||
| 	ERASE, | ||||
| 	RED, | ||||
| 	GREEN, | ||||
| 	BLUE, | ||||
| 	ALPHA, | ||||
| } | ||||
| 
 | ||||
| const _DETAIL := 2 | ||||
| 
 | ||||
| var _albedo_map_textures: Array[Texture2D] = [null, null, null, null] | ||||
| var _albedo_maps: Array[Texture2D] = [null, null, null, null] | ||||
| 
 | ||||
| var _material := ShaderMaterial.new() | ||||
| 
 | ||||
| var _normal_map_textures: Array[Texture2D] = [null, null, null, null] | ||||
| 
 | ||||
| var _mesh := PlaneMesh.new() | ||||
| 
 | ||||
| var _normal_maps: Array[Texture2D] = [null, null, null, null] | ||||
| 
 | ||||
| ## | ||||
| ## | ||||
| ## The height scale range that is used to determine the minimums and maximums of the terrain map. | ||||
| ## | ||||
| @export | ||||
| var height_scale := 100.0: | ||||
| 	get: | ||||
| 		return height_scale | ||||
| 
 | ||||
| 	set(value): | ||||
| 		_material.set_shader_parameter("MAX_HEIGHT", value) | ||||
| 		value = maxf(value, 1.0) | ||||
| 
 | ||||
| 		height_scale = max(value, 0.0) | ||||
| 		if not(is_equal_approx(value, height_scale)): | ||||
| 			_material.set_shader_parameter("HEIGHT_SCALE", value) | ||||
| 
 | ||||
| var _size := Vector2i.ZERO | ||||
| 		height_scale = value | ||||
| 
 | ||||
| ## | ||||
| ## Size of the terrain geometry (in engine units). | ||||
| ## | ||||
| @export | ||||
| var size := Vector2i.ONE: | ||||
| 	set(value): | ||||
| 		value = Vector2i(maxi(value.x, 1), maxi(value.y, 1)) | ||||
| 
 | ||||
| 		if (value.x != size.x) or (value.y != size.y): | ||||
| 			_mesh.subdivide_width = value.x * _DETAIL | ||||
| 			_mesh.subdivide_depth = value.y * _DETAIL | ||||
| 			_mesh.size = value | ||||
| 
 | ||||
| 			_material.set_shader_parameter("SIZE", value) | ||||
| 
 | ||||
| 		size = value | ||||
| 
 | ||||
| func _init() -> void: | ||||
| 	_material.shader = preload("res://terrain/terrain.gdshader") | ||||
| 	_material.shader = _SHADER | ||||
| 
 | ||||
| 	RenderingServer.instance_set_base(get_instance(), _mesh) | ||||
| 	_mesh.surface_set_material(0, _material) | ||||
| 	RenderingServer.instance_set_base(get_instance(), _mesh) | ||||
| 
 | ||||
| ## | ||||
| ## Returns the size of the terrain mesh (as in-engine units). | ||||
| ## Updates the terrain map being used by the terrain to be [code]terrain_map[/code]. | ||||
| ## | ||||
| func get_size() -> Vector2i: | ||||
| 	return _size | ||||
| func update_map(terrain_map: Texture2D) -> void: | ||||
| 	_material.set_shader_parameter("TERRAIN_MAP", terrain_map) | ||||
| 
 | ||||
| ## | ||||
| ## Attempts to resize the terrain to be equal to [code]size[/code] (in-engine units), with | ||||
| ## [code]Vector2i.ONE[/code] as the minimum size. | ||||
| ## | ||||
| func resize(size: Vector2i) -> void: | ||||
| 	var width := maxi(size.x, 1) | ||||
| 	var height := maxi(size.y, 1) | ||||
| 
 | ||||
| 	if (width != _size.x) or (height != _size.y): | ||||
| 		_mesh.subdivide_width = width * _DETAIL | ||||
| 		_mesh.subdivide_depth = height * _DETAIL | ||||
| 		_mesh.size = size | ||||
| 
 | ||||
| 		_material.set_shader_parameter("SIZE", Vector2(size)) | ||||
| 
 | ||||
| 	_size = size | ||||
| 
 | ||||
| ## | ||||
| ## Updates the paint used by the paint channel identified by [code]paint_slot[/code] to | ||||
| ## Updates the [TerrainPaint] being used by the terrain in [code]paint_slot[/code] to be | ||||
| ## [code]terrain_paint[/code]. | ||||
| ## | ||||
| func update_paint(paint_slot: PaintSlot, paint: TerrainPaint) -> void: | ||||
| 	if paint == null: | ||||
| 		_albedo_map_textures[paint_slot] = null | ||||
| 		_normal_map_textures[paint_slot] = null | ||||
| func update_paint(paint_slot: PaintSlot, terrain_paint: TerrainPaint) -> void: | ||||
| 	if _albedo_maps[paint_slot] != terrain_paint.albedo_map: | ||||
| 		_albedo_maps[paint_slot] = terrain_paint.albedo_map | ||||
| 
 | ||||
| 	else: | ||||
| 		_albedo_map_textures[paint_slot] = paint.albedo_map | ||||
| 		_normal_map_textures[paint_slot] = paint.normal_map | ||||
| 		_material.set_shader_parameter("ALBEDO_MAPS", _albedo_maps) | ||||
| 
 | ||||
| 	_material.set_shader_parameter("ALBEDO_MAPS", _albedo_map_textures) | ||||
| 	_material.set_shader_parameter("NORMAL_MAPS", _normal_map_textures) | ||||
| 	if _normal_maps[paint_slot] != terrain_paint.normal_map: | ||||
| 		_normal_maps[paint_slot] = terrain_paint.normal_map | ||||
| 
 | ||||
| ## | ||||
| ## Updates the terrain map to [code]terrain_map[/code]. | ||||
| ## | ||||
| func update_terrain(terrain_map: Texture2D) -> void: | ||||
| 	_material.set_shader_parameter("TERRAIN_MAP", terrain_map) | ||||
| 		_material.set_shader_parameter("NORMAL_MAPS", _normal_maps) | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user