6

我想在 SceneKit 中创建步行人动画。我正在从 3DSMax + OpenCollada 导出动画 .dae 文件,我还使用ConvertToXcodeCollada将所有动画合二为一。我如何获得动画:

 SCNScene *humanScene = [SCNScene sceneNamed:@"art.scnassets/myScene.DAE"];

CAAnimation *Animation = [[humanScene rootNode] animationForKey:@"myScene-1"];

我也尝试从“SCNSceneSource”获取动画

我如何添加动画:

SCNNode *humanNode = [humanScene.rootNode childNodeWithName:@"myScene-1" recursively:YES];
[humanNode addAnimation:walkingAnimation forKey:@"myScene-1"];

或者:

SCNNode* humanNode = [SCNNode new];
for(SCNNode* node in humanScene.rootNode.childNodes){
    [humanNode addChildNode:node];
} 
[humanNode addAnimation:walkingAnimation forKey:@"myScene-1"];

我的对象“walkingAnimation”是“CAAnimationGroup”。

但它在应用程序中没有动画。我只能在 Xcode sceneKit 编辑器中看到动画。

我的 .DAE 文件示例

4

1 回答 1

0

使用您的模型尝试以下代码。有用。我在 macOS 12.1 中使用了 Xcode 13.2.1。

初填viewDidLoad方法:

#import "GameViewController.h"

@implementation GameViewController

SCNView *sceneView;
bool notRunningSwitch;
NSMutableDictionary<NSString*, CAAnimation*> *animations;
SCNNode *node;

- (void)viewDidLoad
{
    [super viewDidLoad];

    sceneView = (SCNView *)self.view;
    notRunningSwitch = YES;
    animations = @{}.mutableCopy;
    node = [SCNNode node];
    
    SCNScene *scene = [SCNScene scene];
    sceneView.scene = scene;
    sceneView.autoenablesDefaultLighting = YES;
    sceneView.allowsCameraControl = YES;

    [self anime];
}

然后animeloadAnime方法:

- (void)anime
{
    SCNScene *standStillScene = [SCNScene sceneNamed:@"art.scnassets/Idle"];
    
    for (SCNNode *childNode in standStillScene.rootNode.childNodes)
    {
        [node addChildNode:childNode];
    }
    
    node.scale = SCNVector3Make(0.1, 0.1, 0.1);
    node.position = SCNVector3Make(0, 0,-2.5);

    [sceneView.scene.rootNode addChildNode: node];

    [self loadAnime:@"running" inScene:@"art.scnassets/Running" withID:@"Running-1"];
}

- (void)loadAnime:(NSString*)withKey inScene:(NSString*)scene withID:(NSString*)id
{
    NSURL *url = [NSBundle.mainBundle URLForResource:scene withExtension:@"usdz"];
    
    SCNSceneSource *source = [SCNSceneSource sceneSourceWithURL:url options:Nil];
    
    CAAnimation *charAnimation = [source entryWithIdentifier:id 
                                                   withClass:CAAnimation.self];

    charAnimation.repeatCount = 1;
    charAnimation.fadeInDuration = 1;
    charAnimation.fadeOutDuration = 1;

    [animations setValue:charAnimation forKey:withKey];
}

最后touchesBegan

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [touches.allObjects.firstObject locationInView: sceneView];

    NSDictionary<SCNHitTestOption, id> *options = @{ SCNHitTestBoundingBoxOnlyKey: @(YES) };
    
    NSArray<SCNHitTestResult *> *hitTestResults = [sceneView hitTest: point options: options];
        
    if (notRunningSwitch == YES) {
        [sceneView.scene.rootNode addAnimation:animations[@"running"] forKey:@"running"];
    } else {
        [sceneView.scene.rootNode removeAnimationForKey:@"running" blendOutDuration:1.0];
    }
    notRunningSwitch = !notRunningSwitch;
    
    NSLog(@"%@", hitTestResults.firstObject.node.name);
    NSLog(@"%f", hitTestResults.firstObject.modelTransform.m43);
}

@end
于 2022-01-26T14:01:06.007 回答