当前位置:网站首页>Unity shader (data type in cghlsl)

Unity shader (data type in cghlsl)

2022-07-07 09:25:00 heater404

Unity Shader(Cg/HLSL Data types in )

stay Shader in , We are Properties The variables defined in are for display in the material panel and convenient for us to adjust , If you want to in Cg/HLSL If it is used in, it must be declared again ( The naming is required to be consistent ).

Cg/HLSL Data types in

  • float

High precision type ,32 position , Usually used for positions in world coordinates , texture UV, Or scalar computation involving complex functions , Like trigonometric functions 、 Power operation, etc .

  • half

Medium precision type ,16 position , The range of values is [-60000,+60000], Usually used for location in local coordinates 、 The direction of the vector 、HDR Color, etc. .

  • fixed

Low precision type ,11 position , The range of values is [-2,+2], Usually used for regular colors and maps , And some operational variables between low precision .

stay PC The platform doesn't care about you Shader It says half still fixed, All will be regarded as float To deal with it .half And fixed Only effective on some mobile devices .
A common rule is , In addition to position and coordinates float outside , All the rest are used half. The main reason is also because most modern GPU Only support 32 Bit and 16 position , That is to say, only support float and half, I won't support it fixed.

  • interger

Integer type , Usually used for loop and array indexing .

stay Direct3D 9 and OpenGL ES 2.0 On the platform, integer may be directly processed with floating-point numbers , stay Direct3D 11、OpenGL ES 3 Etc GPU Can be handled correctly with integer type .

  • sampler2D、sampler3D And samplerCUBE

texture , By default, textures on mobile platforms will be automatically converted to low precision texture types , If you need medium precision or high precision, you need to declare it in the following way :

sampler2D_half( Medium precision 2D texture )

sampler2D*_*float( High precision 2D texture )

sampler3D_half( Medium precision 3D texture )

sampler3D*_*float( High precision 3D texture )

samplerCUBE_halft( Medium precision cube texture )

samplerCUBE_float( High precision cube texture )

Corresponding types

So in Properties And Cg/HLSL How does the in correspond ?

  • Int/float/Range Expressed in floating-point values , That is to say float、half perhaps fixed, Define according to the accuracy you need .
  • Vector/Color use float4、half4 perhaps fixed4 Express .
  • 2D Type use sampler2D Express .
  • 3D type sampler3D Express .
  • CUBE Type use samplerCUBE Express .

It doesn't matter Vector still Color, Are composed of four floating-point values with the same accuracy , So when we define it, we write float4、half4 perhaps fixed4.

such as , We are Properties The following colors are declared in :

_Color("Color", Color) = (1,1,1,1)

stay Cg/HLSL We need to make the same statement again :

fixed4 _Color;
原网站

版权声明
本文为[heater404]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130625200379.html