ios - 创建更平滑的边缘或以其他方式修复 SCNView 中框上的锯齿状边缘?

标签 ios swift 3d scenekit glkit

下面的代码在 SCNView 中生成一个红色框。但是,面向您的侧面/元素的顶部和底部的边缘呈锯齿状(如附件所示)。目标是渲染类似于 Minecraft 盒子的更平滑的边缘。更改相机位置会减少某些边缘的像素化,这是相机角度问题吗?如果是,是否有可能无论摄像机角度如何都渲染出边缘平滑的框?

例如,将相机位置设置为 SCNVector3(x: 0.0, y: 0.0, z: 10.0) 可以有效地在 2D 中渲染框并具有清晰的边缘(第二个附件)。

enter image description here enter image description here

    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: 0.0, z: 10.0)

    let ambientLight = SCNLight()
    ambientLight.type = SCNLightTypeAmbient
    ambientLight.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
    cameraNode.light = ambientLight

    let light = SCNLight()
    light.type = SCNLightTypeOmni
    let lightNode = SCNNode()
    lightNode.light = light
    lightNode.position = SCNVector3(x: 0, y: 20, z: 10)

    let cubeGeometry = SCNBox(width: 3.0, height: 3.0, length: 3.0, chamferRadius: 0.0)
    let cubeNode = SCNNode(geometry: cubeGeometry)

    let planeGeometry = SCNPlane(width: 100.0, height: 100.0)
    let planeNode = SCNNode(geometry: planeGeometry)
    planeNode.eulerAngles = SCNVector3(x: GLKMathDegreesToRadians(-90), y: 0, z: 0)
    planeNode.position = SCNVector3(x: 0, y: -0.5, z: 0)

    let redMaterial = SCNMaterial()
    redMaterial.diffuse.contents = UIColor.redColor()
    cubeGeometry.materials = [redMaterial]

    let greenMaterial = SCNMaterial()
    greenMaterial.diffuse.contents = UIColor.greenColor()
    planeGeometry.materials = [greenMaterial]

    let constraint = SCNLookAtConstraint(target: cubeNode)
    constraint.gimbalLockEnabled = true
    cameraNode.constraints = [constraint]
    lightNode.constraints = [constraint]

    scene.rootNode.addChildNode(lightNode)
    scene.rootNode.addChildNode(cameraNode)
    scene.rootNode.addChildNode(cubeNode)
    scene.rootNode.addChildNode(planeNode)

最佳答案

如评论中所述,如果你想对每一帧进行抗锯齿,请设置 antialiasingMode。它不像 CoreGraphics 那样出色,但相当不错。但是,它确实会增加大量的工作量,因此您会消耗电池并降低帧速率。

在 OS X 上,默认是每帧 4 倍多重采样。在 iOS 上,默认情况下没有多重采样,因为它非常昂贵。

幸运的是,有一个我更喜欢的解决方案,那就是打开jittering。这有点像多重采样但很懒惰。当对象移动时,它们会正常渲染(有锯齿),但当它们停止移动时,渲染器会在后台运行 96 帧以上并消除所有锯齿。

最终的外观比抗锯齿好得多(因为没有“96x 抗锯齿模式”),而且在我展示过的快速硬件上,大多数人都没有意识到物体移动时质量会发生变化。

关于ios - 创建更平滑的边缘或以其他方式修复 SCNView 中框上的锯齿状边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34645484/

相关文章:

ios - 为什么信号在 ReactiveCocoa 中被调用两次?

iphone - 捕获照片点击事件的事件的代码:iPhone

Swift Timer - 如何阻止 UIButton 文本重置?

ios - 如何使用自动调整高度的 SnapKit 以编程方式创建 UITableViewCell?

ios - 从远程数据库填充 UITableView 单元格

Java3D 形状只能从一个角度看到

ios - 在 iOS 上将 SwiftUI View 转换为 PDF

ios - 初始化单例异步 iOS

java - 如何将按钮添加到 libgdx 中的 ApplicationAdapter?

python - 使用欧拉角/矩阵在 3d 空间中旋转?