2016년 7월 1일 금요일

Unity shader Built-in include files ( #include "UnityCG.cginc")

유니티 사이트

CGPROGRAM
// ...
#include "UnityCG.cginc"  <---
// ...

ENDCG

Data structures in UnityCG.cginc
struct appdata_base: position, normal, one texture coordinate.
struct appdata_tan:  position, normal, tangent, one texture coordinate.
struct appdata_full: position, normal, tangent, vertex color and two texture coordinates.

struct appdata_img:  position and one texture coordinate.



Vertex transformation functions in UnityCG.cginc

- float4 UnityObjectToClipPos(float3 pos)
Transforms a point from object space to the camera’s clip space in homogeneous coordinates.
This is the equivalent of mul(UNITY_MATRIX_MVP, float4(pos, 1.0)),
and should be used in its place.

- float3 UnityObjectToViewPos(float3 pos)
Transforms a point from object space to view space.
This is the equivalent of mul(UNITY_MATRIX_MV, float4(pos, 1.0)).xyz,
and should be used in its place.


Generic helper functions in UnityCG.cginc

float3 WorldSpaceViewDir (float4 v) : UnityWorldSpaceViewDir(worldPos)
Returns world space direction (not normalized) from given object space vertex position towards 
the camera.

- float3 ObjSpaceViewDir (float4 v)
Returns object space direction (not normalized) from given object space vertex position towards
the camera.

- float2 ParallaxOffset (half h, half height, half3 viewDir)
calculates UV offset for parallax normal mapping.

- fixed Luminance (fixed3 c)
Converts color to luminance (grayscale).

- fixed3 DecodeLightmap (fixed4 color)
Decodes color from Unity lightmap (RGBM or dLDR depending on platform).

- float4 EncodeFloatRGBA (float v)
Encodes [0..1) range float into RGBA color, for storage in low precision render target.

- float DecodeFloatRGBA (float4 enc)
Decodes RGBA color into a float.

- float2 EncodeFloatRG (float v) Encodes [0..1)
range float into a float2.

- float DecodeFloatRG (float2 enc)
Decodes a previously-encoded RG float.

- float2 EncodeViewNormalStereo (float3 n)
Encodes view space normal into two numbers in 0..1 range.

- float3 DecodeViewNormalStereo (float4 enc4)
Decodes view space normal from enc4.xy.

-> 찾아봐야 할 것

- UnityObjectToWorldNormal(i.normal) : 노말을 월드 노말로 
UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.worldRefl) : 디폴트 리플렉션 큐브맵을 뽑는다.
half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.worldRefl);


Forward rendering helper functions in UnityCG.cginc

These functions are only useful when using forward rendering
(ForwardBase or ForwardAdd pass types).

- float3 WorldSpaceLightDir (float4 v)
Computes world space direction (not normalized) to light, given object space vertex position.

- float3 ObjSpaceLightDir (float4 v)
Computes object space direction (not normalized) to light, given object space vertex position.


- float3 Shade4PointLights (...)
Computes illumination from four point lights, with light data tightly packed into vectors.
Forward rendering uses this to compute per-vertex lighting.


Vertex-lit helper functions in UnityCG.cginc

These functions are only useful when using per-vertex lit shaders (“Vertex” pass type).

- float3 ShadeVertexLights (float4 vertex, float3 normal)
Computes illumination from four per-vertex lights and ambient,
given object space position & normal.


=========================================================================================

Built-in shader variables



Transformations


All these matrices are float4x4 type.

UNITY_MATRIX_MVP Current model * view * projection matrix.
UNITY_MATRIX_MV        Current model * view matrix.
UNITY_MATRIX_V         Current view matrix.
UNITY_MATRIX_P         Current projection matrix.
UNITY_MATRIX_VP        Current view * projection matrix.
UNITY_MATRIX_T_MV Transpose of model * view matrix.
UNITY_MATRIX_IT_MV Inverse transpose of model * view matrix.

_Object2World          Current model matrix. vertex와 곱하면 월드 스페이스의 포지션
                       float3 worldPosition = mul(_Object2World, v.vertex).xyz;

_World2Object          Inverse of current world matrix.


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.


- _WorldSpaceCameraPos : float3
World space position of the camera.

- _ProjectionParams :float4
x 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.

- _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
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),
w is (y/far).

- unity_OrthoParams : float4
x is orthographic camera’s width,
y is orthographic camera’s height,
z is unused
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

- _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):

_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):

_LightColor : float4
Light color.

_LightMatrix0 :float4x4
World-to-light matrix. Used to sample cookie & attenuation textures.


Fog and Ambient


unity_AmbientSky : fixed4
Sky ambient lighting color in gradient ambient lighting case.

unity_AmbientEquator : fixed4
Equator ambient lighting color in gradient ambient lighting case.

unity_AmbientGround : fixed4
Ground ambient lighting color in gradient ambient lighting case.

UNITY_LIGHTMODEL_AMBIENT : fixed4
Ambient lighting color (sky color in gradient ambient case). Legacy variable.

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.




Ti







댓글 없음:

댓글 쓰기