CG着色器.docx

上传人:b****6 文档编号:6462142 上传时间:2023-01-06 格式:DOCX 页数:11 大小:197.24KB
下载 相关 举报
CG着色器.docx_第1页
第1页 / 共11页
CG着色器.docx_第2页
第2页 / 共11页
CG着色器.docx_第3页
第3页 / 共11页
CG着色器.docx_第4页
第4页 / 共11页
CG着色器.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

CG着色器.docx

《CG着色器.docx》由会员分享,可在线阅读,更多相关《CG着色器.docx(11页珍藏版)》请在冰豆网上搜索。

CG着色器.docx

CG着色器

首页用户手册组件手册脚本手册论坛

传递顶点数据到顶点程序Providingvertexdatatovertexprograms

Date:

2012-03-0505:

12

ForCgvertexprograms,thevertexdatamustbepassedinasastructure.SeveralcommonlyusedvertexstructuresaredefinedinUnityCG.cgincincludefile,andinmostcasesit'senoughjusttousethem.Thestructuresare:

Cg顶点程序必须在结构中传递顶点数据。

几种常用的顶点结构定义在文件UnityCG.cginc中。

在大部分情况下仅仅使用它们就够了。

结构如下:

∙appdata_base:

vertexconsistsofposition,normalandonetexturecoordinate.

appdata_base:

包含顶点位置,法线和一个纹理坐标。

∙appdata_tan:

vertexconsistsofposition,tangent,normalandonetexturecoordinate.

appdata_tan:

包含顶点位置,切线,法线和一个纹理坐标。

Forexample,thisshadercolorsthemeshbasedonit'snormalsandjustusesappdata_baseasvertexprograminput:

例如:

下面是这个着色器颜色网是基于它的法线,它仅仅在顶点程序中使用了appdata_base输入结构。

Shader"VertexInputSimple"{

SubShader{

Pass{

CGPROGRAM

#pragmavertexvert

#include"UnityCG.cginc"

structv2f{

float4pos:

SV_POSITION;

fixed4color:

COLOR;

};

v2fvert(appdata_basev)

{

v2fo;

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

o.color.xyz=v.normal*0.5+0.5;

returno;

}

ENDCG

}

}

}

Ifyouwanttoaccessdifferentvertexdata,youhavetodeclarevertexstructureyourself.Thestructuremembersmustbefromthefollowinglist:

如果你想访问个别的顶点数据,你必须自己声明顶点结构。

结构中的成员必须是属于以下列表中的:

∙float4vertexisthevertexposition

float4vertex是顶点位置

∙float3normalisthevertexnormal

float3normal是顶点法线

∙float4texcoordisthefirstUVcoordinate

float4texcoord是第一UV坐标

∙float4texcoord1isthesecondUVcoordinate

float4texcoord1是第二UV坐标

∙float4tangentisthetangentvector(usedfornormalmapping)

float4tangent是切线向量(用在法线贴图中)

∙float4colorisper-vertexcolor

float4color是每个顶点(per-vertex)颜色

Examples范例

VisualizingUVs可视化UV

Thefollowingshaderexampleusesvertexpositionandfirsttexturecoordinateasvertexshaderinput(definedinstructureappdata).ItisveryusefultodebugUVcoordinatesofthemesh.UVcoordinatesarevisualizedasredandgreencolors,andcoordinatesoutsideof0..1rangehaveadditionalbluetintapplied.

以下着色器示例通过顶点着色器输入结构使用了顶点位置和第一纹理坐标。

这在网格中调试UV坐标是非常有用的。

UV坐标是可见的红色和绿色,坐标以外应用的是取值范围在0到1之间的蓝色值。

Shader"!

Debug/UV1"{

SubShader{

Pass{

Fog{ModeOff}

CGPROGRAM

#pragmavertexvert

#pragmafragmentfrag

//vertexinput:

position,UV

structappdata{

float4vertex:

POSITION;

float4texcoord:

TEXCOORD0;

};

structv2f{

float4pos:

SV_POSITION;

float4uv:

TEXCOORD0;

};

v2fvert(appdatav){

v2fo;

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

o.uv=float4(v.texcoord.xy,0,0);

returno;

}

half4frag(v2fi):

COLOR{

half4c=frac(i.uv);

if(any(saturate(i.uv)-i.uv))

c.b=0.5;

returnc;

}

ENDCG

}

}

}

DebugUV1shaderappliedtoatorusknotmodel

将着色器应用到扣环模型上调式UV1着色器的效果

Similarly,thisshadervizualizesthesecondUVsetofthemodel:

类似的,下面这个着色器可视化使用的是第二UV模式:

Shader"!

Debug/UV2"{

SubShader{

Pass{

Fog{ModeOff}

CGPROGRAM

#pragmavertexvert

#pragmafragmentfrag

//vertexinput:

position,secondUV

structappdata{

float4vertex:

POSITION;

float4texcoord1:

TEXCOORD1;

};

structv2f{

float4pos:

SV_POSITION;

float4uv:

TEXCOORD0;

};

v2fvert(appdatav){

v2fo;

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

o.uv=float4(v.texcoord1.xy,0,0);

returno;

}

half4frag(v2fi):

COLOR{

half4c=frac(i.uv);

if(any(saturate(i.uv)-i.uv))

c.b=0.5;

returnc;

}

ENDCG

}

}

}

Visualizingvertexcolors可视化顶点颜色

Thefollowingshaderusesvertexpositionandper-vertexcolorsasvertexshaderinput(definedinstructureappdata).

下面这个着色器通过顶点着色器输入结构使用了顶点位置和每个顶点颜色。

Shader"!

Debug/Vertexcolor"{

SubShader{

Pass{

Fog{ModeOff}

CGPROGRAM

#pragmavertexvert

//vertexinput:

position,color

structappdata{

float4vertex:

POSITION;

float4color:

COLOR;

};

structv2f{

float4pos:

SV_POSITION;

fixed4color:

COLOR;

};

v2fvert(appdatav){

v2fo;

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

o.color=v.color;

returno;

}

ENDCG

}

}

}

DebugColorsshaderappliedtoamodelthathasilluminationbakedintocolors

将着色器应用到模型上调试颜色着色器,颜色有光照烘培效果

Visualizingnormals可视化法线

Thefollowingshaderusesvertexpositionandnormalasvertexshaderinput(definedinstructureappdata).Normal'sX,Y,ZcomponentsarevisualizedasR,G,Bcolors.Becausethenormalcomponentsarein-1..1range,wescaleandbiasthemsothattheoutputcolorsareindisplayable0..1range.

下面这个着色器通过顶点着色器输入结构使用了顶点位置和法线。

法线的X,Y,Z分量对应的是颜色的R,G,B。

因为法线的取值范围是-1到1,而颜色取值范围是0到1,所以我们在这里使用了一个小技巧来解决这个问题。

Shader"!

Debug/Normals"{

SubShader{

Pass{

Fog{ModeOff}

CGPROGRAM

#pragmavertexvert

//vertexinput:

position,normal

structappdata{

float4vertex:

POSITION;

float3normal:

NORMAL;

};

structv2f{

float4pos:

SV_POSITION;

fixed4color:

COLOR;

};

v2fvert(appdatav){

v2fo;

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

o.color.xyz=v.normal*0.5+0.5;

o.color.w=1.0;

returno;

}

ENDCG

}

}

}

DebugNormalsshaderappliedtoamodel.Youcanseethatthemodelhashardshadingedges.

将着色器应用到模型上调试法线着色器。

你可以看到模型的边缘有硬阴影。

Visualizingtangentsandbinormals可视化切线与副法线

Tangentandbinormalvectorsareusedfornormalmapping.InUnityonlythetangentvectorisstoredinvertices,andbinormalisderivedfromnormalandtangent.

切线和副法线向量是用在法线贴图上的。

在Unity里是只在顶点中存储切线向量的,副法线是通过法线和切线得到的。

Thefollowingshaderusesvertexpositionandtangentasvertexshaderinput(definedinstructureappdata).Tangent'sX,Y,ZcomponentsarevisualizedasR,G,Bcolors.Becausethenormalcomponentsarein-1..1range,wescaleandbiasthemsothattheoutputcolorsareindisplayable0..1range.

下面这个着色器通过顶点着色器输入结构使用了顶点位置和切线。

切线的X,Y,Z分量对应的是颜色的R,G,B。

因为切线的取值范围是-1到1,而颜色取值范围是0到1,所以我们在这里使用了一个小技巧来解决这个问题。

Shader"!

Debug/Tangents"{

SubShader{

Pass{

Fog{ModeOff}

CGPROGRAM

#pragmavertexvert

//vertexinput:

position,tangent

structappdata{

float4vertex:

POSITION;

float4tangent:

TANGENT;

};

structv2f{

float4pos:

SV_POSITION;

fixed4color:

COLOR;

};

v2fvert(appdatav){

v2fo;

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

o.color=v.tangent*0.5+0.5;

returno;

}

ENDCG

}

}

}

DebugTangentsshaderappliedtoamodel.

将着色器应用到模型上调试切线(Tangents)着色器。

Thefollowingshadervisualizesbinormals.Itusesvertexposition,normalandtangentasvertexinput.Binormaliscalculatedfromnormalandtangent.Justlikenormalortangent,itneedstobescaledandbiasedintoadisplayable0..1range.

下面这个着色器是可视化副法线着色器。

它通过顶点着色器输入结构使用了顶点位置,法线和切线。

副法线是通过法线与切线计算得来。

取值范围就像法线或者切线,需要通过缩放与裁减来使颜色最终的颜色值在0到1的区间内。

Shader"!

Debug/Binormals"{

SubShader{

Pass{

Fog{ModeOff}

CGPROGRAM

#pragmavertexvert

//vertexinput:

position,normal,tangent

structappdata{

float4vertex:

POSITION;

float3normal:

NORMAL;

float4tangent:

TANGENT;

};

structv2f{

float4pos:

SV_POSITION;

float4color:

COLOR;

};

v2fvert(appdatav){

v2fo;

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

//calculatebinormal

float3binormal=cross(v.normal,v.tangent.xyz)*v.tangent.w;

o.color.xyz=binormal*0.5+0.5;

o.color.w=1.0;

returno;

}

ENDCG

}

}

}

DebugBinormalsshaderappliedtoamodel.Pretty!

页面最后更新:

2012-01-19

标签:

着色器语法ShaderLabShader着色器顶点数据顶点程序

分类:

Components|评论:

0|翻译:

风里疯语

投资合作请联系ceeger@英文部分版权属©Unity公司所有,中文部分©Unity圣典版权所有,未经许可,严禁转载。

浙ICP备10041254号-1

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1