Particules

Un émetteur qui suit la souris, avec une texture de point procédurale et une rampe de couleur. Des particules CPU, celles que le moteur de rendu web dessine.

L'éditeur ci-dessus fait tourner ce projet. Changez une ligne et il se recharge.

Classes Godot utilisées

CPUParticles2DGradientTexture2DGradient

Le code

scripts/main.gd
extends Node

var particles: CPUParticles2D


func _ready() -> void:
	_add_background()

	particles = CPUParticles2D.new()
	particles.amount = 400
	particles.lifetime = 1.6
	particles.texture = _make_dot()
	particles.direction = Vector2(0, -1)
	particles.spread = 30.0
	particles.initial_velocity_min = 180.0
	particles.initial_velocity_max = 340.0
	particles.gravity = Vector2(0, 320)
	particles.scale_amount_min = 0.4
	particles.scale_amount_max = 1.2
	particles.color_ramp = _make_ramp()
	particles.position = get_viewport().get_visible_rect().size * 0.5
	add_child(particles)

	print('move the mouse over the game to drag the emitter')


func _process(_delta: float) -> void:
	var mouse: Vector2 = get_viewport().get_mouse_position()

	if get_viewport().get_visible_rect().has_point(mouse):
		particles.position = mouse


func _add_background() -> void:
	var background: ColorRect = ColorRect.new()
	background.color = Color('#0d0f16')
	add_child(background)
	background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)


func _make_dot() -> GradientTexture2D:
	var gradient: Gradient = Gradient.new()
	gradient.set_color(0, Color(1, 1, 1, 1))
	gradient.set_color(1, Color(1, 1, 1, 0))

	var dot: GradientTexture2D = GradientTexture2D.new()
	dot.gradient = gradient
	dot.fill = GradientTexture2D.FILL_RADIAL
	dot.fill_from = Vector2(0.5, 0.5)
	dot.fill_to = Vector2(1.0, 0.5)
	dot.width = 32
	dot.height = 32

	return dot


func _make_ramp() -> Gradient:
	var ramp: Gradient = Gradient.new()
	ramp.set_color(0, Color('#8da5f3'))
	ramp.set_color(1, Color(0.98, 0.5, 0.5, 0))

	return ramp

Plus d'exemples de Particules