'전체 글'에 해당되는 글 716건

  1. 2026.09.22 [Godot] Flickering Candles 바람에 흔들리는 촛불
반응형

바람에 흔들리는 촛불을 만들어 보자.

 

 

노드를 위와 같이 구성한다.

 

 

AnimatedSprite2D는 위와 같이 구성한다.

 

Candle.zip
0.00MB

 

 

 

 

PointLight2D는 위와 같이 구성한다.

 

using Godot;

public partial class PointLight2d : PointLight2D
{
    private FastNoiseLite _noise = new FastNoiseLite();
    private float _timePassed = 0f;
    private float _baseEnergy;
    private float _baseTextureScale;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        // 시작할 때 에디터에 설정해둔 기본 Energy 값을 가져온다.
        _baseEnergy = Energy;
        // 시작할 때 에디터에 설정해둔 기본 TextureScale 값을 가져온다.
        _baseTextureScale = TextureScale;

        // 노이즈 설정: Simplex 방식이 불꽃의 자연스러운 일렁임에 잘 맞다.
        _noise.NoiseType = FastNoiseLite.NoiseTypeEnum.Simplex;
        _noise.Seed = (int)GD.Randi(); // 촛불이 여러 개일 때 각자 다르게 일렁이도록 시드값 랜덤화
        _noise.Frequency = 0.05f; // 일렁이는 속도 (값이 클수록 빠르고 정신없이 깜빡임)
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        // 시간이 흐르게 한다. 속도를 조절하려면 곱하는 값을 변경한다. (예: 100f -> 50f로 줄이면 일렁임 속도가 절반으로 느려짐)
        _timePassed += (float)delta * 100f;

        // GetNoise1d는 시간에 따라 -1.0 ~ 1.0 사이의 부드러운 난수를 반환한다.
        float noiseValue = _noise.GetNoise1D(_timePassed);

        // 일렁이는 폭(진폭)을 설정한다. (예: 0.3f면 기본 에너지에서 ±0.3만큼 변함)
        float flickerAmplitude = 0.3f;

        // 기본 에너지에 노이즈 변화량을 더해 실제 조명 에너지에 적용한다.
        Energy = _baseEnergy + (noiseValue * flickerAmplitude);
        // 기본 TextureScale에 노이즈 변화량을 더해 실제 조명 텍스처 스케일에 적용한다.
        TextureScale = _baseTextureScale + (noiseValue * 0.1f);
    }
}

 

PointLight2D에 적용한 스크립트는 위와 같이 작성한다.

 

 

게임에 적용하면 위와 같이 바람에 흔들리는 양초가 만들어진다.

 

반응형
Posted by J-sean
: