@tool

extends Resource

class_name PlanetData

@export var radius : float = 1:
	get:
		return radius
	set(value):
		radius = value
		emit_signal("changed")

@export var resolution : int = 50:
	get:
		return resolution
	set(value):
		resolution = value
		emit_signal("changed")

@export var noise_map : FastNoiseLite = FastNoiseLite.new():
	get:
		return noise_map
	set(value):
		noise_map = value
		emit_signal("changed")
		if noise_map != null and not noise_map.is_connected("changed", on_data_changed):
			noise_map.connect("changed", on_data_changed)

@export var amplitude : float = 1.0:
	get:
		return amplitude
	set(value):
		amplitude = value
		emit_signal("changed")

@export var min_height : float = 0.9:
	get:
		return min_height
	set(value):
		min_height = value
		emit_signal("changed")

@export var flatness : float = 6:
	get:
		return flatness
	set(value):
		flatness = value
		emit_signal("changed")

func on_data_changed():
	emit_signal("changed")

func point_on_planet(point_on_sphere : Vector3) -> Vector3:
	assert(noise_map != null, "Noise map not set")
	var elevation = 1 / (1 + exp(noise_map.get_noise_3dv(point_on_sphere) * -flatness))
	elevation = max(0.0, (elevation + 1)/2 - min_height)
	return point_on_sphere * radius * (elevation + 1)
