i want draw smooth* , random paths objects follow , decided go quadratic bezier curves (but i'm open other ideas).
my code moving objects in random, not smooth* way.
preview: https://youtu.be/eg9pekuh4za
my question is: how can make direction changes smoother? should ditch bezier solution or there way polish code achieve want?
*smooth == no abrupt direction changes
my code:
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() { (int = 0; < 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]; (int = 0; < 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) { (int = 0; < 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(); } (int = 0; < bezier_path_points; i++) { float percent = (float)i / bezier_path_points; (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); } } } }
if have curve going b middle point, you'll have select c accordingly angle (a,b,c) @ b not small or big.
additionally you'll have select middle points in appropriate way, see e.g. https://www.particleincell.com/2012/bezier-splines/ , change in direction not big.
Comments
Post a Comment