将一个SCNGeometry渲染成线框图[英] Render an SCNGeometry as a wireframe

本文是小编为大家收集整理的关于将一个SCNGeometry渲染成线框图的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在iOS上使用SceneKit,并且我想作为线框渲染的几何形状.所以基本上我只想绘制线条,所以没有纹理.

我发现我可以使用使用的SCNMaterial的shaderModifiers属性来完成此操作.着色器修饰符的示例:

material.shaderModifiers = [
    SCNShaderModifierEntryPointFragment: "_output.color.rgb = vec3(1.0) - _output.color.rgb;"
]

这个示例显然简单地将输出颜色倒转.我对这种" GLSL"语言一无所知,我必须用于着色器片段.

任何人都可以告诉我,我应该用什么代码作为着色器片段仅在边缘附近绘制,以使几何形状看起来像线框?

或也许还有其他方法可以将几何形状作为线框.我很想听听.

推荐答案

尝试设置材料 fillmode "> fillmode iOS 11+和MacOS 10.13+):

sphereNode.geometry?.firstMaterial?.fillMode = .lines

其他推荐答案

现在有可能(至少在可可中)使用:

gameView.debugOptions.insert(SCNDebugOptions.showWireframe)

或者,如果启用统计信息:

gameView.showsStatistics = true

(gameview是SCNView的实例)

其他推荐答案

这不是(完全)的答案,因为这个问题没有简单的答案.

在着色器代码中完全渲染线框比看起来应该更加困难,尤其是在没有几何形状着色器的移动设备上.问题在于,顶点着色器(随后片段着色器)没有知道多边形边缘所需的信息.

我对这种" GLSL"语言一无所知,我必须用于着色器片段.

如果您真的想解决此问题,则需要了解有关GLSL(OpenGL阴影语言)的更多信息.有很多书籍和教程.

一旦您有一些glsl,请看一下一些问题(例如从相关的相关中拉出侧边栏)和其他东西人们已经写了关于这个问题的. (请注意,当您寻找特定于移动的限制时,OpenGL ES具有与桌面上的WebGL相同的限制.)

使用SceneKit,您可能没有barycentric-coordinates vertex属性(又名SCNGeometrySource),可以使用您的几何形状,并且您可能不想做硬产生一个的工作.在OS X中,您可以使用SCNProgram geometryShader在顶点/片段着色器运行之前添加barycentric坐标 - 但随后您必须做自己的阴影(即,您无法像您一样在Spineekit上踩着像您一样的阴影.可以使用着色器修饰符).而且iOS中这是不可用的 - 那里的硬件不做几何设计器.如果这些几何形状恰好对齐,您也许可以使用纹理坐标伪造它.


使用行绘制对象可能会更容易 - 尝试从原始(固体)几何形状的来源和元素制作新的SCNGeometry,但是当重新创建SCNGeometryElement时,使用SCNPrimitiveTypeLine. p>

本文地址:https://www.itbaoku.cn/post/2091100.html

问题描述

I'm using SceneKit on iOS and I have a geometry I want to render as a wireframe. So basically I want to draw only the lines, so no textures.

I figured out that I could use the shaderModifiers property of the used SCNMaterial to accomplish this. Example of a shader modifier:

material.shaderModifiers = [
    SCNShaderModifierEntryPointFragment: "_output.color.rgb = vec3(1.0) - _output.color.rgb;"
]

This example apparently simply inverts the output colors. I know nothing about this 'GLSL' language I have to use for the shader fragment.

Can anybody tell me what code I should use as the shader fragment to only draw near the edges, to make the geometry look like a wireframe?

Or maybe there is a whole other approach to render a geometry as a wireframe. I would love to hear it.

推荐答案

Try setting the material fillMode to .lines (iOS 11+, and macOS 10.13+):

sphereNode.geometry?.firstMaterial?.fillMode = .lines

其他推荐答案

Now it is possible (at least in Cocoa) with:

gameView.debugOptions.insert(SCNDebugOptions.showWireframe)

or you can do it interactively if enabling the statistics with:

gameView.showsStatistics = true

(gameView is an instance of SCNView)

其他推荐答案

This is not (quite) an answer, because this a question without an easy answer.

Doing wireframe rendering entirely in shader code is a lot more difficult than it seems like it should be, especially on mobile where you don't have a geometry shader. The problem is that the vertex shader (and subsequently the fragment shader) just doesn't have the information needed to know where polygon edges are.

I know nothing about this 'GLSL' language I have to use for the shader fragment.

If you really want to tackle this problem, you'll need to learn some more about GLSL (the OpenGL Shading Language). There are loads of books and tutorials out there for that.

Once you've got some GLSL under your belt, take a look at some of the questions (like this one pulled from the Related sidebar) and other stuff people have written about the problem. (Note that when you're looking for mobile-specific limitations, OpenGL ES has the same limitations as WebGL on the desktop.)

With SceneKit, you have the additional wrinkle that you probably don't have a barycentric-coordinates vertex attribute (aka SCNGeometrySource) for the geometry you're working with, and you probably don't want to do the hard work of generating one. In OS X, you can use an SCNProgram with a geometryShader to add barycentric coordinates before the vertex/fragment shaders run — but then you have to do your own shading (i.e. you can't piggyback on the SceneKit shading like you can with shader modifiers). And that isn't available in iOS — the hardware there doesn't do geometry shaders. You might be able to fake it using texture coordinates if those happen to be lined up right in your geometry.


It might be easier to just draw the object using lines — try making a new SCNGeometry from the sources and elements of your original (solid) geometry, but when recreating the SCNGeometryElement, use SCNPrimitiveTypeLine.