gpt4 book ai didi

swift - 给 SCNPlane 添加阴影

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:01 24 4
gpt4 key购买 nike

我有 ARKit 艺术品应用程序。我希望我的作品在其背后有一个阴影。阴影不能是动态的。只是静态阴影。

我试图在艺术品后面添加一个平面。并为其设置阴影。但我发现这并不容易。

你能帮帮我吗?

let shadow = SCNPlane(width: artwork.size.width * 0.0254 + 0.03, height: artwork.size.height * 0.0254 + 0.015)

let layer = CALayer()
layer.frame = CGRect(origin: .zero, size: CGSize(width: shadow.width, height: shadow.height))
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 1
layer.shadowOffset = .zero
layer.shadowRadius = 10

shadow.firstMaterial?.diffuse.contents = layer

阴影应该与 UIKit 的 UIView 类似。但是这段代码根本行不通。

我需要像 this 这样的东西

最佳答案

已更新

为了从 3D 灯光转换和接收阴影,您必须使用 3D 基元(如立方体、球体、圆柱体等),或格式为 .usdz 的 3D 模型, .dae.obj

可见平面上的阴影。

使用以下代码测试光线如何为 3D 几何体生成阴影:

let scene = SCNScene()

let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = .spot
lightNode.light!.castsShadow = true
lightNode.light!.shadowMode = .deferred
lightNode.rotation = SCNVector4(x: -1, y: 0, z: 0, w: CGFloat.pi/2)
lightNode.position = SCNVector3(x: 0, y: 20, z: 0)
scene.rootNode.addChildNode(lightNode)

let sphereNode = SCNNode()
sphereNode.geometry = SCNSphere(radius: 2)
sphereNode.position = SCNVector3(x: 0, y: -2, z: 0)
scene.rootNode.addChildNode(sphereNode)

let planeNode = SCNNode()
planeNode.geometry = SCNPlane(width: 15, height: 15)
planeNode.position = SCNVector3(x: 0, y: -5, z: 0)
planeNode.rotation = SCNVector4(x: -1, y: 0, z: 0, w: CGFloat.pi/2)

planeNode.geometry?.materials.first?.diffuse.contents = UIColor.red

scene.rootNode.addChildNode(planeNode)

不可见平面上的阴影。

如果您需要一个接收阴影不可见平面,请使用以下代码:

planeNode.geometry?.materials.first?.colorBufferWriteMask = []
planeNode.geometry?.materials.first?.writesToDepthBuffer = true
planeNode.geometry?.materials.first?.lightingModel = .constant

假影子。

如果你想在几何体上使用一个写成 .png(png 文件格式可以容纳 4 个 channel )的假阴影纹理(带有预乘的 alpha channel – RGB x A),使用以下方法:

planeNode.geometry?.materials.first?.diffuse.contents = UIImage(named: "shadow.png")

lightNode.light!.castsShadow = false

这是 my answer在假阴影上。

这是 .png 格式的预乘阴影(只需将其拖放到桌面上):

enter image description here

You can change its size and transparency and, of course, you can blur it.

关于swift - 给 SCNPlane 添加阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56692274/

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