返回介绍

Vector3.Slerp 球形插值

发布于 2019-12-18 15:38:44 字数 3083 浏览 1150 评论 0 收藏 0

JavaScript => static function Slerp(from: Vector3, to: Vector3, t: float): Vector3;
C# => static Vector3 Slerp(Vector3 from, Vector3 to, float t);

Description 描述

Spherically interpolates between two vectors.

两个向量之间的弧形插值

我感觉叫“弧线插值”更直观一些。

Interpolatesbetween from and to by amount t. The difference between this and linear interpolation (aka, “lerp”) is that the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated by the angle and its magnitude is interpolated between the magnitudes of from and to.

通过t数值在from和to之间插值。这与线性内插之间的不同(即, “线性插值” )是该向量被视为方向而不是空间中的点。返回的向量的方向是由角内插,其大小是from和to的幅度之间进行内插。

/t/ is clamped between [0…1]. See Also: Lerp function.

t的值在[0…1]。

参见:Lerp函数

JavaScript:

	// Animates the position in an arc between sunrise and sunset.
 
	var sunrise : Transform;
	var sunset : Transform;
 
	// Time to move from sunrise to sunset position, in seconds.
	var journeyTime = 1.0;
 
	// The time at which the animation started.
	private var startTime: float;
 
 
	function Start() {
		// Note the time at the start of the animation.
		startTime = Time.time;
	}
 
 
	function Update () {
	    // The center of the arc
	    var center = (sunrise.position + sunset.position) * 0.5;
	    // move the center a bit downwards to make the arc vertical
	    center -= Vector3(0,1,0);
 
	    // Interpolate over the arc relative to center
	    var riseRelCenter = sunrise.position - center;
	    var setRelCenter = sunset.position - center;
 
	    // The fraction of the animation that has happened so far is
	    // equal to the elapsed time divided by the desired time for
	    // the total journey.
	    var fracComplete = (Time.time - startTime) / journeyTime;
	    transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
	    transform.position += center;
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Transform sunrise;
    public Transform sunset;
    public float journeyTime = 1.0F;
    private float startTime;
    void Start() {
        startTime = Time.time;
    }
    void Update() {
        Vector3 center = (sunrise.position + sunset.position) * 0.5F;
        center -= new Vector3(0, 1, 0);
        Vector3 riseRelCenter = sunrise.position - center;
        Vector3 setRelCenter = sunset.position - center;
        float fracComplete = (Time.time - startTime) / journeyTime;
        transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
        transform.position += center;
    }
}

Vector3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文