laptou
laptou

Reputation: 6981

Simple Three.js animation

I'm trying to create a simple color animation in Three.js. This is my code:

var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
var cube = new THREE.Mesh(geometry, material);
game.camera.position.y = 5;
game.camera.lookAt(new THREE.Vector3());
game.scene.add(cube);

var colorAnim = new THREE.ColorKeyframeTrack(
    ".material.color",
    [0, 2, 3, 4, 5],
    [0xff0000, 0xaa00aa, 0x0000ff, 0x00aaaa, 0x00ff00]);
var colorClip = new THREE.AnimationClip(null, 5, [colorAnim]);
var colorMixer = new THREE.AnimationMixer(cube);
var colorAction = colorMixer.clipAction(colorClip);
colorAction.play();

var clock = new THREE.Clock();

var render = function ()
{
    var delta = clock.getDelta();

    requestAnimationFrame(render);

    cube.rotation.x += Math.PI * delta;
    cube.rotation.y += Math.PI * delta;

    colorMixer.update(delta * colorMixer.timeScale);

    game.renderer.render(game.scene, game.camera);
};

render();

But the animation does not work properly, and instead does this:

The thing that it is doing

I am just trying to create a 5-second color animation, what am I doing wrong?

Upvotes: 0

Views: 1206

Answers (1)

laptou
laptou

Reputation: 6981

All of the components must be specified separately, and the range of values is [0, 1], not 0x00-0xFF.

This works:

var colorAnim = new THREE.ColorKeyframeTrack(
        ".material.color",
        [0, 2],
        [1, 0, 0, 0, 0, 1])

Upvotes: 1

Related Questions