[英]SceneKit - How to know if SCNNode with MDLObject is loaded and visible?
i have a problem with animating SCNNode "on rendered", so my question is how to know that SCNNode is fully loaded and visible on scene ? 我在“渲染时”为SCNNode设置动画时遇到问题,所以我的问题是如何知道SCNNode已完全加载并在场景中可见? I want to animate its scale right after load/render.
我想在加载/渲染后立即对其比例进行动画处理。
Here's how i initialize it: 这是我如何初始化它:
NSString* ring = [[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:@"sweep"]
ofType:@"obj"];
NSURL *url = [NSURL fileURLWithPath:ring];
MDLAsset *asset = [[MDLAsset alloc]initWithURL:url];
self.node = [SCNNode nodeWithMDLObject:[asset objectAtIndex:0]];
self.node.geometry.firstMaterial = goldMaterial;
self.node.geometry.subdivisionLevel = 1;
[self.node setRotation:SCNVector4Make(-1, 0, 0, M_PI/2.5)]; //M_PI = 180d
[self.node setScale:SCNVector3Make(0.1, 0.1, 0.1)];
[scene.rootNode addChildNode: self.node];
and right after that i tried to add action like this 在那之后我试图添加这样的动作
SCNAction *scaleUp = [SCNAction scaleTo:1.0f duration:0.5f];
scaleUp.timingMode = SCNActionTimingModeEaseInEaseOut;
[self.node runAction:scaleUp];
but my node just shows scaled up, because it takes like ~2s to load obj asset. 但是我的节点只是显示放大,因为加载obj资产大约需要2秒钟的时间。 When i change duration to like 5s, i can see animation.
当我将持续时间更改为5s时,我可以看到动画。
I also tried to delegate SCNSceneRenderer: didRenderScene, but i also couldn't run that animation succesfuly. 我也尝试委托SCNSceneRenderer:didRenderScene,但我也无法成功运行该动画。
You are lucky. 你很幸运。 I am dealing with this topic recently.
我最近正在处理这个话题。 Assume you are using a SCNView as the SCNSceneRender, you can add the rootNode to the following call and then in completion callback handle other operations, like this:
假设您使用SCNView作为SCNSceneRender,可以将rootNode添加到以下调用中,然后在完成回调时处理其他操作,如下所示:
// setup everything here. It's your code.
NSString* ring = [[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:@"sweep"]
ofType:@"obj"];
NSURL *url = [NSURL fileURLWithPath:ring];
MDLAsset *asset = [[MDLAsset alloc]initWithURL:url];
self.node = [SCNNode nodeWithMDLObject:[asset objectAtIndex:0]];
self.node.geometry.firstMaterial = goldMaterial;
self.node.geometry.subdivisionLevel = 1;
[self.node setRotation:SCNVector4Make(-1, 0, 0, M_PI/2.5)]; //M_PI = 180d
[self.node setScale:SCNVector3Make(0.1, 0.1, 0.1)];
[scene.rootNode addChildNode: self.node];
SCNAction *scaleUp = [SCNAction scaleTo:1.0f duration:0.5f];
scaleUp.timingMode = SCNActionTimingModeEaseInEaseOut;
[self.node runAction:scaleUp];
SCNView * view; // you don't need this if you have one already.
[view prepareObjects:@[scene.rootNode] withCompletionHandler:^(BOOL success) {
if(success) {
//scene will be rendered after this point.
}
}];
Hope it's what you need. 希望这就是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.