GetWorldSpaceNormalizeViewDir

HLSL
half3 GetWorldSpaceNormalizeViewDir(float3 positionWS)

获取从世界空间中某一点指向相机的归一化视线方向(带透视类型处理)。(也就是Raymarching里的rd)


获取相机位置

[!warning|aside-r] 仅URP

HLSL
float3 GetCameraPositionWS()
float3 GetCurrentViewPosition()

这两个函数本质上是为未来的“相机相对渲染”(Camera Relative Rendering)功能预留的抽象层,截至 Unity 6.3,这两个函数仍然只是直接返回 {{< link title=“内置变量” url="/posts/31dac212#_WorldSpaceCameraPos" icon="/favicon.ico" >}}。


Clip 裁剪

[!warning|aside-r] 仅限片段着色器

在片元着色器阶段对像素进行裁剪(丢弃),如果传入的参数任何分量小于0,则丢弃当前像素。

HLSL
clip(0); // 大于等于0,保留
clip(-1); // 小于0,裁剪

案例:画圆⚪,即丢弃圆外的像素

HLSL
// 画圆⚪,即丢弃圆外的像素
float dist = distance(uv, float2(0.5)); // 圆中心(0.5, 0.5)
clip(0.5 - dist); // 半径0.5

: Cheyne Xie

: https://chaim.eu.org/posts/8aa0d89d/

: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.