본문으로 바로가기

Unity Built-in shader variables

category Technical Report/Unity Shader 2015. 11. 24. 16:25
반응형

Built-in shader variables

Unity provides a handful of built-in global variables for your shaders: 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.
    
Name     Value

UNITY_MATRIX_MVP     현재 모델 * 뷰 * 투영 행렬 (model*view*projection)
UNITY_MATRIX_MV     현재 모델 * 뷰 행렬.
UNITY_MATRIX_V     현재 뷰 행렬.
UNITY_MATRIX_P     현재 투영 행렬
UNITY_MATRIX_VP     현재 뷰 행렬 * 투영 행렬
UNITY_MATRIX_T_MV     모델 * 뷰 행렬의 전치 행렬
UNITY_MATRIX_IT_MV     모델 * 뷰 행렬의 역행렬의 전치 행렬
_Object2World     현재 모델 매트릭스
_World2Object     현재 월드 행렬의 역


Camera and screen
These variables will correspond to the Camera that is rendering. 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.
    
Name     Type     Value

_WorldSpaceCameraPos     float3     카메라의 월드 공간 위치
_ProjectionParams     float4     x는 1.0 또는 –1.0, 반전한 투영 행렬에서 현재 렌더링하는 경우는 음의 값. y는 카메라에 가까운 평면, z는 카메라에서 먼 평면, w는 1/FarPlane입니다.
_ScreenParams     float4     x 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.
_ZBufferParams     float4     Used to linearize Z buffer values. x is (1-far/near), y is (far/near), z is (x/far) and w is (y/far).
unity_OrthoParams     float4     x 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_CameraProjection     float4x4     Camera’s projection matrix.
unity_CameraInvProjection     float4x4     Inverse of camera’s projection matrix.
unity_CameraWorldClipPlanes[6]     float4     Camera frustum plane world space equations, in this order: left, right, bottom, top, near, far.


Time   
 
Name     Type     Value

_Time     float4     Time since level load (t/20, t, t*2, t*3), use to animate things inside the shaders.
_SinTime     float4     Sine of time: (t/8, t/4, t/2, t).
_CosTime     float4     Cosine of time: (t/8, t/4, t/2, t).
unity_DeltaTime     float4     Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).


Lighting

Light parameters are passed to shaders in different ways depending on which Rendering Path is used, and which LightMode Pass Tag is used in the shader.

Forward rendering (ForwardBase and ForwardAdd pass types):
   
    
Name     Type     Value

LightColor0 (declared in Lighting.cginc) | fixed4 |Light color. | |WorldSpaceLightPos0     float4     Directional lights: (world space direction, 0). Other lights: (world space position, 1).
_LightMatrix0 (declared in AutoLight.cginc)     float4x4     World-to-light matrix. Used to sample cookie & attenuation textures.
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0     float4     (ForwardBase pass only) world space positions of first four non-important point lights.
unity_4LightAtten0     float4     (ForwardBase pass only) attenuation factors of first four non-important point lights.
unity_LightColor     half4[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):
    
Name     Type     Value

LightColor | float4 | Light color. | |LightMatrix0     float4x4     World-to-light matrix. Used to sample cookie & attenuation textures.

Spherical harmonics coefficients (used by ambient and light probes) are set up for ForwardBase, PrePassFinal and Deferred pass 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.   
    
Name     Type     Value

unity_LightColor     half4[8]     Light colors.
unity_LightPosition     float4[8]     View-space light positions. (-direction,0) for directional lights; (position,1) for point/spot lights.
unity_LightAtten     half4[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_SpotDirection     float4[8]     View-space spot light positions; (0,0,1,0) for non-spot lights.


Fog and Ambient
    
Name     Type     Value

UNITY_LIGHTMODEL_AMBIENT     fixed4     Ambient lighting color (Equator ambient in three-color lighting case).
unity_AmbientSky     fixed4     Sky ambient lighting color in three-color lighting case.
unity_AmbientGround     fixed4     Ground ambient lighting color in three-color lighting case.
unity_FogColor     fixed4     Fog color.
unity_FogParams     float4     Parameters 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
   
    
Name     Type     Value
unity_LODFade     float4     Level-of-detail fade when using LODGroup. x is fade (0..1), y is fade quantized to 16 levels, z and w unused.




Time.time의 경우 Time.timeScale의 영향을 받기 때문에 timeScale의 영향을 받지 않는 시간의 경우 보통 Time.realtimeSinceStartup을 통해서 계산한 다음, 받는다. 다만 Time.realtimeSinceStartup의 경우, 여러가지 문제가 있음

NGUI도 그래서 RealTime 클래스 내부 구현이 좀 복잡하게 되어있음. 그리고 버그도 존재하는데, Awake에서 Time.realtimeSinceStartup을 받아오면 엉뚱한 값들이 넘어오는 현상.

2. Time.time의 경우 시간값이 생각보다 정확하지 않음.

그래서 정확한 시간을 요구하는 부분에선 Time.realtimeSinceStartup을 주로 사용해야했다. 하지만 이 경우에도 1에서 언급된 문제점들 때문에 쓰기 좀 곤란한 상황이 생긴다.


3. 유니티 포럼에서 이 부분에 대한 언급이 좀 있었는지, 4.5버전부터는 Time.unscaledTime, Time.unscaledDeltaTime 이라는 것이 생겼다. 이 멤버들은 Time.timeScale의 영향을 받지 않으며, Time.timeScale이 1이라고 하더라도 Time.time보다 훨씬 정확하다.



반응형