繁体   English   中英

场景套件。 如何创建具有圆边和不同半径的盒子?

[英]SceneKit. How to create box with rounded edges and with different radiuses?

我已经开始学习 SceneKit。 我试过SCNBox 它有倒角半径。 但是半径适用于所有边缘。 但我想实现类似于下面屏幕截图中的东西在此处输入图像描述

您可以通过挤出UIBezierPath来做到这一点:

    // rounded rect bezier path
    let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 1.0, height: 1.0), cornerRadius: 0.1)
    
    path.flatness = 0
    
    // extrude the path
    let shape = SCNShape(path: path, extrusionDepth: 0.05)
    
    let mat = SCNMaterial()
    mat.diffuse.contents = UIColor(white: 0.9, alpha: 1.0)
    shape.materials = [mat]
    
    let shapeNode = SCNNode(geometry: shape)

结果:

在此处输入图像描述

这是一个完整的例子(注意:我只看了 SceneKit,所以我以本教程作为起点Introduction to SceneKit ):

import UIKit
import SceneKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let sceneView = SCNView(frame: self.view.frame)
        self.view.addSubview(sceneView)

        let scene = SCNScene()
        sceneView.scene = scene
        
        let camera = SCNCamera()
        let cameraNode = SCNNode()
        cameraNode.camera = camera
        cameraNode.position = SCNVector3(x: 3.0, y: 2.0, z: 1.5)
        
        let ambientLight = SCNLight()
        ambientLight.type = .ambient
        ambientLight.color = UIColor(white: 0.9, alpha: 1.0)
        cameraNode.light = ambientLight

        let light = SCNLight()
        light.type = SCNLight.LightType.spot
        light.spotInnerAngle = 30.0
        light.spotOuterAngle = 80.0
        light.castsShadow = true
        let lightNode = SCNNode()
        lightNode.light = light
        lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5)

        
        // rounded rect bezier path
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 1.0, height: 1.0), cornerRadius: 0.1)
        
        path.flatness = 0
        
        // extrude the path
        let shape = SCNShape(path: path, extrusionDepth: 0.05)
        
        let mat = SCNMaterial()
        mat.diffuse.contents = UIColor(white: 0.9, alpha: 1.0)
        shape.materials = [mat]
        
        let shapeNode = SCNNode(geometry: shape)
        
        
        let planeGeometry = SCNPlane(width: 50.0, height: 50.0)
        let planeNode = SCNNode(geometry: planeGeometry)
        planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
        planeNode.position = SCNVector3(x: 0, y: 0.0, z: 0)

        let floorMaterial = SCNMaterial()
        floorMaterial.diffuse.contents = UIColor.lightGray
        planeGeometry.materials = [floorMaterial]
        
        scene.rootNode.addChildNode(lightNode)
        scene.rootNode.addChildNode(cameraNode)
        scene.rootNode.addChildNode(shapeNode)
        scene.rootNode.addChildNode(planeNode)
        
        let constraint = SCNLookAtConstraint(target: shapeNode)
        constraint.isGimbalLockEnabled = true
        cameraNode.constraints = [constraint]
        lightNode.constraints = [constraint]

    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM