2017-03-10 10 views
0

내 기본 코드는 거의이이 튜토리얼에 있습니다 : https://code.tutsplus.com/tutorials/an-introduction-to-scenekit-fundamentals--cms-23847떠오르는 태양처럼 Scenekit에서 그림자를 움직이거나 변화시키는 방법이 있습니까?

내가 일출 동작을 만들고 싶어. lightNode와 같은 것은 제한된 cubeNode와 동일한 높이에서 시작하여 그림자가 시간이 지남에 따라 작아 지도록 위로 이동합니다. 따라서 SCNAction.move를 통해 노드를 이동하려고했습니다 (...). lightNode에

조치 -> 아무것도 제약 cubeNode에

액션을 발생하지 않습니다 -> 그림자 깜박하기 시작하지만 변경

은 내가 shadowModes 주변에 시도하지. 나는 이것을 오해했다고 생각한다. 유용한 결과가 나오지 않았습니다.

scenekit이 동적으로 그림자를 바꾸는 것과 같은 것을 지원하는지 아이디어가 있습니까?

+0

태양이 떠오르는 모습을 사진처럼 보여주세요. –

+0

여기에 나는 Scenekit으로 만들고 싶은 그림자 이동 효과를 보여주는 비디오를 발견했다 : https://www.youtube.com/watch?v=305b3X_FDZM –

+0

보기 후에 YouTube 동영상을 보았을 때 나는 대답보다 더 많은 질문을 가지고있다.자, 비디오에서 큐브로 어떤 부분을 만들고 싶은지 잘 모르겠습니다. 또한 "lightNode에 대한 작업 -> 아무 일도 일어나지 않습니다"라고 말하면 코드 줄을 표시하지 않으므로 아무 말도하지 않아도됩니다. 추가 정보가 없으면이 주제가 닫히도록 제안합니다. –

답변

1

나는 그것을 만드는 방법을 찾았습니다. 내 큰 실수였다 :

  1. 은 어떻게 든 (A lightNode에 대한 조치가 호출 될 때 그냥, 아무 효과가 없습니다)

  2. 내가 SCNAction.move(to...)를 통해 이동하려는 lightNode에 액세스 할 작동하지 않습니다. 대답은 종 방향 움직임 대신 회전을 사용하는 것입니다.

답은 lightNode 대신 제약 Node를 액세스하고있었습니다. 이 코드에서 cubeNode는 보이지 않는 centerPoint으로 바뀝니다 (lightNode이 보이는 제한 노드로). 그림자의 캔버스를 만들기 위해 boxNode이 추가되었습니다. lightNodecenterPoint에 추가해야하며 장면에는 추가하면 안됩니다.

변경된 viewDidLoad -method가 있습니다. 이것을 확인하려면 Xcode를 열고 SceneKit 게임으로 새 프로젝트를 시작한 다음 viewDidLoad을 다음 코드로 바꿉니다.

override func viewDidLoad() { 
    super.viewDidLoad() 

    // create a new scene 
    let scene = SCNScene(named: "art.scnassets/ship.scn")! 

    // create and add a camera to the scene 
    let cameraNode = SCNNode() 
    cameraNode.camera = SCNCamera() 
    scene.rootNode.addChildNode(cameraNode) 

    // place the camera 
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15) 

    // create and add a light to the scene 
    let centerPoint = SCNNode.init() 
    centerPoint.position = SCNVector3Make(0, 0, 0) 
    scene.rootNode.addChildNode(centerPoint) 

    let light = SCNLight() 
    light.type = SCNLight.LightType.spot 
    light.spotInnerAngle = 30 
    light.spotOuterAngle = 80 
    light.castsShadow = true 
    light.color = UIColor.init(colorLiteralRed: 0.95, green: 0.8, blue: 0.8, alpha: 1) 
    light.zFar = 200 

    let lightNode = SCNNode() 
    lightNode.light = light 
    lightNode.position = SCNVector3Make(20, 100, 50) 

    let constraint = SCNLookAtConstraint(target: centerPoint) 
    constraint.isGimbalLockEnabled = true 
    lightNode.constraints = [constraint] 
    centerPoint.addChildNode(lightNode) 

    let ambientLight = SCNLight.init() 
    ambientLight.type = SCNLight.LightType.ambient 
    ambientLight.color = UIColor.darkGray 

    scene.rootNode.light = ambientLight 

    // retrieve the ship node 
    let material = SCNMaterial.init() 
    material.diffuse.contents = UIColor.yellow 
    material.lightingModel = SCNMaterial.LightingModel.phong 
    material.locksAmbientWithDiffuse = true 

    let boxNode = SCNNode.init(geometry: SCNBox.init(width: 10, height: 0.1, length: 10, chamferRadius: 0)) 
    boxNode.geometry?.firstMaterial = material 
    boxNode.position = SCNVector3Make(0, -2, 0) 

    scene.rootNode.addChildNode(boxNode) 

    // animate spot light rotation 
    centerPoint.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: CGFloat(M_PI), y: 0, z: CGFloat(M_PI), duration: 5))) 

    // animate color light change 
    let lightColorChange: CABasicAnimation = CABasicAnimation.init(keyPath: "color") 
    lightColorChange.fromValue = UIColor.init(colorLiteralRed: 0.95, green: 0.8, blue: 0.8, alpha: 1) 
    lightColorChange.toValue = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0.4, alpha: 1) 
    lightColorChange.duration = 5.0 
    lightColorChange.autoreverses = true 
    lightColorChange.repeatCount = Float.infinity 
    light.addAnimation(lightColorChange, forKey: "changeLight") 

    // retrieve the SCNView 
    let scnView = self.view as! SCNView 

    // set the scene to the view 
    scnView.scene = scene 

    // allows the user to manipulate the camera 
    scnView.allowsCameraControl = true 

    // show statistics such as fps and timing information 
    scnView.showsStatistics = true 

    // configure the view 
    scnView.backgroundColor = UIColor.black 

    // add a tap gesture recognizer 
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))) 
    scnView.addGestureRecognizer(tapGesture) 
} 

빛의 변화도 있습니다. CABasicAnimation을 사용하면 시간이 지남에 따라 light.color 속성을 변경할 수 있습니다. 모든 색상 단계가있는 완벽한 일출과 같지 않지만 애니메이션을 더욱 복잡하게 만드는 방법이 있습니다. ("wenderlich 복잡한 로딩 애니메이션을 만드는 방법"을 찾으려면

그림자 색을 변경하는 방법을 찾지 못했습니다. 야간에는 흰색 그림자를 주며 낮에는 검은 색 그림자가있는 좋은 효과가 될 수 있습니다 light.shadowColor 아직 도움이되지 않았습니다. 누군가가 아이디어를 가지고 있다면 매우 높이 평가됩니다. 매우 편리합니다.

1

예. 스포트라이트가 움직이지 않는 경우, 자습서에 제약 조건을 설정했기 때문일 수 있습니다. 제거해보십시오.

// lightNode.constraints = [constraint] 
+0

답장을 보내 주셔서 감사합니다. 모든 라이트 유형으로 시도해 보았습니다. 'lightNode'를 움직이면'omni'와'directional'이 작동합니다. 그러나 그것은 단지 같은 표면에 그림자를 만듭니다 http://www.directupload.net/file/d/4657/oajhkka8_png.htm 'ambient' 그림자를 만들기위한 옵션을 제공하지 않습니다. 그리고 솔직히 말하면, "스폿"으로 나는 수동으로 물체 위로 빛의 위치를 ​​보정 할 수 없었습니다 (웃음). 제약 없이는 스포트라이트를 위해 올바른 위치를 잡을 수있는 방법이 없습니다. 그러나 'spot'을 사용하면 다음과 같이 만들 수 있습니다. http://www.directupload.net/file/d/4657/yh7e52ok_png.htm –

+0

그러나 'SCNAction.move (to ...)'가 잘못된 효과. 그러나 회전은 내가 찾던 해돋이 효과와 아주 흡사하다. 작동하는 코드를 게시했습니다. 그런데 , 당신은 지상에 그림자 명소의 더 나은 렌더링을하는 지 알고 있나요? 나는'light.shadowSampleCount = 10'으로 이동하려고했으나 그림자가 사라 앱의 성능이 aweful되었다. –

+0

스포트라이트에 zNear을 설정하십시오. –