shader內置變量


內置變量都在UnityShaderVariables.cginc文件中聲明

變換矩陣

All these matrices arefloat4x4 type.

Name

Value

UNITY_MATRIX_MVP

Current model * view * projection matrix.

從object space轉到 projection space

UNITY_MATRIX_MV

Current model * view matrix.

從object space轉到camera space

UNITY_MATRIX_V

Current view matrix.

從world space轉到camera space

UNITY_MATRIX_P

Current projection matrix.

從camera space轉到projection space

UNITY_MATRIX_VP

Current view * projection matrix.

從world space轉到projection space

UNITY_MATRIX_T_MV

Transpose of model * view matrix.

UNITY_MATRIX_MV 的轉置矩陣

UNITY_MATRIX_IT_MV

Inverse transpose of model * view matrix.

UNITY_MATRIX_MV的逆轉置矩陣,法線的矩陣轉換需要用變換矩陣的逆轉置矩陣.

_Object2World

Current model matrix.

從object space轉到world space

_World2Object

Inverse of current world matrix.

_Object2World的逆矩陣,從world space 到object space

 

Camera and screen

These variables will correspond to the Camerathat 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

World space position of the camera.

相機的世界坐標

_ProjectionParams

float4

Xis 1.0 (or –1.0 if currently rendering with aflipped projection matrix),yis the camera’s near plane,zis the camera’s far plane andwis 1/FarPlane.

x 值是1.0或-1.0(當為反轉的投影矩陣時)

y 近截面,z遠截面,w 1/遠截面

_ScreenParams

float4

xis the camera’s render target width in pixels,yis the camera’s render target height in pixels,zis 1.0 + 1.0/width andwis 1.0 + 1.0/height.

屏幕尺寸x寬,y高,z 1+1.0/width,w 1+1.0/height

_ZBufferParams

float4

Used to linearize Z buffer values.xis (1-far/near),yis (far/near),zis (x/far) andwis (y/far).

用於線性化zbuffer

unity_OrthoParams

float4

xis orthographic camera’s width,yis orthographic camera’s height,zis unused andwis 1.0 when camera is orthographic, 0.0 when perspective.

正交攝像機的相關參數,w值1時是正交投影,0時是透視投影

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 differentways depending on which Rendering Pathis used, and which LightModePass Tag is used in the shader.

Forward rendering (ForwardBase andForwardAdd passtypes):

forward渲染的光照變量

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

World space 光照位置,平行光的w值為0,其他光源的w值為1

_LightMatrix0(declared in AutoLight.cginc)

float4x4

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

World space轉到light space矩陣

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.

 

在正向渲染中,影響物體的最亮的幾個光源使用逐像素光照模式。接下來,最多有4個點光源會以逐頂點渲染的方式被計算。其他光源將以球面調和(Spherical Harmonics)的方式進行計算,球面調和技術計算很快但只能得到近似值。

Deferred shading and deferred lighting, usedin 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.

World space轉到light space矩陣

Spherical harmonics coefficients (used by ambientand light probes) are set up forForwardBase,PrePassFinalandDeferredpasstypes. They contain 3rd order SH to be evaluated by world space normal (seeShadeSH9fromUnityCG.cginc).The variables are all half4 type,unity_SHArandsimilar names.

Vertex-lit rendering (Vertex pass type):                  

Up to 8 lights are set up for aVertexpass type; always sorted starting from the brightest one. So if you wantto render objects affected by two lights at once, you can just take first two entriesin the arrays. If there are less lights affecting the object than 8, the rest willhave 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.

View space光照位置,平行光的w值為0,其他光源的w值為1

unity_LightAtten

half4[8]

Light attenuation factors.xis cos(spotAngle/2) or –1 for non-spot lights;yis 1/cos(spotAngle/4) or 1 for non-spot lights;zis quadratic attenuation;wis squared light range.

光照衰減值

unity_SpotDirection

float4[8]

View-space spot light positions; (0,0,1,0) for non-spot lights.

View space聚光燈位置

 

Fog and Ambient

Window > Lighting> Scene 里設置的相應的顏色

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 LODGroupx is fade (0..1), y is fade quantized to 16 levels,z and w unused.

Built-in shader helper functions


其他一些雜七雜八的變量

#pragmatarget 3.0  定義Shader模型為Shader Model 3.0,

 

Image Effect取到depthTexture相關的步驟

GetComponent<Camera>().depthTextureMode=DepthTextureMode.Depth//設置攝像機模式

Shader中聲明  sampler2D _CameraDepthTexture;//深度紋理的聲明

取樣float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,uv) , //0-1越遠越大

 

生成一個深度+法線紋理

GetComponent<Camera>().depthTextureMode=DepthTextureMode.DepthNormals;

Shader中聲明

sampler2D _CameraDepthTexture;//深度紋理的聲明

sampler2D _CameraNormalsTexture;//法線紋理的聲明

用tex2d方法進行法線取樣

 

或者聲明

sampler2D _CameraDepthNormalsTexture.//

DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture,i.screen.xy), depthValue, normalValues);取得法線跟深度值

 

DecodeDepthNormalto decode depth and normalfrom the encoded pixel value. Returned depth is in 0.1 range.

 

_CameraDepthTexturealways refers to the camera’sprimary depth texture. By contrast, you can use_LastCameraDepthTextureto refer to the last depthtexture rendered by any camera.This couldbe useful for example if you render a half-resolution depth texture in script usinga secondary camera and want to make it available to a post-process shader.

_LastCameraDepthTexture取得最后渲染的深度紋理

 

Texturesize  紋理尺寸獲得

{TextureName}_TexelSize- a float4 property contains texture size information:

xcontains1.0/width

ycontains1.0/height

zcontainswidth

wcontainsheight

例子

uniform float4 _MainTex_TexelSize分別對應Vector4(1 / width, 1 / height, width, height),

 

Texturetiling & offset 紋理偏移

Materialsoften have Tiling and Offset fields for their texture properties.This information is passed into shaders in a float4 {TextureName}_STproperty:

xcontainsX tiling value

ycontainsY tiling value

zcontainsX offset value

wcontainsY offset value

例:

o.uv =TRANSFORM_TEX(v.texcoord,_MainTex);

相當於

o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;

o.uv = v.texcoord.xy相當於Tiling為(1,1)Offset為(0,0)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM