2

我正在尝试使用 SCNMaterial 属性快速为 3D 模型添加法线贴图。漫反射属性正在工作,但在屏幕上看不到包括正常属性在内的其他属性。当我调试以检查节点的材质是否包含正常属性时,它显示该属性与我添加的图像一起存在。我还在 SceneKit 编辑器中检查了我使用的正常图像是否正确,它可以正常工作。我已经添加了我正在使用的代码。

let node = SCNNode()
node.geometry = SCNSphere(radius: 0.1)
node.geometry!.firstMaterial!.diffuse.contents = UIColor.lightGray
node.geometry!.firstMaterial!.normal.contents = UIImage(named: "normal")
node.position = SCNVector3(0,0,0)
sceneView.scene.rootNode.addChildNode(node)

这是我得到的输出

截屏

我期待这样的事情 截屏

4

3 回答 3

2

我得到了解决方案。由于我没有启用 DefaultLighting,因此场景中没有照明。将此添加到代码中。

sceneView.autoenablesDefaultLighting = true
于 2019-12-24T15:55:17.800 回答
1

从截图来看,场景中似乎没有光照,或者材质对光照没有反应,因为球体没有着色。要使法线贴图正常工作,必须考虑光照,因为它会响应光照方向。您是否尝试过创建一个全新的 SCNMaterial 并使用它的属性?(IE https://developer.apple.com/documentation/scenekit/scnmaterial/lightingmodel似乎很有趣)

我会尝试设置

node.geometry!.firstMaterial!.lightingModel = .physicallyBased
于 2019-12-24T15:41:32.163 回答
0

尝试这个。

let scene =  SCNScene()
let sphere = SCNSphere(radius: 0.1)
let sphereMaterial = SCNMaterial()
sphereMaterial.diffuse.contents = UIImage(named: "normal.png")

let sphereNode = SCNNode()
sphereNode.geometry = sphere
sphereNode.geometry?.materials = [sphereMaterial]
sphereNode.position = SCNVector3(0.5,0.1,-1)

scene.rootNode.addChildNode(sphereNode)
sceneView.scene = scene
于 2019-12-24T13:17:59.163 回答