ios - 如何使用 SceneKit iOS 将背面图像添加为纹理
问题描述
我是 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
解决方案
我已经通过添加 Material array 来完成它。
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
推荐阅读
- youtube-iframe-api - YouTube 更新 API rel:0 弃用自定义编码的替代方案
- python - TensorFlow Convolution 返回 Nan
- maven - 使用变量作为键来访问 Maven 中文件的属性
- java - 如何在 sturts2 的迭代器标签内应用验证
- c# - 创建知道程序集中类的构造函数参数类型的 Roslyn C# 分析器
- angular - 在 Angular 5 中刷新屏幕后,ActivatedRoute.queryParams 未定义
- python - Python Pandas 基于需要过滤 DataFrame 的函数创建列
- regex - python正则表达式纠正以下代码
- .net - 从 SP 发起的 SSO 转换为 IdP 发起的 SSO
- javascript - 在javascript中循环一个未知的数据结构(数组/对象)