分享

Unity

 tiancaiwrk 2019-05-08
  • Built-in shader variables

Built-in shader helper functions
Making multiple shader program variants

Built-in shader variables

Unity provides a handful of built-in global variables for your shadersA small script that contains the mathematical calculations and algorithms for calculating the Color of each pixel rendered, based on the lighting input and the Material configuration. More info
See in Glossary: things like current object’s transformation matrices, light parameters, current time and so on. You use them in shader programs like any other variable, the only difference is that you don’t have to declare them - they are all declared in UnityShaderVariables.cginc include file that is included automatically.

Transformations

All these matrices are float4x4 type.



NameValue
UNITY_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.
unity_ObjectToWorldCurrent model matrix.
unity_WorldToObjectInverse of current world matrix.

Camera and screen

These variables will correspond to the CameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary that is renderingThe process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. More info
See in Glossary. For example during shadowmap rendering, they will still refer to the Camera component values, and not the “virtual camera” that is used for the shadowmap projection.




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 width of the camera’s target texture in pixelsThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary, y is the height of the camera’s target texture 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), y is (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.

Time




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).

Lighting

Light parameters are passed to shaders in different ways depending on which Rendering PathThe technique Unity uses to render graphics. Choosing a different path affects the performance of your game, and how lighting and shading are calculated. Some paths are more suited to different platforms and hardware than others. More info
See in Glossary is used, and which LightMode Pass Tag is used in the shader.

Forward renderingA rendering path that renders each object in one or more passes, depending on lights that affect the object. Lights themselves are also treated differently by Forward Rendering, depending on their settings and intensity. More info
See in Glossary (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.
unity_WorldToShadowfloat4x4[4]World-to-shadow matrices. One matrix for spot lights, up to four for directional light cascades.

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.
unity_WorldToShadowfloat4x4[4]World-to-shadow matrices. One matrix for spot lights, up to four for directional light cascades.

Spherical harmonics coefficients (used by ambient and light probes) are set up for ForwardBase, PrePassFinal and Deferredpass types. They contain 3rd order SH to be evaluated by world space normal (see ShadeSH9 from UnityCG.cginc). The variables are all half4 type, unity_SHAr and similar names.

Vertex-lit rendering (Vertex pass type):

Up to 8 lights are set up for a Vertex pass type; always sorted starting from the brightest one. So if you want to render objects affected by two lights at once, you can just take first two entries in the arrays. If there are less lights affecting the object than 8, the rest will have their color set to black.




NameTypeValue
unity_LightColorhalf4[8]Light colors.
unity_LightPositionfloat4[8]View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.
unity_LightAttenhalf4[8]Light attenuation factors. x is cos(spotAngle/2) or –1 for non-spot lights; y is 1/cos(spotAngle/4) or 1 for non-spot lights; z is quadratic attenuation; w is squared light range.
unity_SpotDirectionfloat4[8]View-space spot light positions; (0,0,1,0) for non-spot lights.

Fog and Ambient




NameTypeValue
unity_AmbientSkyfixed4Sky ambient lighting color in gradient ambient lighting case.
unity_AmbientEquatorfixed4Equator ambient lighting color in gradient ambient lighting case.
unity_AmbientGroundfixed4Ground ambient lighting color in gradient ambient lighting case.
UNITY_LIGHTMODEL_AMBIENTfixed4Ambient lighting color (sky color in gradient ambient case). Legacy variable.
unity_FogColorfixed4Fog color.
unity_FogParamsfloat4Parameters for fog calculation: (density / sqrt(ln(2)), density / ln(2), –1/(end-start), end/(end-start)). x is useful for Exp2 fog mode, y for Exp mode, z and w for Linear mode.

Various




NameTypeValue
unity_LODFadefloat4Level-of-detail fade when using LODGroup. x is fade (0..1), y is fade quantized to 16 levels, z and w unused.
_TextureSampleAddfloat4Set automatically by Unity for UI(User Interface) Allows a user to interact with your application. More info
See in Glossary only
based on whether the texture being used is in Alpha8 format (the value is set to (1,1,1,0)) or not (the value is set to (0,0,0,0)).

Did you find this page useful? Please give it a rating:


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多