ARKit 添加 X 翻转的 2D 视频 [英] ARKit add 2D Video flipped by X

查看:20
本文介绍了ARKit 添加 X 翻转的 2D 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 let wall = SCNPlane(width: CGFloat(distance),高度:CGFloat(height))wall.firstMaterial = wallMaterial()让节点 = SCNNode(几何:墙)//总是在沙滩球之前渲染node.renderingOrder = -10//获取中心点node.position = SCNVector3(from.x + (to.x - from.x) * 0.5,from.y + 高度 * 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 是命中结果让 node = SCNNode(geometry: SCNPlane(width: CGFloat(width) , height: CGFloat(height))node.geometry?.firstMaterial?.isDoubleSided = truenode.geometry?.firstMaterial?.diffuse.contents = self.create2DVideoScene(xScale: first.node.eulerAngles.y <0 ? -1 : nil)node.position = nodesWithDistance.previous.node.mainNode.positionnode.eulerAngles = first.node.eulerAngles

这里我是如何创建二维节点的

///创建 2D 视频场景private func create2DVideoScene (xScale:CGFloat?) ->SK场景{var videoPlayer = AVPlayer()if let validURL = Bundle.main.url(forResource: "video", withExtension: "mp4", subdirectory: "/art.scnassets") {让 item = AVPlayerItem(url: validURL)videoPlayer = AVPlayer(playerItem: item)}让 videoNode = SKVideoNode(avPlayer: videoPlayer)videoNode.yScale *= -1//在调试时我观察到如果 first.node.rotation.y 在 - 中,那么我们需要将 xScale 更改为 -1(当墙从右侧绘制时 -> 左侧)如果让 xScale = xScale {videoNode.xScale *= xScale}videoNode.play()让 skScene = SKScene(大小:self.sceneView.frame.size)skScene.scaleMode = .aspectFillskScene.backgroundColor = .greenskScene.addChild(videoNode)videoNode.position = CGPoint(x: skScene.size.width/2, y: skScene.size.height/2)videoNode.size = skScene.size返回skScene}

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

如果我从右到左绘制意味着第一个点在右侧,第二个点在左侧并在它们之间画线,则视频完全没问题.

为了解决这个问题,我检查了 wall eulerAngles 检查了 self.create2DVideoScene 行,但这并不是在现实世界的每个区域都有效

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

编辑

视频被翻转,因为 eulerAngles 在创建墙时两种情况都不同

<块引用>

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

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


发行视频

视频翻转

解决方案

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

问题是墙.从右向左绘制时,墙壁会翻转到相机的另一侧.我已经通过设置 isDoubleSided = false 并通过将图像应用为具有文本的漫反射内容来解决这个问题,我可以看到图像本身被翻转.

我尝试了很多东西,但这对我有帮助

  1. 标准化向量
  2. 找到 SCNVectors 之间的交叉
  3. 如果 y > 0 我只是从和交换到 Value

代码

 let normalizedTO = to.normalized()让 normalizedFrom = from.normalized()让angleBetweenTwoVectors = normalizedTO.cross(normalizedFrom)var from = fromvar to = to如果angleBetweenTwoVectors.y >0 {让温度 = 从从 = 到到 = 温度}//SCNVector3 的内部扩展func normalized() ->SCNVector3 {如果 self.length() == 0 {回归自我}返回 self/self.length()}func cross(_ vec: SCNVector3) ->SCNVector3 {返回 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)}

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

So I have following code to create custom wall

    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)

And Now I am adding simple SCNPlane on hit test and add video (skscene to it)

          // 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

Here How I created 2d node

 /// 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
    }

Issue :: If I draw wall node from left to right means first point is on left side and other point on right side and draw wall between them. Then Video is flipped.

If I draw from right to left means first point is on right side and second point is on left side and draw line between them then video is perfectly fine.

To fix this I checked wall eulerAngles check the line self.create2DVideoScene but this is not working in every area of real world

I want video should not be start flipped in front of user

EDIT

video is flipped because of eulerAngles is different in both case while create a wall

Angle point1 to point2 --> (0.000000 -1.000000 0.000000 3.735537)

Angle point2 to point1 -- > (0.000000 -1.000000 0.000000 0.478615)


Issue video Click here to play video

Please please provide the suggestion or solution of this issue .

Video flipped

解决方案

I have fixed issue with temporary solution still looking for a better one

Issue is of wall. wall is flipped other side of camera when draw from right to left direction. I have figure it out by setting isDoubleSided = false and by applying a image as diffuse contents which has text and I can see that image is flipped itself.

I have tried many things but this one helps me

  1. Normalize vector
  2. Find the cross between to SCNVectors
  3. If y > 0 I just swapped from and to Value

Code

    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)
    }

Hope it is helpful. If anyone know better solution please answer it.

这篇关于ARKit 添加 X 翻转的 2D 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆