Shader第二十七讲 Geometry Shaders(转)

本文

Geometry Shader简称为g

Vertex Shader简称为v

Fragment Shader简称为f

Geometry Shader是继Vertex Shader和Fragment Shader之后,由Shader Model 4(第四代显卡着色架构)正式引入的第三个着色器。性能差因为不像 V和F那样高度并行.

Geometry Shader 是vertex和pixel之间的一个optional的stage.

V的功能是处理顶点,G是处理图元,最后光栅化后传给F.通俗理解:G是用来处理三角形的

V的输出 传给 G 作为输入,G 处理完成后,G的输出 传给 F作为输入

G输出的是最后的齐次裁剪空间,也就是经过mvp。

使用步骤

(1)使用声明

#pragmageometryGS_Main

(2)函数

【设置输出最大顶点数量】函数前添加 [maxvertexcount(4)]

【输入】的图元

必须添加以下前缀之一,例如添加Point就是指这个三角形被当作一个point读取第一个点。(注意如果处理一个quad,2个三角形,所以实际上只有2个点,而不是全部4个顶点)

point line triangle lineadj triangleadj

【输出】必须加inout前缀,triStream.Append(pIn);

PointStreamLineStreamTriangleStream

[maxvertexcount(11)]

voidGS_Main(triangleGS_INPUTp[3],inoutTriangleStreamtriStream)

{

for(inti=0;i <3;i++)

{

FS_INPUTf;

f.pos=p[i].pos;

f.tex0=p[i].tex0;

triStream.Append(f);

}

triStream.RestartStrip();

}

[例:不处理]

本例实现的效果和普通的Vertex/Fragment Shader效果一样。

Shader"Custom/0same"

{

Properties

{

_MainTex("ParticleTexture",2D)="white"{}

}

SubShader

{

Pass

{

Tags{"RenderType"="Opaque"}

CGPROGRAM

#pragmatarget5.0

#pragmavertexVS_Main

#pragmafragmentFS_Main

#pragmageometryGS_Main

#include"UnityCG.cginc"

sampler2D_MainTex;

float4_MainTex_ST;

structGS_INPUT

{

float4pos:POSITION;

float3normal:NORMAL;

float2tex0:TEXCOORD0;

};

structFS_INPUT

{

float4pos:SV_POSITION;

float2tex0:TEXCOORD0;

};

//step1

GS_INPUTVS_Main(appdata_basev)

{

GS_INPUToutput=(GS_INPUT)0;

output.pos=mul(UNITY_MATRIX_MVP,v.vertex);

output.tex0=TRANSFORM_TEX(v.texcoord,_MainTex);

output.normal=v.normal;

returnoutput;

}

//step2

[maxvertexcount(11)]

voidGS_Main(triangleGS_INPUTp[3],inoutTriangleStreamtriStream)

{

for(inti=0;i<3;i++)

{

FS_INPUTf;

f.pos=p[i].pos;

f.tex0=p[i].tex0;

triStream.Append(f);

}

triStream.RestartStrip();

}

//step3

float4FS_Main(FS_INPUTi):COLOR

{

fixed4col=tex2D(_MainTex,i.tex0);

returncol;

}

ENDCG

}

}

}

[例一:显示模型顶点]

Shader第二十七讲 Geometry Shaders(转)_第1张图片

Shader"Custom/ShowDots"

{

Properties

{

}

SubShader

{

Pass

{

Tags{"RenderType"="Opaque"}

LOD200

CGPROGRAM

#pragmatarget5.0

#pragmavertexVS_Main

#pragmafragmentFS_Main

#pragmageometryGS_Main

#include"UnityCG.cginc"

structGS_INPUT

{

float4pos:POSITION;

float3normal:NORMAL;

float2tex0:TEXCOORD0;

};

structFS_INPUT

{

float4pos:POSITION;

float2tex0:TEXCOORD0;

};

//step1

GS_INPUTVS_Main(appdata_basev)

{

GS_INPUToutput=(GS_INPUT)0;

output.pos=mul(_Object2World,v.vertex);

output.normal=v.normal;

output.tex0=float2(0,0);

returnoutput;

}

//step2

[maxvertexcount(4)]

voidGS_Main(pointGS_INPUTp[1],inoutPointStreamtriStream)

{

float4v=float4(p[0].pos.x,p[0].pos.y,p[0].pos.z,1.0f);

//canmoveupalittlebit

//v=v+float4(0,1,0,0);

float4x4vp=mul(UNITY_MATRIX_MVP,_World2Object);

FS_INPUTpIn;

pIn.pos=mul(vp,v);

pIn.tex0=float2(0.0f,0.0f);

triStream.Append(pIn);

}

//step3

float4FS_Main(FS_INPUTinput):COLOR

{

returnfloat4(1,1,1,1);

}

ENDCG

}

}

}

因为每个三角形只读一个点,如果改成如下,就每个点都显示了。

Shader"Custom/1ShowPoints2"

{

Properties

{

}

SubShader

{

Pass

{

Tags{"RenderType"="Opaque"}

CGPROGRAM

#pragmatarget5.0

#pragmavertexVS_Main

#pragmafragmentFS_Main

#pragmageometryGS_Main

#include"UnityCG.cginc"

structGS_INPUT

{

float4pos:POSITION;

float3normal:NORMAL;

float2tex0:TEXCOORD0;

};

structFS_INPUT

{

float4pos:POSITION;

float2tex0:TEXCOORD0;

};

//step1

GS_INPUTVS_Main(appdata_basev)

{

GS_INPUToutput=(GS_INPUT)0;

output.pos=v.vertex;

output.normal=v.normal;

output.tex0=float2(0,0);

returnoutput;

}

//step2

[maxvertexcount(11)]

voidGS_Main(triangleGS_INPUTp[3],inoutPointStreamtriStream)

{

for(inti=0;i<3;i++)

{

float4v=float4(p[i].pos.x,p[i].pos.y,p[i].pos.z,1.0f);

float4x4vp=mul(UNITY_MATRIX_MVP,_World2Object);

FS_INPUTpIn;

pIn.pos=mul(UNITY_MATRIX_MVP,v);

pIn.tex0=float2(0.0f,0.0f);

triStream.Append(pIn);

}

}

//step3

float4FS_Main(FS_INPUTinput):COLOR

{

returnfloat4(1,1,1,1);

}

ENDCG

}

}

}

[例二:模型每个顶点显示一个公告板]

Shader第二十七讲 Geometry Shaders(转)_第2张图片

Shader"Custom/2Billboard"

{

Properties

{

_SpriteTex("Base(RGB)",2D)="white"{}

_Size("Size",Range(0,3))=0.5

}

SubShader

{

Pass

{

Tags{"RenderType"="Opaque"}

LOD200

CGPROGRAM

#pragmatarget5.0

#pragmavertexVS_Main

#pragmafragmentFS_Main

#pragmageometryGS_Main

#include"UnityCG.cginc"

structGS_INPUT

{

float4pos:POSITION;

float3normal:NORMAL;

float2tex0:TEXCOORD0;

};

structFS_INPUT

{

float4pos:POSITION;

float2tex0:TEXCOORD0;

};

float_Size;

float4x4_VP;

Texture2D_SpriteTex;

SamplerStatesampler_SpriteTex;

GS_INPUTVS_Main(appdata_basev)

{

GS_INPUToutput=(GS_INPUT)0;

output.pos=mul(_Object2World,v.vertex);

output.normal=v.normal;

output.tex0=float2(0,0);

returnoutput;

}

[maxvertexcount(4)]

voidGS_Main(pointGS_INPUTp[1],inoutTriangleStreamtriStream)

{

float3up=float3(0,1,0);

float3look=_WorldSpaceCameraPos-p[0].pos;

look.y=0;

look=normalize(look);

float3right=cross(up,look);

floathalfS=0.5f*_Size;

float4v[4];

v[0]=float4(p[0].pos+halfS*right-halfS*up,1.0f);

v[1]=float4(p[0].pos+halfS*right+halfS*up,1.0f);

v[2]=float4(p[0].pos-halfS*right-halfS*up,1.0f);

v[3]=float4(p[0].pos-halfS*right+halfS*up,1.0f);

float4x4vp=mul(UNITY_MATRIX_MVP,_World2Object);

FS_INPUTpIn;

pIn.pos=mul(vp,v[0]);

pIn.tex0=float2(1.0f,0.0f);

triStream.Append(pIn);

pIn.pos=mul(vp,v[1]);

pIn.tex0=float2(1.0f,1.0f);

triStream.Append(pIn);

pIn.pos=mul(vp,v[2]);

pIn.tex0=float2(0.0f,0.0f);

triStream.Append(pIn);

pIn.pos=mul(vp,v[3]);

pIn.tex0=float2(0.0f,1.0f);

triStream.Append(pIn);

}

float4FS_Main(FS_INPUTinput):COLOR

{

return_SpriteTex.Sample(sampler_SpriteTex,input.tex0);

}

ENDCG

}

}

}

[例三:输出金字塔]

Shader第二十七讲 Geometry Shaders(转)_第3张图片

Shader"Custom/3Pyramid"

{

Properties

{

_MainTex("ParticleTexture",2D)="white"{}

_Explode("Explodefactor",Range(0.0,4.0))=1.0

}

SubShader

{

Pass

{

CGPROGRAM

#pragmatarget5.0

#pragmavertexvert

#pragmageometrygeom

#pragmafragmentfrag

#include"UnityCG.cginc"

sampler2D_MainTex;

float4_MainTex_ST;

float_Explode;

float4x4_ViewMatrix;

structVS_INPUT

{

float4Pos:POSITION;

float3Norm:NORMAL;

float2Tex:TEXCOORD0;

};

//GEOMETRY

structGSPS_INPUT

{

float4Pos:SV_POSITION;

float3Norm:TEXCOORD0;

float2Tex:TEXCOORD1;

};

//VertexShader

GSPS_INPUTvert(VS_INPUTinput)

{

GSPS_INPUToutput=(GSPS_INPUT)0;

output.Pos=input.Pos;//mul(float4(input.Pos,1),_Object2World);//mul(float4(input.Pos,1),World);

output.Norm=input.Norm;//mul(input.Norm,(float3x3)_Object2World);//mul(input.Norm,(float3x3)World);

output.Tex=TRANSFORM_TEX(input.Tex,_MainTex);//input.Tex;

returnoutput;

}

//GeometryShader

[maxvertexcount(12)]

voidgeom(triangleGSPS_INPUTinput[3],inoutTriangleStreamoutStream)

{

GSPS_INPUToutput;

//Calculatethefacenormal

float3faceEdgeA=input[1].Pos-input[0].Pos;

float3faceEdgeB=input[2].Pos-input[0].Pos;

float3faceNormal=normalize(cross(faceEdgeA,faceEdgeB));

float3ExplodeAmt=faceNormal*_Explode;

//Calculatethefacecenter

float3centerPos=(input[0].Pos.xyz+input[1].Pos.xyz+input[2].Pos.xyz)/3.0;

float2centerTex=(input[0].Tex+input[1].Tex+input[2].Tex)/3.0;

centerPos+=faceNormal*_Explode;

//Outputthepyramid

for(inti=0;i<3;i++)

{

output.Pos=input[i].Pos+float4(ExplodeAmt,0);

output.Pos=mul(UNITY_MATRIX_MVP,output.Pos);

output.Norm=input[i].Norm;

output.Tex=input[i].Tex;

outStream.Append(output);

intiNext=(i+1)%3;

output.Pos=input[iNext].Pos+float4(ExplodeAmt,0);

output.Pos=mul(UNITY_MATRIX_MVP,output.Pos);

output.Norm=input[iNext].Norm;

output.Tex=input[iNext].Tex;

outStream.Append(output);

output.Pos=float4(centerPos,1)+float4(ExplodeAmt,0);

output.Pos=mul(UNITY_MATRIX_MVP,output.Pos);

output.Norm=faceNormal;

output.Tex=centerTex;

outStream.Append(output);

outStream.RestartStrip();

}

for(inti=2;i>=0;i--)

{

output.Pos=input[i].Pos+float4(ExplodeAmt,0);

output.Pos=mul(UNITY_MATRIX_MVP,output.Pos);

output.Norm=-input[i].Norm;

output.Tex=input[i].Tex;

outStream.Append(output);

}

outStream.RestartStrip();

}

//FRAG

fixed4frag(GSPS_INPUTi):COLOR0

{

fixed4col=tex2D(_MainTex,i.Tex);

returncol;

}

ENDCG

}

}

FallbackOff

}

你可能感兴趣的:(Shader第二十七讲 Geometry Shaders(转))