我有一个从Blender导出的动画模型(collada)。根据我的理解,着色器尝试在动画过程中插值法线,我想防止这种情况发生。我的模型非常笨重,但Scenekit让它像Apple Product一样平滑。我希望它看起来像“我的世界”。
发布于 2017-03-10 09:01:58
我假设你想要一个平坦的阴影低多边形,看起来类似于下面的左边所示。
本例使用SCNSphere
作为几何源,但它也应该适用于从dae文件加载的几何体。
您加载的模型和默认的SCNSphere由许多面组成。在平滑模型中,顶点通常在面之间共享,每个顶点只能有一条法线。我们需要做的是确保每个面不与相邻的面共享顶点,并且每个顶点上使用的法线仅基于面的法线方向计算。
SceneKit本身没有任何原生功能来做这件事,但是苹果的另一个框架有……ModelIO。
您需要导入ModelIO。
import ModelIO
import SceneKit.ModelIO
我用来生成上面两个球体的代码如下所示。
let geom = SCNSphere(radius: 0.5)
// convert SceneKit geometry to ModelIO mesh object
let modelMesh = MDLMesh(scnGeometry: geom)
// ensure vertices are not shared with neighbouring faces
modelMesh.makeVerticesUnique()
// replace existing 'smooth' normal definition with an 'non-smoothed' normal
modelMesh.addNormals(withAttributeNamed: "normal", creaseThreshold: 1.0)
// create SceneKit geometry from ModelIO mesh object
let flattenedGeom = SCNGeometry(mdlMesh: modelMesh)
let flattenedNode = SCNNode(geometry: flattenedGeom)
scene.rootNode.addChildNode(flattenedNode)
// smooth sphere for comparison only
let sphereNodeSmooth = SCNNode(geometry: SCNSphere(radius: 0.5))
sphereNodeSmooth.position = SCNVector3Make(0, 1.2, 0)
scene.rootNode.addChildNode(sphereNodeSmooth)
当然,我可能会建议您在Blender中修改模型,以便以这种方式导出(唯一顶点,非平滑法线)。这将使您的应用程序不必在加载资产时进行比所需更多的处理。不幸的是,我的搅拌器知识有点欠缺。
发布于 2018-03-27 12:20:50
当使用所有相邻面平均顶点法线时,将出现平滑着色。在这种情况下,渲染引擎通过为相邻面插入颜色来渲染边缘,看起来面会变得更加“平滑”。
您可以删除顶点的法线信息以防止出现这种情况。
如果您更喜欢使用SCNGeometry
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;
}
或ModelI/O
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
//creaseThreshold is the cos value of MAX crease angle you can tolerate
//[newMesh addNormalsWithAttributeNamed:@"normals" creaseThreshold:1];
SCNGeometry *flatGeo = [SCNGeometry geometryWithMDLMesh:newMesh];
return flatGeo;
}
https://stackoverflow.com/questions/42688892
复制