Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for interpolation/tweening arrays of values #13

Open
davidpuetter opened this issue Aug 12, 2019 · 1 comment
Open

Support for interpolation/tweening arrays of values #13

davidpuetter opened this issue Aug 12, 2019 · 1 comment

Comments

@davidpuetter
Copy link

The standard TWEEN library allows you to pass an array of values to the tween to be able to interpolate (linearly, catmull or bezier) between those values and change the tweened object like that.

https://github.com/tweenjs/tween.js/blob/master/docs/user_guide.md#tweening-to-arrays-of-values

Looking at this library, it doesn't seem to support it. It would be super cool if that could be implemented one day.

@kungfooman
Copy link

Just wrote some code to tween between multiple colors, which is quite simple to use:

function genCurveSet(curvesetdata) {
	var n = curvesetdata[0].length;
	var curves = new Array(n);
	for (var i=0; i<n; i++) {
		var curve = [];
		var time = 0;
		var timeAdd = 1 / (curvesetdata.length - 1); // e.g. 1/(3-1) == [0, 0.5, 1]
		for (var j=0; j<curvesetdata.length; j++) {
			var curvedata = curvesetdata[j];
			curve.push(time);
			curve.push(curvedata[i]);
			time += timeAdd;
		}
		curves[i] = curve;
	}
	return new pc.CurveSet(curves);
}

And then we can define some animations:

	var curveset_rgb = genCurveSet([
		[1, 0, 0],
		[0, 1, 0],
		[0, 0, 1]
	]);

	var curveset_rainbow = genCurveSet([
		[1, 0, 0], // red
		[1, 1, 0], // yellow
		[0, 1, 0], // green
		[0, 0, 1], // blue
		[1, 1, 1], // white
	]);

	var curveset_redgreen = genCurveSet([
		[1, 0, 0], // red
		[0, 1, 0], // green
	]);
curveset_redgreen.value(0) /* = */ [1, 0, 0]
curveset_redgreen.value(1) /* = */ [0, 1, 0]
curveset_redgreen.value(0.3) /* = */ [0.784, 0.216, 0]

And a material could be animated like:

	var target = {time: 0};
	var to = {time: 1};
	var testTween = app.tween(target)
	.to(to, 3.0, pc.SineInOut)
	.loop(true)
	.yoyo(true)
	.on('update', function () {
		var newColor = new pc.Color(curveset_rainbow.value(this.target.time))
		appSimplified.red.diffuse = newColor;
		appSimplified.red.update();
	})
	.start();

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants