Perlinノイズを使ったカメラの揺れ
keijiroさんのやられているPerlinノイズを使ったカメラの揺れのテストをしてみました。
keijiroさんの書かれているものもあったのですがサンプルがNot Foundになっていますね。
今回は単純に上下の揺れのみ。
Perlin.csにはNoise()系のメソッドとFbm()系のメソッドがありますが、Fbm系はNoiseを重ね合わせてより柔らかなノイズ(その分処理は重く)なる感じでしょうか。
using UnityEngine;
using System.Collections;
public class PerlinSwing : MonoBehaviour {
/**
* オリジナルの高さ。
*/
private float firstY;
/**
* スイングカウント。
*/
private float swingCount = 0.0f;
/**
* スイングスピード。
*/
private float swingSpeed = 0.05f;
/**
* スイング幅。
*/
private float swingDepth = 0.5f;
/**
* 回転半径。
*/
private float rotateR = 8f;
/**
* 回転スピード。
*/
private float rotateSpeed = 0.02f;
/**
* 回転ラジアン。
*/
private float rotateRad = 0.0f;
/**
* Awake.
*/
public void Awake() {
this.firstY = this.transform.position.y;
}
/**
* Update.
*/
public void Update() {
Vector3 pos = new Vector3(
Mathf.Cos(this.rotateRad) * this.rotateR,
this.firstY + Perlin.Fbm(this.swingCount, 2) * this.swingDepth,
Mathf.Sin(this.rotateRad) * this.rotateR
);
this.transform.position = pos;
this.rotateRad += this.rotateSpeed;
this.transform.LookAt(new Vector3(0f, 1f, 0f));
this.swingCount += this.swingSpeed;
}
/**
* OnGUI.
*/
public void OnGUI() {
GUI.Label(new Rect(10f, 10f, 200f, 30f), "swing depth");
this.swingDepth = GUI.HorizontalSlider(new Rect(10f, 32f, 200f, 30f), this.swingDepth, 0f, 2f);
GUI.Label(new Rect(10f, 42f, 200f, 30f), "swing speed");
this.swingSpeed = GUI.HorizontalSlider(new Rect(10f, 64f, 200f, 30f), this.swingSpeed, 0f, 1f);
}
}

コメントを残す