Unity Manual


  • 首页

  • 分类

  • 公益404

  • 搜索

Shader Level of Detail

发表于 2017-07-20 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Shader Level of Detail

网址:Shader Level of Detail

#Shader Level of Detail

  • VertexLit kind of shaders = 100
  • Decal, Reflective VertexLit = 150
  • Diffuse = 200
  • Diffuse Detail, Reflective Bumped Unlit, Reflective Bumped VertexLit = 250
  • Bumped, Specular = 300
  • Bumped Specular = 400
  • Parallax = 500
  • Parallax Specular = 600

Platform-specific rendering differences

发表于 2017-07-20 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Platform-specific rendering differences

网址:Platform-specific rendering differences

Render Texture coordinates

Vertical Texture coordinate conventions differ between two types of platforms: Direct3D-like and OpenGL-like.

  • Direct3D-like: The coordinate is 0 at the top and increases downward. This applies to Direct3D, Metal and consoles.
  • OpenGL-like: The coordinate is 0 at the bottom and increases upward. This applies to OpenGL and OpenGL ES.
    Unity 会统一使用 OpenGL
    特别注意使用的地方:
  • Render Texture
  • Image Effects
  • UV Space

Image Effects

Rendering in UV space

Clip space coordinates

Similar to Texture coordinates, the clip space coordinates (also known as post-projection space coordinates) differ between Direct3D-like and OpenGL-like platforms:

  • Direct3D-like: The clip space depth goes from 0.0 at the near plane to +1.0 at the far plane. This applies to Direct3D, Metal and consoles.
  • OpenGL-like: The clip space depth goes from –1.0 at the near plane to +1.0 at the far plane. This applies to OpenGL and OpenGL ES.

Precision of Shader computations

PC GPUs treat all floating point types (float, half and fixed) as the same - they do all calculations using full 32-bit precision, while many mobile device GPUs do not do this.

Const declarations in Shaders

  • Microsoft’s HLSL const has much the same meaning as it does in C# and C++ in that the variable declared is read-only within its scope but can be initialized in any way.
  • OpenGL’s GLSL const means that the variable is effectively a compile time constant, and so it must be initialized with compile time constraints (either literal values or calculations on other consts).

Semantics used by Shaders

  • Vertex Shader output (clip space) position: SV_POSITION. Sometimes Shaders use POSITION semantics to get Shaders working on all platforms. Note that this does not not work on Sony PS4 or with tessellation.
  • Fragment Shader output color: SV_Target. Sometimes Shaders use COLOR or COLOR0 to get Shaders working on all platforms. Note that this does not work on Sony PS4.

Direct3D Shader compiler syntax

DirectX 11 (DX11) HLSL syntax in Shaders

Using Shader framebuffer fetch

The Depth (Z) direction in Shaders


Camera’s Depth Texture

发表于 2017-07-20 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Camera’s Depth Texture

网址:Camera’s Depth Texture

Camera’s Depth Texture

There are three possible depth texture modes:

  • DepthTextureMode.Depth: a depth texture.
  • DepthTextureMode.DepthNormals: depth and view space normals packed into one texture.*
  • DepthTextureMode.MotionVectors: per-pixel screen space motion of each screen texel for the current frame. Packed into a RG16 texture.

DepthTextureMode.Depth texture

  • 深度图使用 shadow caster pass,所以如果 shader 不支持,则物体不会出现在深度图中
  • 在 shader 的 fallback 添加那些有 shadow casting pass 的 shader,或者在 surfase shader 中添加 addshadow 也会生成 shadow pass
  • 仅有 render queue <= 2500 的会渲染到深度图中

DepthTextureMode.DepthNormals texture

  • 32bits 深度图,View Space Normals Stereographic projection 加密到 RG Channel,depth 加密到 BA Channel
  • DecodeDepthNormal 用于解密图到 0~1 范围

DepthTextureMode.MotionVectors texture

  • This builds a screen-sized RG16 (16-bit float/channel) texture, where screen space pixel motion is encoded into the R&G channels. The pixel motion is encoded in screen UV space.
  • When sampling from this texture motion from the encoded pixel is returned in a rance of –1..1. This will be the UV offset from the last frame to the current frame.

Shader variables

Depth textures are available for sampling in shaders as global shader properties. By declaring a sampler called _CameraDepthTexture you will be able to sample the main depth texture for the camera.
_CameraDepthTexture always refers to the camera’s primary depth texture. By contrast, you can use _LastCameraDepthTexture to refer to the last depth texture rendered by any camera. This could be useful for example if you render a half-resolution depth texture in script using a secondary camera and want to make it available to a post-process shader.
The motion vectors texture (when enabled) is avaialable in Shaders as a global Shader property. By declaring a sampler called ‘_CameraMotionVectorsTexture’ you can sample the Texture for the curently rendering Camera.

Under the hood

Depth textures can come directly from the actual depth buffer, or be rendered in a separate pass, depending on the rendering path used and the hardware. Typically when using Deferred Shading or Legacy Deferred Lighting rendering paths, the depth textures come “for free” since they are a product of the G-buffer rendering anyway.
When the DepthNormals texture is rendered in a separate pass, this is done through Shader Replacement. Hence it is important to have correct “RenderType” tag in your shaders.
When enabled, the MotionVectors texture always comes from a extra render pass. Unity will render moving GameObjects into this buffer, and construct their motion from the last frame to the current frame.


Using Depth Textures

发表于 2017-07-20 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Using Depth Textures

网址:Using Depth Textures

Using Depth Textures

Depth Textures are supported on most modern hardware and graphics APIs. Special requirements are listed below:

  • Direct3D 11+ (Windows), OpenGL 3+ (Mac/Linux), OpenGL ES 3.0+ (Android/iOS), Metal (iOS) and consoles like PS4/Xbox One all support depth textures.
  • Direct3D 9 (Windows) requires a graphics driver to support “INTZ” texture format to get depth textures.
  • OpenGL ES 2.0 (iOS/Android) requires GL_OES_depth_texture extension to be present.
  • WebGL requires WEBGL_depth_texture extension.

Depth Texture Shader helper macros

  • UNITY_TRANSFER_DEPTH(o): computes eye space depth of the vertex and outputs it in o (which must be a float2). Use it in a vertex program when rendering into a depth texture. On platforms with native depth textures this macro does nothing at all, because Z buffer value is rendered implicitly.
  • UNITY_OUTPUT_DEPTH(i): returns eye space depth from i (which must be a float2). Use it in a fragment program when rendering into a depth texture. On platforms with native depth textures this macro always returns zero, because Z buffer value is rendered implicitly.
  • COMPUTE_EYEDEPTH(i): computes eye space depth of the vertex and outputs it in o. Use it in a vertex program when not rendering into a depth texture.
  • DECODE_EYEDEPTH(i)/LinearEyeDepth(i): given high precision value from depth texture i, returns corresponding eye space depth.
  • Linear01Depth(i): given high precision value from depth texture i, returns corresponding linear depth in range between 0 and 1.

Rendering with Replaced Shaders

发表于 2017-07-20 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Rendering with Replaced Shaders

网址:Rendering with Replaced Shaders

Rendering with Replaced Shaders

  • If replacementTag is empty, then all objects in the scene are rendered with the given replacement shader.
  • If replacementTag is not empty, then for each object that would be rendered:
    • The real object’s shader is queried for the tag value.
    • If it does not have that tag, object is not rendered.
    • A subshader is found in the replacement shader that has a given tag with the found value. If no such subshader is found, object is not rendered.
    • Now that subshader is used to render the object.

Shader replacement tags in built-in Unity shaders

All built-in Unity shaders have a “RenderType” tag set that can be used when rendering with replaced shaders. Tag values are the following:


Performance tips when writing shaders

发表于 2017-07-20 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Performance tips when writing shaders

网址:Performance tips when writing shaders

Optimized Surface Shaders

  • The approxview directive for shaders that use view direction (i.e. Specular) makes the view direction normalized per vertex instead of per pixel. This is approximate, but often good enough.
  • The halfasview for Specular shader types is even faster. The half-vector (halfway between lighting direction and view vector) is computed and normalized per vertex, and the lighting function receives the half-vector as a parameter instead of the view vector.
  • noforwardadd makes a shader fully support one-directional light in Forward rendering only. The rest of the lights can still have an effect as per-vertex lights or spherical harmonics. This is great to make your shader smaller and make sure it always renders in one pass, even with multiple lights present.
  • noambient disables ambient lighting and spherical harmonics lights on a shader. This can make performance slightly faster.

Precision of computations

  • For world space positions and texture coordinates, use float precision.
  • For everything else (vectors, HDR colors, etc.), start with half precision. Increase only if necessary.
  • For very simple operations on texture data, use fixed precision.
  • PC 一般都是仅使用 float 的
  • Mobile 一般都是仅使用 half 的
  • 除了老显卡会支持使用 fixed,大部分都是直接使用 half

Alpha Testing

  • However, on PowerVR GPUs found in iOS and some Android devices, alpha testing is resource-intensive. Do not try to use it for performance optimization on these platforms, as it causes the game to run slower than usual.

Color Mask

  • On some platforms (mostly mobile GPUs found in iOS and Android devices), using ColorMask to leave out some channels (e.g. ColorMask RGB) can be resource-intensive, so only use it if really necessary.

Unity’s Rendering Pipeline

发表于 2017-07-12 | 更新于 2017-07-20 | 分类于 Graphics , Graphics Reference , Shader Reference , Advanced ShaderLab topics , Unity’s Rendering Pipeline

网址:Unity’s Rendering Pipeline

Rendering Paths

  • 前向渲染,使用 ForwardBase 和 ForwardAdd
  • 延迟渲染,使用 Deferred
  • 对于上述任何光照,ShaowCaster 用于渲染阴影和深度图

Forward Rendering path

  • 如果使用前向渲染,但是没有设置 pass,将会使用 Vertex Lit Path

ForwardBase

  • 环境光
  • 光照贴图
  • 主要的方向光
  • 不重要的顶点光照

    ForwardAdd

  • 像素光照,每个光照对对象使用了一次

Deferred Shading path

  • Deferred pass renders all information needed for lighting (in built-in shaders: diffuse color, specular color, smoothness, world space normal, emission). It also adds lightmaps, reflection probes and ambient lighting into the emission channe

ShaderLab: SubShader Tags

发表于 2017-07-12 | 更新于 2017-07-26 | 分类于 Graphics , Graphics Reference , Shader Reference , ShaderLab Syntax , ShaderLab SubShader , ShaderLab SubShader Tags

网址:ShaderLab: SubShader Tags

Details

Rendering Order - Queue tag

  • Background - this render queue is rendered before any others. You’d typically use this for things that really need to be in the background.
  • Geometry (默认) - this is used for most objects. Opaque geometry uses this queue.
  • AlphaTest - alpha tested geometry uses this queue. It’s a separate queue from Geometry one since it’s more efficient to render alpha-tested objects after all solid ones are drawn.
  • Transparent - this render queue is rendered after Geometry and AlphaTest, in back-to-front order. Anything alpha-blended (i.e. shaders that don’t write to depth buffer) should go here (glass, particle effects).
  • Overlay - this render queue is meant for overlay effects. Anything rendered last should go here (e.g. lens flares).

RenderType tag

  • 用于 Shader Replacement 或者 camera’s depth texture

DisableBatching tag

  • True:关闭 batching
  • False:不关闭,默认
  • LODFading:当开启 LOD 的时候关闭,一般用于树

ForceNoShadowCasting tag

  • True:一般用于渲染透明物体的时候,关闭从其它 subshader 那里的投影

IgnoreProjector tag

  • True:一般用于半透明物体,因为 Projectors 没办法很好的处理

CanUseSpriteAtlas tag

  • False:当用于 Sprite 的时候,如果使用了 Atlas,设定无效

PreviewType tag

  • Spheres:默认
  • Plane
  • Skybox

ShaderLab: Pass Tags

发表于 2017-07-11 | 更新于 2017-07-26 | 分类于 Graphics , Graphics Reference , Shader Reference , ShaderLab Syntax , ShaderLab SubShader , ShaderLab Pass , ShaderLab Pass Tags

网址:ShaderLab: Pass Tags

Details

LightMode tag

  • Always: Always rendered; no lighting is applied.
  • ForwardBase: Used in Forward rendering, ambient, main directional light, vertex/SH lights and lightmaps are applied.
  • ForwardAdd: Used in Forward rendering; additive per-pixel lights are applied, one pass per light.
  • Deferred: Used in Deferred Shading; renders g-buffer.
  • ShadowCaster: Renders object depth into the shadowmap or a depth texture.
  • MotionVectors: Used to calculate per-object motion vectors.

PassFlags tag

  • OnlyDirectional: When used in ForwardBase pass type, this flag makes it so that only the main directional light and ambient/lightprobe data is passed into the shader. This means that data of non-important lights is not passed into vertex-light or spherical harmonics shader variables. See Forward rendering for details.

RequireOptions tag

  • SoftVegetation: Render this pass only if Soft Vegetation is on in Quality Settings.

ShaderLab: Stencil

发表于 2017-07-11 | 更新于 2017-07-26 | 分类于 Graphics , Graphics Reference , Shader Reference , ShaderLab Syntax , ShaderLab SubShader , ShaderLab Pass , ShaderLab Stencil

网址:ShaderLab: Stencil

ShaderLab: Stencil

  • 用于保存或者丢弃像素
  • 一般是8位integer每像素

Syntax

Ref

  • Ref referenceValue

ReadMask

  • ReadMask readMask
  • 默认255

WriteMask

  • WriteMask writeMask
  • 默认255

Comp

  • Comp comparisonFunction

Pass

  • Pass stencilOperation

Fail

  • Fail stencilOperation

Comp

  • Comp comparisonFunction

ZFail

  • ZFail stencilOperation

1234…7
Fly

Fly

作者懒得写

69 日志
113 分类
GitHub CSDN
© 2018 Fly
由 Hexo 强力驱动
主题 - NexT.Pisces
0%