我想繪製平滑的*和隨機路徑,我的對象將遵循,我決定去與二次貝塞爾曲線(但我打開其他想法)。繪製具有二次貝塞爾曲線的隨機路徑
我的代碼以隨機但不平滑的方式移動我的對象。
前瞻:https://youtu.be/Eg9PEKuH4zA
我的問題是:我怎麼能做出方向變化更順暢?我應該徹底拋棄Bezier解決方案,還是有辦法擦亮我的代碼以實現我想要的?
*光滑==沒有突然的方向變化
我的代碼:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Lights : MonoBehaviour {
public float paintHeight = -90.0f;
public static int NUMBER_OF_LIGHTS = 3;
private static int BEZIER_PATH_POINTS = 100;
private float GOLDEN_COLOR_RATIO = 0.618033988749895f;
private Light[] lights = new Light[NUMBER_OF_LIGHTS];
Vector3 RandomPoint() {
float obj_width = gameObject.GetComponent<RectTransform>().rect.width;
float screenX = Random.Range(-obj_width/2, obj_width/2);
float obj_height = gameObject.GetComponent<RectTransform>().rect.height;
float screenY = Random.Range(-obj_height/2, obj_height/2);
return new Vector3(screenX, screenY, -paintHeight);
}
Vector3 QuadraticBezierPoint(Vector3 startPoint, Vector3 endPoint, Vector3 vertexPoint, float t) {
/*
* vertex
* /╲
* / ╲
* /p ╲
* /. . ╲
* /. · ╲
* /· · ╲
* start · end
*
* 0 < t < 1
*
* B(t) = (1 - t)^2 * P0 + 2 * (1-t) * t * P1 + t^2 * P2
*
*/
return Mathf.Pow((1 - t), 2) * startPoint + 2 * (1 - t) * t * vertexPoint + Mathf.Pow(t, 2) * endPoint;
}
Color RandomColor() {
float h = Random.Range(0.0f, 1.0f) + GOLDEN_COLOR_RATIO;
h %= 1;
return Color.HSVToRGB(h, 0.99f, 0.99f);
}
void Start() {
for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
GameObject light_obj = new GameObject();
Light light = light_obj.AddComponent<Light>();
light.type = LightType.Point;
light.range = 10.0f;
light.intensity = 3.5f;
light.renderMode = LightRenderMode.ForcePixel;
light.name = "Light" + i;
light.transform.parent = gameObject.transform;
lights[i] = light;
}
StartCoroutine("Move");
}
IEnumerator Move() {
Dictionary<string, Vector3>[] light_points = new Dictionary<string, Vector3>[NUMBER_OF_LIGHTS];
Dictionary<string, Color>[] light_colors = new Dictionary<string, Color>[NUMBER_OF_LIGHTS];
for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
light_points[i] = new Dictionary<string, Vector3>();
light_colors[i] = new Dictionary<string, Color>();
//light_points[i]["startPoint"] = RandomPoint();
//light_points[i]["vertexPoint"] = RandomPoint();
light_points[i]["endPoint"] = RandomPoint();
light_colors[i]["nextColor"] = RandomColor();
}
while(true) {
for (int i = 0; i < NUMBER_OF_LIGHTS; i++) {
light_points[i]["startPoint"] = light_points[i]["endPoint"];
light_points[i]["vertexPoint"] = RandomPoint();
light_points[i]["endPoint"] = RandomPoint();
light_colors[i]["currentColor"] = light_colors[i]["nextColor"];
light_colors[i]["nextColor"] = RandomColor();
}
for (int i = 0; i < BEZIER_PATH_POINTS; i++) {
float percent = (float)i/BEZIER_PATH_POINTS;
for (int j = 0; j < NUMBER_OF_LIGHTS; j++) {
lights[j].transform.localPosition = QuadraticBezierPoint(
light_points[j]["startPoint"],
light_points[j]["endPoint"],
light_points[j]["vertexPoint"],
percent
);
lights[j].color = Color.Lerp(light_colors[j]["currentColor"], light_colors[j]["nextColor"], percent);
}
yield return new WaitForSeconds(0.02f);
}
}
}
}
您的視頻並沒有真正的幫助,它是瀰漫的燈光在令人分心的背景上移動,因此無法確定正在追蹤的路徑。通過描述實際的問題開始:單曲線是平滑的,所以是從一個曲線去到下一個不順暢的問題?如果是這樣,解決方案是確保G1或甚至G2連續性,爲更高階曲線更好,像一個三次曲線。在那一點上,使用Catmul-Rom曲線更有意義,因爲那些曲線通過您定義的點(曲線上的每個部分都簡單地轉換爲Bezier形式) –