Last active
January 17, 2019 02:48
-
-
Save kankikuchi/5ea9c71c90c436c422fe6408371beedf to your computer and use it in GitHub Desktop.
パーリンノイズでランダムに光が揺らめくライト【Unity】
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PerlinNoiseGenerator.cs | |
// http://kan-kikuchi.hatenablog.com/entry/PerlinNoise_Light | |
// | |
// Created by kan.kikuchi on 2019.01.03. | |
using UnityEngine; | |
/// <summary> | |
/// パーリンノイズでランダムに光が揺らめくライト | |
/// </summary> | |
[RequireComponent(typeof(Light))]//Light必須 | |
public class FlickeringLight : MonoBehaviour { | |
//パーリンノイズ生成用クラス | |
private PerlinNoiseGenerator _bigNoiseGenerator = null, _smallNoiseGenerator = null; | |
//揺らめく対象のライト | |
private Light _light = null; | |
//LightのRangeの最小と最高 | |
[SerializeField] | |
private float _rangeMin = 1, _rangeMax = 2; | |
//ゆらめきの速さ(値が大きいほど早い) | |
[SerializeField] | |
private float _rateOfChange = 1f; | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
private void Start() { | |
Init(); | |
} | |
//実行中のInspectorの値が変えられた時に更新する用 | |
private void OnValidate() { | |
if(Application.isPlaying){ | |
Init(); | |
} | |
} | |
//ノ初期化 | |
private void Init() { | |
_light = GetComponent<Light>(); | |
_bigNoiseGenerator = new PerlinNoiseGenerator(_rangeMin * 0.9f, _rangeMax * 0.9f, _rateOfChange); | |
_smallNoiseGenerator = new PerlinNoiseGenerator(_rangeMin * 0.1f, _rangeMax * 0.1f, _rateOfChange * 4); | |
//最初の更新 | |
Update(); | |
} | |
//================================================================================= | |
//更新 | |
//================================================================================= | |
private void Update() { | |
_light.range = _bigNoiseGenerator.GetNoise() + _smallNoiseGenerator.GetNoise(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PerlinNoiseGenerator.cs | |
// http://kan-kikuchi.hatenablog.com/entry/Combine_PerlinNoise | |
// | |
// Created by kan.kikuchi on 2018.12.15. | |
using UnityEngine; | |
/// <summary> | |
/// パーリンノイズの生成するクラス | |
/// </summary> | |
public class PerlinNoiseGenerator { | |
//パーリンノイズのシード(厳密にはシードではないがシード代わりに使っている値) | |
private float _seed = 0; | |
//ノイズの最小、最大値 | |
private float _min = 0, _max = 1; | |
//ノイズの変化率 | |
private float _rateOfChange = 1f; | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
public PerlinNoiseGenerator(float min = 0, float max = 1, float rateOfChange = 1) { | |
//シードはランダムに決定(ランダムなパーリンノイズになる) | |
_seed = Random.Range(0f, 100f); | |
_min = min; | |
_max = max; | |
_rateOfChange = rateOfChange; | |
} | |
/// <summary> | |
/// シードを設定する(パーリンノイズを再現出来る) | |
/// </summary> | |
public void SetSeed(float seed) { | |
_seed = seed; | |
} | |
//================================================================================= | |
//取得 | |
//================================================================================= | |
/// <summary> | |
/// パーリンノイズを取得(xは時間経過で変化、yはシードで固定) | |
/// </summary> | |
public float GetNoise() { | |
return GetNoise(Time.time); | |
} | |
/// <summary> | |
/// パーリンノイズを取得(任意のxでの値、yはシードで固定) | |
/// </summary> | |
public float GetNoise(float x) { | |
return GetNoise(x, _seed); | |
} | |
/// <summary> | |
/// パーリンノイズを取得(任意のxとyでの値) | |
/// </summary> | |
public float GetNoise(float x, float y) { | |
return _min + Mathf.PerlinNoise(x * _rateOfChange, y * _rateOfChange) * (-_min + _max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment