tool
- Xcode9
- iOS 11
New project
Here, you don't need to do anything, start the project and you can see a plane.
Keywords and key categories
- Key words: scene view, scene, geometry, node, renderer (all literal translation, self understanding according to class name)
- Key categories: ARSCNView, SCNScene, SCNGeometry, SCNNode, SCNMaterial
How to put a plane in space?
- Create the scene view ARSCNView and set the proxy
- Create a scene SCNScene and bind the scene to the scene view
- Create a plane geometry SCNPlane (inherited from SCNGeometry)
- Create a node SCNNode based on geometry
- Create a renderer, SCNMaterial, to render flat geometry
- Add a node to the root node of the scene
In a word, apple API always assembles key classes.
No more nonsense, just code
@interface ViewController () <ARSCNViewDelegate> // The control of creating scene view has been added by default. Of course, it can also be added manually @property (nonatomic, strong) IBOutlet ARSCNView *sceneView; @property (nonatomic, strong) AVPlayer *player; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1. Set the proxy of scene view self.sceneView.delegate = self; // Whether to display information such as fps or timing self.sceneView.showsStatistics = YES; //2. Create a scene SCNScene *scene = [SCNScene scene]; //2.1 binding scene to scene view self.sceneView.scene = scene; //3. Create a plane geometry with a height of 0.1M and a width of 0.1M SCNPlane *plane = [SCNPlane planeWithWidth:0.1 height:0.1]; //4. Create nodes based on geometry SCNNode *node = [SCNNode nodeWithGeometry:plane]; // The creation of nodes is not only based on the plane, but also on the box, sphere, cone, ring, pyramid and so on. If you are interested, try it again. node.position = SCNVector3Make(0, 0, -0.3); // Node settings location //5. Create a renderer SCNMaterial *material = [SCNMaterial material]; // Note that there's something to do with the renderer. It's no longer color, it's video // NSURL *url = [NSURL URLWithString:@"mp4.mp4"]; NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"mp4"]; // Create AVPlayer to prepare for rendering AVPlayer *player = [AVPlayer playerWithURL:url]; material.diffuse.contents = player; // The renderer can decide how to render. This contents property can be set to many things, such as UILabel, UIImage and even AVPlayer node.position = SCNVector3Make(0, 0, -0.3); //5.5. Rendering geometry with renderer plane.materials = @[material]; //6. Add a node for the root node of the scene [scene.rootNode addChildNode:node]; [player play]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // Create a session configuration ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new]; // Run the view's session [self.sceneView.session runWithConfiguration:configuration]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // Pause the view's session [self.sceneView.session pause]; }