因此,要明确我的目标,因为我没有要共享的代码...假设我有一个SCNNode,它位于摄像机和另一个SCNNode之间。第一个SCNNode是一个SCNBox,但是没有纹理,因此可以在它后面看到第二个SCNNode。我想为第一个节点提供透明材料,但也要使它后面的所有节点都遮住,好像它是不透明的一样。在常规场景中,这意味着您可能会看到场景背景颜色,也许是黑色,但是我正计划在ARKit中进行此操作,这更有意义,因为这意味着您将仅看到其背后的真实世界。
我正在尝试使用 SceneKit 为 tvOS 开发游戏,但遇到了问题。当我在向物理主体施加脉冲之前设置节点的 eulerAngle 时,节点将重置到其原始位置。
我期望看到节点在地板平面上移动,但在每次点击时,节点都会在施加脉冲之前移动到原点位置。
我是这个框架的新手,所以我想知道错误在哪里。我正在使用带有 tvOS 9.0 和 XCode 7.1.1 的新 AppleTV
要重现它,您可以创建一个新的 xcode 项目(Game for tvOS)并用以下代码替换 GameViewController.m:
#import "GameViewController.h"
SCNNode *ship;
SCNNode *node;
SCNNode *ground;
@implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new scene
SCNScene *scene = [[SCNScene alloc] init];
scene.physicsWorld.gravity = SCNVector3Make(0, -800, 0);
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.zFar = 10000;
[scene.rootNode addChildNode:cameraNode];
// place the camera …Run Code Online (Sandbox Code Playgroud) 我对SceneKit很陌生,在我看来,我偶然发现了一个很奇怪的行为.
当枢轴放置在position节点本身指定的点时,更改节点枢轴会移动它锚定到其父节点的点.这很好,但后来我尝试将一个子节点添加到一个子节点,其枢轴移动(仅翻译),子节点(默认位置(0,0,0))将自己放置在其父节点的几何中心,一个圆柱体用SCNCylinder,而不是它的移动枢轴.
使用移动枢轴的变换重新计算将孩子相对于父枢轴放置的位置不是问题,但是我想知道是否有一个有效的理由不将坐标系与节点的枢轴一起移动关于这个事实在网上没有找到任何东西.
我希望得到一个当前节点位置形成一个SCNNode
我知道的动画presentationNode,我试图在那里提取位置形式,但没有运气
这是我的代码
SCNNode * pelvis = [_unit childNodeWithName:@"Bip01_Pelvis" recursively:YES];
SCNNode * present = pelvis.presentationNode;
SCNVector3 position = [self convertPosition:present.position toNode:self.parentNode];
Run Code Online (Sandbox Code Playgroud)
我所拥有的位置是SCNNode在CAAnimation应用之前处于"休息"模式的位置.
如何从'presentationNode'获取位置等属性?
编辑
类本身相关部分:
@interface UndeadSimple : RepresentationNode <CAAnimationDelegate>
{
//Animations
SCNNode * _undead;
CAAnimation * _currentAnimation;
NSString * _currAnimationkey;
CAAnimation * _death1;
...
SCNNode * _audioNode;
SCNAudioSource * _deathSource;
SCNAudioPlayer * _player;
}
Run Code Online (Sandbox Code Playgroud)
在里面:
NSString * sceneName = @ZOMBIE;
SCNScene *scene2 = [SCNScene sceneNamed:sceneName];
_undead = [SCNNode node];
for(SCNNode * node …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现与场景编辑器相同的旋转,但是使用代码使对象始终围绕所选轴旋转,但是如果我在编辑器中查看角度(x,y,z),它们会随机变化
![本地节点轴] [1]
我试图使用四元数,但无法使其正常工作
PS.我的坏是使用旋转属性而不是方向SCNVector4,已正确阅读文档)
我在SCNode命中检测方面遇到了一些麻烦.我需要检测哪个对象是在具有SCNNode的情景感动了,我已经实现了这一段代码,但它似乎当我接触的对象,但做工不错,当我接触sceneView其余崩溃.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first as! UITouch
if(touch.view == self.sceneView){
print("touch working")
let viewTouchLocation:CGPoint = touch.location(in: sceneView)
guard let result = sceneView.hitTest(viewTouchLocation, options: nil).first else {
return
}
if (bottleNode?.contains(result.node))! { //bottleNode is declared as SCNNode? and it's crashing here
print("match")
}
}
}
Run Code Online (Sandbox Code Playgroud) 根据本答案中的建议,目标是向场景添加环境光以及相机的定向光.
这适用于Xcode场景编辑器,定向灯设置为70%白色,环境光设置为50%白色.方向灯的欧拉角使用默认值.
编码这种安排失败了.就好像没有添加定向灯一样.对场景没有影响; 好像只有环境光存在.
x欧拉角分量的不同值没有区别 - 尝试了PI/2,PI和其他值,但仍然没有变化.
但是,将方向光添加到场景的根节点(scene.rootNode.addChildNode(directionalLightNode))确实会将光添加到场景中.
fileprivate func addLightNode() {
// Create ambient light
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = .ambient
ambientLightNode.light!.color = UIColor(white: 0.70, alpha: 1.0)
// Add ambient light to scene
scene.rootNode.addChildNode(ambientLightNode)
// Create directional light
let directionalLight = SCNNode()
directionalLight.light = SCNLight()
directionalLight.light!.type = .directional
directionalLight.light!.color = UIColor(white: 1.0, alpha: 1.0)
directionalLight.eulerAngles = SCNVector3(x: 0, y: 0, z: 0)
// Add directional light to camera
let cameraNode …Run Code Online (Sandbox Code Playgroud) 下面的代码旨在模拟SceneKit中的默认相机控件,尤其是缩放和旋转功能。
但是,该代码不像默认控件那样流畅和通用。
例如,如果您加载了这两个模型,则默认缩放对这两个模型均一致。下面的代码仅适用于模型1,但对于模型2的缩放速度过快。
任何帮助,将不胜感激。
模式1:https://poly.google.com/view/6mRHqTCZHxw
模式2:https://poly.google.com/view/cKryD9VnDEZ
// Create camera
let camera = SCNCamera()
camera.automaticallyAdjustsZRange = true
camera.xFov = 30
camera.yFov = 0
// Compute distance to place camera
let theta = GLKMathDegreesToRadians(Float(camera.xFov/2))
let adjacentLength = oppositeLength / Float(tan(theta))
// Add camera to <cameraNode>
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: adjacentLength)
// Add <cameraNode> to <orbitNode>
orbitNode.addChildNode(cameraNode)
// Add <orbitNode>
scene.rootNode.addChildNode(orbitNode)
// Handle pinches
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(didSceneViewPinch)) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 ARKit 项目,该项目在水平平面上点击时需要波纹动画效果。为此,我采用了 UIView 对象并将其作为 SCNPlane 对象材料的内容传递。我已将 Ripple 动画添加到 UIView 对象。一切正常。但我无法将 SCNPlane 颜色更改为清晰颜色。我可以为材质使用透明度属性。但它也隐藏了 Ripple 动画。所以,我在这里需要的是,SCNPlane 对象的背景颜色应该是透明的,并且应该只向用户显示 Ripple 动画。有人可以帮我解决这个问题。
这是我的代码。
let location = tapGesture.location(in: self.sceneView)
let results = self.sceneView.hitTest(location, types: .existingPlane)
if !results.isEmpty{
guard let result = results.first else{ return }
let myUIView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 300, height: 300)))
myUIView.backgroundColor = .red
let plane = SCNPlane(width: 1.0, height: 1.0)
plane.firstMaterial?.diffuse.contents = myUIView
let planeViewNode = SCNNode(geometry: plane)
planeViewNode.eulerAngles.x = Float(-Double.pi / 2)
planeViewNode.position = SCNVector3(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
self.sceneView.scene.rootNode.addChildNode(planeViewNode) …Run Code Online (Sandbox Code Playgroud) 只是想知道是否有可能(我知道是,但如何)像在 Measure 应用程序中一样在 ARSCNView 中绘制虚线?也许有一种方法可以开箱即用地使用场景节点,idk。
我一直在使用 SCNCylinder 画一条直线,IDK 是否可以重复使用它并进行调整,或者我们必须使用一种非常不同的方式来制作虚线。
import SceneKit
class CylinderLineNode: SCNNode {
private(set) var cylinder: SCNCylinder
private(set) var positionA: SCNVector3
private(set) var positionB: SCNVector3
init(with positionA: SCNVector3, positionB: SCNVector3, radius: CGFloat = 0.02, color: UIColor = .red) {
self.positionA = positionA
self.positionB = positionB
let vector = positionB - positionA
let height = vector.length()
cylinder = SCNCylinder(radius: radius, height: CGFloat(height))
cylinder.radialSegmentCount = 8
cylinder.firstMaterial?.diffuse.contents = color
super.init()
geometry = cylinder
position = (positionB + positionA) / 2 …Run Code Online (Sandbox Code Playgroud)