图形处理之Unity3DShader(一)Vertex and Fragment Shader

来源:互联网 发布:杭州恩牛网络 上市 编辑:程序博客网 时间:2024/06/13 22:47

一、Properties

_MyColor ("Some Color", Color) = (1,1,1,1)

_MyVector ("Some Vector", Vector) = (0,0,0,0)

_MyFloat ("My float", Float) = 0.5

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

_MyCubemap ("Cubemap", CUBE) = "" {}

二、Tags

Possible values for LightMode tag are:

  • Always: Always rendered; no lighting is applied.
  • ForwardBase: Used in Forward rendering, ambient, main directional light, vertex/SH lights and lightmaps are applied.
  • ForwardAdd: Used in Forward rendering; additive per-pixel lights are applied, one pass per light.
  • Deferred: Used in Deferred Shading; renders g-buffer.
  • ShadowCaster: Renders object depth into the shadowmap or a depth texture.
  • PrepassBase: Used in legacy Deferred Lighting, renders normals and specular exponent.
  • PrepassFinal: Used in legacy Deferred Lighting, renders final color by combining textures, lighting and emission.
  • Vertex: Used in legacy Vertex Lit rendering when object is not lightmapped; all vertex lights are applied.
  • VertexLMRGBM: Used in legacy Vertex Lit rendering when object is lightmapped; on platforms where lightmap is RGBM encoded (PC & console).
  • VertexLM: Used in legacy Vertex Lit rendering when object is lightmapped; on platforms where lightmap is double-LDR encoded (mobile platforms).

Possible values for LightMode tag are:

  • SoftVegetation: Render this pass only if Soft Vegetation is on in Quality Settings.

Possible values for Queue tag are:

  • Background - this render queue is rendered before any others. You’d typically use this for things that really need to be in the background.
  • Geometry (default) - this is used for most objects. Opaque geometry uses this queue.
  • AlphaTest - alpha tested geometry uses this queue. It’s a separate queue from Geometry one since it’s more efficient to render alpha-tested objects after all solid ones are drawn.
  • Transparent - this render queue is rendered after Geometry and AlphaTest, in back-to-front order. Anything alpha-blended (i.e. shaders that don’t write to depth buffer) should go here (glass, particle effects).
  • Overlay - this render queue is meant for overlay effects. Anything rendered last should go here (e.g. lens flares).

  • Possible values for RenderType tag are:

  • Opaque: most of the shaders (Normal, Self Illuminated, Reflective, terrain shaders).
  • Transparent: most semitransparent shaders (Transparent, Particle, Font, terrain additive pass shaders).
  • TransparentCutout: masked transparency shaders (Transparent Cutout, two pass vegetation shaders).
  • Background: Skybox shaders.
  • Overlay: GUITexture, Halo, Flare shaders.
  • TreeOpaque: terrain engine tree bark.
  • TreeTransparentCutout: terrain engine tree leaves.
  • TreeBillboard: terrain engine billboarded trees.
  • Grass: terrain engine grass.
  • GrassBillboard: terrain engine billboarded grass.

DisableBatching tag

True:(always disables batching for this shader)
False: (does not disable batching; this is default) 
LODFading”:(disable batching when LOD fading is active; mostly used on trees).

ForceNoShadowCasting tag

If ForceNoShadowCasting tag is given and has a value of “True”, then an object that is rendered using this subshader will never cast

shadows. This is mostly useful when you are using shader replacement on transparent objects and you do not wont to inherit a shadow

pass from another subshader.

IgnoreProjector tag

if ignoreProjector tag is given and has a value of “True”, then an object that uses this shader will not be affected by Projectors. This is mostly 

useful on semitransparent objects, because there is no good way for Projectors to affect them.

CanUseSpriteAtlas tag

Set CanUseSpriteAtlas tag to “False” if the shader is meant for sprites, and will not work when they are packed into atlases .

PreviewType tag

PreviewType indicates how the material inspector preview should display the material. By default materials are displayed as 

spheres, but PreviewType can also be set to “Plane” (will display as 2D) or “Skybox” (will display as skybox).

三、测试
1.Cull Back | Front | Off
2.ZWrite On | Off

Controls whether pixels from this object are written to the depth buffer (default is On). If you’re drawng solid objects, leave this on. 

If you’re drawing semitransparent effects, switch to ZWrite Off. For more details read below.

3.ZTest Less | Greater | LEqual | GEqual | Equal | NotEqual | Always

 How should depth testing be performed. Default is LEqual (draw objects in from or at the distance as existing objects; 

hide objects behind them).

4.Offset Factor, Units

Allows you specify a depth offset with two parameters. factor and unitsFactor scales the maximum Z slope, with respect to X or Y of the polygon, 

and units scale the minimum resolvable depth buffer value. This allows you to force one polygon to be drawn on top of another although they are

actually in the same position. For example Offset 0, –1 pulls the polygon closer to the camera ignoring the polygon’s slope, whereas Offset –1, –1 

 will pull the polygon even closer when looking at a grazing angle.

5.Blend

Blend Off;Blend SrcFactor DstFactor;Blend SrcFactor DstFactor, SrcFactorA DstFactorA

OneThe value of one - use this to let either the source or the destination color come through fully.ZeroThe value zero - use this to remove either the source or the destination values.SrcColorThe value of this stage is multiplied by the source color value.SrcAlphaThe value of this stage is multiplied by the source alpha value.DstColorThe value of this stage is multiplied by frame buffer source color value.DstAlphaThe value of this stage is multiplied by frame buffer source alpha value.OneMinusSrcColorThe value of this stage is multiplied by (1 - source color).OneMinusSrcAlphaThe value of this stage is multiplied by (1 - source alpha).OneMinusDstColorThe value of this stage is multiplied by (1 - destination color).OneMinusDstAlphaThe value of this stage is multiplied by (1 - destination alpha).BlendOp
AddAdd source and destination together.SubSubtract destination from source.RevSubSubtract source from destination.MinUse the smaller of source and destination.MaxUse the larger of source and destination.

6.Stencil

Ref

    Ref referenceValue

The value to be compared against (if Comp is anything else than always) and/or the value to be written to the buffer (if either Pass, Fail or ZFail is set to replace). 0–255 integer.

ReadMask

    ReadMask readMask

An 8 bit mask as an 0–255 integer, used when comparing the reference value with the contents of the buffer (referenceValue & readMaskcomparisonFunction (stencilBufferValue & readMask). Default: 255.

WriteMask

    WriteMask writeMask

An 8 bit mask as an 0–255 integer, used when writing to the buffer. Default: 255.

Comp

    Comp comparisonFunction

The function used to compare the reference value to the current contents of the buffer. Default: always.

Pass

    Pass stencilOperation

What to do with the contents of the buffer if the stencil test (and the depth test) passes. Default: keep.

Fail

    Fail stencilOperation

What to do with the contents of the buffer if the stencil test fails. Default: keep.

ZFail

    ZFail stencilOperation

What to do with the contents of the buffer if the stencil test passes, but the depth test fails. Default: keep.

Comp, Pass, Fail and ZFail will be applied to the front-facing geometry, unless Cull Front is specified, in which case it’s back-facing geometry. You can also explicitly specify the two-sided stencil state by defining CompFront, PassFront, FailFront, ZFailFront (for front-facing geometry), and CompBack, PassBack, FailBack, ZFailBack (for back-facing geometry).

Comparison Function

Comparison function is one of the following:

  GreaterOnly render pixels whose reference value is greater than the value in the buffer.GEqualOnly render pixels whose reference value is greater than or equal to the value in the buffer.LessOnly render pixels whose reference value is less than the value in the buffer.LEqualOnly render pixels whose reference value is less than or equal to the value in the buffer.EqualOnly render pixels whose reference value equals the value in the buffer.NotEqualOnly render pixels whose reference value differs from the value in the buffer.AlwaysMake the stencil test always pass.NeverMake the stencil test always fail.

Stencil Operation

Stencil operation is one of the following:

  KeepKeep the current contents of the buffer.ZeroWrite 0 into the buffer.ReplaceWrite the reference value into the buffer.IncrSatIncrement the current value in the buffer. If the value is 255 already, it stays at 255.DecrSatDecrement the current value in the buffer. If the value is 0 already, it stays at 0.InvertNegate all the bits.IncrWrapIncrement the current value in the buffer. If the value is 255 already, it becomes 0.DecrWrapDecrement the current value in the buffer. If the value is 0 already, it becomes 255.7.AlphaTest

AlphaTest off; AlphaTest comparison AlphaValue;

  GreaterOnly render pixels whose alpha is greater than AlphaValue.GEqualOnly render pixels whose alpha is greater than or equal to AlphaValue.LessOnly render pixels whose alpha value is less than AlphaValue.LEqualOnly render pixels whose alpha value is less than or equal to from AlphaValue.EqualOnly render pixels whose alpha value equals AlphaValue.NotEqualOnly render pixels whose alpha value differs from AlphaValue.AlwaysRender all pixels. This is functionally equivalent to AlphaTest Off.NeverDon’t render any pixels.8.Fog

Mode Off | Global | Linear | Exp | Exp2

Color ColorValue

Density FloatValue

Range FloatValue, FloatValue

四、#pragma

  • #pragma vertex name - compile function name as the vertex shader.
  • #pragma fragment name - compile function name as the fragment shader.
  • #pragma geometry name - compile function name as DX10 geometry shader. Having this option automatically turns on #pragma target 4.0, described below.
  • #pragma hull name - compile function name as DX11 hull shader. Having this option automatically turns on #pragma target 5.0, described below.
  • #pragma domain name - compile function name as DX11 domain shader. Having this option automatically turns on #pragma target 5.0, described below.

Other compilation directives:

  • #pragma target name - which shader target to compile to. See Shader targets below for details.
  • #pragma only_renderers space separated names - compile shader only for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
  • #pragma exclude_renderers space separated names - do not compile shader for given renderers. By default shaders are compiled for all renderers. See Renderers below for details.
  • #pragma multi_compile …_ - for working with multiple shader variants.
  • #pragma enable_d3d11_debug_symbols - generate debug information for shaders compiled for DirectX 11, this will allow you to debug shaders via Visual Studio 2012 (or higher) Graphics debugger.
5.Built-in shader variables

NameValueUNITY_MATRIX_MVPCurrent model * view * projection matrix.UNITY_MATRIX_MVCurrent model * view matrix.UNITY_MATRIX_VCurrent view matrix.UNITY_MATRIX_PCurrent projection matrix.UNITY_MATRIX_VPCurrent view * projection matrix.UNITY_MATRIX_T_MVTranspose of model * view matrix.UNITY_MATRIX_IT_MVInverse transpose of model * view matrix._Object2WorldCurrent model matrix._World2ObjectInverse of current world matrix.   NameTypeValue_WorldSpaceCameraPosfloat3World space position of the camera._ProjectionParamsfloat4x is 1.0 (or –1.0 if currently rendering with a flipped projection matrix), y is the camera’s near plane, z is the camera’s far plane and w is 1/FarPlane._ScreenParamsfloat4x is the camera’s render target width in pixels, y is the camera’s render target height in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height._ZBufferParamsfloat4Used to linearize Z buffer values. x is (1-far/near), yis (far/near), z is (x/far) and w is (y/far).unity_OrthoParamsfloat4x is orthographic camera’s width, y is orthographic camera’s height, z is unused and w is 1.0 when camera is orthographic, 0.0 when perspective.unity_CameraProjectionfloat4x4Camera’s projection matrix.unity_CameraInvProjectionfloat4x4Inverse of camera’s projection matrix.unity_CameraWorldClipPlanes[6]float4Camera frustum plane world space equations, in this order: left, right, bottom, top, near, far.NameTypeValue_Timefloat4Time since level load (t/20, t, t*2, t*3), use to animate things inside the shaders._SinTimefloat4Sine of time: (t/8, t/4, t/2, t)._CosTimefloat4Cosine of time: (t/8, t/4, t/2, t).unity_DeltaTimefloat4Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).

Forward rendering (ForwardBase and ForwardAdd pass types):

   NameTypeValue_LightColor0 (declared in Lighting.cginc)fixed4Light color._WorldSpaceLightPos0float4Directional lights: (world space direction, 0). Other lights: (world space position, 1)._LightMatrix0 (declared in AutoLight.cginc)float4x4World-to-light matrix. Used to sample cookie & attenuation textures.unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0float4(ForwardBase pass only) world space positions of first four non-important point lights.unity_4LightAtten0float4(ForwardBase pass only) attenuation factors of first four non-important point lights.unity_LightColorhalf4[4](ForwardBase pass only) colors of of first four non-important point lights.

Deferred shading and deferred lighting, used in the lighting pass shader (all declared in UnityDeferredLibrary.cginc):

   NameTypeValue_LightColorfloat4Light color._LightMatrix0float4x4World-to-light matrix. Used to sample cookie & attenuation textures.

LOD

  • VertexLit kind of shaders = 100
  • Decal, Reflective VertexLit = 150
  • Diffuse = 200
  • Diffuse Detail, Reflective Bumped Unlit, Reflective Bumped VertexLit = 250
  • Bumped, Specular = 300
  • Bumped Specular = 400
  • Parallax = 500
  • Parallax Specular = 600


原创粉丝点击