Editor Terrain Brush #2
							
								
								
									
										
											BIN
										
									
								
								editor.scn
									 (Stored with Git LFS)
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								editor.scn
									 (Stored with Git LFS)
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										100
									
								
								editor/editable_terrain.gd
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								editor/editable_terrain.gd
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,100 @@ | |||||||
|  | class_name EditableTerrain extends Node | ||||||
|  | 
 | ||||||
|  | var _image := Image.create(1, 1, false, Image.FORMAT_RGBAF) | ||||||
|  | 
 | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | @export | ||||||
|  | var channels := Vector3.ZERO | ||||||
|  | 
 | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | @export | ||||||
|  | var size := Vector2i.ZERO: | ||||||
|  | 	get: | ||||||
|  | 		return size | ||||||
|  | 
 | ||||||
|  | 	set(value): | ||||||
|  | 		_image.resize(value.x, value.y) | ||||||
|  | 		_image.fill(Color(0.0, 0.0, 0.0, 0.5)) | ||||||
|  | 
 | ||||||
|  | 		size = value | ||||||
|  | 
 | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | func get_image() -> void: | ||||||
|  | 	return _image | ||||||
|  | 
 | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | func paint(point: Vector2i, elevation_level: float, | ||||||
|  | 	brush_mask: Image, brush_scale: float, brush_intensity: float) -> void: | ||||||
|  | 
 | ||||||
|  | 	# Convert worldspace point to image-space coordinates for brush and calculate drawable area. | ||||||
|  | 	var brush_mask_size := brush_mask.get_size() | ||||||
|  | 	var brush_size := brush_mask_size * brush_scale | ||||||
|  | 	var image_size := _image.get_size() | ||||||
|  | 
 | ||||||
|  | 	var draw_area := Rect2i(Vector2i.ZERO, image_size).intersection( | ||||||
|  | 		Rect2i((point - Vector2i(brush_size * 0.5)) + Vector2i(image_size * 0.5), brush_size)) | ||||||
|  | 
 | ||||||
|  | 	if draw_area.has_area(): | ||||||
|  | 		var scaled_brush_mask_size := brush_mask_size / draw_area.size | ||||||
|  | 
 | ||||||
|  | 		elevation_level = clampf(elevation_level, -1.0, 1.0) | ||||||
|  | 
 | ||||||
|  | 		for y in draw_area.size.y: | ||||||
|  | 			var brush_mask_y := int(y * scaled_brush_mask_size.y) | ||||||
|  | 
 | ||||||
|  | 			for x in draw_area.size.x: | ||||||
|  | 				var terrain_map_coord := draw_area.position + Vector2i(x, y) | ||||||
|  | 				var pixel := _image.get_pixelv(terrain_map_coord) | ||||||
|  | 
 | ||||||
|  | 				var mask := brush_mask.get_pixel( | ||||||
|  | 					int(x * scaled_brush_mask_size.x), brush_mask_y).a | ||||||
|  | 
 | ||||||
|  | 				var mask_intensity := brush_intensity * mask | ||||||
|  | 
 | ||||||
|  | 				_image.set_pixelv(terrain_map_coord, Color( | ||||||
|  | 					lerpf(pixel.r, channels.x, mask_intensity), | ||||||
|  | 						lerpf(pixel.g, channels.y, mask_intensity), | ||||||
|  | 							lerpf(pixel.b, channels.z, mask_intensity), | ||||||
|  | 								lerpf(pixel.a, pixel.a + (elevation_level * mask), brush_intensity))) | ||||||
|  | 
 | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | ## | ||||||
|  | func smooth(point: Vector2i, smoothing_level: float, | ||||||
|  | 	brush_mask: Image, brush_scale: float, brush_intensity: float) -> void: | ||||||
|  | 
 | ||||||
|  | 	# Convert worldspace point to image-space coordinates for brush and calculate drawable area. | ||||||
|  | 	var brush_mask_size := brush_mask.get_size() | ||||||
|  | 	var brush_size := brush_mask_size * brush_scale | ||||||
|  | 	var terrain_map_size := _image.get_size() | ||||||
|  | 
 | ||||||
|  | 	var draw_area := Rect2i(Vector2i.ZERO, terrain_map_size).intersection( | ||||||
|  | 		Rect2i((point - Vector2i(brush_size * 0.5)) +\ | ||||||
|  | 			Vector2i(terrain_map_size * 0.5), brush_size)) | ||||||
|  | 
 | ||||||
|  | 	if draw_area.has_area(): | ||||||
|  | 		var scaled_brush_mask_size := brush_mask_size / draw_area.size | ||||||
|  | 
 | ||||||
|  | 		for y in draw_area.size.y: | ||||||
|  | 			var brush_mask_y := int(y * scaled_brush_mask_size.y) | ||||||
|  | 
 | ||||||
|  | 			for x in draw_area.size.x: | ||||||
|  | 				var terrain_map_coord := draw_area.position + Vector2i(x, y) | ||||||
|  | 				var pixel := _image.get_pixelv(terrain_map_coord) | ||||||
|  | 
 | ||||||
|  | 				var mask_intensity := brush_intensity * brush_mask.get_pixel( | ||||||
|  | 					int(x * scaled_brush_mask_size.x), brush_mask_y).a | ||||||
|  | 
 | ||||||
|  | 				_image.set_pixelv(terrain_map_coord, Color( | ||||||
|  | 					lerpf(pixel.r, channels.x, mask_intensity), | ||||||
|  | 						lerpf(pixel.g, channels.y, mask_intensity),  | ||||||
|  | 							lerpf(pixel.b, channels.z, mask_intensity), | ||||||
|  | 								lerpf(pixel.a, smoothing_level, mask_intensity))) | ||||||
| @ -9,6 +9,11 @@ | |||||||
| config_version=5 | config_version=5 | ||||||
| 
 | 
 | ||||||
| _global_script_classes=[{ | _global_script_classes=[{ | ||||||
|  | "base": "Node", | ||||||
|  | "class": &"EditableTerrain", | ||||||
|  | "language": &"GDScript", | ||||||
|  | "path": "res://editor/editable_terrain.gd" | ||||||
|  | }, { | ||||||
| "base": "Node3D", | "base": "Node3D", | ||||||
| "class": &"PlayerController", | "class": &"PlayerController", | ||||||
| "language": &"GDScript", | "language": &"GDScript", | ||||||
| @ -25,6 +30,7 @@ _global_script_classes=[{ | |||||||
| "path": "res://terrain_instance_3d.gd" | "path": "res://terrain_instance_3d.gd" | ||||||
| }] | }] | ||||||
| _global_script_class_icons={ | _global_script_class_icons={ | ||||||
|  | "EditableTerrain": "", | ||||||
| "PlayerController": "", | "PlayerController": "", | ||||||
| "Settings": "", | "Settings": "", | ||||||
| "TerrainInstance3D": "" | "TerrainInstance3D": "" | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user