Crash on MDLLightProbe

77 views Asked by At

I was trying to bake an MDLLightProbe but the app is crashing. I have stripped down the code to bare minimum but still getting the same crash. Attaching the code and the stack.

    SCNNode *ambientLight = [SCNNode node];
    ambientLight.light = [SCNLight light];
    ambientLight.light.type = SCNLightTypeAmbient;
    ambientLight.light.color = [NSColor whiteColor];
    ambientLight.light.intensity = 1000.0;

    MDLLight *light = [MDLLight lightWithSCNLight:ambientLight.light];
    NSArray<MDLLight*> *lights = [NSArray arrayWithObjects: light, nil];

    
    MDLTransform* t = [[MDLTransform alloc] initWithIdentity];

    SCNNode* sph = [SCNNode node];
    sph.geometry = [SCNSphere sphereWithRadius:1.];
    sph.position = SCNVector3Make(1., 0.1, 0.);
    sph.geometry.firstMaterial.diffuse.contents = [NSColor grayColor];

    NSArray<MDLObject*> *obs = [NSArray arrayWithObjects: sph, nil];
    MDLLightProbe *probe2 = [MDLLightProbe lightProbeWithTextureSize:256 forLocation:t lightsToConsider: lights objectsToConsider:obs reflectiveCubemap:nil irradianceCubemap:nil];
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x66736e617288)
    frame #0: 0x00007ff803415bdd libobjc.A.dylib`objc_msgSend_stret + 29
    frame #1: 0x00007ff90b3bb094 SceneKit`-[SCNNode transform] + 38
    frame #2: 0x00007ff9169e5b53 ModelIO`___lldb_unnamed_symbol16530$$ModelIO + 107
    frame #3: 0x00007ff9169e5c25 ModelIO`___lldb_unnamed_symbol16531$$ModelIO + 9
    frame #4: 0x00007ff916a07f5d ModelIO`___lldb_unnamed_symbol16765$$ModelIO + 459
    frame #5: 0x00007ff91691eff7 ModelIO`___lldb_unnamed_symbol14669$$ModelIO + 552

xcode 13.3.1 macOS 12.3.1 . Any help is appreciated. Thanks!

1

There are 1 answers

0
Larme On BEST ANSWER

I don't use theses libs, but after checking the doc:

The method you use is:

+ (MDLLightProbe *)lightProbeWithTextureSize:(NSInteger)textureSize 
                                 forLocation:(MDLTransform *)transform 
                            lightsToConsider:(NSArray<MDLLight *> *)lightsToConsider 
                           objectsToConsider:(NSArray<MDLObject *> *)objectsToConsider 
                           reflectiveCubemap:(MDLTexture *)reflectiveCubemap 
                           irradianceCubemap:(MDLTexture *)irradianceCubemap;

So in objectsToConsider, it awaits for a NSArray of MDLObject.

So, in order to do so, you created:

NSArray<MDLObject*> *obs = [NSArray arrayWithObjects: sph, nil];

But the thing, is that Objective-C doesn't really check the type of objects you put in an NSArray. It's more a "for your information".

Here, sph is a SCNNode, not a MDLObject. Let's check if there is a common class between them:

@interface SCNNode : NSObject
@interface MDLObject : NSObject

There are different. So you need to create a MDLObject from a SCNNode.

And there is one: objectWithSCNNode:

So it should be:

NSArray<MDLObject*> *obs = [NSArray arrayWithObjects: [MDLObject objectWithSCNNode:sph], nil];

Unrelated, but with modern Objective-C, you could write @[light]; instead if [NSArray arrayWithObjects: light, nil];, might be "simpler" to read.