首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在SceneKit中处理平坦的着色多边形/几何体,即不平滑

如何在SceneKit中处理平坦的着色多边形/几何体,即不平滑
EN

Stack Overflow用户
提问于 2016-01-12 06:00:49
回答 1查看 1.8K关注 0票数 4

我正在尝试使我的几何体在SceneKit中看起来平坦而不平滑。正如您在图像中看到的,默认情况下,绿色球体在SceneKit中具有平滑着色。我想要的是另一个图像中的扁平“外观”,其中它表示“扁平”。

我在SceneKit中看不到任何有关如何禁用此功能的选项?这是我的游乐场上的代码:

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2018-03-27 17:19:57

看起来Apple会为你应用正常的平滑效果。当SceneKit遇到平面网格时,可以更多地观察这一点。从网格中移除平滑的法线将使其看起来像低多边形。

如果使用ModelI/O加载三维资源。(为了测试,我创建了MDLMesh表单SCNGeometry)

代码语言:javascript
复制
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。

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34731942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档