ios - 如何使用 Swift 在 SceneKit 中创建一个圆盘形光源

标签 ios swift scenekit scnlight

我希望创建一个类似于太阳使用 SceneKit 照亮地球的方式的光源。这是一个业余天文类(class)项目。由于太阳比地球大,全向 SCNLight 是行不通的,因为它发出的光是从一个点发出的。从太阳发出的光基本上是从一个非常大的球体发出的,而不是一个点。

该图像是使用全向光源创建的,但并未真实地显示地球的阴影区域。具体来说,北极没有点亮,但应该点亮(在这种情况下,我们在夏天。picture

第二张图像更逼真,您可以看到北极被照亮,就像在夏天一样。

enter image description here

问题是要获得第二张图像,我必须非常繁琐地定位定向光才能使图像正确(这是我手动完成的。对于第一张图像,我只是将全向光定位在与太阳相同的位置领域)。由于整个事情都是为了动画,使用定向光并且必须随着地球沿着其轨道前进而不断地重新定位它,这将需要一些相当复杂的数学计算。

所以我想创建一个使用以编程方式创建的模型 I/O 灯对象初始化的 SCNLight。根据 Apple 的“文档”,在 Swift 中,可以使用 initializer from a specified model I/O light object 创建 SCNLight ,据我所知允许创建 "light source [that] illuminates a scene in all directions from an area in the shape of a disc"

“文档”说明了“创建灯光”的以下内容:

init(mdlLight: MDLLight)

它被定义为一个方便的初始化程序:

convenience init(mdlLight: MDLLight)

我希望能够做到以下几点:
let lightModelObject = MDLLight()
lightModelObject.lightType = .discArea

let discShapedSceneLight = SCNLight(lightModelObject) //cannot convert value ... error.

但最后一条语句让我得到一个:“无法将‘MDLLight’类型的值转换为预期的参数类型‘NSCoder’”错误。我也试过:
let discShapedSceneLight = SCNLight.init(lightModelObject)

但也没有运气。

我完全被困住了!感觉就像在 Swift 中使用初始化器有一些基本的东西我不明白。

任何评论将不胜感激。

编辑 :我还在objective-C中尝试了以下内容,根据相同的文档:
#import <ModelIO/MDLLight.h>
...
SCNLight *discLight = [SCNLight light];
MDLPhysicallcPlausibleLight *ppl = [MDLPhysicallyPlausibleLight lightWithSCNLight:discLight];

但收到此错误:“选择器'lightWithSCNLight:'没有已知的类方法”

编辑 :
感谢@EmilioPelaez 解决了这个问题。

我已经将带有所需照明的完整代码放在下面,也许它会对其他人有所帮助。
import UIKit
import QuartzCore
import SceneKit
import SceneKit.ModelIO

class GameViewController: UIViewController {

    var scnView:SCNView!
    var scnScene:SCNScene!
    var cameraNode:SCNNode!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()
        setupScene()
        setupCamera()
        drawSolarSystem()
    }

    func setupView() {
        scnView = self.view as! SCNView
        scnView.showsStatistics = true
        scnView.allowsCameraControl = true
        scnView.autoenablesDefaultLighting = false
    }

    func setupScene() {
        scnScene = SCNScene()
        scnView.scene = scnScene
        scnScene.background.contents = UIColor.systemBlue
    }

    func setupCamera() {
        cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
        scnScene.rootNode.addChildNode(cameraNode)
    }

    func drawSolarSystem() {
      var geometryObject:SCNGeometry

//SUN object and light source
        let sunX:Float = -3.5   ///position a little off to the left of center

        //create the material for the Sun's surface
        let sunMaterial = SCNMaterial()
        sunMaterial.emission.contents = UIColor.yellow          ///the color the material emits.
//        sunMaterial.transparency = 0.3
//        sunMaterial.diffuse.contents = UIColor.systemYellow ///the color the material reflects when lit.

        //create the Sphere and assign it the material
        geometryObject = SCNSphere(radius: 1.0)
        geometryObject.firstMaterial=sunMaterial

        //create the node and assign it the geometry (object) previously created.
        let sunNode = SCNNode(geometry: geometryObject)
        sunNode.position = SCNVector3(x:sunX, y:0, z:0)
        scnScene.rootNode.addChildNode(sunNode)

        //create the light source and position it same place as the sun
        //create an MDLAreaLight, since the "normal" SCNLight types - such as omni - are not suitable.
        //The .omni type emanates from a point, and so doesn't correctly represent the sun lighting the earth
         let lightModelObject = MDLAreaLight()
         lightModelObject.lightType = .discArea
//         lightModelObject.areaRadius = 5.01 ///This doesn't appear to affect the light.

        //create the node and assign it the MDLAreaLight
         let sunLightNode = SCNNode()
         sunLightNode.light = SCNLight(mdlLight:lightModelObject)
         sunLightNode.light?.color = UIColor .white
         sunLightNode.position = SCNVector3(x:sunX, y:0, z:0)
         scnScene.rootNode.addChildNode(sunLightNode)


        //EARTH EQUATORIAL PLANE but centered on the Sun
        let floorObject = SCNFloor()
        floorObject.reflectivity = 0
        floorObject.width = 2
        floorObject.length = 3
        let earthEquatorialPlaneNode = SCNNode(geometry: floorObject)
        earthEquatorialPlaneNode.position = SCNVector3(x:sunX, y:0, z:0)
        scnScene.rootNode.addChildNode(earthEquatorialPlaneNode)


//EARTH main node - node with 2 subnodes, one sphere and one axis
        ///a node can only have a single geometry object attached. In order to attach multiple geometries, create a (parent) node without any geometry, and then attach subnodes with one geometry each.

        //The parent node
        let earthNode = SCNNode()
        earthNode.position = SCNVector3(x: 0, y:-1.2, z:0)
        scnScene.rootNode.addChildNode(earthNode)

        //the child node for the earth axis of rotation object
        geometryObject = SCNCylinder(radius: 0.01, height: 1.2)
        let earthAxisNode = SCNNode(geometry: geometryObject)
        earthNode.addChildNode(earthAxisNode)

        //the child node for the earth sphere object
        geometryObject = SCNSphere(radius: 0.5)
        let earthSphereNode = SCNNode(geometry: geometryObject)
        earthNode.addChildNode(earthSphereNode)

        //put some meridians and an equator onto the sphere.
        let earthSphereMaterial = SCNMaterial()
        geometryObject.firstMaterial = earthSphereMaterial
        earthSphereMaterial.diffuse.contents = "coordinateGrid.png"
        earthSphereMaterial.lightingModel = .lambert

    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

}

最佳答案

如果您添加 import SceneKit.ModelIO您应该能够使用当前不起作用的初始化程序。

关于ios - 如何使用 Swift 在 SceneKit 中创建一个圆盘形光源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62395798/

相关文章:

ios - 为 Notification.Name 创建扩展

ios - 在 ARC 中,dealloc 方法调用包含对 self 的弱引用的方法/ block 导致 weakSelf = nil

ios - 如何在 Storyboard中从一个 View 转到另一个 View

swift - 如何在 Swift 中以字符串格式 "English"(不是 "en")显示当前语言?

ios - 调用 setWorldOrigin 时子节点不可见?

ios - 打印数组中具有特定字符串的所有元素

swift - 在 ARSCNView 中显示 "ARAnchor"

ios - 如何在 swift 中维护 View 层次结构

scenekit - 场景套件覆盖SCNNode渲染

swift - 如何根据用户手势在 SceneKit 中正确实现 throw 弹丸?