@All你好,
我正在尝试在initializeScene方法之外添加和显示.pod文件。我可以添加它们并在initializeScene方法中正确显示它们但我需要在初始化场景后动态显示一些.pod文件。
我检查了方法onOpen但是我尝试添加那里但它没有出现。
我正在尝试以下方法: -
-(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文件,它一切正常。
答案 0 :(得分:1)
在 cocos3d 中,当您在后台线程上加载资源时,每次出现的addChild:
方法实际上都会从 返回 渲染线程的后台线程实际上将节点添加到其父节点。
这样做的原因是同步线程活动,具体来说,是为了避免在渲染器迭代相同的结构来绘制节点时将新节点添加到节点结构中。如果您想查看丑陋的详细信息,请查看CC3Node addChild:
方法的实现。
由于这种双重调度,在addSceneContentAsynchronously
实现中,当moveWithDuration:toShowAllOf:withPadding:
运行时,您的节点可能尚未实际添加到场景中。
moveWithDuration:toShowAllOf:withPadding:
方法在方法开始时调查场景一次,以确定摄像机应移动到的位置。因此,结果是相机在执行调查时可能不知道您新添加的节点是否存在。
您可以通过在调用moveWithDuration:toShowAllOf:withPadding:
之前将短暂睡眠放入后台线程来解决此问题。
[NSThread sleepForTimeInterval: 0.1];
另一种选择是覆盖自定义CC3Scene实现中的addChildNow:
方法,以便它调用超类实现,然后移动相机。 addChildNow:
方法是在渲染线程上运行的,作为双重调度的结果,实际上将节点添加到其父节点。
但是,如果您要在场景中添加大量内容,这可能会变得很麻烦,因为每次添加新内容时相机都会反弹。但话说回来,也许这会在你的应用程序中有意义。