学习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

相关推荐
淡海水2 小时前
11-02-Unity-Mono-vs-IL2CPP-两种脚本后端的数据结构行为差异
数据结构·unity·游戏引擎·il2cpp·mono
彧azz3 小时前
初学Unity:编辑器
笔记·学习·unity·游戏引擎
小小数媒成员4 小时前
GPU优化(1)
游戏·unity·游戏引擎
淡海水4 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
小小数媒成员20 小时前
如何优化代码适应托管内存?
游戏·unity·游戏引擎
新的瑞拉公主2 天前
Unity游戏发布微信小游戏:从构建到提审
unity·webgl·微信小游戏·开放数据域
心前阳光4 天前
Unity发布PC软件图标不能改变
unity·游戏引擎
an86950014 天前
unity如何在visual studio中打断点debug
unity·游戏引擎·visual studio
xcLeigh4 天前
Unity基础:使用Transform控制物体移动——Translate与position详解
unity·游戏引擎