我希望SWIFT / Scenekit中的所有对象都能在地板上留下阴影

时间:2018-01-10 08:43:53

标签: ios swift xcode scenekit arkit

我正在使用swift进行ARKit项目。

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

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

在swift中,我创建了光和平面,如下所示。我不再使用现场,因为场景太大了。这就是我创造一个非常大的飞机的原因。我将它们添加到灯光的环境和方向。环境因此它看起来不是黑色和方向,所以阴影显示。事实并非如此。物体看起来很奇怪,没有阴影。

send_keys(user_id)

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

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

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

我希望你能帮助我!谢谢

编辑:

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

代码:

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)

2 个答案:

答案 0 :(得分:3)

我正在玩你的照明设置,发现环境光给它一个非常褪色的外观......&阴影似乎太不自然了。

null_resource provider

Anwway我将您的灯光设置配置调整为以下内容。我一起摆脱了环境光,我在对象节点(木盒子)上添加了一个约束就是这种情况。我还用一个方向光来控制照明强度。

        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)

答案 1 :(得分:1)

问题是我实际上并没有使用这种材料。工作守则:

 let worldGroundPlaneGeometry = SCNFloor()
    let worldGroundPlane = SCNNode()
    let worldGroundMaterial = SCNMaterial()
    worldGroundMaterial.lightingModel = .constant
    worldGroundMaterial.writesToDepthBuffer = true
    worldGroundMaterial.colorBufferWriteMask = []
    worldGroundMaterial.isDoubleSided = true
    worldGroundPlaneGeometry.materials = [worldGroundMaterial]

    worldGroundPlane.geometry = worldGroundPlaneGeometry        
    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?.shadowRadius = 16
    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)
    directionalNode.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)

    // Add the lights to the container

    self.addChildNode(ambientLight)
    self.addChildNode(directionalNode)