gpt4 book ai didi

ios - 我希望 SWIFT/Scenekit 中的所有对象在地板上都有阴影

转载 作者:可可西里 更新时间:2023-11-01 00:33:39 26 4
gpt4 key购买 nike

我正在 swift 开发一个 ARKit 项目。

在 SWIFT 中,我将对象添加到场景中。我想对这些对象做的是在它们下面显示一个阴影,使它们更逼真。我尝试了一些东西,但都没有用。我会在这里解释。

请理解我对 swift 还是个新手。我在 SWIFT 中以编程方式创建对象。我想我可以通过在所有物体下方创建一个不可见的平面并在场景上方放置一盏灯来做到这一点。我了解到,通过取消选中“写入颜色”值红色、绿色、蓝色和 alpha,我可以在 Xcode 的场景编辑器中的不可见平面上转换阴影。我在它上面放了一个聚光灯,它起作用了。现在我想以编程方式执行此操作。

我 swift 创建了灯光和平面,如下所示。我不再使用点了,因为场景太大了。这就是为什么我创造了一架非常大的飞机。我将它们添加到光的环境光和方向光中。环境所以它在侧面看起来不是黑色和方向性所以显示阴影。这不是。这些物体看起来奇怪地发光并且没有阴影。

let worldGroundPlaneGeometry = SCNPlane(width: 1000, height: 1000)
worldGroundPlaneGeometry.firstMaterial?.colorBufferWriteMask = SCNColorMask(rawValue: 0)
let worldGroundPlane = SCNNode()

worldGroundPlane.geometry = worldGroundPlaneGeometry
worldGroundPlane.position = worldPosition
worldGroundPlane.castsShadow = true
worldGroundPlane.eulerAngles = SCNVector3(Float.pi / 2, 0, 0)
self.addChildNode(worldGroundPlane)

// Create a ambient light
let ambientLight = SCNNode()
ambientLight.light = SCNLight()
ambientLight.light?.color = UIColor.white
ambientLight.light?.type = SCNLight.LightType.ambient
ambientLight.position = SCNVector3(x: 0,y: 5,z: 0)

// Create a directional light node with shadow
let directionalNode = SCNNode()
directionalNode.light = SCNLight()
directionalNode.light?.type = SCNLight.LightType.directional
directionalNode.light?.color = UIColor.white
directionalNode.light?.castsShadow = true
directionalNode.light?.automaticallyAdjustsShadowProjection = true
directionalNode.light?.shadowSampleCount = 64
directionalNode.light?.shadowMode = .deferred
directionalNode.light?.shadowMapSize = CGSize(width: 2048, height: 2048)
directionalNode.light?.shadowColor = UIColor.black.withAlphaComponent(0.75)
directionalNode.position = SCNVector3(x: 0,y: 5,z: 0)

// Add the lights to the container
self.addChildNode(ambientLight)
self.addChildNode(directionalNode)

它需要在整个场景中看起来像这样:

附言如果我在应用程序内的手机上渲染对象和阴影,它看起来是一样的。

我希望你能帮助我找到实现上述目标的解决方案。

希望你能帮帮我!谢谢😊

编辑:

下面提供的解决方案不是解决方案。现在我的场景看起来像这样,仍然没有阴影:

代码:

let worldGroundPlaneGeometry = SCNPlane(width: 1000, height: 1000)
let worldGroundPlane = SCNNode()
worldGroundPlane.geometry?.firstMaterial?.lightingModel = .constant
worldGroundPlane.geometry?.firstMaterial?.writesToDepthBuffer = true
worldGroundPlane.geometry?.firstMaterial?.colorBufferWriteMask = []
worldGroundPlane.geometry = worldGroundPlaneGeometry
worldGroundPlane.position = worldPosition
worldGroundPlane.castsShadow = true
worldGroundPlane.eulerAngles = SCNVector3(Float.pi / 2, 0, 0)
self.addChildNode(worldGroundPlane)

// Create a ambient light
let ambientLight = SCNNode()
ambientLight.light = SCNLight()
ambientLight.light?.shadowMode = .deferred
ambientLight.light?.color = UIColor.white
ambientLight.light?.type = SCNLight.LightType.ambient
ambientLight.position = SCNVector3(x: 0,y: 5,z: 0)

// Create a directional light node with shadow
let directionalNode = SCNNode()
directionalNode.light = SCNLight()
directionalNode.light?.type = SCNLight.LightType.directional
directionalNode.light?.color = UIColor.white
directionalNode.light?.castsShadow = true
directionalNode.light?.automaticallyAdjustsShadowProjection = true
directionalNode.light?.shadowSampleCount = 64
directionalNode.light?.shadowMode = .deferred
directionalNode.light?.shadowMapSize = CGSize(width: 2048, height: 2048)
directionalNode.light?.shadowColor = UIColor.black.withAlphaComponent(0.75)
directionalNode.position = SCNVector3(x: 0,y: 5,z: 0)

// Add the lights to the container
self.addChildNode(ambientLight)
self.addChildNode(directionalNode)

最佳答案

我在玩弄你的照明设置,发现环境光使它看起来很褪色......而且阴影似乎太暗了。

enter image description here

无论如何,我将您的灯光设置配置调整为以下内容。我一起摆脱了环境光,并且在这种情况下我在对象节点(木箱)上添加了一个约束。我还只用一盏定向灯控制照明强度。

        let directionalNode = SCNNode()
let constraint = SCNLookAtConstraint(target:node)
directionalNode.light = SCNLight()
directionalNode.light?.type = .directional
directionalNode.light?.color = UIColor.white
directionalNode.light?.castsShadow = true
directionalNode.light?.intensity = 2000
directionalNode.light?.shadowRadius = 16
directionalNode.light?.shadowMode = .deferred
directionalNode.eulerAngles = SCNVector3(Float.pi/2,0,0)
directionalNode.light?.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3)
directionalNode.position = SCNVector3((node?.position.x)! + 10,(node?.position.y)! + 30,(node?.position.z)!+30)
directionalNode.constraints = [constraint]
// Add the lights to the container
self.sceneView.scene.rootNode.addChildNode(directionalNode)

关于ios - 我希望 SWIFT/Scenekit 中的所有对象在地板上都有阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48183385/

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