Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9529

【Unity】動画で見るSlerp

$
0
0

Vector3のSLerpを動画にしました。
SLerpをなんとなく使っている人に見ていただけると幸いです。
動画で見るLerpも投稿していますのそちらも参考に。

動画

オブジェクトの色も線形補間(Color.Lerp)によって変化させています。

Slerp(Vector3 a, Vector3 b, float t);

引数

  • Vector3 a :開始点
  • Vector3 b :終了点
  • float t :二点の補間値

計算式は

Slerp(a,b;t) = \frac{sin[(1-t)Ω]}{sinΩ}a + \frac{sin[tΩ]}{sinΩ}b

a,bの二点間を球面的に補間するということを表してるわけですが、
これだけ見るとなんだかよくわかりませんね。
この辺は私がわざわざ説明するより、先人達の記事の方が何倍もわかりやすいので以下にリンクを貼っておきます。

○×つくろ〜ドットコム その57 クォータニオンを"使わない"球面線形補間

SlerpExample

SlerpExample.cs
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassSlerpExample:MonoBehaviour{publicTransformsunrise;publicTransformsunset;//始点から終点までの時間publicfloatjourneyTime=1.0f;[System.NonSerialized]publicfloatfracComplete;privatefloatstartTime;privatevoidStart(){startTime=Time.time;}privatevoidUpdate(){//弧の中心Vector3center=(sunrise.position+sunset.position)*0.5f;//弧を中心にするため調整center-=newVector3(0,-1,0);//中心を基準として円弧を補間するVector3riseRelCenter=sunrise.position-center;Vector3setRelCenter=sunset.position-center;fracComplete=(Time.time-startTime)/journeyTime;transform.position=Vector3.Slerp(riseRelCenter,setRelCenter,fracComplete);transform.position+=center;}}

ColorLerp

Lerpの記事から少し改良しています。

ColorLerp.cs
singSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassColorLerp:MonoBehaviour{publicenumLerpType{Lerp,Slerp}publicLerpTypelerpType;privateLerpExamplelerpExample;privateSlerpExampleslerpExample;privateMaterialmat;privatevoidStart(){mat=this.GetComponent<Renderer>().material;lerpExample=GetComponent<LerpExample>();slerpExample=GetComponent<SlerpExample>();}voidUpdate(){switch(lerpType){caseLerpType.Lerp:mat.color=Color.Lerp(Color.blue,Color.red,lerpExample.fractionOfJourney);break;caseLerpType.Slerp:mat.color=Color.Lerp(Color.blue,Color.red,slerpExample.fracComplete);break;}}}

まとめ

  • Slerpは球面線形補間をするメソッド
  • 回転表現に使用できる
  • Quaternionだと理解しにくいので動画を見て、直感的に理解してくれたら幸いです

Viewing all articles
Browse latest Browse all 9529

Trending Articles