学习100个Unity Shader (18) --- 几何着色器(Geometry Shader)

文章目录

概述

  1. vertex shader --> geometry shader --> fragment shader。\[\]: 可选阶段。
  2. 输入图元 ---> geometry shader ---> 其他图元

编写格式

c 复制代码
[maxcertexcount(N)]
void ShaderName (PrimitiveType InputVertexType InputName[NumElements], 
inout StreamOutputObjectVertexType) OutputName){// 几何着色器具体实现}

几何着色器每次输出的顶点个数都可能不同,[maxvertexcount(N)]用来指定几何着色器单次调用所输出的顶点数量最大值,输出点=1,输出线=2,输出三角形=3。
PrimitiveType图元类型:

图元类型
point 输入图元拓扑类型为点列表
line 输入图元拓扑类型为线列表或线条带
triangle 输入的图元拓扑类型为三角形列表或三角形带
lineadj 输入的图元拓扑类型为线条列表/带及其邻接图元
triangleadj 输入的图元为三角形列表/带及其邻接图元

StreamOutputObjectVertexType 输出流类型

输出流类型
PointStream 一系列顶点所定义的点列表
LineStream 一系列顶点所定义的线条带
TriangleStream 一系列顶点所定义的三角形带

举例

c 复制代码
//The data structure from the application to the vertex shader.
struct a2v  
{  
    float4 vertex : POSITION;  
    float2 uv : TEXCOORD0;  
};  

//The data structure from the vertex shader to the geometry shader.
struct v2g  
{  
    float4 vertex : POSITION;  
    float2 uv : TEXCOORD0;  
};

//The data structure from the geometry shader to the fragment shader.
struct g2f
{  
    float4 vertex : SV_POSITION;  
    float2 uv : TEXCOORD0;
}; 
[maxvertexcount(3)]  
void geom(triangle v2g input[3], inout TriangleStream<g2f> outStream)  
{  
      g2f o;
      for(int i = 0; i < 3; i++)  
      {  
            o.vertex = UnityObjectToClipPos(input[i].vertex);  
            o.uv = input[i].uv;  
            outStream.Append(o);  //Append函数用来将几何着色器的输出数据追加到一个现有的流中。   
      }  
      outStream.RestartStrip();  //输出流是三角,重置输出流
}

应用举例(用预制体球的每个顶点画一个立方体)

参考

Unity几何着色器详解
GITHUB-GeometryShaderCookbook

相关推荐
年少无知且疯狂3 小时前
【Unity】Shader spine动画冻结效果
unity·spine
淡海水3 小时前
05-02-源码-Boehm GC标记阶段
算法·unity·c#·游戏引擎·gc
淡海水8 小时前
05-03-源码-Boehm GC分配与碎片管理
开发语言·算法·unity·c#·游戏引擎·gc
WarPigs2 天前
Unity无法修改Assembly-CSharp.csproj错误
unity
FairGuard手游加固3 天前
手游加固怎么做?SO、IL2CPP、AssetBundle 与运行时保护详解
安全·游戏·unity
阿松爱学习4 天前
【Unity开发】监听Scene场景事件
unity·c#
xiaogai_gai4 天前
BOM 物料清单同步实战:金蝶云星空 → 四化智造 MES 修改场景深度拆解
unity·游戏引擎
淡海水4 天前
03-03-架构篇-SGen GC架构解析
unity·架构·c#·gc·sgen
萌萌的提莫队长5 天前
Unity相机捕捉,图像识别,抠像+合成
opencv·unity
ly_Enhs5 天前
从曲面细分到计算着色器:我的 Vulkan 实验记录
着色器·vulkan图形渲染c/c++