如何在SceneKit中做平面阴影多边形/几何,即不平滑

时间:2016-01-11 22:00:49

标签: swift polygon scenekit

我正试图让我的几何图形在SceneKit中看起来平滑而不平滑。正如您在图像中看到的那样,绿色球体在SceneKit中默认具有平滑阴影。我想要的是另一幅图像中的平面“外观”,它表示“平坦”。

SCNSphere SmoothVSFlat 我没有在SceneKit中看到有关如何禁用此功能的任何选项? 这是我操场上的代码:

import Cocoa
import SceneKit
import QuartzCore
import XCPlayground

var sceneView = SCNView(frame:CGRect(x:0, y:0, width:300, height:300))
var scene = SCNScene()
sceneView.backgroundColor = NSColor.darkGrayColor()
sceneView.scene = scene
sceneView.autoenablesDefaultLighting = true
XCPlaygroundPage.currentPage.liveView = sceneView

let g = SCNSphere(radius:1)
g.firstMaterial?.diffuse.contents = NSColor.greenColor()
g.firstMaterial?.litPerPixel = false
g.segmentCount = 5
let node = SCNNode(geometry:g)
node.position = SCNVector3(x:0, y:0, z:0)
scene.rootNode.addChildNode(node)

var spin = CABasicAnimation(keyPath:"rotation")
spin.toValue = NSValue(SCNVector4:SCNVector4(x:1, y:1, z:0, w:CGFloat(2.0*M_PI)))
spin.duration = 3
spin.repeatCount = HUGE
node.addAnimation(spin, forKey:"spin")

1 个答案:

答案 0 :(得分:1)

似乎Apple将为您应用正常平滑。当SceneKit遇到平面网格时,会更多地观察到这一点。 从网格中删除平滑法线将使其看起来低多边形。

如果使用ModelI / O加载3D资源。 (对于测试,我创建了MDLMesh表单SCNGeometry

SCNGeometry* flatGeoUsingModelIO(SCNGeometry *geo){
    MDLMesh *mesh = [MDLMesh meshWithSCNGeometry:geo];
    MDLMesh *newMesh = [MDLMesh newSubdividedMesh:mesh submeshIndex:0 subdivisionLevels:0];
    [newMesh removeAttributeNamed:@"normals"];
    //Replace current vertex normals with a non-smooth one. Same result with above line
    //[newMesh addNormalsWithAttributeNamed:@"normals" creaseThreshold:1];
    SCNGeometry *flatGeo = [SCNGeometry geometryWithMDLMesh:newMesh];
    return flatGeo;
}

或者你对SceneKit更熟悉。

SCNGeometry* flatGeoUsingScnKit(SCNGeometry *geo){
    NSArray<SCNGeometrySource*> *SourceArr = geo.geometrySources;
    NSMutableArray<SCNGeometrySource*> *newSourceArr = [NSMutableArray new];
    for (SCNGeometrySource *source in SourceArr) {
        if (source.semantic != SCNGeometrySourceSemanticNormal) {
            [newSourceArr addObject:source];
        }
    }
    SCNGeometry *flatGeo = [SCNGeometry geometryWithSources:newSourceArr elements:geo.geometryElements];
    return flatGeo;
}

这是从右边创建的低多边形SCNSphere

enter image description here

相关问题