Duck
Duck

Reputation: 36013

SCNText getBoundingBoxMin:max: always zero

I am trying to determine the bounding box of a SCNText but getBoundingBoxMin:max: always gives me zero.

This is the code that runs inside a SCNText class extension, so self is the [textNode geometry].

  [self setString:newText];
  SCNVector3 min = SCNVector3Zero;
  SCNVector3 max = SCNVector3Zero;

  SCNNode *textNode = [SCNNode nodeWithGeometry:self];
  [textNode getBoundingBoxMin:&min max:&max];

  CGSize sizeMax = CGSizeMake( max.x - min.x,
                               max.y - min.y);

I have also tried this

  [self setString:newText];
  SCNVector3 min = SCNVector3Zero;
  SCNVector3 max = SCNVector3Zero;

  [self getBoundingBoxMin:&min max:&max];

  CGSize sizeMax = CGSizeMake( max.x - min.x,
                               max.y - min.y);

sizeMax is always zero.

NOTE: I have discovered why the problem happens.

The problem happens when this adjust is called inside a block like this:

  dispatch_async(dispatch_get_main_queue(),
                 ^{                  });

so, If I call this code from the main thread, it works:

[self setString:newText];
SCNVector3 min = SCNVector3Zero;
SCNVector3 max = SCNVector3Zero;

SCNNode *textNode = [SCNNode nodeWithGeometry:self];
[textNode getBoundingBoxMin:&min max:&max];

CGSize sizeMax = CGSizeMake( max.x - min.x,
                             max.y - min.y);

but If I call this from another thread it will not work

dispatch_async(dispatch_get_main_queue(),
             ^{                
[self setString:newText];
SCNVector3 min = SCNVector3Zero;
SCNVector3 max = SCNVector3Zero;

SCNNode *textNode = [SCNNode nodeWithGeometry:self];
[textNode getBoundingBoxMin:&min max:&max];

CGSize sizeMax = CGSizeMake( max.x - min.x,
                             max.y - min.y);

});

the problem is that this block is called from a dispatch block, so I need to re-dispatch it to the main queue but doing that prevents the code from working. In theory dispatching a block to the main queue should be equivalent of running it from the main thread but apparently it is not.

Do you guys know any workaround?

Upvotes: 1

Views: 473

Answers (1)

mnuages
mnuages

Reputation: 13462

from comment

You can try to wrap that in a transaction:

[SCNTransaction begin];
[SCNTransaction setAnimationDuration:0];
...
[SCNTransaction commit];

Upvotes: 0

Related Questions