SCENE KIT:加载材料错误

时间:2017-03-23 13:24:45

标签: 3d swift3 textures scenekit material

你能解释一下 - 为什么我的纹理没有在Scene Kit的3D视图中显示?

我正在尝试添加漫反射颜色和环境 - 但它不起作用。

所以这是我的问题 - 为什么?

仅显示灰色材质: enter image description here 这是我在swift中的GameViewController代码示例:

import UIKit
import QuartzCore
import SceneKit
import SpriteKit

class GameViewController: UIViewController {

var sceneView: SCNView!
var scene: SCNScene!
var label: UILabel!
var timer: Timer!
var time: Int = 0
var globeNode: SCNNode!

override func viewDidLoad() {
    super.viewDidLoad()

    sceneView = self.view as! SCNView
    sceneView.allowsCameraControl = true
    scene = SCNScene()
    sceneView.scene = scene

    let camera = SCNCamera()
    let cameraNode = SCNNode()
    cameraNode.camera = camera
    cameraNode.position = SCNVector3(x: -3.0, y: 3.0, z: 3.0)

    let ambientLight = SCNLight()
    ambientLight.type = SCNLight.LightType.ambient
    ambientLight.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
    cameraNode.light = ambientLight

    let light = SCNLight()
    light.type = SCNLight.LightType.spot
    light.spotInnerAngle = 30.0
    light.spotOuterAngle = 80.0
    light.castsShadow = true
    let lightNode = SCNNode()
    lightNode.light = light
    lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)

    let cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
    let cubeNode = SCNNode(geometry: cubeGeometry)

    let planeGeometry = SCNPlane(width: 100.0, height: 100.0)
    let planeNode = SCNNode(geometry: planeGeometry)
    planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
    planeNode.position = SCNVector3(x: 0, y: -0.5, z: 0)

    self.globeNode = SCNNode()
    let theGlobeGeometry = SCNSphere(radius: 110)
    theGlobeGeometry.firstMaterial?.diffuse.contents = UIImage(named:"earth_diffuse.jpg")
    theGlobeGeometry.firstMaterial?.ambient.contents = UIImage(named:"earth_ambient2.jpeg")
    //      theGlobeGeometry.firstMaterial.ambient.contents = UIImage(named:"earth_ambient.jpg")
    theGlobeGeometry.firstMaterial?.specular.contents = UIImage(named:"earth_specular.jpg")
    theGlobeGeometry.firstMaterial?.emission.contents = nil
    theGlobeGeometry.firstMaterial?.transparent.contents = nil
    theGlobeGeometry.firstMaterial?.reflective.contents = nil
    theGlobeGeometry.firstMaterial?.multiply.contents = nil
    theGlobeGeometry.firstMaterial?.normal.contents = UIImage(named:"earth_normal.jpg")

    let theGlobeModelNode = SCNNode(geometry: theGlobeGeometry)
    self.globeNode.addChildNode(theGlobeModelNode)

    let theCloudGeometry = SCNSphere(radius:0.501)
    theCloudGeometry.firstMaterial?.diffuse.contents = nil
    theCloudGeometry.firstMaterial?.ambient.contents = nil
    theCloudGeometry.firstMaterial?.specular.contents = nil
    theCloudGeometry.firstMaterial?.emission.contents = nil
    theCloudGeometry.firstMaterial?.transparent.contents = UIImage(named:"earth_clouds.png")
    theCloudGeometry.firstMaterial?.reflective.contents = nil
    theCloudGeometry.firstMaterial?.multiply.contents = nil
    theCloudGeometry.firstMaterial?.normal.contents = nil

    let theCloudModelNode = SCNNode(geometry: theCloudGeometry)
    self.globeNode.addChildNode(theCloudModelNode)

    // animate the 3d object
    let animation: CABasicAnimation = CABasicAnimation(keyPath: "rotation")
    animation.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 1, z: 0, w: Float(M_PI)*2))
    animation.duration = 5
    animation.repeatCount = MAXFLOAT //repeat forever
    globeNode.addAnimation(animation, forKey: nil)

    let redMaterial = SCNMaterial()
    redMaterial.diffuse.contents = UIColor.red
    cubeGeometry.materials = [redMaterial]

    let greenMaterial = SCNMaterial()
    greenMaterial.diffuse.contents = UIColor.green
    planeGeometry.materials = [greenMaterial]

    let constraint = SCNLookAtConstraint(target: cubeNode)
    constraint.isGimbalLockEnabled = true
    cameraNode.constraints = [constraint]
    lightNode.constraints = [constraint]

    scene.rootNode.addChildNode(lightNode)
    scene.rootNode.addChildNode(cameraNode)
    //scene.rootNode.addChildNode(cubeNode)
    scene.rootNode.addChildNode(planeNode)
    scene.rootNode.addChildNode(globeNode)

}

func addSceneToScene() {
    let geoScene = SCNScene(named: "art.scnassets/driod.dae")
    scene.rootNode.addChildNode((geoScene?.rootNode.childNode(withName: "Head_009", recursively: true))!)

}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
   // UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.landscapeLeft.rawValue)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

这可能是因为它没有找到纹理图像。是bundle中的图片吗?

或者您是否忘记将图像包含在目标中?