- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下场景:
[Root Node]
|
[Main Container]
| |
[Node A Wrapper] [Node B Wrapper]
| |
[Node A] [Node B]
我设置了平移手势识别器,当您在开放空间中平移时,[Main Container] 会在选定的方向上旋转 +/- Double.pi/2 (90deg)。当平移在子节点 A、B 之一上开始时(我在 touchesBegan 上对此进行了 HitTest ),我想沿世界轴方向旋转子节点(再次 90 度增量)。
我正在使用 rootNode 的 convertTransform() 旋转 [Main Container],它工作正常,并且旋转是沿着世界轴执行的 - 主容器的位置是 (0,0,0),我相信这使得容易多了。
我包裹子节点的原因是它们在包裹器内有局部位置 (0,0,0),这应该有助于围绕它们的原点旋转。但是当我在 [Main Container] 上执行旋转时它们也会旋转,它们的局部轴的方向会改变并且旋转是围绕与我想要的不同的轴执行的。
根据我对变换矩阵的(非常有限的)理解,我假设我需要以某种方式链接和乘以由父节点的 convertTransform 生成的矩阵,或者以某种方式使用 worldTransform 属性,但我尝试的任何事情都会导致奇怪的旋转。任何帮助,将不胜感激!
最佳答案
我已经建立了一个基于 SceneKit 模板的小型示例项目,其中的控件与您描述的类似。它在 Objective C 中,但相关部分几乎相同:
- (void) handlePan:(UIPanGestureRecognizer*)gestureRecognize {
CGPoint delta = [gestureRecognize translationInView:(SCNView *)self.view];
if (gestureRecognize.state == UIGestureRecognizerStateChanged) {
panHorizontal = NO;
if (fabs(delta.x) > fabs(delta.y)) {
panHorizontal = YES;
}
} else if (gestureRecognize.state == UIGestureRecognizerStateEnded) {
SCNMatrix4 rotMat;
int direction = 0;
if (panHorizontal) {
if (delta.x <0) {
direction = -1;
} else if (delta.x >1) {
direction = 1;
}
rotMat= SCNMatrix4Rotate(SCNMatrix4Identity, M_PI_2, 0, direction, 0);
} else {
if (delta.y <0) {
direction = -1;
} else if (delta.y >1) {
direction = 1;
}
rotMat= SCNMatrix4Rotate(SCNMatrix4Identity, M_PI_2, direction, 0, 0);
}
if (selectedNode == mainPlanet) {
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform, rotMat);
} else { //_selectedNode is a child node of mainPlanet, i.e. moons.
//get the translation matrix of the child node
SCNMatrix4 transMat = SCNMatrix4MakeTranslation(selectedNode.position.x, selectedNode.position.y, selectedNode.position.z);
//move the child node the origin of its parent (but keep its local rotation)
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform, SCNMatrix4Invert(transMat));
//apply the "rotation" of the mainPlanet extra (we can use the transform because mainPlanet is at world origin)
selectedNode.transform = SCNMatrix4Mult( selectedNode.transform, mainPlanet.transform);
//perform the rotation based on the pan gesture
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform, rotMat);
//remove the extra "rotation" of the mainPlanet (we can use the transform because mainPlanet is at world origin)
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform,SCNMatrix4Invert(mainPlanet.transform));
//add back the translation mat
selectedNode.transform = SCNMatrix4Mult(selectedNode.transform,transMat);
}
}
}
在 handleTap 中:
selectedNode = result.node;
在viewDidLoad中:
mainPlanet = [scene.rootNode childNodeWithName:@"MainPlanet" recursively:YES];
orangeMoon = [scene.rootNode childNodeWithName:@"orangeMoon" recursively:YES];
yellowMoon = [scene.rootNode childNodeWithName:@"yellowMoon" recursively:YES];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[gestureRecognizers addObject:panGesture];
和本地变量:
SCNNode *mainPlanet;
SCNNode *orangeMoon;
SCNNode *yellowMoon;
SCNNode *selectedNode;
BOOL panHorizontal;
MainPlanet 将是您的 mainContainer 并且不必可见(在我的示例中它是可见的,因为必须点击它才能知道要旋转什么...)。两个月亮就是你的节点A和B,主节点的子节点。不需要 wrapper 。关键部分显然是注释部分。
通常在局部空间旋转子节点(如果父节点在 0,0,0)
正如您所注意到的,这将在其局部轴心点和局部轴上旋转子节点。在您旋转父节点之前,这可以正常工作。解决方案是在基于平移手势旋转子节点之前将相同的旋转应用到子节点(步骤 2),然后再次将其删除。
所以为了得到你想要的结果:
我确定还有其他可能的路线,也许可以使用转换为/从转换矩阵将旋转矩阵转换为主节点,而不是第 2 步和第 4 步,但通过这种方式您可以清楚地知道发生了什么。
关于ios - Scenekit - 基于世界轴的旋转子子节点的 convertTransform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46723827/
目标是创建无限的棋盘图案。 使用SCNFloor和附加的图像,我们可以生成接近但不太像棋盘的东西。一些黑色正方形在不应该合并的位置合并。 我们尝试了Scale,WrapS,WrapT,Min filt
这看起来很简单,但我就是无法缩小!使用这些搜索词的所有在线内容都是关于在应用程序中编写效果的。我的问题不在应用程序中;我只想在用户界面中评估和编辑 SceneKit 场景中的所有对象,并在我可以看到整
我在这里遗漏了什么吗?在各种向量/矩阵函数中here ,我看不到任何简单地将 SCNMatrix4 变换应用于 SCNVector4 的函数。我当然可以通过直接写出矩阵/向量乘法来做到这一点,但这肯定
我试图了解 ArKit 中引入的不同元素与 SceneKit 中可能的等价物之间的区别: SCNNode.simdTransform对比 SCNNode.transform .在 ARKit 中,似乎
我已经实现了向 ARKit 和 SceneKit 场景添加阴影平面的多种方法之一。它工作得很好,阴影看起来很好。 问题是大部分时间这架飞机也有灰白色。换句话说,它不是完全透明的。另一方面,有时灰色的类
我在游戏执行过程中随机弹出以下错误: 我正在随机时刻创建 carNode 的多个实例,并且所有实例都具有从场景中删除节点的操作。大多数情况下,这不是问题,但在某些情况下,应用程序会崩溃并显示照片中的错
我正在使用 Metal 计算内核生成网格数据(目前是三角汤)。 是否可以高效(无需任何复制)将网格数据导入 SceneKit 以利用 SceneKit 的渲染器?网格数据会经常更新,因此避免数据复制非
我想使用 Metal 计算着色器来计算一些位置,然后将这些位置输入到 Metal 着色器中。听起来很简单,但我无法将 MTLBuffer 数据导入基于 Metal 的 SCNProgram。 计算内核
在 Xcode 8 中,SceneKit 编辑器 允许您在使用 SCNLightingModelPhysicallyBased 时为 metalness 属性设置浮点值。选项包括金属、电介质、浮点值或
我注意到,如果您有一个空场景,然后在其中加载具有基于物理照明的 3D 模型,则在对象出现时会出现一些卡顿。如果之后我再添加一个不同的对象,就不会发生口吃。 查看分析器,似乎默认 PBR 着色器正在第一
我有一个 SceneKit 游戏,我正在尝试将其与 GameplayKit 集成。 为了让 GameplayKit 正常工作,我需要通过调用 updateWithDeltaTime(seconds:)
我正在 iOS SceneKit 中使用 SCNMorpher 在从 Blender 导出为 DAE 文件的 3D 面部模型上的不同面部表情之间进行变形。变形本身效果很好。 在我第一次在变形器上调用
文档 states GameplayKit also works well for 3D games built with the SceneKit framework 但是,似乎没有提及使用Scen
我通过使用 Interface Builder 将 3D 文本拖到场景中创建了这个 SceneKit 文本。 它是这样创建的: 轴在左下方。有没有一种方法可以使用界面生成器使轴在该文本上居中? 最佳答
我正在尝试将 SwiftUI 路径添加到 Scenekit 场景(作为 SCNShape)。 我创建了:Scenekit View (图片 1)SwiftUI 路径(图 2) 然后我尝试根据以下方法调
我想之前一定有人问过这个问题,但我现在找不到,所以暂时先问一下。 当 SceneKit 动画停止时,SpriteKit 的动画覆盖在 SceneKit 上。这是出乎意料的。这就像整个 SCNView
如何绘制边框以突出显示 SCNNode 并向用户指示该节点已被选中? 在我的项目中,用户可以放置多个虚拟对象,用户可以随时选择任何对象。选择后,我应该向用户显示突出显示的 3D 对象。有没有办法直接实
我正在 Scenekit 中加载一个 .obj 模型。当我尝试加载像(水瓶)这样的小模型时,它工作得很好。但是当我尝试加载更大的模型(如沙发、 table )时,它加载完美,但超出了场景的边界。 在S
我正在编写一个 iOS 应用程序,它使用自定义几何体在 SceneKit 中渲染点云。 This post对我实现这一目标非常有帮助(尽管我将其翻译为 Objective-C),David Rönnq
必须使用什么 orthographicProjection 才能在 SceneKit 中以 1:1 SceneKit 点与屏幕点/像素比率制作 2D 应用程序? 例子:我想在屏幕上的 (200, 20
我是一名优秀的程序员,十分优秀!