MDLLightProbe 崩溃

Crash on MDLLightProbe

我正在尝试烘焙 MDLLightProbe,但应用程序崩溃了。我已经将代码精简到最低限度,但仍然遇到同样的崩溃。附加代码和堆栈。

    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 。任何帮助表示赞赏。谢谢!

我不使用这些库,但在查看文档后:

您使用的方法是:

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

因此在 objectsToConsider 中,它等待 MDLObjectNSArray

因此,为了这样做,您创建了:

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

但问题是 Objective-C 并没有真正检查您放入 NSArray 中的对象类型。它更像是“供您参考”。

这里的sphSCNNode,不是MDLObject。让我们检查它们之间是否有共同的class:

@interface SCNNode : NSObject
@interface MDLObject : NSObject

有不同。所以你需要从 SCNNode.

创建一个 MDLObject

还有一个:objectWithSCNNode:

所以应该是:

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

无关,但对于现代 Objective-C,您可以写成 @[light];,如果 [NSArray arrayWithObjects: light, nil]; 可能更“容易”阅读。