0
votes

I am trying to simulate a blinking light on an object, so I am animating the material color. I then export the blender model into three.js via the collada exporter.

To demonstrate the problem, I have created a model with a single cube on which I change the color from red to green and back to red. I also move the cube a little.

At frame 0, I make the cube red with no displacement: enter image description here

At frame 12, I make it green and displace it a little: enter image description here

When I play the animation within blender, I see the color change and the cube displace. However, when I export it with collada into three.js, the cube moves, but it stays one color (the color of the cube that was active at the time I exported it e.g it can be either red or green, but always one color). I have created a plunker demonstrating the problem. My collada code is based on the three.js collada pump example. Here is the code used to load the model:

  factory.loadColladaModel = function () {
    console.log(`now in loadColladaModel`);
    var loader = new THREE.ColladaLoader();
    loader.options.convertUpAxis = true;

    var promise = new Promise( (resolve, reject) => {
      loader.load( 'color_animation.dae', (collada) => {
        console.log(`now in collada load closure`);

        let model = collada.scene;
        factory.scene.add(model);
        factory.animations = collada.animations;
        model.scale.x = model.scale.y = model.scale.z = 5.0;

        for ( var i = 0; i < factory.animations.length; ++i ) {
          var animation = factory.animations[ i ];

          var kfAnimation = new THREE.KeyFrameAnimation( animation );
          kfAnimation.timeScale = 1;
          factory.kfAnimations.push( kfAnimation );
        }

        resolve('loaded');
      })
    })

    return promise;
  }

What do I do have to do in order to get three.js to animate the material (color) changes as well as the motion? I do see color entries under the 'animation' tag of the collada file, so I assume collada supports it. I saw this prior question where someone had to set morphTargets in order to get materials to work. Do I need to do something like this too? Is morphTargets only for motion, or is it for color too?

three.js r84 blender 2.78b

Many Thanks.

1

1 Answers

0
votes

It basically looks like the three.js ColladaLoader simply does not support material animations (it only supports position and rotation animations). I determined this by looking at raw data structure returned by the collada loader. Note how Object 0, which corresponds to the position animation has sids (string ids) and keys etc. Object 1, which corresponds to the material animation, has no sids or keys. While the material animation data is in the raw collada file, the ColladaLoader parser is simply not reading it, thus no color animations.

js console

Apparently, the Collada loader is "kind of deprecated" per mrdoob (as of 5/2016). There is a ColladaLoader2, which is supposed to be the next version of the ColladaLoader, but I tried that and it appears to not support any animations at all. I've been reading up on the collada loader and indeed it is an aging format and basically looks likes it's a dead end as far as future growth goes (?)

Unfortunately, I've tried the obj, json, and stl loaders and the collada loader is by far the best. I'm currently investigating the gltf format, which basically seems like the next generation of collada (e.g the new and improved version also from the Khronos group). I found this discussion very useful. Basically, a couple of years ago there was a three.js user who wrote his own ColladaLoader, but he has since abandoned Collada and now backs glTF.

Update: I tried the GLTFLoader and GLTF2Loader loaders, but unfortunately they are failing on a semi-complex scene (a scene with 20 objects and 310 faces). It looks like a promising format though. I just think that between a test exporter and test loaders, it's just not quite stable at the moment. I was almost able to get animation working with the format (not material animation though) when I tested with a simple cube.

Looks like I'll have to stay with Collada for now and just do material animations in javascript.