菜鸟教程小白 发表于 2022-12-11 18:31:11

ios - 如何使用场景文件中的几何图形与多个对象(几何图形)?


                                            <p><p>我有一个混合器文件,我已导出为 DAE/collada,然后使用 Xcode 转换为 Scenekit 的场景文件。我在使用场景文件中的几何图形时遇到问题。</p>

<p>场景文件(“model.scn”)非常基本:</p>

<ul>
<li>组(无几何元素)

<ul>
<li>Shape1(几何元素)</li>
<li>Shape2(几何元素)</li>
<li>Shape3(几何元素)</li>
</ul></li>
</ul>

<p>当我尝试使用该模型时,我无法将 3 个形状的组合几何体用于与 SCNPhysicsBody 一起使用的 SCNGeometry。</p>

<p>我尝试了各种方法,但都不起作用:</p>

<h2>方法 1</h2>

<pre><code>let scene = SCNScene(named: &#34;art.scnassets/model.scn&#34;)!
let node = scene.rootNode.childNode(withName: &#34;Group&#34;, recursively: true)!
guard let geo = node.geometry else { return }
// node.geometry is nil so returns here

let physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(geometry: geo))
// this is what I need the custom/aggregate geometry for
</code></pre>

<h2>方法2</h2>

<pre><code>let scene = SCNScene(named: &#34;art.scnassets/model.scn&#34;)!
let geoNode = SCNNode()
let node1 = scene.rootNode.childNode(withName: &#34;Shape1&#34;, recursively: true)!
let node2 = scene.rootNode.childNode(withName: &#34;Shape2&#34;, recursively: true)!
let node3 = scene.rootNode.childNode(withName: &#34;Shape3&#34;, recursively: true)!
geoNode.addChildNode(node1)
geoNode.addChildNode(node2)
geoNode.addChildNode(node3)

// expected geoNode.geometry is not nil, but it is
</code></pre>

<p>根据<a href="https://developer.apple.com/documentation/scenekit/scnnode/1407966-geometry" rel="noreferrer noopener nofollow">Apple&#39;s documentation</a> </p>

<blockquote>
<p>A node can have only one geometry attached to it. To combine geometries so they can be controlled or animated together, create a node with no geometry and add other nodes to it.</p>
</blockquote>

<p>但它似乎无法正常工作,因为父节点的几何选项仍然为零。</p>

<p>我想做的事情应该很简单,但我做错了。我想使用我的 dae/collada/scene 文件中的几何图形。我不想使用默认值(立方体、圆柱体、金字塔、球体、圆环等)。</p>

<p>我做错了什么?谢谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>SceneKit 不会自动为您合并 <code>SCNGeometry</code> 实例。为此,您必须先创建新的 <code>SCNGeometrySource</code> 和 <code>SCNGeometryElement</code> 对象,然后再创建合并的 <code>SCNGeometry</code>。</p>

<p>文档可能有点误导。它想说的是,如果您想同时操作多个几何图形,那么将多个节点分组在一个共同的父节点下会使事情变得更容易,因为操作这个单个节点会影响(平移、旋转、缩放)它的所有子节点。 </p>

<p>请注意,在您的情况下,您可以使用 <a href="https://developer.apple.com/documentation/scenekit/scnphysicsshape/1508886-init" rel="noreferrer noopener nofollow"><code>init(shapes:transforms:)</code></a>显式合并多个 <code>SCNPhysicsShape</code> 实例。首先为每个几何体创建一个形状,然后创建组合形状。 <code>transforms</code> 参数将由 <a href="https://developer.apple.com/documentation/scenekit/scnnode/1407964-transform" rel="noreferrer noopener nofollow"><code>transform</code></a> 组成分组在其共同父级下的节点数。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用场景文件中的几何图形与多个对象(几何图形)?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45778853/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45778853/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用场景文件中的几何图形与多个对象(几何图形)?