LoginSignup
12
6

More than 5 years have passed since last update.

[Unity]カメラの消失点を中央からずらすスクリプト

Last updated at Posted at 2017-12-22

実行手順

スクリプトを保存してAdd

ObliqueProection.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[ExecuteInEditMode] 
public class ObliqueProection : MonoBehaviour {

    [Range(-1f,1f)] public float horizontal;
    [Range(-1f,1f)] public float vertical;
    public float distance;
    private Vector3 org_position_;

    void OnEnable()
   {
       EditorApplication.update += EditorUpdate;
       org_position_ = transform.localPosition;
   }

   void OnDisable()
   {
       EditorApplication.update -= EditorUpdate;
   }

    void EditorUpdate ()
    {
        var cam = GetComponent<Camera>();
        var proj = cam.projectionMatrix;
        proj.m02 = horizontal;
        proj.m12 = vertical;
        cam.projectionMatrix = proj;        
        var tan = Mathf.Tan(cam.fieldOfView*0.5f*Mathf.Deg2Rad);
        var rect = cam.pixelRect;
        var ratio = (float)rect.width/(float)rect.height;
        transform.localPosition = org_position_ + new Vector3(-horizontal*distance*tan*ratio, -vertical*distance*tan, 0f);
    }
}

上記のスクリプトを ObliqueProjection.cs に保存し、カメラオブジェクトにアタッチします。

カメラから基準面への距離を設定

Distance に入れた値で基準面を決定します。
distance.png

動かします

oblique00.gif

Enjoy!

原理の解説はなく使い方だけ書きました。エディタ機能で作りましたが、プレイ中にのみ使用する場合は EditorUpdate を Update にすれば良いでしょう。

消失点を変更する演出は、ゲームよりも映像系で重宝しそうですね。めでたしめでたし。

12
6
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
12
6