ARKit - 在平面上放置 3D 对象的阴影 [英] ARKit –Drop a shadow of 3D object on the plane surface

查看:36
本文介绍了ARKit - 在平面上放置 3D 对象的阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来在平面上显示对象的函数.

This is the function that I use to display object on the plane surface.

private func loadScene(path: String) -> SCNNode {

    let spotLight = SCNLight()
    spotLight.type = SCNLight.LightType.probe

    spotLight.spotInnerAngle = 30.0
    spotLight.spotOuterAngle = 80.0
    spotLight.castsShadow = true

    let result = SCNNode()
    result.light = spotLight
    result.position = SCNVector3(-10.0, 20.0, 10.5)
    result.addChildNode(result)

    let scene = SCNScene(named: path)!
    for node in scene.rootNode.childNodes {
        result.addChildNode(node)
    }       
    return result
}

我想像这张图片一样在平面上显示阴影.

I want to display shadow on the plane surface like this image.

当我设置如下聚光灯类型时

When I set spotlight type like below

spotLight.type = SCNLight.LightType.directional

它以明暗阴影显示物体本身,不会在表面上留下阴影.

It shows the object itself with light/dark shadow and does not drop the shadow on the surface.

有人可以指导我如何实现如图所示的输出吗?

Can someone please guide me how can I achieve the output as shown in the image?

推荐答案

//在 3D 模型上添加阴影 只需复制粘贴此代码,它就会在地面上出现 3D 模型的阴影

// To Add Shadow on 3D Model Just Copy Paste this code and it will appear a shadow of 3D Model on Ground

let flourPlane = SCNFloor()
let groundPlane = SCNNode()
let groundMaterial = SCNMaterial()
groundMaterial.lightingModel = .constant
groundMaterial.writesToDepthBuffer = true
groundMaterial.colorBufferWriteMask = []
groundMaterial.isDoubleSided = true
flourPlane.materials = [groundMaterial]
groundPlane.geometry = flourPlane
//
mainNode.addChildNode(groundPlane)
// 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 myNode = SCNNode()
myNode.light = SCNLight()
myNode.light?.type = SCNLight.LightType.directional
myNode.light?.color = UIColor.white
myNode.light?.castsShadow = true
myNode.light?.automaticallyAdjustsShadowProjection = true
myNode.light?.shadowSampleCount = 64
myNode.light?.shadowRadius = 16
myNode.light?.shadowMode = .deferred
myNode.light?.shadowMapSize = CGSize(width: 2048, height: 2048)
myNode.light?.shadowColor = UIColor.black.withAlphaComponent(0.75)
myNode.position = SCNVector3(x: 0,y: 5,z: 0)
myNode.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)
// Add the lights to the container
mainNode.addChildNode(ambientLight)
mainNode.addChildNode(myNode)
// End

这篇关于ARKit - 在平面上放置 3D 对象的阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆