ARKit add 2D Video flipped by X

坚强是说给别人听的谎言 提交于 2019-12-24 00:38:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/53282351/arkit-add-2d-video-flipped-by-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!