SceneKit中的金属着色器可概述对象 [英] Metal shader in SceneKit to outline an object

查看:106
本文介绍了SceneKit中的金属着色器可概述对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩耍,试图在SceneKit中实现Metal着色器,以勾勒出一个对象.

I'm playing around and trying to implement Metal shader in SceneKit that will outline an object.

想法是绘制类似于此图片的轮廓(或轮廓),该图片位于此博客文章(博客文章不包含任何代码):

The idea is to draw an outline (or silhouette) similar to this image found in this blogpost (the blogpost doesn't contain any code):

我是SceneKit和Metal着色器的新手,所以我仅能绘制一些几何图形并编写简单的顶点或片段着色器.我很好奇如何达到这种效果?它是通过多次传递完成的吗?

I'm new to SceneKit and Metal shaders so I'm able just to draw some geometry and write simple vertex or fragment shader. I'm curious how can I achieve this kind of effect? Is it done in multiple passes?

干杯!

推荐答案

此处的基本思想是克隆选定的"节点及其几何,然后使用自定义顶点和片段着色器将几何沿推"出.顶点法线,仅绘制纯色克隆几何的背面.

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.

Swift的核心代码如下:

The core Swift code looks like this:

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;

从那里,您只需应用典型的model-view-projection矩阵,然后从片段着色器返回纯色即可.

From there, you just apply your typical model-view-projection matrix and return a flat color from the fragment shader.

这篇关于SceneKit中的金属着色器可概述对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆