본문 바로가기
Unity/FAS project 코드 정리

Light Flicker 만들기

by 민트코코넛 2020. 12. 13.
728x90
반응형

1. Light의 점멸 효과 만들기

using System.Collections.Generic;
using UnityEngine;

public class LightFlicker : MonoBehaviour
{
    public Light p_light;
    public float minIntensity = 0f;
    public float maxIntensity = 1f;

    // light flicker의 날카로움과 부드러움 조절     / 1: 날카로움  / 50 : 부드러움
    [Range(1,50)]
    public int smoothing = 5;

    Queue<float> smoothQueue;
    float lastSum = 0;
    public void Reset()
    {
        smoothQueue.Clear();
        lastSum = 0;
    }
    void Start()
    {
        // light
        smoothQueue = new Queue<float>(smoothing);
        // External or internal light?
        if (p_light == null)
        {
            p_light = GetComponent<Light>();
        }
    }

    void Update()
    {
        lightFlicker();
    }

    void lightFlicker()
    {
        if (p_light == null)
            return;

        // pop off an item if too big
        while (smoothQueue.Count >= smoothing)
        {
            lastSum -= smoothQueue.Dequeue();
        }

        // Generate random new item, calculate new average
        float newVal = Random.Range(minIntensity, maxIntensity);
        smoothQueue.Enqueue(newVal);
        lastSum += newVal;

        // Calculate new smoothed average
        p_light.intensity = lastSum / (float)smoothQueue.Count;
    }
}

** 깃허브를 통해서 적용은 했으나, 코드의 내부 내용을 전체적으로 이해한 것은 아니므로, 추가 공부가 필요함

728x90

'Unity > FAS project 코드 정리' 카테고리의 다른 글

Camera 인식 범위 조정  (0) 2020.12.13
Skybox 회전하기  (0) 2020.12.13
Object Color Flicker 만들기  (0) 2020.12.13
Trigger Enter, Stay, Exit 정리  (0) 2020.12.13
App 종료 & 시스템 강제 종료  (0) 2020.12.13