SceneKit获取动画SCNNode的当前位置

5

我希望能够从一个正在动画中的SCNNode获取当前节点位置。我知道可以使用presentationNode来提取位置,但是没有成功。

以下是我的代码:

SCNNode * pelvis = [_unit childNodeWithName:@"Bip01_Pelvis" recursively:YES];
SCNNode * present = pelvis.presentationNode;



SCNVector3 position = [self convertPosition:present.position toNode:self.parentNode];

我所拥有的是在应用CAAnimation之前,SCNNode处于“静止”模式下的位置。

如何从“presentationNode”获取像位置这样的属性?

编辑

类本身相关部分:

@interface UndeadSimple : RepresentationNode <CAAnimationDelegate>
{
    //Animations
    SCNNode * _undead;

    CAAnimation * _currentAnimation;
    NSString    * _currAnimationkey;

    CAAnimation * _death1;
    ...

    SCNNode        * _audioNode;
    SCNAudioSource * _deathSource;
    SCNAudioPlayer * _player;
}

初始化:

NSString * sceneName = @ZOMBIE;
SCNScene *scene2 = [SCNScene sceneNamed:sceneName];

_undead = [SCNNode node];
for(SCNNode * node in scene2.rootNode.childNodes)
{
    [_undead addChildNode:node];
    [node removeAllAnimations];
}
[_undead setScale:(SCNVector3Make(0.024, 0.02, 0.024))];

[self addChildNode:_undead];
[_undead removeAllAnimations];

_currentAnimation = _idleAnimation;
_currAnimationkey = @"resting";

[_undead addAnimation:_currentAnimation forKey:_currAnimationkey];

动画和事件:

SCNAnimationEvent * event2 = [SCNAnimationEvent animationEventWithKeyTime:0.9 block:^(CAAnimation * _Nonnull animation, id  _Nonnull animatedObject, BOOL playingBackward)
{
    [self.delegate finishedDeathAnimation];
}];

_death1 = anims.death1.mutableCopy;
[_death1 setDelegate:self];
[_death1 setFillMode:kCAFillModeForwards];
_death1.removedOnCompletion = NO;
[_death1 setAnimationEvents:@[event2]];

动画调用:

- (void)die
{

    if([_currAnimationkey isEqualToString:@"dead"])
    {
        return;
    }

   [_undead removeAllAnimations];
   CAAnimation * death = nil;



    int anim = arc4random_uniform(3);
    if(anim < 1)
    {
        death = _death1;
    }
    else if(anim < 2)
    {
        death = _death2;
    }
    else
    {
        death = _death3;
    }

    _currAnimationkey = @"dead";
    _currentAnimation = death;

//获取演示节点的辅助方法

- (SCNNode *)getSnapNode
{
    SCNNode * repNode = [_undead presentationNode];
    _undead.transform = repNode.transform;
    repNode = _undead.clone;
    return repNode;
}

//在回调函数中尝试获取位置信息:

- (void)finishedDeathAnimation
{
    SCNPlane * bloodgeometry = [SCNPlane planeWithWidth:4 height:4];
    bloodgeometry.firstMaterial.diffuse.contents = [NSImage imageNamed:@"blood"];

    SCNNode * bloodNode = [SCNNode node];
    [bloodNode setEulerAngles:SCNVector3Make(-M_PI_2, 0, 0)];
    bloodNode.geometry = bloodgeometry;

    //Bip01_Pelvis
    SCNNode * deadBody = [_unit getSnapNode];

    SCNNode * pelvis = [deadBody childNodeWithName:@"Bip01_Pelvis" recursively:YES];

    SCNNode * present = pelvis.presentationNode;
    SCNVector3 position = [self convertPosition:present.position toNode:self.parentNode];

   NSLog(@"pelvis position %lf,%lf,%lf", present.position.x, present.position.y, present.position.z); //Always {0,0,0}


    bloodNode.position = SCNVector3Make(position.x, 0.0001, position.z);

    [self.parentNode addChildNode:bloodNode];

    [self removeFromParentNode];
}

编辑2

我也试图将其简化:

另一个辅助方法:

//在undead/unit类中

- (SCNVector3)getPosition
{
    SCNNode * repNode = [[_undead childNodeWithName:@"Bip01_Pelvis" recursively:YES] presentationNode];
    return repNode.position;
}

回调函数:

- (void)finishedDeathAnimation
{
    position = [_unit getPosition];
    NSLog(@"pelvis position %lf,%lf,%lf", position.x, position.y, position.z); //Alway 0,0,0 as well :(
}
2个回答

3

我很少能找到像我这样仍然相信Objective C而不是Swift的人,所以我愿意提供充分的帮助。

我曾经遇到过一个问题,即从Blender导出为Collada格式的动画,但经过几次尝试成功地得到了实际位置。

使用presentationNode是正确的方法,但你需要在制作动画时从子节点获取它。例如,以下是如何对名为_monkey的节点运行动画。请注意,在最后一行代码中,我从child.presentationNode而不是_monkey.presentationNode获取了_present。此外,在枚举期间而不是之后获得它。

因此,首先我需要定义:

NSNode * _present; 

然后我播放动画。
   [_monkey enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop)
     {
         for(NSString *key in child.animationKeys)
         {               // for every animation key
             CAAnimation *animation = [child animationForKey:key]; // get the animation
             animation.usesSceneTimeBase = NO;                     // make it system time based
             animation.repeatCount = FLT_MAX;                      // make it repeat forever
             animation.speed = 0.2;
             [child addAnimation:animation forKey:key];            // animations are copied upon addition, so we have to replace the previous animation
         }
         _present = child.presentationNode;
     }];

然后,在渲染下,我可以检查结果随着动画的移动而改变(如果您有 iOS11 的工作经验,您也可以检查 worldPosition 而不是 position,它也会给出很好的结果)。

- (void)renderer:(id <SCNSceneRenderer>)renderer didSimulatePhysicsAtTime:(NSTimeInterval)time
{
     NSLog(@" Location X:%f Y:%f Z:%f", _present.position.x, _present.position.y, _present.position.z);

}

最终结果如下:
Y:-2.505034 Z:4.426160
2017-10-11 04:42:19.700435+0200 monkey[547:137368]  Location X:-5.266642 Y:-2.480119 Z:4.427162
2017-10-11 04:42:19.720763+0200 monkey[547:137368]  Location X:-5.215315 Y:-2.455228 Z:4.428163
2017-10-11 04:42:19.740449+0200 monkey[547:137346]  Location X:-5.163589 Y:-2.430143 Z:4.429171
2017-10-11 04:42:19.760397+0200 monkey[547:137368]  Location X:-5.112190 Y:-2.405216 Z:4.430173
2017-10-11 04:42:19.780557+0200 monkey[547:137346]  Location X:-5.060925 Y:-2.380355 Z:4.431173
2017-10-11 04:42:19.800418+0200 monkey[547:137342]  Location X:-5.008560 Y:-2.355031 Z:4.432024

这让我的结果很好。希望它也能对你有用。

1
在你展示的代码中,没有任何动画正在运行。在动画播放时,检查演示节点的位置,可能需要在renderer:updateAtTime:(参见SCNRendererDelegate)中进行。

我保证动画正在运行,我创建了一个SCNAnimationEvent来获取代理,当它接近结束时 - 并尝试从演示节点中提取位置 - 但它总是返回SCNVectorZero :-( - Coldsteel48
请编辑您的问题,展示您实际使用的代码,包括启动动画的代码。这些东西的时间和顺序很重要,而且您提供的信息相对较少。 - Hal Mueller
Hal先生,我编辑了我的问题并添加了几乎所有相关的代码。 - Coldsteel48
我已经放弃了,我将从Max中导出所需位置的DAE文件 - 非常悲伤:(。一般来说,我认为SceneKit团队应该重新设计动画 - 使其更适合游戏开发并以某种方式进行优化,因为现在动画占据了我的循环时间的80%,这迫使我在不到2ms的时间内完成所有更新逻辑,这太糟糕了。 - Coldsteel48
你的演示节点副本看起来有些可疑。但我还没有时间深入调查。请提交Radar! - Hal Mueller
是的,这是经过999次尝试解决问题的产物 :-) ,因此我创建了更简单的方法来返回位置 - 它会产生相同的结果 :( - Coldsteel48

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接