gpt4 book ai didi

swift - 如何通过代码在 SceneKit 中设置基于物理的渲染?

转载 作者:可可西里 更新时间:2023-11-01 01:57:32 26 4
gpt4 key购买 nike

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

然后我尝试创建 Material :

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 没有。怎么了?

最佳答案

通过位于 SCNGeometry 中的任何 Material 槽分配它:

// Swift 5.6
let sphereNode = SCNNode(geometry: SCNSphere(radius: 1))
let material = sphereNode.geometry?.firstMaterial
material?.roughness.contents = 0.12
material?.metalness.contents = 0.95
material?.lightingModel = .physicallyBased

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

这是一个用 Objective-C 编写的用于测试的 macOS 代码示例:

#import "GameViewController.h"

@implementation GameViewController

- (void)viewDidLoad
{
[super viewDidLoad];

SCNView *sceneView = (SCNView *)self.view;
SCNScene *scene = [SCNScene scene];
sceneView.scene = scene;
sceneView.allowsCameraControl = YES;
sceneView.autoenablesDefaultLighting = YES;
sceneView.backgroundColor = [NSColor blackColor];

SCNMaterial *material = [SCNMaterial material];
material.lightingModelName = SCNLightingModelPhysicallyBased;
material.diffuse.contents = [NSImage imageNamed:@"YourPicture"];
material.metalness.contents = [NSColor whiteColor];
material.roughness.contents = [NSColor blackColor];

SCNNode *sphere = [SCNNode node];
sphere.geometry = [SCNSphere sphereWithRadius:0.25];
sphere.geometry.firstMaterial = material;
[scene.rootNode addChildNode:sphere];
}
@end

关于swift - 如何通过代码在 SceneKit 中设置基于物理的渲染?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50646173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com