0
votes

@All Hello,

I am trying to add and display .pod files outside initializeScene method. I can add them and display them properly inside initializeScene method but i need to show some of the .pod files dynamically after initializing scene.

I checked there is method onOpen but i tried adding there but it does not show up.

I am trying with below methods:-

-(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];
}

I have .pod models separately so i just need to add them into scene and show them in camera. But asynchronously and dynamically.

Also if i add .pod files in initializeScene method it all works fine.

1

1 Answers

1
votes

In cocos3d, when you load assets on the background thread, each occurrence of the addChild: method actually dispatches back from the background thread to the rendering thread to actually add the node to its parent.

The reason for this is to synchronize the thread activity, specifically, to avoid the new node being added into the node structure while the renderer is iterating that same structure to draw the nodes. Review the implementation of the CC3Node addChild: method if you want to see the ugly details.

Because of this double-dispatch, in your addSceneContentAsynchronously implementation, your node has probably not actually been added to the scene yet when the moveWithDuration:toShowAllOf:withPadding: runs.

The moveWithDuration:toShowAllOf:withPadding: method surveys the scene once, at the beginning of the method, to determine where the camera should move to. So, the result is that the camera likely doesn't know of the existence of your newly added node when it performs that survey.

You can likely resolve this by placing a short sleep into the background thread just before invoking moveWithDuration:toShowAllOf:withPadding:.

[NSThread sleepForTimeInterval: 0.1];

Another option would be to override the addChildNow: method in your custom CC3Scene implementation, so that it invokes the superclass implementation, and then moves the camera. The addChildNow: method is what runs on the rendering thread, as a result of the double-dispatch, to actually add the node to its parent.

However, that might get cumbersome if you're adding a lot of content to your scene, as it will cause the camera to bounce around each time something new is added. But then again, maybe that will make sense in your app.