gpt4 book ai didi

ios - Scenekit - 将 UIImage Material 添加到自定义几何体

转载 作者:行者123 更新时间:2023-12-01 22:51:51 26 4
gpt4 key购买 nike

我在 ARKit 中运行这个,如果你尝试一下,会出现三角形(非常高)并且 2 在相机的右侧,稍微平移。我遇到的问题是我无法将图像 Material 应用于对象,我可以更改它的颜色,但不能应用图像:

所以这有效:

triangle.firstMaterial?.diffuse.contents = UIColor.red

但这不是:

triangle.firstMaterial?.diffuse.contents = UIImage(named: "wallpaper.jpg")

以下是 Xcode9 中新的 ARKit 项目的完整 ViewController 代码:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

@IBOutlet var sceneView: ARSCNView!

override func viewDidLoad() {
super.viewDidLoad()

sceneView.delegate = self

let scene = SCNScene()

let triangle = SCNGeometry.triangleFrom(vector1: SCNVector3(x: 0.059511181, y: -0.181795284, z: -0.0939711183), vector2: SCNVector3(x: 0.113824688, y: -0.181780666, z: -0.0674673915), vector3: SCNVector3(x: 0.113824688, y: 1.8182193, z: -0.0674673915))

triangle.firstMaterial?.diffuse.contents = UIImage(named: "wallpaper.jpg")

let triangleNode = SCNNode(geometry: triangle)

triangleNode.name = "Triangle"

scene.rootNode.addChildNode(triangleNode)

sceneView.scene = scene

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Create a session configuration
let configuration = ARWorldTrackingSessionConfiguration()

// Run the view's session
sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}

}

extension SCNGeometry {
class func triangleFrom(vector1: SCNVector3, vector2: SCNVector3, vector3: SCNVector3) -> SCNGeometry {

let indices: [Int32] = [0, 1, 2]

let source = SCNGeometrySource(vertices: [vector1, vector2, vector3])

let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)

return SCNGeometry(sources: [source], elements: [element])
}
}

我唯一没有包含的是纹理图像,但下载的任何图像都可以。

谢谢

最佳答案

为了能够将纹理映射到几何体上,除了顶点的位置之外,SceneKit 还需要具有语义 .texcoord 的几何源。

关于ios - Scenekit - 将 UIImage Material 添加到自定义几何体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636517/

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