UnityShader权威指南中文翻译.docx

上传人:b****6 文档编号:4081543 上传时间:2022-11-27 格式:DOCX 页数:31 大小:949.40KB
下载 相关 举报
UnityShader权威指南中文翻译.docx_第1页
第1页 / 共31页
UnityShader权威指南中文翻译.docx_第2页
第2页 / 共31页
UnityShader权威指南中文翻译.docx_第3页
第3页 / 共31页
UnityShader权威指南中文翻译.docx_第4页
第4页 / 共31页
UnityShader权威指南中文翻译.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

UnityShader权威指南中文翻译.docx

《UnityShader权威指南中文翻译.docx》由会员分享,可在线阅读,更多相关《UnityShader权威指南中文翻译.docx(31页珍藏版)》请在冰豆网上搜索。

UnityShader权威指南中文翻译.docx

UnityShader权威指南中文翻译

UnityShader权威指南—中文翻译

目录:

1.

1.1

1.1.1

1.1.2

1.1.3

1.1.3.1

1.1.3.2

1.1.3.3

1.1.3.4

1.1.3.5

1.1.3.6

1.1.3.7

1.1.3.8

1.1.3.9

1.1.4

1.1.4.1

1.1.4.2

1.1.4.3

1.1.4.4

1.1.4.5TransparentBumpedSpecular透明法线高光着色器

1.1.4.6TransparentParallaxDiffuse透明视差漫反射着色器

1.1.4.7TransparentParallaxSpecular透明视差高光着色器

1.1.5

1.1.5.1

1.1.5.2

1.1.5.3

1.1.5.4

1.1.5.5

1.1.6

1.1.6.1

1.1.6.2

1.1.6.3

1.1.6.4

1.1.6.5

1.1.6.6

1.1.6.7

1.1.7

1.1.7.1

1.1.7.2

1.1.7.3

1.1.7.4

1.1.7.5

1.1.7.6

1.1.7.7

1.1.7.8

1.1.7.9

2

2.1 

2.1.1 属性

2.1.2 子着色器

2.1.2.1 传递

2.1.2.1.1 颜色,材质,光照

2.1.2.1.2 

2.1.2.1.3 

2.1.2.1.4 

2.1.2.1.5 

2.1.2.1.6 

2.1.2.1.7

2.1.2.1.8

2.1.2.1.9

2.1.2.2 

2.1.2.3 

2.1.2.4 

2.1.3 

2.1.4

2.2 

2.2.1 

2.2.2 

2.2.3 

2.3 

2.3.1 

2.3.2

2.3.3

2.3.4 

2.4

2.4.1

2.4.2

2.4.3

2.4.4

2.4.5

2.4.6

2.4.7

2.5

3Tutorial

3.1

3.2

3.3

4

 

Shaders

AllrenderinginUnityisdonewith Shaders -smallscriptsthatletyouconfigurethehowthegraphicshardwareissetupforrendering.Unityshipswith60+built-inshaders(documentedinthe ).Youcanextendthisbymakingyourownshaders.

ShadersinUnitycanbewritteninoneofthreedifferentways:

∙SurfaceShaders willprobablybeyourbestbet.Writeyourshaderasasurfaceshaderifitneedstointeractproperlywithlighting,shadows,projectors,etc.Surfaceshadersalsomakeiteasytowritecomplexshadersinacompactway-it'sahigherlevelofabstraction.Lightingformostsurfaceshaderscanbecalculatedinadeferredmanner(theexceptionissomeverycustomlightingmodels),whichallowsyourshadertoefficientlyinteractwithmanyrealtimelights.Youwritesurfaceshadersinacoupleoflinesof Cg/HLSL andalotmorecodegetsauto-generatedfromthat.

∙VertexandFragmentShaders willberequired,ifyouneedsomeveryexoticeffectsthatthesurfaceshaderscan'thandle,ifyourshaderdoesn'tneedtointeractwithlightingorifit'sanimageeffect.Shaderprogramswrittenthiswayarethemostflexiblewaytocreatetheeffectyouneed(evensurfaceshadersareautomaticallyconvertedtoabunchofvertexandfragmentshaders),butthatcomesataprice:

youhavetowritemorecodeandit'shardertomakeitinteractwithlighting.Theseshadersarewrittenin Cg/HLSL aswell.

∙FixedFunctionShaders needtobewrittenforoldhardwarethatdoesn'tsupportprogrammableshaders.Youwillprobablywanttowritefixedfunctionshadersasann-thfallbacktoyourfancyfragmentorsurfaceshaders,tomakesureyourgamestillrenderssomethingsensiblewhenrunonoldhardwareorsimplermobileplatforms.Fixedfunctionshadersareentirelywritteninalanguagecalled ShaderLab,whichissimilartoMicrosoft's.FXfilesorNVIDIA'sCgFX.

Regardlessofwhichtypeyouchoose,theactualmeatoftheshadercodewillalwaysbewrappedinShaderLab,whichisusedtoorganizetheshaderstructure.Itlookslikethis:

Shader"MyShader"{

Properties{

_MyTexture("MyTexture",2D)="white"{}

//otherpropertieslikecolorsorvectorsgohereaswell

}

SubShader{

//heregoesthe'meat'ofyour

//-surfaceshaderor

//-vertexandfragmentshaderor

//-fixedfunctionshader

}

SubShader{

//heregoesasimplerversionoftheSubShaderabovethatcanrunonoldergraphicscards

}

}

WerecommendthatyoustartbyreadingaboutsomebasicconceptsoftheShaderLabsyntaxinthe  andthenmoveontothetutorialslistedbelow.

Thetutorialsincludeplentyofexamplesforthedifferenttypesofshaders.Forevenmoreexamplesofsurfaceshadersinparticular,youcangetthesourceofUnity'sbuilt-inshadersfromthe .Unity's  packagecontainsalotofinterestingvertexandfragmentshaders.

Readonforanintroductiontoshaders,andcheckoutthe !

着色器

所有在Unity的渲染都用着色器-小脚本完成,着色器让您配置该如何设置图形硬件进行渲染。

Unity有60+内置的着色器,但你可以扩展,制作更多的自己的着色器。

内置的着色器都记录在。

写Unity的着色器可以使用三种不同的方法之一:

∙表面着色器,可能会是你最好的选择。

如果它需要与灯光,阴影,投影器,等进行交互,以表面着色器方式写着色器。

表面着色器也可以很容易简洁地写在复杂的着色器-这是一个更高的抽象层次。

大多数表面着色器的光照以延迟光照的方式进行计算(唯一的例外是一些自定义的光照模型),它允许着色器有效率地与许多实时灯光互动。

表面着色器用CG/HLSL语言编写几行代码和更多的代码会自动生成。

∙顶点和片段着色器,如果你需要一些非常奇特的效果,表面着色器不能处理的,如果着色器并不需要与照明互动或如果这只是图像效果。

这种编写着色器方案是最灵活的方式创建你需要的效果(甚至表面着色器是自动转换成一堆顶点和片段着色器),但这有代价的:

你需要写更多的代码和它很难使其与照明互动。

这些着色器同样是用CG/HLSL语言编写。

∙固定功能着色器,需要为不支持可编程着色器的旧硬件编写。

您可能会想要编写固定功能着色器,以确保旧的硬件或简单的移动平台上运行时,你的游戏渲染的东西仍然合理的。

固定功能着色器完全是用称为ShaderLab一种语言编写的。

这是类似微软的。

FX文件或NVIDIA的的CgFX。

无论您选择哪种类型,实际的shader代码总是会被包裹在ShaderLab,这是用来组织着色器结构。

它看起来像这样:

Shader"MyShader"{

Properties{

_MyTexture("MyTexture",2D)="white"{}

//otherpropertieslikecolorsorvectorsgohereaswell

}

SubShader{

//heregoesthe'meat'ofyour

//-surfaceshaderor

//-vertexandfragmentshaderor

//-fixedfunctionshader

}

SubShader{

//heregoesasimplerversionoftheSubShaderabovethatcanrunonoldergraphicscards

}

}

我们建议您阅读在ShaderLab参考(ShaderLabreference)中有关ShaderLab语法的一些基本概念开始,然后到下面列出的教程。

该教程包括大量不同类型的着色器的例子。

更多的例子,尤其是表面着色器的例子,你可以在资源部分()得到Unity内置着色器的源代码。

Unity的图像效果()资源包中包含了很多有趣的顶点和片段着色器。

阅读着色器介绍,以及:

∙教程:

ShaderLab和固定功能着色器

∙教程:

顶点和片段着色器

∙教程:

表面着色器

Built-inShaderGuide

Ifyou'relookingforthebestinformationforusingUnity'sbuilt-inshaders,you'vecometotherightplace.Unityincludesmorethan40built-inshaders,andofcourseyoucanwritemanymoreonyourown!

Thisguidewillexplaineachfamilyofthebuilt-inshaders,andgointodetailforeachspecificshader.Withthisguide,you'llbeabletomakethemostoutofUnity'sshaders,toachievetheeffectyou'reaimingfor.

UsingShaders

ShadersinUnityareusedthrough Materials,whichessentiallycombineshadercodewithparametersliketextures.Anin-depthexplanationoftheShader/Materialrelationshipcanberead .

Materialpropertieswillappearinthe Inspector wheneithertheMaterialitselfora GameObject thatusestheMaterialisselected.TheMaterialInspectorlookslikethis:

EachMaterialwilllookalittledifferentintheInspector,dependingonthespecificshaderitisusing.TheshaderiselfdetermineswhatkindofpropertieswillbeavailabletoadjustintheInspector.Materialinspectorisdescribedindetailin .RememberthatashaderisimplementedthroughaMaterial.SowhiletheshaderdefinesthepropertiesthatwillbeshownintheInspector,eachMaterialactuallycontainstheadjusteddatafromsliders,colors,andtextures.ThemostimportantthingtorememberaboutthisisthatasingleshadercanbeusedinmultipleMaterials,butasingleMaterialcannotusemultipleshaders.

内置着色器指南

假如要了解关于Unity内置shader的更多重要信息,你已经进入正确的地方。

Unity包括超过40种内置的shader,当然也可以自己编写更多属于自己的shader!

本指导将依次解释每个内置的shader,并讲述每个指定shader的详细信息。

跟着此向导可以创建置更多Unity之外的shader,完成预期的效果。

使用着色器

在Unity中shader是通过Materials(材质)来应用的,实际上每个shader关联的代码和参数类似Textures(纹理)一样,关于一个更全面的Shader/Material之间的解释可以到阅读。

当Material(材质)自身或者应用了此材质的GameObject(游戏对象)被选择时材质属性会显示在Inspector(检视面板)中,这个材质检视面板就像下面这样:

在检视面板中每个材质都看似有些不同,这依赖于其使用的特定Shader,这个Shader自身将确定在检视面板中匹配哪些属性是可用的,关于材质检视面板详细信息可以到查看。

牢记shader是通过Material(材质)才能执行。

于此同时这个Shader定义的属性将显示在检视面板,每个材质实际包含通过滑块,颜色及材质调整过的数据信息。

要牢记非常重要的事情是一个单一的Shader能够被应用在多维材质中,但是一个单一的材质不能应用在多维Shader中

MaterialsandShaders

Thereisacloserelationshipbetween Materials and Shaders inUnity.Shaderscontaincodethatdefineswhatkindofpropertiesandassetstouse.Materialsallowyoutoadjustpropertiesandassignassets.

AShaderisimplementedthroughaMaterial

TocreateanewMaterial,use Assets->Create->Material fromthemainmenuorthe ProjectView contextmenu.OncetheMaterialhasbeencreated,youcanapplyittoanobjectandtweakallofitspropertiesinthe Inspector.Toapplyittoanobject,justdragitfromthe ProjectView toanyobjectinthe Scene orHierarchy.

SettingMaterialProperties

YoucanselectwhichShaderyouwantanyparticularMaterialtouse.Simplyexpandthe Shader drop-downintheInspector,andchooseyournewShader.TheShaderyouchoosewilldictatetheavailablepropertiestochange.Thepropertiescanbecolors,sliders,textures,numbers,orvectors.IfyouhaveappliedtheMaterialtoanactiveobjectinthe Scene,youwillseeyourpropertychangesappliedtotheobjectinreal-time.

Therearetwowaystoapplya Texture toaproperty.

1.DragitfromtheProjectViewontopoftheTexturesquare

2.Clickthe Select button,andchoosethetexturefromthedrop-downlistthatappears

Twoplacementoptionsareavailableforeach Texture:

Tiling

Scalesthetexturealongthedifferent.

Offset

Slidesthetexturearound.

Built-inShaders

Thereisalibraryof  thatcomestandardwitheveryinstallationofUnity.Thereareover30ofthese ,andsixbasicfamilies.

∙:

Foropaquetexturedobjects.

∙:

Forpartlytransparentobjects.Thetexture'salphachanneldefinestheleveloftransparency.

∙:

Forobjectsthathaveonlyfullyopaqueandfullytransparentareas,likefences.

∙:

Forobjectsthathavelightemittingparts.

∙:

Foropaquetexturedobjectsthatreflectanenvironment Cubemap.

Ineachgroup,built-inshadersrangebycomplexity,fromthesimple VertexLit tothecomplex ParallaxBumpedwithSpecular.FormoreinformationaboutperformanceofShaders,pleasereadthebuilt-inShader 

Thisgriddisplaysathumbnailofallbuilt-inShaders:

ThebuiltinUnityshadersmatrix

Shadertechnicaldetails

UnityhasanextensiveShadersystem,allowingyoutotweakthelookofallin-gamegraphics.Itworkslikethis:

AShaderbasicallydefinesaformulaforhowthein-gameshadingshouldlook.WithinanygivenShaderisanumberofproperties(typicallytextures).Shadersareimplementedthrough Materials,whichareattacheddirectlytoindivi

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

当前位置:首页 > 初中教育 > 政史地

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

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