如何正确使用NSImage作为SCNGeometry形状的材质?

时间:2019-04-27 14:47:57

标签: swift xcode macos

我在大学里完成了一项任务,为SCNGeometry八面体添加图片作为纹理。这是我在Swift中的第一个项目。

关于带有UIImage类的UIKit,有很多建议,但是我正在将AppKit用于macOS和NSImage类。而且我在网络上找到的所有选项都还不适用于我。可能我误解了一些基本知识。 好吧,首先我将一张名为“ sims.jpg”的图片拖放到我的项目文件夹和art.scnassets文件夹中。并且还通过文件→将文件添加到“ art.scnassets”和常规文件夹中来添加它们。而且对Assets.xcassets没有任何作用。

然后是如何创建形状:

func createOctahedron() {

   let vertices: [SCNVector3] = [
       SCNVector3(0, 1, 0),
       SCNVector3(-0.5, 0, 0.5),
       SCNVector3(0.5, 0, 0.5),
       SCNVector3(0.5, 0, -0.5),
       SCNVector3(-0.5, 0, -0.5),
       SCNVector3(0, -1, 0)
   ]

   let source = SCNGeometrySource(vertices: vertices)

   let indices: [UInt16] = [
       0, 1, 2,
       2, 3, 0,
       3, 4, 0,
       4, 1, 0,
       1, 5, 2,
       2, 5, 3,
       3, 5, 4,
       4, 5, 1
   ]

   let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
   let geometry = SCNGeometry(sources: [source], elements: [element])
   let node = SCNNode(geometry: geometry)

   node.geometry?.firstMaterial?.diffuse.contents = NSColor.green // назначаем цвет октаэдру

   let scnView = self.view as! SCNView
   scnView.scene?.rootNode.addChildNode(node)

   let rotateAction = SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: .pi, z: 0, duration: 5))
   node.runAction(rotateAction)
}

以防万一我留下一个full code

所以,我将像这样添加图像

let imageMaterial = SCNMaterial()
let image = NSImage.Name("sims")
imageMaterial.diffuse.contents = image
geometry.materials = [imageMaterial, imageMaterial, imageMaterial, imageMaterial, imageMaterial, imageMaterial, imageMaterial, imageMaterial]

还是那样?

node.geometry?.firstMaterial?.diffuse.contents = NSImage.Name("sims")

还是我应该以其他方式映射它?请帮我,因为我真的不明白。 Xcode输出的只是旋转的八面体,没有额外的纹理,也没有错误

2 个答案:

答案 0 :(得分:1)

此行:

let image = NSImage.Name("sims")

仅声明名称。您需要:

let image = NSImage(named: NSImage.Name("sims"))

您的代码进行编译的原因是contents属性的类型为Any?,因此您可以在该属性中放入任何旧的粘滞符。它无声地失败。

答案 1 :(得分:1)

我了解了这笔交易的内容。由于没有UV贴图,因此没有出现纹理。我应该借助CGPoint在段[0,1]上声明一个二维坐标数组,然后将其添加到source数组中。

这是大致的样子:

         let width:CGFloat = 1/8
         let coordinates: [CGPoint] = [
         CGPoint(x: 0,     y: 0),// BAC
         CGPoint(x: width, y: 0),
         CGPoint(x: width, y: 1),
         CGPoint(x: 0,     y: 1),// BAE
         CGPoint(x: width * 7, y: 0),
         CGPoint(x: 1,         y: 0),
         CGPoint(x: 1,         y: 1),// EDA
         CGPoint(x: width * 7, y: 1),
         CGPoint(x: width * 6, y: 0),
         CGPoint(x: width * 7, y: 0),// DAC
         CGPoint(x: width,     y: 0),
         CGPoint(x: width * 4, y: 0),
         CGPoint(x: width * 7, y: 1),// DFC
         CGPoint(x: width * 6, y: 1),
         CGPoint(x: width * 4, y: 1),
         CGPoint(x: width,     y: 1),// BFC
         CGPoint(x: width * 4, y: 0),
         CGPoint(x: width * 5, y: 0),
         CGPoint(x: width * 5, y: 1),// BFE
         CGPoint(x: width * 6, y: 1),
         CGPoint(x: width * 5, y: 0),
         CGPoint(x: width * 6, y: 0),// EFD
         CGPoint(x: width * 4, y: 1),
         CGPoint(x: width * 5, y: 1),
         ]

        let source = [
            SCNGeometrySource(vertices: vertices),
            SCNGeometrySource(textureCoordinates: coordinates)
        ]
        let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)

        let octahedron = SCNGeometry(sources: source, elements: [element])

指定坐标后,就足以显示以下内容:

        let image = NSImage(named: NSImage.Name("sims"))
        octahedron.firstMaterial?.diffuse.contents = image

图像将被拉伸到我们的八面体的脸上。 full code附加