如何从代码在 SceneKit 中设置基于物理的渲染?

Posted

技术标签:

【中文标题】如何从代码在 SceneKit 中设置基于物理的渲染?【英文标题】:How to setup physical based rendering in SceneKit from code? 【发布时间】:2018-11-11 18:11:30 【问题描述】:

我想用 PBR 呈现一些场景。我创建了金属度和粗糙度纹理,并希望将其应用于网格。当我尝试在 Xcode 中执行此操作时 - 一切都很好。我可以将此模型添加到场景中,然后将此纹理添加到模型中。

但我想以编程方式进行。这是我的代码:

NSData *vertices = [[NSData alloc] initWithBytes:modelPly->vertices length:modelPly->vertexCount * 3 * sizeof(float)];
SCNGeometrySource *vertexSource = [SCNGeometrySource geometrySourceWithData:vertices
                                                                   semantic:SCNGeometrySourceSemanticVertex
                                                                vectorCount:modelPly->vertexCount
                                                            floatComponents:YES
                                                        componentsPerVector:3
                                                          bytesPerComponent:sizeof(float)
                                                                 dataOffset:0
                                                                 dataStride:sizeof(float) * 3];

// Normal source
NSData *normals = [[NSData alloc] initWithBytes:modelPly->normals length:modelPly->vertexCount * 3 * sizeof(float)];
SCNGeometrySource *normalSource = [SCNGeometrySource geometrySourceWithData:normals
                                                                   semantic:SCNGeometrySourceSemanticNormal
                                                                vectorCount:modelPly->vertexCount
                                                            floatComponents:YES
                                                        componentsPerVector:3
                                                          bytesPerComponent:sizeof(float)
                                                                 dataOffset:0
                                                                 dataStride:sizeof(float) * 3];


// Texture coordinates source
NSData *facesTextures = [[NSData alloc] initWithBytes:modelPly->texCoord length:modelPly->vertexCount * 2 * sizeof(float)];
SCNGeometrySource *texcoordSource = [SCNGeometrySource geometrySourceWithData:facesTextures
                                                                     semantic:SCNGeometrySourceSemanticTexcoord
                                                                  vectorCount:modelPly->vertexCount
                                                              floatComponents:YES
                                                          componentsPerVector:2
                                                            bytesPerComponent:sizeof(float)
                                                                   dataOffset:0
                                                                   dataStride:sizeof(float) * 2];


NSData *indicesData = [NSData dataWithBytes:modelPly->indices length:modelPly->indexCount * sizeof(uint) * 3];
SCNGeometryElement *elements = [SCNGeometryElement geometryElementWithData:indicesData
                                                            primitiveType:SCNGeometryPrimitiveTypeTriangles
                                                           primitiveCount:modelPly->indexCount
                                                            bytesPerIndex:sizeof(uint)];

SCNGeometry *geometry = [SCNGeometry geometryWithSources:@[vertexSource, normalSource, texcoordSource] elements:@[elements]];
MDLMesh *mesh = [MDLMesh meshWithSCNGeometry:geometry];

然后我正在尝试创建材料:

MDLPhysicallyPlausibleScatteringFunction *scatFunction = [MDLPhysicallyPlausibleScatteringFunction new];
MDLMaterial *mdlMaterial = [[MDLMaterial alloc] initWithName:@"material" scatteringFunction:scatFunction];
[mdlMaterial removeAllProperties];
MDLMaterialProperty *bcProperty = [[MDLMaterialProperty alloc] initWithName:@"BaseColor" semantic:MDLMaterialSemanticBaseColor URL:plyItem.textureFileURL];
MDLMaterialProperty *metalnessProperty = [[MDLMaterialProperty alloc] initWithName:@"metallic" semantic:MDLMaterialSemanticMetallic URL:[[NSBundle mainBundle] URLForResource:@"metalness" withExtension:@"jpg"]];
MDLMaterialProperty *roughnessProperty = [[MDLMaterialProperty alloc] initWithName:@"roughness" semantic:MDLMaterialSemanticRoughness URL:[[NSBundle mainBundle] URLForResource:@"roughness" withExtension:@"jpg"]];

[mdlMaterial setProperty:bcProperty];
[mdlMaterial setProperty:metalnessProperty];
[mdlMaterial setProperty:roughnessProperty];

并将其应用于我的几何体:

SCNNode *node = [SCNNode nodeWithMDLObject:mesh];
SCNMaterial *material = [SCNMaterial materialWithMDLMaterial:mdlMaterial];
material.lightingModelName = SCNLightingModelPhysicallyBased;
node.geometry.firstMaterial = material;

如果我签入调试器,mdlMaterial 包含有关粗糙度和金属度的所有信息,但 ma​​terial 不包含。怎么了?

【问题讨论】:

【参考方案1】:

通过几何槽中的材质分配它:

// Swift 5.3
let sphereNode = SCNNode(geometry: SCNSphere(radius: 1))
let material = sphereNode.geometry?.firstMaterial
material?.roughness.contents = 0.72
material?.metalness.contents = 0.33
material?.lightingModel = .physicallyBased

// Objective-C
sphereNode.geometry.firstMaterial.roughness.contents = @0.72;
sphereNode.geometry.firstMaterial.metalness.contents = @0.33;
sphereNode.geometry.firstMaterial.lightingModelName = SCNLightingModelPhysicallyBased;

【讨论】:

【参考方案2】:

仅通过 SCNGeometry 设法做到这一点。

SCNNode *node = [SCNNode nodeWithGeometry:geometry];
node.geometry.firstMaterial.diffuse.contents = plyItem.textureFileURL;
node.geometry.firstMaterial.metalness.contents = [[[NSBundle mainBundle] URLForResource:@"metalness" withExtension:@"jpg"] path];
node.geometry.firstMaterial.roughness.contents = [[[NSBundle mainBundle] URLForResource:@"roughness" withExtension:@"jpg"] path];
node.geometry.firstMaterial.lightingModelName = SCNLightingModelPhysicallyBased;

【讨论】:

以上是关于如何从代码在 SceneKit 中设置基于物理的渲染?的主要内容,如果未能解决你的问题,请参考以下文章

如何设置 SceneKit 碰撞检测

如何在 Spring Boot 中设置基于预认证头的认证?

在 OSX 上的 C 代码中设置工作目录

在Windows7系统中设置虚拟内存大小

如何在基于 MFC 对话框的应用程序中设置主对话框的大小

如何从代码中设置 PagerTitleStrip 的文本颜色、文本大小?