2

I'm mapping an SKScene with SKVideoNode onto one of the sides of a SCNBox. I'm struggling to get the aspect ratio of the video correct. Here's the code:

    NSURL * url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], @"preview.mp4"]];
    AVAsset * asset = [AVAsset assetWithURL:url];
    AVAssetTrack * track = [[asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    CGSize size = track.naturalSize;

    NSLog(@"size w is %f Size h is %f", size.width, size.height);

    float max = MAX(size.width, size.height);
    size = CGSizeMake(max, max);

    SKScene * videoScene = [SKScene sceneWithSize:size];
    videoScene.scaleMode = SKSceneScaleModeAspectFit;

    videoPlayer = [AVPlayer playerWithURL:url];

    videoNode = [SKVideoNode videoNodeWithAVPlayer:videoPlayer];
    videoNode.position = CGPointMake(size.width/2, size.height/2);
    videoNode.size = size;
    videoNode.yScale = -1;
    [videoNode play];
    [videoScene addChild:videoNode];

    for (int n = 0; n < 6; n++){

        SCNMaterial * material = [SCNMaterial material];
        material.diffuse.contents = (n == 4) ? videoScene : bodyImage;
        [materials addObject:material];

    }

It's ignoring any aspect ratio settings, like fit and fill. It just squashes it to fit the bounding box. Setting the bounding box bigger doesn't seem to help. Even at a fixed size relative to the video the aspect ratio is ignored.

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
  • 2
    Note that if you're supporting only iOS 11 and up, you don't need AVPlayer > SKVideoNode > SKScene > SCNMaterialProperty — you can set `diffuse.contents` to an AVPlayer directly. – rickster Jul 26 '18 at 17:50
  • I tried setting the player layer directly, but it wouldn't take. Do you know the code for this? – Johnny Rockex Jul 26 '18 at 21:53
  • 1
    Not player layer, just player — per [docs](https://developer.apple.com/documentation/scenekit/scnmaterialproperty/1395372-contents), `SCNMaterialProperty.contents` can take an `AVPlayer` object in iOS 11 and up. You might or might not have issues if that player is also connected to a player layer. – rickster Jul 26 '18 at 21:55
  • 1
    I ran into this problem too. Setting AVPlayer directly to diffuse.contents worked - thanks. But no fix for the aspect ratio problem. – Jonny Sep 27 '18 at 15:17
  • 1
    BTW this looks related: https://stackoverflow.com/questions/34928396/determine-size-aspect-ratio-of-video-for-skvideonode-in-scenekit?rq=1 will take a look later. – Jonny Sep 27 '18 at 15:36

0 Answers0