gpt4 book ai didi

ios - 如何在曲面上添加阴影?

转载 作者:行者123 更新时间:2023-11-28 05:43:23 24 4
gpt4 key购买 nike

我有一个问题,在 ARView 中显示物体上的阴影但没有显示在表面上,我附上了尝试代码?它正在工作 在对象上显示阴影但不显示在表面上。 Show Image

代码:

var light2 = SCNLight()
var lightNodeb2 = SCNNode()
light2.castsShadow = true
light2.automaticallyAdjustsShadowProjection = true
light2.maximumShadowDistance = 40.0
light2.orthographicScale = 1
light2.type = .directional
light2.shadowMapSize = CGSize(width: 2048, height: 2048)
light2.shadowMode = .forward
light2.shadowSampleCount = 128
light2.shadowRadius = 3
light2.shadowBias = 32
light2.zNear=1;
light2.zFar=1000;
lightNodeb2.light = light2
lightNodeb2.position = SCNVector3(x: -1, y: 10, z: 1)
lightNodeb2.rotation = SCNVector4Make(2, 0, 0, -Float.pi / 3)
self.sceneView.scene.rootNode.addChildNode(lightNodeb2)

最佳答案

我在这里看到 2 个可能的问题:

您的灯光设置正常。我认为第一个问题如下:您在场景图中将编程光与“非编程”3D 对象一起使用。

检查一下。在此代码中,所有对象都是程序化的:

let sphereNode1 = SCNNode(geometry: SCNSphere(radius: 1))
sphereNode1.position = SCNVector3(x: 0, y: 5, z: 3)
scene.rootNode.addChildNode(sphereNode1)

let sphereNode2 = SCNNode(geometry: SCNSphere(radius: 3))
sphereNode2.position = SCNVector3(x: 0, y: 1, z: 0)
scene.rootNode.addChildNode(sphereNode2)

let boxNode = SCNNode(geometry: SCNBox(width: 20, height: 0.1, length: 20, chamferRadius: 0))
boxNode.position = SCNVector3(x: 0, y: -3, z: 0)
scene.rootNode.addChildNode(boxNode)

let light2 = SCNLight()
let lightNodeb2 = SCNNode()
light2.castsShadow = true
light2.automaticallyAdjustsShadowProjection = true
light2.maximumShadowDistance = 40.0
light2.orthographicScale = 1
light2.type = .directional
light2.shadowMapSize = CGSize(width: 2048, height: 2048)

light2.shadowMode = .deferred // Renders shadows in a postprocessing pass

light2.shadowSampleCount = 128
light2.shadowRadius = 3
light2.shadowBias = 32
light2.zNear = 1
light2.zFar = 1000
lightNodeb2.light = light2

// DIRECTIONAL LIGHT POSITION doesn't matter. Matters only its direction.
// lightNodeb2.position = SCNVector3(x: -1, y: 10, z: 1)

lightNodeb2.rotation = SCNVector4(2, 0, 0, -Float.pi/3)
scene.rootNode.addChildNode(lightNodeb2)

默认情况下,所有对象Shadow castings 都是On

enter image description here

第一个问题的解决方案:

To make your 3D model to become accessible for virtual programmatic lights use the following approach:

func childNode(withName name: String, recursively: Bool) -> SCNNode? {
return SCNNode()
}

let geometryNode = childNode(withName: "art.scnassets/your3Dmodel",
recursively: true)!

scene.rootNode.addChildNode(geometryNode)

第二个问题的解决方案:

And if you wanna have a hidden plane with a shadow use this code:

hiddenPlaneNode.castsShadow = false
hiddenPlaneNode.geometry?.firstMaterial?.lightingModel = .constant
hiddenPlaneNode.geometry?.firstMaterial?.isDoubleSided = true
hiddenPlaneNode.geometry?.firstMaterial?.readsFromDepthBuffer = true
hiddenPlaneNode.geometry?.firstMaterial?.writesToDepthBuffer = true
hiddenPlaneNode.geometry?.firstMaterial?.colorBufferWriteMask = []

light2.shadowMode = .deferred

关于ios - 如何在曲面上添加阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55773261/

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