LoginSignup
1
0

More than 1 year has passed since last update.

PostProcessingによるフラッシュで世界に色がつく

Last updated at Posted at 2022-11-07

デモ

Postprocessing による Unity のフラッシュです!

Postporcessing 公式ページ

目的

モノクロ -> カラー の変化をフラッシュを使って行いたかった。

PostProcessingの設定

PostProcessingについての基本的な設定は良記事が多いので割愛します。

今回使用するのは、

  • Color Grading
  • AutoExposure

の2つです。

Color Gradingはモノクロとカラーの制御、AutoExposureでフラッシュの制御を行います。

AutoExposure

image.png

Color Grading

image.png

インスペクターでの表示はこんな感じです。

実装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

public class TestFlash : MonoBehaviour
{

    [SerializeField] float switchingTime = 3.0f;

    [SerializeField] PostProcessVolume ColorGradingPostProcess;

    [SerializeField] PostProcessVolume AutoExposurePostProcess;

    private AutoExposure AutoExposureExposure;

    private float elapsedTime;

    private bool phase1;


    [SerializeField] float gain = 10000.0f;

    // Start is called before the first frame update
    void Start()
    {
        ColorPostProcess.weight = 1.0f;
        AutoExposurePostProcess.weight = 0.0f;
        AutoExposurePostProcess.sharedProfile.TryGetSettings<AutoExposure>(out AutoExposureExposure);
        AutoExposureExposure.maxLuminance.value = 0.0f;
        AutoExposureExposure.minLuminance.value = 0.0f;
        AutoExposureExposure.keyValue.value = gain;
    }

    private void Update()
    {

        if(Input.GetKeyDown(KeyCode.Space)){
            AutoExposureFlash();
        }


        if(phase1){
            elapsedTime += Time.deltaTime;
            if(elapsedTime / switchingTime > 1.0f){
                phase1 = false;
                ColorGradingPostProcess.weight = 0.0f;
                elapsedTime = 0.0f;
                Debug.Log("phase 1 finished");
                AutoExposureExposure.keyValue.value = 1.0f;
                return;
            }
            var t = elapsedTime / switchingTime;
            Debug.Assert(t <= 1.0f);
            ColorGradingPostProcess.weight = 1.0f - t;
            AutoExposurePostProcess.weight = t;
        }

    }

    void SwithFlash(){
        elapsedTime = 0.0f;
        phase1 = true;
        Debug.Log("Switching!");
        AutoExposurePostProcess.weight = 1.0f;
    }
}

まず、PostProcessing自体は UnityEngine.Rendering.PostProcessingモジュールを使用し、PostProcessVolumeクラスを使うことで操作できます。

その中の AutoExposure や ColorGrading を取り扱いたい場合は、先に宣言をしておいて、

ポストプロセス.sharedProfile.TryGetSettings<AutoExposure>(out オーバーライド名);

でできます。

まず、スタート時にカラーをモノクロのまま、AutoExposureの強さを10000にして、weightを0にしておきます。

フラシュ時の動作については、まずAutoExposureのweghtを1にしておきます。
そのあとで、いい感じの時間を維持するために時間制御で、

ColorGradingPostProcess.weight = 1.0f - t;
AutoExposurePostProcess.weight = t;

で制御します。

最後に

AutoExposureExposure.keyValue.value = 1.0f;

により、AutoExposureを通常の光量と同じにしています。

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0