gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:18 27 4
gpt4 key购买 nike

我试图让我的几何图形在 SceneKit 中看起来平坦而不平滑。正如您在图像中看到的,在 SceneKit 中,绿色球体默认具有平滑的阴影。我想要的是另一张图片中的扁平“外观”,上面写着“扁平”。

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

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")

最佳答案

Apple 似乎会为您应用正常的平滑处理。这在 SceneKit 遇到平面网格时更容易观察到。从网格中移除平滑的法线将使它看起来是低多边形。

如果您使用 ModelI/O 加载 3D Assets 。 (为了测试,我创建了 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

关于swift - 如何在 SceneKit 中做平面阴影多边形/几何图形,即不平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34731942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com