iOS Cocos3D在initializeScene方法之外添加.pod文件



@大家好,

我正在尝试在initializeScene方法之外添加和显示.pod文件。我可以添加它们并在initializeScene方法中正确显示它们,但我需要在初始化场景后动态显示一些.pod文件。

我检查了Open上是否有方法,但我尝试在那里添加,但它没有显示。

我正在尝试以下方法:-

-(void) initializeScene {
   _autoRotate = TRUE;
   [self setTouchEnabled:YES];
// Create the camera, place it back a bit, and add it to the scene
  cam = [CC3Camera nodeWithName: @"Camera"];
  cam.location = cc3v( 0.0, 0.0, 8.0 );
   _cameraAngle  = M_PI / 2.0f;
   _cameraAngleY  = M_PI / 2.0f;
   _cameraHeight = INIT_VIEW_HEIGHT;
   _cameraDistance = INIT_VIEW_DISTANCE;
   _swipeSpeed = 0.0f;
   [self addChild: cam];
// Create a light, place it back and to the left at a specific
// position (not just directional lighting), and add it to the scene
   CC3Light* lamp = [CC3Light nodeWithName: @"Lamp"];
   lamp.location = cc3v( -2.0, 0.0, 0.0 );
   lamp.isDirectionalOnly = NO;
   [cam addChild: lamp];
    [self createGLBuffers];
    [self releaseRedundantContent];
    [self selectShaderPrograms];    
    [self createBoundingVolumes];
LogInfo(@"The structure of this scene is: %@", [self structureDescription]);
// ------------------------------------------
   [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(void) onOpen {
[self.viewSurfaceManager.backgrounder runBlock: ^{
    [self addSceneContentAsynchronously];
}];
}
 -(void) addSceneContentAsynchronously {
[self addContentFromPODFile:@"myObject.pod" withName:@"Mesh1"];
CC3MeshNode *meshNode =  (CC3MeshNode *) [self getNodeNamed:@"Mesh1"];
[self addChild:meshNode];
self.activeCamera.target = meshNode;
self.activeCamera.shouldTrackTarget = YES;
[self.activeCamera moveWithDuration: 0.5 toShowAllOf: self withPadding: 2.5f];
}

我有单独的.pod模型,所以我只需要把它们添加到场景中,并在相机中显示它们。但是异步和动态。

此外,如果我在initializeScene方法中添加.pod文件,一切都会很好。

cocos3d中,当您在后台线程上加载资产时,每次出现addChild:方法都会将从后台线程调度回渲染线程,以实际将节点添加到其父线程。

这样做的原因是为了同步线程活动,特别是避免在渲染器迭代相同结构以绘制节点时将新节点添加到节点结构中。如果您想了解丑陋的细节,请查看CC3Node addChild:方法的实现。

由于这种双重调度,在addSceneContentAsynchronously实现中,当moveWithDuration:toShowAllOf:withPadding:运行时,节点可能实际上还没有添加到场景中。

moveWithDuration:toShowAllOf:withPadding:方法在方法开始时对场景进行一次调查,以确定相机应该移动到哪里。因此,结果是,相机在执行调查时可能不知道新添加的节点的存在。

您可以通过在调用moveWithDuration:toShowAllOf:withPadding:之前在后台线程中放置一个短暂的睡眠来解决这个问题。

[NSThread sleepForTimeInterval: 0.1];

另一个选项是覆盖自定义CC3Scene实现中的addChildNow:方法,以便它调用超类实现,然后移动相机。addChildNow:方法是在渲染线程上运行的,作为双重调度的结果,将节点实际添加到其父节点。

然而,如果你在场景中添加了大量内容,这可能会变得很麻烦,因为每次添加新内容时,都会导致相机来回跳动。但话说回来,也许这在你的应用程序中是有意义的。

最新更新