gpt4 book ai didi

ios - 如何使用 SceneKit iOS 添加背面图像作为纹理

转载 作者:行者123 更新时间:2023-11-29 05:31:09 26 4
gpt4 key购买 nike

我是 SceneKit 新手,创建了一个对象,我将图像作为纹理添加到对象中,但它在正面和背面都显示。我想要的是在其后面显示另一个图像。是否可以添加两个纹理为了一个物体?

    let scene = SCNScene()
var planet : SCNGeometry
let tempScene = SCNScene(named: "FrameOBJ.obj")
planet = tempScene!.rootNode.childNodes[0].geometry!
let material = SCNMaterial()

let url = URL(string: "www.imageasf.com/123.png")
let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
let image = UIImage(data: data!)

material.diffuse.contents = image
material.diffuse.wrapT = SCNWrapMode.mirror
material.diffuse.wrapS = SCNWrapMode.mirror
material.isDoubleSided = true
planet.materials = [material]
let planetNode = SCNNode(geometry: planet)
scene.rootNode.addChildNode(planetNode)
sceneView.cameraControlConfiguration.allowsTranslation = true
sceneView.allowsCameraControl = true
sceneView.scene = scene

Front View Back View

最佳答案

我是通过添加 Material 数组来完成的。

    let tempScene = SCNScene(named: "untitled.obj")
planet = tempScene!.rootNode.childNodes[0].geometry!

var image = UIImage()
let url = URL(string: "https://www.images123.jpg")!
//make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
var node = SCNNode()

do {
let data = try Data(contentsOf: url)
image = UIImage(data: data)!
node = SCNNode(geometry: SCNBox(width: 0.05, height: 1, length:1, chamferRadius: 0.5))
node.geometry?.firstMaterial?.diffuse.contents = image
// do something with data
// if the call fails, the catch block is executed
} catch {
print(error.localizedDescription)
}

let allMaterial = SCNMaterial()
allMaterial.diffuse.contents = UIImage(named: "woods.png")

let smallMaterial1 = SCNMaterial()
smallMaterial1.diffuse.contents = image
node.scale = SCNVector3(0.5,0.5,0.5)
node.geometry?.materials = [smallMaterial1,allMaterial,smallMaterial1,smallMaterial1,smallMaterial1,smallMaterial1];

scene.rootNode.addChildNode(node)
print(node.scale)
print(scene.rootNode.scale)
sceneView.cameraControlConfiguration.allowsTranslation = true
sceneView.allowsCameraControl = true
sceneView.scene = scene

关于ios - 如何使用 SceneKit iOS 添加背面图像作为纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553075/

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