iOS開發之SceneKit框架--SCNParametricGeometry.h


1、SCNParametricGeometry簡介

  SCNParametricGeometry用於創建簡單的3D模型,比如SCNPlane 平面、SCNPyramid 錐形(金字塔)、SCNBox 立方體、SCNSphere 球體、SCNCylinder 圓柱、SCNCone 圓錐體、SCNTube 圓柱管道、SCNCapsule 膠囊、SCNTorus 圓環面、SCNFloor 地板、SCNText 字體、SCNShape 自定義幾何體!

 

2、SCNPlane 平面

  • 效果圖

  • 相關代碼
- (void)addPlane{
    SCNPlane *plane = [SCNPlane planeWithWidth:10 height:3];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:plane];
    
    plane.firstMaterial.multiply.contents = @"pic5.ipg";
    plane.firstMaterial.diffuse.contents=@"pic5.ipg";
    plane.firstMaterial.multiply.intensity = 0.5;
    plane.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 6, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:2 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

//初始化
+ (instancetype)planeWithWidth:(CGFloat)width height:(CGFloat)height;

//平面寬度
@property(nonatomic) CGFloat width;

//平面高度
@property(nonatomic) CGFloat height;

//沿X軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger widthSegmentCount;

//沿Y軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger heightSegmentCount;

//平面圓角半徑
@property(nonatomic) CGFloat cornerRadius API_AVAILABLE(macos(10.9));

//圓角的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值是10。
@property(nonatomic) NSInteger cornerSegmentCount API_AVAILABLE(macos(10.9));

 

3、SCNBox 立方體

  • 效果圖

 

  • 相關代碼
- (void)addBody{
    SCNBox *box = [SCNBox boxWithWidth:3 height:3 length:3 chamferRadius:0];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:box];

    box.firstMaterial.multiply.contents = @"pic5.ipg";
    box.firstMaterial.diffuse.contents=@"pic5.ipg";
    box.firstMaterial.multiply.intensity = 0.5;
    box.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 2, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

/**
 創建立方體

 @param width 寬度
 @param height 高度
 @param length 長度
 @param chamferRadius 邊的圓角半徑
 */
+ (instancetype)boxWithWidth:(CGFloat)width height:(CGFloat)height length:(CGFloat)length chamferRadius:(CGFloat)chamferRadius;

//寬度可動畫 默認1
@property(nonatomic) CGFloat width;

//高度可動畫 默認1
@property(nonatomic) CGFloat height;

//長度可動畫 默認1
@property(nonatomic) CGFloat length;

//圓角半徑可動畫 默認0
@property(nonatomic) CGFloat chamferRadius;

//沿X軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger widthSegmentCount;

//沿Y軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger heightSegmentCount;

//沿Z軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger lengthSegmentCount;

//圓角的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值是5。
@property(nonatomic) NSInteger chamferSegmentCount;

 

4、SCNPyramid 錐形(金字塔)

  • 效果圖

 

  • 相關代碼
- (void)addPyramid{
    SCNPyramid *pyramid = [SCNPyramid pyramidWithWidth:5 height:10 length:5];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:pyramid];
    
    pyramid.firstMaterial.multiply.contents = @"pic5.ipg";
    pyramid.firstMaterial.diffuse.contents=@"pic5.ipg";
    pyramid.firstMaterial.multiply.intensity = 0.5;
    pyramid.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 2, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

 

/**
 初始化

 @param width 寬度
 @param height 高度
 @param length 長度
 */
+ (instancetype)pyramidWithWidth:(CGFloat)width height:(CGFloat)height length:(CGFloat)length;

//寬度可動畫 默認1
@property(nonatomic) CGFloat width;

//高度可動畫 默認1
@property(nonatomic) CGFloat height;

//長度可動畫 默認1
@property(nonatomic) CGFloat length;

//沿Z軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1
@property(nonatomic) NSInteger widthSegmentCount;

//沿Y軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1
@property(nonatomic) NSInteger heightSegmentCount;

//沿Z軸的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1
@property(nonatomic) NSInteger lengthSegmentCount;

 

5、SCNSphere 球體

  • 效果圖

  • 相關代碼
- (void)addSphere{
    SCNSphere *sphere = [SCNSphere sphereWithRadius:5];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:sphere];
    
    sphere.firstMaterial.multiply.contents = @"pic5.ipg";
    sphere.firstMaterial.diffuse.contents=@"pic5.ipg";
    sphere.firstMaterial.multiply.intensity = 0.5;
    sphere.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 6, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

 

//初始化 球半徑
+ (instancetype)sphereWithRadius:(CGFloat)radius;

//半徑
@property(nonatomic) CGFloat radius;

//是否用多邊形網格來渲染球體 默認NO
@property(nonatomic, getter=isGeodesic) BOOL geodesic;

//沿經緯線的子划分數。可以做成動畫。
//如果值小於3,則行為未定義。默認值為24
@property(nonatomic) NSInteger segmentCount;

 

6、SCNCylinder 圓柱

  • 效果圖 

  • 相關代碼
- (void)addCylinder{
    SCNCylinder *cylinder = [SCNCylinder cylinderWithRadius:6 height:10];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:cylinder];
    
    cylinder.firstMaterial.multiply.contents = @"pic5.ipg";
    cylinder.firstMaterial.diffuse.contents=@"pic5.ipg";
    cylinder.firstMaterial.multiply.intensity = 0.5;
    cylinder.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 6, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

/**
 初始化

 @param radius 圓柱半徑
 @param height 高度
 */
+ (instancetype)cylinderWithRadius:(CGFloat)radius height:(CGFloat)height;

//半徑
@property(nonatomic) CGFloat radius;

//高度
@property(nonatomic) CGFloat height;

//沿徑向坐標的子划分數。可以做成動畫。
//如果值小於3,則該行為是未定義的。默認值是48。
@property(nonatomic) NSInteger radialSegmentCount;

//在Y軸上的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger heightSegmentCount;

 

7、SCNCone 圓錐體

  • 效果圖

 

  • 相關代碼
- (void)addCone{
    SCNCone *cone = [SCNCone coneWithTopRadius:2 bottomRadius:5 height:10];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:cone];
    
    cone.firstMaterial.multiply.contents = @"pic5.ipg";
    cone.firstMaterial.diffuse.contents=@"pic5.ipg";
    cone.firstMaterial.multiply.intensity = 0.5;
    cone.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 6, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片) 

/**
 初始化

 @param topRadius 頂部半徑(頂部為0時就是圓錐)
 @param bottomRadius 底部半徑
 @param height 高度
 */
+ (instancetype)coneWithTopRadius:(CGFloat)topRadius bottomRadius:(CGFloat)bottomRadius height:(CGFloat)height;

//頂部半徑可動畫 默認0
@property(nonatomic) CGFloat topRadius;

//底部半徑可動畫 默認0.5
@property(nonatomic) CGFloat bottomRadius;

//高度可動畫 默認1
@property(nonatomic) CGFloat height;

//沿徑向坐標的子划分數。可以做成動畫。
//如果值小於3,則該行為是未定義的。默認值是48。
@property(nonatomic) NSInteger radialSegmentCount;

//在Y軸上的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger heightSegmentCount;

 

8、SCNTube 圓柱管道

  • 效果圖 

  • 相關代碼
- (void)addTube{
    SCNTube *tube = [SCNTube tubeWithInnerRadius:4 outerRadius:5 height:5];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:tube];
    
    tube.firstMaterial.multiply.contents = @"pic5.ipg";
    tube.firstMaterial.diffuse.contents=@"pic5.ipg";
    tube.firstMaterial.multiply.intensity = 0.5;
    tube.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 6, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:2 y:0 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

/**
 初始化

 @param innerRadius 內圓半徑
 @param outerRadius 外圓半徑
 @param height 高度
 */
+ (instancetype)tubeWithInnerRadius:(CGFloat)innerRadius outerRadius:(CGFloat)outerRadius height:(CGFloat)height;

//內圓半徑可動畫 默認0.25
@property(nonatomic) CGFloat innerRadius;

//外圓半徑可動畫 默認0.5
@property(nonatomic) CGFloat outerRadius;

//高度可動畫 默認1
@property(nonatomic) CGFloat height;

//沿徑向坐標的子划分數。可以做成動畫。
//如果值小於3,則該行為是未定義的。默認值是48。
@property(nonatomic) NSInteger radialSegmentCount;

//在Y軸上的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger heightSegmentCount;

 

9、SCNCapsule 膠囊

  • 效果圖 

  • 相關代碼
- (void)addCapsule{
    SCNCapsule *capsule = [SCNCapsule capsuleWithCapRadius:2 height:10];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:capsule];
    
    capsule.firstMaterial.multiply.contents = @"pic5.ipg";
    capsule.firstMaterial.diffuse.contents=@"pic5.ipg";
    capsule.firstMaterial.multiply.intensity = 0.5;
    capsule.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 6, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片) 

/**
 初始化

 @param capRadius 球半徑
 @param height 高度
 */
+ (instancetype)capsuleWithCapRadius:(CGFloat)capRadius height:(CGFloat)height;

//兩端球半徑 可動畫默認0.5
@property(nonatomic) CGFloat capRadius;

//高度 可動畫默認2
@property(nonatomic) CGFloat height;

//沿徑向坐標的子划分數。可以做成動畫。
//如果值小於3,則該行為是未定義的。默認值是48。
@property(nonatomic) NSInteger radialSegmentCount;

//在Y軸上的子划分數。可以做成動畫。
//如果值小於1,則行為未定義。默認值為1。
@property(nonatomic) NSInteger heightSegmentCount;

//帽子上的子划分數。可以做成動畫。
//如果值小於2,則行為未定義。默認值是24。
@property(nonatomic) NSInteger capSegmentCount;

 

10、SCNTorus 圓環面

  • 效果圖

  • 相關代碼
- (void)addToruse{
    SCNTorus *torus = [SCNTorus torusWithRingRadius:4 pipeRadius:1];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:torus];
    
    torus.firstMaterial.multiply.contents = @"pic5.ipg";
    torus.firstMaterial.diffuse.contents=@"pic5.ipg";
    torus.firstMaterial.multiply.intensity = 0.5;
    torus.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(0, 2, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

/**
 初始化

 @param ringRadius 圓環外半徑
 @param pipeRadius 圓環半徑
 */
+ (instancetype)torusWithRingRadius:(CGFloat)ringRadius pipeRadius:(CGFloat)pipeRadius;

//圓環外半徑 可動畫默認0.5
@property(nonatomic) CGFloat ringRadius;

//圓環半徑 可動畫默認0.25
@property(nonatomic) CGFloat pipeRadius;

//環的子划分數。可以做成動畫。
//如果值小於3,則該行為是未定義的。默認值是48。
@property(nonatomic) NSInteger ringSegmentCount;

//管子的子划分數。可以做成動畫。
//如果值小於3,則該行為是未定義的。默認值是24。
@property(nonatomic) NSInteger pipeSegmentCount;

 

11、SCNFloor 地板

  • 效果圖(下面紅色就是地板) 

  • 相關代碼
- (void)addFloor{
    SCNNode *node = [SCNNode node];
    node.geometry = ({
        SCNFloor *floor = [SCNFloor floor];
        floor.firstMaterial.diffuse.contents = [UIImage imageNamed:@"sun.jpg"];
        floor;
    });
    node.position = SCNVector3Make(0, 0, 0);
    [self.scnScene.rootNode addChildNode:node];
}
  • 相關屬性
//初始化
+ (instancetype)floor;

//指定地板的反射率。可以做成動畫。
//如果值大於零,則表面將反映場景中的其他對象。默認值為0.25。
@property(nonatomic) CGFloat reflectivity;

//指定從地板開始下降的距離。可以做成動畫。默認值為0。
@property(nonatomic) CGFloat reflectionFalloffStart;

//指定從地板上的跌落結束的距離。可以做成動畫。
//如果值為0,則沒有衰減。默認值為0。
@property(nonatomic) CGFloat reflectionFalloffEnd;

//確定要反射的節點類別
@property(nonatomic) NSUInteger reflectionCategoryBitMask API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0));

//在X軸上的地板范圍。可以做成動畫。
//如果值等於0,那么在X軸上的地板是無限的。默認值為0。
@property(nonatomic) CGFloat width API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0));

//在Z軸上的地板范圍。可以做成動畫。
//如果值為0,那么在Z軸上的地板是無限的。默認值為0。
@property(nonatomic) CGFloat length API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0));

//指定用於呈現反射的緩沖區的分辨率比例因子。默認0.5
@property(nonatomic) CGFloat reflectionResolutionScaleFactor API_AVAILABLE(macos(10.10));

 

12、SCNText 字體

  • 效果圖

 

  • 相關代碼
- (void)addText{
    SCNText *text = [SCNText textWithString:@"你好" extrusionDepth:3];
    text.font = [UIFont systemFontOfSize:8];
    text.alignmentMode = kCAAlignmentCenter;
    SCNNode *geoNode = [SCNNode nodeWithGeometry:text];
    
    text.firstMaterial.multiply.contents = @"pic5.ipg";
    text.firstMaterial.diffuse.contents=@"pic5.ipg";
    text.firstMaterial.multiply.intensity = 0.5;
    text.firstMaterial.lightingModelName =  SCNLightingModelConstant;
    geoNode.position = SCNVector3Make(-8, 0, -20);
    [self.scnScene.rootNode addChildNode:geoNode];
    [self addFloor];
    [self addLight];
}
  • 相關屬性(官方圖片)

/**
 創建3D文字

 @param string 文本
 @param extrusionDepth 擠壓深度
 */
+ (instancetype)textWithString:(nullable id)string extrusionDepth:(CGFloat)extrusionDepth;

//擠壓深度 可動畫如果值為0就是單面2D版本
@property(nonatomic) CGFloat extrusionDepth;

//文本必須是NSString或NSAttributedString的實例。
@property(nonatomic, copy, nullable) id string;

//字體屬性大小 默認值是Helvetica size 36。
@property(nonatomic, retain, null_resettable) UIFont *font;

//文本是否被包裝,要包裝的文本,首先需要設置它的邊界,否則文本不會被包裝。默認值是NO。
@property(nonatomic, getter=isWrapped) BOOL wrapped;

//設置文本包裝
@property(nonatomic) CGRect containerFrame;

//截斷文本以適應邊界 枚舉默認值kCATruncationNone,具體參考CATextLayer.h
@property(nonatomic, copy) NSString *truncationMode;

//設置文本對齊方式
@property(nonatomic, copy) NSString *alignmentMode;
CA_EXTERN NSString * const kCAAlignmentNatural
CA_AVAILABLE_STARTING (10.5, 3.2, 9.0, 2.0);
CA_EXTERN NSString * const kCAAlignmentLeft
CA_AVAILABLE_STARTING (10.5, 3.2, 9.0, 2.0);
CA_EXTERN NSString * const kCAAlignmentRight
CA_AVAILABLE_STARTING (10.5, 3.2, 9.0, 2.0);
CA_EXTERN NSString * const kCAAlignmentCenter
CA_AVAILABLE_STARTING (10.5, 3.2, 9.0, 2.0);
CA_EXTERN NSString * const kCAAlignmentJustified
CA_AVAILABLE_STARTING (10.5, 3.2, 9.0, 2.0);

//每個倒棱邊的寬度或深度。可以做成動畫。可以做成動畫。
//值范圍[0,擠壓深度/ 2]。實際的倒角半徑可能與此處指定的倒角不同:大的值被剪切到每個字符的最大值。默認值為0。
@property(nonatomic) CGFloat chamferRadius;

//確定每個倒棱邊的橫截面輪廓的路徑。
@property(nonatomic, copy, nullable) UIBezierPath *chamferProfile;

//指定顯示字體平滑度
//更小的數字會使曲線更平滑,而在頂點上的計算和更重的幾何圖形的代價更大。默認值是1.0,它會產生平滑的曲線。
@property(nonatomic) CGFloat flatness API_AVAILABLE(macos(10.9));

 

13、SCNShape 自定義幾何體

  • 效果圖 

  • 相關代碼 
- (void)addPath{
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(5, 5)];
    [path addLineToPoint:CGPointMake(5, 6)];
    
    [path addArcWithCenter:CGPointMake(2.5, 6) radius:2.5 startAngle:0 endAngle:M_PI clockwise:YES];
    [path addArcWithCenter:CGPointMake(-2.5, 6) radius:2.5 startAngle:0 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(-5, 5)];
    [path closePath];
    
    //添加路徑
    SCNShape *Cylinder = [SCNShape shapeWithPath:path extrusionDepth:6];
    Cylinder.chamferMode =SCNChamferModeBoth;
    Cylinder.firstMaterial.diffuse.contents =[UIImage imageNamed:@"pic5.ipg"];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:Cylinder];
    geoNode.position = SCNVector3Make(0, 0, -20);
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self.scnScene.rootNode addChildNode:geoNode];
    [self addFloor];
    [self addLight];
}
  • 相關屬性
//創建並返回給定形狀與給定的擠壓深度的3D模型
+ (instancetype)shapeWithPath:(nullable UIBezierPath *)path extrusionDepth:(CGFloat)extrusionDepth;

//定義要呈現的形狀的路徑。
@property(nonatomic, copy, nullable) UIBezierPath *path;

//擠壓深度。可以做成動畫。
//如果值為0,我們得到一個單面的2D版本的形狀。
@property(nonatomic) CGFloat extrusionDepth;

//文本的側邊被倒角。默認值是SCNChamferModeBoth。
@property(nonatomic) SCNChamferMode chamferMode;
typedef NS_ENUM(NSInteger, SCNChamferMode) {
    SCNChamferModeBoth,
    SCNChamferModeFront,
    SCNChamferModeBack
} API_AVAILABLE(macos(10.9));

//倒角半徑。可以做成動畫。值被夾緊到范圍[0,擠壓深度/ 2]。默認值為0。
@property(nonatomic) CGFloat chamferRadius;

//描述在“chamferRadius”不是nil時使用的概要文件。當“chamferProfile”為nil時,我們將返回表示象限的路徑。
//概要文件應該是一個2D曲線,從(0,1)開始,到(1,0)結束。“平整度”屬性也用來使這條路徑變平。默認值為nil。
@property(nonatomic, copy, nullable) UIBezierPath *chamferProfile;

 

14、其它相關代碼

  • 燈光
- (void)addLight{
    SCNLight *light = [SCNLight light];
    light.type = SCNLightTypeOmni;
    light.color = [UIColor whiteColor];
    self.scnNode.light = light;
}
  • 場景
- (SCNScene *)scnScene{
    if (!_scnScene) {
        _scnScene = [SCNScene scene];
        _scnScene.background.contents = @"Assets.xcassets/pic5.png";
        _scnScene.physicsWorld.gravity = SCNVector3Make(0, -30, 0);
    }
    return _scnScene;
}
  • 根節點
- (SCNNode *)scnNode{
    if (!_scnNode) {
        _scnNode = [[SCNNode alloc] init];
        _scnNode.camera = [[SCNCamera alloc] init];
        _scnNode.camera.zFar = 200.f;
        _scnNode.camera.zNear = .1f;
//        _scnNode.camera.projectionDirection = SCNCameraProjectionDirectionHorizontal;
        _scnNode.position = SCNVector3Make(0, 5, 20);
    }
    return _scnNode;
}
  • 視圖view
- (SCNView *)scnView{
    if (!_scnView) {
        _scnView = [[SCNView alloc] initWithFrame:self.view.bounds options:@{SCNViewOptionPreferLowPowerDevice:@"MTLDevice"}];
        _scnView.showsStatistics = YES;
        _scnView.autoenablesDefaultLighting = YES;
        [self.view addSubview:_scnView];
        _scnView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_scnView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scnView)]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_scnView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scnView)]];
    }
    return _scnView;
}
  • 例子
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scnView.scene = self.scnScene;
    [self.scnScene.rootNode addChildNode:self.scnNode];
    [self addPath];
}
- (void)addPath{
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(5, 5)];
    [path addLineToPoint:CGPointMake(5, 6)];
    
    [path addArcWithCenter:CGPointMake(2.5, 6) radius:2.5 startAngle:0 endAngle:M_PI clockwise:YES];
    [path addArcWithCenter:CGPointMake(-2.5, 6) radius:2.5 startAngle:0 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(-5, 5)];
    [path closePath];
    
    //添加路徑
    SCNShape *Cylinder = [SCNShape shapeWithPath:path extrusionDepth:6];
    Cylinder.chamferMode =SCNChamferModeBoth;
    Cylinder.firstMaterial.diffuse.contents =[UIImage imageNamed:@"pic5.ipg"];
    SCNNode *geoNode = [SCNNode nodeWithGeometry:Cylinder];
    geoNode.position = SCNVector3Make(0, 0, -20);
    [geoNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    [self.scnScene.rootNode addChildNode:geoNode];
    [self addFloor];
    [self addLight];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM