4

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

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

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

在此处输入图像描述

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

所以我想创建一个使用以编程方式创建的模型 I/O 灯光对象初始化的 SCNLight。根据 Apple 的“文档”,在 Swift 中,可以使用来自指定模型 I/O 灯光对象的初始化程序创建 SCNLight ,据我了解,它允许创建“光源”,从一个圆盘状区域"

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

init(mdlLight: MDLLight)

它被定义为便利初始化器:

便利初始化(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
    }

}
4

2 回答 2

1

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

于 2020-06-16T12:02:03.103 回答
0

请注意,MDLLightTypeDiscArea目前尚未桥接到 SceneKit,您将获得的是一SCNLightTypeOmni盏灯(您可以验证它,但检查初始化程序的结果)。

于 2020-06-17T21:46:45.920 回答