简体   繁体   English

SceneKit中的金属着色器勾勒出一个对象

[英]Metal shader in SceneKit to outline an object

I'm playing around and trying to implement Metal shader in SceneKit that will outline an object. 我正在玩游戏并尝试在SceneKit中实现Metal Shader,它将勾勒出一个对象。

The idea is to draw an outline (or silhouette) similar to this image found in this blogpost (the blogpost doesn't contain any code): 想法是绘制一个与此博客文章中的图像类似的轮廓(或轮廓)(博客文章不包含任何代码):

轮廓

I'm new to SceneKit and Metal shaders so I'm able just to draw some geometry and write simple vertex or fragment shader. 我是SceneKit和Metal着色器的新手,所以我只能绘制一些几何体并编写简单的顶点或片段着色器。 I'm curious how can I achieve this kind of effect? 我很好奇我怎么能达到这种效果呢? Is it done in multiple passes? 它是在多次通过中完成的吗?

Cheers! 干杯!

The basic idea here is to clone the "selected" node and its geometry, then use a custom vertex and fragment shader to "push" the geometry out along the vertex normals, drawing only the back faces of the cloned geometry with a solid color. 这里的基本思想是克隆“选定”节点及其几何体,然后使用自定义顶点和片段着色器沿顶点法线“推”几何体,仅使用纯色绘制克隆几何体的背面。

I wrote a small sample project to demonstrate this and posted it here . 我写了一个小样本项目,以证明这一点,并张贴在这里

The core Swift code looks like this: 核心Swift代码如下所示:

let outlineProgram = SCNProgram()
outlineProgram.vertexFunctionName = "outline_vertex"
outlineProgram.fragmentFunctionName = "outline_fragment"

let outlineNode = duplicateNode(node)
scene.rootNode.addChildNode(outlineNode)
outlineNode.geometry?.firstMaterial?.program = outlineProgram
outlineNode.geometry?.firstMaterial?.cullMode = .front

The portion of the vertex shader responsible for pushing vertices along their normal looks like this: 负责沿正常方向推顶点的顶点着色器部分如下所示:

const float extrusionMagnitude = 0.05;
float3 modelPosition = in.position + normalize(in.normal) * extrusionMagnitude;

From there, you just apply your typical model-view-projection matrix and return a flat color from the fragment shader. 从那里,您只需应用典型的模型 - 视图 - 投影矩阵,并从片段着色器返回平面颜色。

截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM