SceneKitを最近触っています。
SceneKitとは、iOS/MacOS上で3D系の処理を行うことができるライブラリ群です。
SceneKitでblenderで作成したアニメーション付きのdaeファイルを読み込むと、ずっとアニメーションが実行されてしまい、止めることもできず悩んでおりましたが、
下記の通り、node用とanimation用のdaeファイルを別々に読み込むことで解決できたので、共有しておこうと思います。
daeファイル読み込み
事前にanim_test.daeにアニメーション無しのnodeデータを、anim_test2.daeにアニメーションデータを作成しておきます。
// node読み込み
NSURL* urlOfNode = [[NSBundle mainBundle] URLForResource:@"art.scnassets/anim_test" withExtension:@"dae"];
SCNSceneSource* sourceOfNode = [[SCNSceneSource alloc]initWithURL:urlOfNode options:nil];
SCNNode* node = [sourceOfNode entryWithIdentifier:@"Armature" withClass:[SCNNode class]];
node.physicsBody = [SCNPhysicsBody dynamicBody];
node.physicsBody.restitution = 0.9;
node.physicsBody.allowsResting = YES;
node.position = SCNVector3Make(0, 0, 0);
[_scnView.scene.rootNode addChildNode:node];
// animation読み込み
NSURL* urlOfAnimation = [[NSBundle mainBundle] URLForResource:@"art.scnassets/anim_test2" withExtension:@"dae"];
SCNSceneSource* sourceOfAnimation = [[SCNSceneSource alloc]initWithURL:urlOfAnimation options:nil];
id animationIndentifiers = [sourceOfAnimation identifiersOfEntriesWithClass:[CAAnimation class]];
_animationArray = [[NSMutableArray alloc]init];
for (id animationIndentifier in animationIndentifiers) {
CAAnimation* animation = (CAAnimation*)[sourceOfAnimation entryWithIdentifier:animationIndentifier withClass:[CAAnimation class]];
CAAnimationGroup* animationGroup = (CAAnimationGroup*)animation;
animationGroup.repeatCount = 1;
for (CAAnimation* animationTemp in animationGroup.animations) {
animationTemp.repeatCount = 1;
}
[_animationArray addObject:animationGroup];
}
アニメーションの実行
下記でアニメーションが実行されます。
for (CAAnimationGroup* animation in _animationArray) {
[_scnView.scene.rootNode addAnimation:animation forKey:nil];
}
できることはできましたが、SceneKit+blenderでは、animationごとにdaeファイルを用意する必要があるってことでしょうか。。