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

标签 ios opengl-es-2.0 cocos3d

@大家好,

我正在尝试在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文件,一切正常。

最佳答案

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

这样做的原因是为了同步线程事件,具体来说,是为了避免在渲染器迭代同一结构来绘制节点时将新节点添加到节点结构中。如果您想查看丑陋的细节,请查看 CC3Node addChild: 方法的实现。

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

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

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

[NSThread sleepForTimeInterval: 0.1];

另一个选择是重写自定义 CC3Scene 实现中的 addChildNow: 方法,以便它调用父类(super class)实现,然后移动相机。由于双重分派(dispatch),addChildNow: 方法在渲染线程上运行,以实际将节点添加到其父节点。

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

关于iOS Cocos3D 在initializeScene方法之外添加.pod文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20265276/

相关文章:

ios - 如何使用 makehuman、blender 和 PVRShaman 在 Cocos3d 中正确显示 3d 模型?

iphone - 有没有像 cocos3d 这样的开源 3d 框架的好的教程?

ios - AFNetworking downloadTaskWithRequest :progress:destination:completionHandler: not writing file to the path

ios - UISearchBar 中的单词不自动大写

ios - subview 导航栏中不需要的大空间

opengl-es-2.0 - 用于 Nvidia Tegra 的 OpenGL ES 2.0 离线着色器编译器

c - OpenGL ES 2.0 + 开罗 : HUD

ios - 根据 Swift 3 中 UITableView 中的单元格选择删除文件

objective-c - glBindAttribLocation() 的某些名称不起作用。 Objective-C 问题?

addchild - 添加图像作为 CC3Node 的子节点