查看: 5986|回复: 4
打印 上一主题 下一主题

[经验分享] 内置着色器包含文件

[复制链接]

1557

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
454
精华
31

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-10-16 16:12:27 |只看该作者 |倒序浏览

Unity contains several files that can be used by your shader programs to bring in predefined variables and helper functions. This is done by the standard #include directive, e.g.:

Unity包含几个可被你的 着色器程序 使用的文件,它们带有预定义的变量和辅助函数。这通过标准 #include命令来使用,例如:

    CGPROGRAM    // ...    #include "UnityCG.cginc"    // ...    ENDCG

Shader include files in Unity are with .cginc extension, and the built-in ones are:

在Unity中Shader include文件的扩展名是.cginc,内置的文件如下:

  • HLSLSupport.cginc - (automatically included) Helper macros and definitions for cross-platform shader compilation.
    HLSLSupport.cginc :(自动包含)跨平台着色器编译帮助宏和定义
  • UnityCG.cginc - commonly used global variables and helper functions.
    UnityCG.cginc :常用的全局变量和辅助函数
  • AutoLight.cginc - lighting & shadowing functionality, e.g. surface shaders use this file internally.
    AutoLight.cginc :灯光和阴影功能,例如surface shaders 内在地使用此文件。
  • Lighting.cginc - standard surface shader lighting models; automatically included when you're writing surface shaders.
    Lighting.cginc :标准surface shader 灯光模型,当你写表面着色器时自动包含。
  • TerrainEngine.cginc - helper functions for Terrain & Vegetation shaders.
    TerrainEngine.cginc : 地形和植被着色器的辅助函数。

These files are found inside Unity application ({unity install path}/Data/CGIncludes/UnityCG.cginc on Windows,/Applications/Unity/Unity.app/Contents/CGIncludes/UnityCG.cginc on Mac), if you want to take a look at what exactly is done in any of the helper code.

这些文件存放在Unity程序中,在电脑上的具体位置是: Windows系统在{unity install path}/Data/CGIncludes/UnityCG.cginc Mac系统在 /Applications/Unity/Unity.app/Contents/CGIncludes/UnityCG.cginc 如果你想看一看里面的辅助代码究竟是什么,可以在这些位置找到具体文件。

HLSLSupport.cginc

This file is automatically included when compiling shaders. It mostly declares various preprocessor macros to aid in multi-platform shader development.

当编译着色器时这个文件会自动包括。它主要声明各种预处理宏来帮助多平台着色器开发。

UnityCG.cginc

This file is often included in Unity shaders to bring in many helper functions and definitions.

此文件经常包含在Unity着色器中,带来很多辅助函数和定义。

Data structures in UnityCG.cginc 在UnityCG.cginc中的数据结构
  • struct appdata_base: vertex shader input with position, normal, one texture coordinate.
    struct appdata_base: 带有位置、法线和一个纹理坐标的顶点着色器输入
  • struct appdata_tan: vertex shader input with position, normal, tangent, one texture coordinate.
    struct appdata_tan:带有位置、法线、切线和一个纹理坐标的顶点着色器输入
  • struct appdata_full: vertex shader input with position, normal, tangent, vertex color and two texture coordinates.
    struct appdata_full:带有位置、法线、切线、顶点色和两个纹理坐标的顶点着色器输入
  • struct appdata_img: vertex shader input with position and one texture coordinate.
    struct appdata_img:带有位置和一个纹理坐标的顶点着色器输入
Generic helper functions in UnityCG.cginc 在UnityCG.cginc中的通用辅助函数
  • float3 WorldSpaceViewDir (float4 v) - returns world space direction (not normalized) from given object space vertex position towards the camera.
    float3 WorldSpaceViewDir (float4 v):根据给定的局部空间顶点位置到相机返回世界空间的方向(非规范化的)
  • float3 ObjSpaceViewDir (float4 v) - returns object space direction (not normalized) from given object space vertex position towards the camera.
    float3 ObjSpaceViewDir (float4 v):根据给定的局部空间顶点位置到相机返回局部空间的方向(非规范化的)
  • float2 ParallaxOffset (half h, half height, half3 viewDir) - calculates UV offset for parallax normal mapping.
    float2 ParallaxOffset (half h, half height, half3 viewDir):为视差法线贴图计算UV偏移
  • fixed Luminance (fixed3 c) - converts color to luminance (grayscale).
    fixed Luminance (fixed3 c):将颜色转换为亮度(灰度)
  • fixed3 DecodeLightmap (fixed4 color) - decodes color from Unity lightmap (RGBM or dLDR depending on platform).
    fixed3 DecodeLightmap (fixed4 color):从Unity光照贴图解码颜色(基于平台为RGBM 或dLDR)
  • float4 EncodeFloatRGBA (float v) - encodes [0..1) range float into RGBA color, for storage in low precision render target.
    float4 EncodeFloatRGBA (float v):为储存低精度的渲染目标,编码[0..1)范围的浮点数到RGBA颜色。
  • float DecodeFloatRGBA (float4 enc) - decodes RGBA color into a float.
    float DecodeFloatRGBA (float4 enc):解码RGBA颜色到float。
  • Similarly, float2 EncodeFloatRG (float v) and float DecodeFloatRG (float2 enc) that use two color channels.
    同样的,float2 EncodeFloatRG (float v) 和float DecodeFloatRG (float2 enc)使用的是两个颜色通道。
  • float2 EncodeViewNormalStereo (float3 n) - encodes view space normal into two numbers in 0..1 range.
    float2 EncodeViewNormalStereo (float3 n):编码视图空间法线到在0到1范围的两个数。
  • float3 DecodeViewNormalStereo (float4 enc4) - decodes view space normal from enc4.xy.
    float3 DecodeViewNormalStereo (float4 enc4):从enc4.xy解码视图空间法线
Forward rendering helper functions in UnityCG.cginc
UnityCG.cginc正向渲染辅助函数

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

这些函数只有当使用forward rendering(ForwardBase 或 ForwardAdd通道类型)时才有效。

  • 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.
    从4个点光源计算亮度,来将光照数据牢固的装入向量中。正向渲染使用此来计算逐顶点光照。
Vertex-lit helper functions in UnityCG.cginc
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.
    根据局部空间位置和法线,从4个逐顶点灯光和环境光计算亮度。
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

0

主题

0

听众

16

积分

设计初学者

Rank: 1

纳金币
162
精华
0
沙发
发表于 2013-10-16 16:13:27 |只看该作者
你这是从文档中copy来的吧,你可以转外国论坛的,不用非得中文
回复

使用道具 举报

nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-10-17 09:38:30 |只看该作者
好像是文档啊
回复

使用道具 举报

2

主题

1

听众

1143

积分

助理设计师

Rank: 4

纳金币
350
精华
0
地板
发表于 2013-10-28 10:42:05 |只看该作者
感谢楼主分享
回复

使用道具 举报

0

主题

1

听众

1174

积分

助理设计师

Rank: 4

纳金币
92
精华
0
5#
发表于 2014-5-7 04:08:27 |只看该作者
感謝分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-19 20:40 , Processed in 0.137627 second(s), 37 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部