菜鸟教程小白 发表于 2022-12-11 20:48:57

ios - ARKit 添加了 X 翻转的 2D 视频


                                            <p><p>所以我有以下代码来创建自定义墙</p>
<pre><code>    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)
</code></pre>
<p>现在我在 HitTest 中添加简单的 SCNPlane 并添加视频(skscene 到它)</p>
<pre><code>          // 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 &lt; 0 ? -1 : nil)
                         node.position = nodesWithDistance.previous.node.mainNode.position
      
            node.eulerAngles = first.node.eulerAngles
</code></pre>
<p>这里我是如何创建二维节点的</p>
<pre><code> /// Creates 2D video scene
    private func create2DVideoScene (xScale:CGFloat?) -&gt; SKScene {
      var videoPlayer = AVPlayer()

      if let validURL = Bundle.main.url(forResource: &#34;video&#34;, withExtension: &#34;mp4&#34;, subdirectory: &#34;/art.scnassets&#34;) {
            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 -&gt; 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
    }
</code></pre>
<p><strong>问题</strong>::如果我从左到右绘制墙节点意味着第一个点在左侧,另一个点在右侧并在它们之间绘制墙。然后视频翻转。</p>
<p>如果我从右到左绘制意味着第一个点在右侧,第二个点在左侧并在它们之间画线,那么视频就很好了。</p>
<p>为了解决这个问题,我检查了墙 eulerAngles 检查行 <code>self.create2DVideoScene</code> 但这并不是在现实世界的每个领域都有效</p>
<p>我希望视频不应该在用户面前开始翻转</p>
<p><strong>编辑</strong></p>
<p>视频被翻转是因为 <code> eulerAngles</code> 在创建墙时在两种情况下都不同</p>
<blockquote>
<p>Angle point1 to point2 --&gt; (0.000000 -1.000000 0.000000 3.735537)</p>
<p>Angle point2 to point1 -- &gt; (0.000000 -1.000000 0.000000 0.478615)</p>
</blockquote>
<hr/>
<p>问题视频<a href="https://vimeo.com/300709584" rel="noreferrer noopener nofollow">Click here to play video</a> </p>
<p>请提供此问题的建议或解决方案。</p>
<p> <a href="/image/NY4S1.png" rel="noreferrer noopener nofollow"><img src="/image/NY4S1.png" alt="enter image description here"/></a> </p>
<p><strong>视频翻转</strong></p>
<p> <a href="/image/s6cXn.png" rel="noreferrer noopener nofollow"><img src="/image/s6cXn.png" alt="Flipped"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我已经解决了临时解决方案的问题,仍在寻找更好的解决方案</p>

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

<p>我尝试了很多东西,但这个对我有帮助</p>

<ol>
<li>归一化向量</li>
<li>找到 SCNVectors 之间的交叉点</li>
<li>如果 y > 0 我只是从值交换到值</li>
</ol>

<p>代码</p>

<pre><code>    let normalizedTO = to.normalized()
    let normalizedFrom = from.normalized()
    let angleBetweenTwoVectors = normalizedTO.cross(normalizedFrom)

    var from = from
    var to = to
    if angleBetweenTwoVectors.y &gt; 0 {
      let temp = from
      from = to
      to = temp
    }

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

    return self / self.length()
}

func cross(_ vec: SCNVector3) -&gt; 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)
    }
</code></pre>

<p>希望对您有所帮助。如果有人知道更好的解决方案,请回答。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - ARKit 添加了 X 翻转的 2D 视频,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/53282351/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/53282351/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - ARKit 添加了 X 翻转的 2D 视频