@tool

extends Resource

class_name PlanetData

@export var radius : float = 50:
	get:
		return radius
	set(value):
		radius = 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 = 0.1:
	get:
		return amplitude
	set(value):
		amplitude = value
		emit_signal("changed")

var min_elevation := 99999.0
var max_elevation := 0.0

@export var planet_color : GradientTexture1D:
	get:
		return planet_color
	set(value):
		planet_color = value
		emit_signal("changed")
		if planet_color != null and not planet_color.is_connected("changed", on_data_changed):
			planet_color.connect("changed", on_data_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 = noise_map.get_noise_3dv(point_on_sphere) / flatness
	elevation = elevation/2 + 1
	return point_on_sphere * radius * elevation
