ARKit添加X翻转的2D视频

时间:2018-11-13 13:42:04

标签: ios swift scenekit arkit

所以我有以下代码来创建自定义墙

    let wall = SCNPlane(width: CGFloat(distance),
                        height: CGFloat(height))
    wall.firstMaterial = wallMaterial()
    let node = SCNNode(geometry: wall)

    // always render before the beachballs
    node.renderingOrder = -10

    // get center point
    node.position = SCNVector3(from.x + (to.x - from.x) * 0.5,
                               from.y + height * 0.5,
                               from.z + (to.z - from.z) * 0.5)
    node.eulerAngles = SCNVector3(0,
                                  -atan2(to.x - node.position.x, from.z - node.position.z) - Float.pi * 0.5,
                                  0)

现在我要在点击测试中添加简单的SCNPlane并添加视频(skscene)

          // first.node is hittest result 

            let node = SCNNode(geometry: SCNPlane(width: CGFloat(width) , height:  CGFloat(height))
            node.geometry?.firstMaterial?.isDoubleSided = true
            node.geometry?.firstMaterial?.diffuse.contents = self.create2DVideoScene(xScale: first.node.eulerAngles.y < 0 ? -1 : nil)
                         node.position = nodesWithDistance.previous.node.mainNode.position

            node.eulerAngles = first.node.eulerAngles

这是我如何创建2d节点

 /// Creates 2D video scene
    private func create2DVideoScene (xScale:CGFloat?) -> SKScene {
        var videoPlayer = AVPlayer()

        if let validURL = Bundle.main.url(forResource: "video", withExtension: "mp4", subdirectory: "/art.scnassets") {
            let item = AVPlayerItem(url: validURL)
            videoPlayer = AVPlayer(playerItem: item)
        }
        let videoNode = SKVideoNode(avPlayer: videoPlayer)
        videoNode.yScale *= -1

        // While debug I observe that if first.node.rotation.y in  - , then we need to change xScale to -1 (when wall draw from right -> left )

        if let xScale = xScale {
            videoNode.xScale *= xScale

        }


        videoNode.play()

        let skScene = SKScene(size: self.sceneView.frame.size)
        skScene.scaleMode = .aspectFill
        skScene.backgroundColor = .green
        skScene.addChild(videoNode)
        videoNode.position = CGPoint(x: skScene.size.width/2, y: skScene.size.height/2)
        videoNode.size = skScene.size
        return skScene
    }

问题 ::如果我从左到右绘制墙节点,则意味着第一个点在左侧,另一个点在右侧,并在它们之间绘制墙。然后视频被翻转。

如果我从右到左画图,则意味着第一个点在右侧,第二个点在左侧,并在它们之间画线,那么视频就可以了。

要解决此问题,我检查了墙eulerAngles,检查了self.create2DVideoScene行,但这不适用于现实世界的每个区域

我希望视频不应该在用户面前翻转

编辑

由于eulerAngles在创建墙时两种情况下的不同,视频被翻转了

  

从点1到点2的角度->(0.000000 -1.000000 0.000000 3.735537)

     

从点2到点1的角度->(0.000000 -1.000000 0.000000 0.478615)


发布视频Click here to play video

请提供此问题的建议或解决方案。

enter image description here

视频已翻转

Flipped

1 个答案:

答案 0 :(得分:0)

我已经解决了临时解决方案,仍在寻找更好的解决方案

问题在墙上。从右向左绘制时,墙会翻转相机的另一侧。我通过设置isDoubleSided = false并通过将图像应用为包含文本的分散内容来弄清楚了,我可以看到图像本身被翻转了。

我尝试了很多事情,但这可以帮助我

  1. 归一化向量
  2. 找到SCNVectors之间的十字架
  3. 如果y> 0,我刚从交换到值

代码

    let normalizedTO = to.normalized()
    let normalizedFrom = from.normalized()
    let angleBetweenTwoVectors = normalizedTO.cross(normalizedFrom)

    var from = from
    var to = to
    if angleBetweenTwoVectors.y > 0 {
        let temp = from
        from = to
        to = temp
    }

// Inside extension of SCNVector3
func normalized() -> SCNVector3 {
    if self.length() == 0 {
        return self
    }

    return self / self.length()
}

func cross(_ vec: SCNVector3) -> SCNVector3 {
        return SCNVector3(self.y * vec.z - self.z * vec.y, self.z * vec.x - self.x * vec.z, self.x * vec.y - self.y * vec.x)
    }

希望有帮助。如果有人知道更好的解决方案,请回答。