gpt4 book ai didi

ios - 以编程方式更改 SCNScene 的 sceneNamed

转载 作者:行者123 更新时间:2023-11-29 11:52:02 25 4
gpt4 key购买 nike

我正在尝试用代码更改当前的 sceneNamed,但我的方法似乎有一些问题。首先,新场景将发生变化,但我必须触摸或评价对象才能发生变化。其次,childNodeWithName 似乎根本没有变化!这是我的代码:

    - (void)load3DObjectName:(NSString*)name nodeName:(NSString*)nodeName zPhone:(CGFloat)positioniPhone zPad:(CGFloat)positioniPad{

SCNScene * scene = [SCNScene sceneNamed:name];
// retrieve the ship node
SCNNode *trex = [scene.rootNode childNodeWithName:nodeName recursively:YES];


if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {

trex.position = SCNVector3Make(0, 0, positioniPhone);

} else {

trex.position = SCNVector3Make(0, 0, positioniPad);
}



_the3DScence.scene = scene;
_the3DScence.autoenablesDefaultLighting = YES;
_the3DScence.allowsCameraControl = YES;
_the3DScence.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];
}



// Load default object :

- (void)viewDidLoad {

[self load3DObjectName:@"cube.dae" nodeName:@"cube1" zPhone:-40 zPad:-30];

}

//Trying to change the 3D object with button:
- (IBAction)nextObject:(id)sender {

[self load3DObjectName:@"redCube.dae" nodeName:@"cube2" zPhone:-40 zPad:-30];

}

- (IBAction)changeIt:(id)sender {

[self load3DObjectName:@"dayere.dae" nodeName:@"Sphere" zPhone:-40 zPad:-40];
}

这是源代码: https://www.dropbox.com/s/rrvbxmrb9wcrnoj/3D%20Objects%20Change.zip?dl=0

Dropbox 版本中的代码不是我上面发布的。这是 Dropbox 版本:

- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    [self load3DObjectName:@"cube.dae" nodeName:@"Cube" zPhone:-40 zPad:-40];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)load3DObjectName:(NSString*)name nodeName:(NSString*)nodeName zPhone:(CGFloat)positioniPhone zPad:(CGFloat)positioniPad{    SCNScene * scene = [SCNScene sceneNamed:name];    // retrieve the ship node    SCNNode *node = [scene.rootNode childNodeWithName:nodeName recursively:YES];    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {        node.position = SCNVector3Make(0, 0, positioniPhone);    }     else {        node.position = SCNVector3Make(0, 0, positioniPad);    }    _the3DView.scene = scene;    _the3DView.autoenablesDefaultLighting = YES;    _the3DView.allowsCameraControl = YES;    _the3DView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];}- (IBAction)changeIt:(id)sender {     [self load3DObjectName:@"dayere.dae" nodeName:@"Sphere" zPhone:-40 zPad:-40];}

最佳答案

所以我看了一下你的项目。问题是:你的场景中没有相机。所以我将每个场景的相机手动放置在相同的距离,并根据需要移动节点。这是现在的样子:

- (void)load3DObjectName:(NSString*)name nodeName:(NSString*)nodeName zPhone:(CGFloat)positioniPhone zPad:(CGFloat)positioniPad
{
SCNScene * scene = [SCNScene sceneNamed:name];
SCNNode *node = [scene.rootNode childNodeWithName:nodeName recursively:YES];

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
node.position = SCNVector3Make(0, 0, positioniPhone);

} else {

node.position = SCNVector3Make(0, 0, positioniPad);
}

SCNCamera *cam = [[SCNCamera alloc] init];
cam.xFov = 35;
cam.yFov = 35;
cam.zFar = 5000;
cam.zNear = 0;
SCNNode *camNode = [[SCNNode alloc] init];
camNode.camera = cam;

camNode.position = SCNVector3Make(0, 0, 400);

[scene.rootNode addChildNode:camNode];

_the3DView.pointOfView = camNode;

_the3DView.scene = scene;
_the3DView.autoenablesDefaultLighting = YES;
_the3DView.allowsCameraControl = YES;
_the3DView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1.00];
}

并且出于测试目的,我还更改了您的对象位置:

- (IBAction)changeIt:(id)sender
{
[self load3DObjectName:@"dayere.dae" nodeName:@"Sphere" zPhone:-340 zPad:-340];
}

还有这个:

- (void)viewDidLoad
{
[super viewDidLoad];
[self load3DObjectName:@"cube.dae" nodeName:@"Cube" zPhone:-40 zPad:-40];
}

最后但同样重要的是,屏幕截图: cube sphere

关于ios - 以编程方式更改 SCNScene 的 sceneNamed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40720960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com